aboutsummaryrefslogtreecommitdiff
path: root/spaghetti-monster/fsm-ramping.c
diff options
context:
space:
mode:
authorSelene ToyKeeper2021-11-06 00:58:59 -0600
committerSelene ToyKeeper2021-11-06 00:58:59 -0600
commit046a76deda6c3e586162dacd28b18371629ab990 (patch)
tree649814ac426c1543d2d8d52df1c2bf6c8a9a3583 /spaghetti-monster/fsm-ramping.c
parentmerged gchart's recent changes (diff)
parentfixed KR4-tintramp build (switch wasn't working) and added it to MODELS (diff)
downloadanduril-046a76deda6c3e586162dacd28b18371629ab990.tar.gz
anduril-046a76deda6c3e586162dacd28b18371629ab990.tar.bz2
anduril-046a76deda6c3e586162dacd28b18371629ab990.zip
merged recent anduril2 branch updates
Diffstat (limited to 'spaghetti-monster/fsm-ramping.c')
-rw-r--r--spaghetti-monster/fsm-ramping.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/spaghetti-monster/fsm-ramping.c b/spaghetti-monster/fsm-ramping.c
index 05c2e0e..49e173f 100644
--- a/spaghetti-monster/fsm-ramping.c
+++ b/spaghetti-monster/fsm-ramping.c
@@ -102,6 +102,7 @@ void set_level(uint8_t level) {
#endif
} else {
// enable the power channel, if relevant
+ #ifndef USE_TINT_RAMPING // update_tint handles this better
#ifdef LED_ENABLE_PIN
#ifndef LED_ENABLE_PIN_LEVEL_MIN
LED_ENABLE_PORT |= (1 << LED_ENABLE_PIN);
@@ -117,6 +118,7 @@ void set_level(uint8_t level) {
#ifdef LED2_ENABLE_PIN
LED2_ENABLE_PORT |= (1 << LED2_ENABLE_PIN);
#endif
+ #endif // ifndef USE_TINT_RAMPING
// PWM array index = level - 1
level --;
@@ -293,8 +295,14 @@ void update_tint() {
// calculate actual PWM levels based on a single-channel ramp
// and a global tint value
//PWM_DATATYPE brightness = PWM_GET(pwm1_levels, level);
- PWM_DATATYPE brightness = PWM1_LVL;
- PWM_DATATYPE warm_PWM, cool_PWM;
+ uint16_t brightness = PWM1_LVL;
+ uint16_t warm_PWM, cool_PWM;
+ #ifdef USE_DYN_PWM
+ uint16_t top = PWM1_TOP;
+ //uint16_t top = PWM_GET(pwm_tops, actual_level-1);
+ #else
+ const uint16_t top = PWM_TOP;
+ #endif
// auto-tint modes
uint8_t mytint;
@@ -311,29 +319,52 @@ void update_tint() {
// stretch 1-254 to fit 0-255 range (hits every value except 98 and 198)
else { mytint = (tint * 100 / 99) - 1; }
- // middle tints sag, so correct for that effect
PWM_DATATYPE2 base_PWM = brightness;
- // correction is only necessary when PWM is fast
#if defined(TINT_RAMPING_CORRECTION) && (TINT_RAMPING_CORRECTION > 0)
+ // middle tints sag, so correct for that effect
+ // by adding extra power which peaks at the middle tint
+ // (correction is only necessary when PWM is fast)
if (level > HALFSPEED_LEVEL) {
base_PWM = brightness
+ ((((PWM_DATATYPE2)brightness) * TINT_RAMPING_CORRECTION / 64) * triangle_wave(mytint) / 255);
}
+ // fade the triangle wave out when above 100% power,
+ // so it won't go over 200%
+ if (brightness > top) {
+ base_PWM -= 2 * (
+ ((brightness - top) * TINT_RAMPING_CORRECTION / 64)
+ * triangle_wave(mytint) / 255
+ );
+ }
+ // guarantee no more than 200% power
+ if (base_PWM > (top << 1)) { base_PWM = top << 1; }
#endif
cool_PWM = (((PWM_DATATYPE2)mytint * (PWM_DATATYPE2)base_PWM) + 127) / 255;
warm_PWM = base_PWM - cool_PWM;
+ // when running at > 100% power, spill extra over to other channel
+ if (cool_PWM > top) {
+ warm_PWM += (cool_PWM - top);
+ cool_PWM = top;
+ } else if (warm_PWM > top) {
+ cool_PWM += (warm_PWM - top);
+ warm_PWM = top;
+ }
TINT1_LVL = warm_PWM;
TINT2_LVL = cool_PWM;
// disable the power channel, if relevant
#ifdef LED_ENABLE_PIN
- if (! warm_PWM)
+ if (warm_PWM)
+ LED_ENABLE_PORT |= (1 << LED_ENABLE_PIN);
+ else
LED_ENABLE_PORT &= ~(1 << LED_ENABLE_PIN);
#endif
#ifdef LED2_ENABLE_PIN
- if (! cool_PWM)
+ if (cool_PWM)
+ LED2_ENABLE_PORT |= (1 << LED2_ENABLE_PIN);
+ else
LED2_ENABLE_PORT &= ~(1 << LED2_ENABLE_PIN);
#endif
}