aboutsummaryrefslogtreecommitdiff
path: root/spaghetti-monster/fsm-ramping.c
diff options
context:
space:
mode:
authorSelene ToyKeeper2021-09-15 01:40:06 -0600
committerSelene ToyKeeper2021-09-15 01:40:06 -0600
commitb2d963efd4bcbc83a310c26d31c306176e21b1f6 (patch)
treef2f01f6545dac54f9b23bbc2fa14c6dfd84950b7 /spaghetti-monster/fsm-ramping.c
parentdocumented "hold" action in number entry state (diff)
parentadded Noctigon K9.3-tintramp build targets for FET, reduced FET, and noFET (diff)
downloadanduril-b2d963efd4bcbc83a310c26d31c306176e21b1f6.tar.gz
anduril-b2d963efd4bcbc83a310c26d31c306176e21b1f6.tar.bz2
anduril-b2d963efd4bcbc83a310c26d31c306176e21b1f6.zip
merged K9.3-tintramp / D4Sv2-tintramp branch
(which also changed the way tint ramping is implemented, to make things generally cleaner and more flexible)
Diffstat (limited to 'spaghetti-monster/fsm-ramping.c')
-rw-r--r--spaghetti-monster/fsm-ramping.c138
1 files changed, 90 insertions, 48 deletions
diff --git a/spaghetti-monster/fsm-ramping.c b/spaghetti-monster/fsm-ramping.c
index 54ca45c..e8fcde7 100644
--- a/spaghetti-monster/fsm-ramping.c
+++ b/spaghetti-monster/fsm-ramping.c
@@ -89,6 +89,10 @@ void set_level(uint8_t level) {
#if PWM_CHANNELS >= 4
PWM4_LVL = 0;
#endif
+ #ifdef USE_TINT_RAMPING
+ TINT1_LVL = 0;
+ TINT2_LVL = 0;
+ #endif
// disable the power channel, if relevant
#ifdef LED_ENABLE_PIN
LED_ENABLE_PORT &= ~(1 << LED_ENABLE_PIN);
@@ -117,44 +121,6 @@ void set_level(uint8_t level) {
// PWM array index = level - 1
level --;
- #ifdef USE_TINT_RAMPING
- #ifndef TINT_RAMPING_CORRECTION
- #define TINT_RAMPING_CORRECTION 26 // 140% brightness at middle tint
- #endif
- // calculate actual PWM levels based on a single-channel ramp
- // and a global tint value
- uint8_t brightness = PWM_GET(pwm1_levels, level);
- uint8_t warm_PWM, cool_PWM;
-
- // auto-tint modes
- uint8_t mytint;
- #if 1
- // perceptual by ramp level
- if (tint == 0) { mytint = 255 * (uint16_t)level / RAMP_SIZE; }
- else if (tint == 255) { mytint = 255 - (255 * (uint16_t)level / RAMP_SIZE); }
- #else
- // linear with power level
- //if (tint == 0) { mytint = brightness; }
- //else if (tint == 255) { mytint = 255 - brightness; }
- #endif
- // 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
- uint16_t base_PWM = brightness;
- // correction is only necessary when PWM is fast
- if (level > HALFSPEED_LEVEL) {
- base_PWM = brightness
- + ((((uint16_t)brightness) * TINT_RAMPING_CORRECTION / 64) * triangle_wave(mytint) / 255);
- }
-
- cool_PWM = (((uint16_t)mytint * (uint16_t)base_PWM) + 127) / 255;
- warm_PWM = base_PWM - cool_PWM;
-
- PWM1_LVL = warm_PWM;
- PWM2_LVL = cool_PWM;
- #else // ifdef USE_TINT_RAMPING
-
#if PWM_CHANNELS >= 1
PWM1_LVL = PWM_GET(pwm1_levels, level);
#endif
@@ -168,8 +134,6 @@ void set_level(uint8_t level) {
PWM4_LVL = PWM_GET(pwm4_levels, level);
#endif
- #endif // ifdef USE_TINT_RAMPING
-
#ifdef USE_DYN_PWM
uint16_t top = PWM_GET(pwm_tops, level);
#ifdef PWM1_CNT
@@ -214,6 +178,10 @@ void set_level(uint8_t level) {
}
#endif
}
+ #ifdef USE_TINT_RAMPING
+ update_tint();
+ #endif
+
#ifdef PWM1_CNT
prev_level = api_level;
#endif
@@ -236,6 +204,7 @@ void gradual_tick() {
if (gt < actual_level) gt = actual_level - 1;
else if (gt > actual_level) gt = actual_level + 1;
+ /*
#ifdef LED_ENABLE_PIN_LEVEL_MIN
// only enable during part of the ramp
if ((gt >= LED_ENABLE_PIN_LEVEL_MIN)
@@ -244,6 +213,7 @@ void gradual_tick() {
else // disable during other parts of the ramp
LED_ENABLE_PORT &= ~(1 << LED_ENABLE_PIN);
#endif
+ */
gt --; // convert 1-based number to 0-based
@@ -251,14 +221,23 @@ void gradual_tick() {
#if PWM_CHANNELS >= 1
target = PWM_GET(pwm1_levels, gt);
- if ((gt < actual_level) // special case for FET-only turbo
- && (PWM1_LVL == 0) // (bypass adjustment period for first step)
- && (target == PWM_TOP)) PWM1_LVL = PWM_TOP;
- else if (PWM1_LVL < target) PWM1_LVL ++;
+ #if PWM_CHANNELS > 1
+ if ((gt < actual_level) // special case for FET-only turbo
+ && (PWM1_LVL == 0) // (bypass adjustment period for first step)
+ && (target == PWM_TOP)) PWM1_LVL = PWM_TOP;
+ else
+ #endif
+ if (PWM1_LVL < target) PWM1_LVL ++;
else if (PWM1_LVL > target) PWM1_LVL --;
#endif
#if PWM_CHANNELS >= 2
target = PWM_GET(pwm2_levels, gt);
+ #if PWM_CHANNELS > 2
+ if ((gt < actual_level) // special case for FET-only turbo
+ && (PWM2_LVL == 0) // (bypass adjustment period for first step)
+ && (target == PWM_TOP)) PWM2_LVL = PWM_TOP;
+ else
+ #endif
if (PWM2_LVL < target) PWM2_LVL ++;
else if (PWM2_LVL > target) PWM2_LVL --;
#endif
@@ -287,14 +266,77 @@ void gradual_tick() {
#endif
)
{
- actual_level = gt + 1;
+ //actual_level = gt + 1;
+ set_level(gt + 1);
}
- #ifdef USE_DYNAMIC_UNDERCLOCKING
- auto_clock_speed();
- #endif
+ // is handled in set_level()
+ //#ifdef USE_TINT_RAMPING
+ //update_tint();
+ //#endif
+ // is handled in set_level()
+ //#ifdef USE_DYNAMIC_UNDERCLOCKING
+ //auto_clock_speed();
+ //#endif
}
#endif // ifdef OVERRIDE_GRADUAL_TICK
#endif // ifdef USE_SET_LEVEL_GRADUALLY
+
+#if defined(USE_TINT_RAMPING) && (!defined(TINT_RAMP_TOGGLE_ONLY))
+void update_tint() {
+ #ifndef TINT_RAMPING_CORRECTION
+ #define TINT_RAMPING_CORRECTION 26 // 140% brightness at middle tint
+ #endif
+
+ // 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;
+
+ // auto-tint modes
+ uint8_t mytint;
+ uint8_t level = actual_level - 1;
+ #if 1
+ // perceptual by ramp level
+ if (tint == 0) { mytint = 255 * (uint16_t)level / RAMP_SIZE; }
+ else if (tint == 255) { mytint = 255 - (255 * (uint16_t)level / RAMP_SIZE); }
+ #else
+ // linear with power level
+ //if (tint == 0) { mytint = brightness; }
+ //else if (tint == 255) { mytint = 255 - brightness; }
+ #endif
+ // 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)
+ if (level > HALFSPEED_LEVEL) {
+ base_PWM = brightness
+ + ((((PWM_DATATYPE2)brightness) * TINT_RAMPING_CORRECTION / 64) * triangle_wave(mytint) / 255);
+ }
+ #endif
+
+ cool_PWM = (((PWM_DATATYPE2)mytint * (PWM_DATATYPE2)base_PWM) + 127) / 255;
+ warm_PWM = base_PWM - cool_PWM;
+
+ TINT1_LVL = warm_PWM;
+ TINT2_LVL = cool_PWM;
+
+ // disable the power channel, if relevant
+ #ifdef LED_ENABLE_PIN
+ if (! warm_PWM)
+ LED_ENABLE_PORT &= ~(1 << LED_ENABLE_PIN);
+ #endif
+ #ifdef LED2_ENABLE_PIN
+ if (! cool_PWM)
+ LED2_ENABLE_PORT &= ~(1 << LED2_ENABLE_PIN);
+ #endif
+}
+#endif // ifdef USE_TINT_RAMPING
+
+
#endif // ifdef USE_RAMPING
#endif