diff options
Diffstat (limited to 'quantum/backlight/backlight_timer.c')
| -rw-r--r-- | quantum/backlight/backlight_timer.c | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/quantum/backlight/backlight_timer.c b/quantum/backlight/backlight_timer.c index c32c35c154..82fb6a6a83 100644 --- a/quantum/backlight/backlight_timer.c +++ b/quantum/backlight/backlight_timer.c | |||
| @@ -14,14 +14,14 @@ static uint16_t backlight_timer_get_duty(void); | |||
| 14 | 14 | ||
| 15 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | 15 | // See http://jared.geek.nz/2013/feb/linear-led-pwm |
| 16 | static uint16_t cie_lightness(uint16_t v) { | 16 | static uint16_t cie_lightness(uint16_t v) { |
| 17 | if (v <= 5243) // if below 8% of max | 17 | if (v <= 5243) // if below 8% of max |
| 18 | return v / 9; // same as dividing by 900% | 18 | return v / 9; // same as dividing by 900% |
| 19 | else { | 19 | else { |
| 20 | uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | 20 | uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare |
| 21 | // to get a useful result with integer division, we shift left in the expression above | 21 | // to get a useful result with integer division, we shift left in the expression above |
| 22 | // and revert what we've done again after squaring. | 22 | // and revert what we've done again after squaring. |
| 23 | y = y * y * y >> 8; | 23 | y = y * y * y >> 8; |
| 24 | if (y > 0xFFFFUL) // prevent overflow | 24 | if (y > 0xFFFFUL) // prevent overflow |
| 25 | return 0xFFFFU; | 25 | return 0xFFFFU; |
| 26 | else | 26 | else |
| 27 | return (uint16_t)y; | 27 | return (uint16_t)y; |
| @@ -61,7 +61,9 @@ static void backlight_timer_top(void) { | |||
| 61 | } | 61 | } |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | static void backlight_timer_cmp(void) { backlight_pins_off(); } | 64 | static void backlight_timer_cmp(void) { |
| 65 | backlight_pins_off(); | ||
| 66 | } | ||
| 65 | 67 | ||
| 66 | void backlight_task(void) {} | 68 | void backlight_task(void) {} |
| 67 | 69 | ||
| @@ -77,7 +79,9 @@ static uint16_t breathing_counter = 0; | |||
| 77 | static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | 79 | static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 78 | 80 | ||
| 79 | // Use this before the cie_lightness function. | 81 | // Use this before the cie_lightness function. |
| 80 | static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); } | 82 | static inline uint16_t scale_backlight(uint16_t v) { |
| 83 | return v / BACKLIGHT_LEVELS * get_backlight_level(); | ||
| 84 | } | ||
| 81 | 85 | ||
| 82 | void breathing_task(void) { | 86 | void breathing_task(void) { |
| 83 | uint8_t breathing_period = get_breathing_period(); | 87 | uint8_t breathing_period = get_breathing_period(); |
| @@ -91,13 +95,17 @@ void breathing_task(void) { | |||
| 91 | backlight_timer_set_duty(cie_lightness(scale_backlight((uint16_t)breathing_table[index] * 256))); | 95 | backlight_timer_set_duty(cie_lightness(scale_backlight((uint16_t)breathing_table[index] * 256))); |
| 92 | } | 96 | } |
| 93 | 97 | ||
| 94 | bool is_breathing(void) { return breathing; } | 98 | bool is_breathing(void) { |
| 99 | return breathing; | ||
| 100 | } | ||
| 95 | 101 | ||
| 96 | void breathing_enable(void) { | 102 | void breathing_enable(void) { |
| 97 | breathing_counter = 0; | 103 | breathing_counter = 0; |
| 98 | breathing = true; | 104 | breathing = true; |
| 99 | } | 105 | } |
| 100 | void breathing_disable(void) { breathing = false; } | 106 | void breathing_disable(void) { |
| 107 | breathing = false; | ||
| 108 | } | ||
| 101 | 109 | ||
| 102 | void breathing_pulse(void) { | 110 | void breathing_pulse(void) { |
| 103 | backlight_set(is_backlight_enabled() ? 0 : BACKLIGHT_LEVELS); | 111 | backlight_set(is_backlight_enabled() ? 0 : BACKLIGHT_LEVELS); |
| @@ -140,8 +148,12 @@ static void timerCallback(void) { | |||
| 140 | } | 148 | } |
| 141 | } | 149 | } |
| 142 | 150 | ||
| 143 | static void backlight_timer_set_duty(uint16_t duty) { s_duty = duty; } | 151 | static void backlight_timer_set_duty(uint16_t duty) { |
| 144 | static uint16_t backlight_timer_get_duty(void) { return s_duty; } | 152 | s_duty = duty; |
| 153 | } | ||
| 154 | static uint16_t backlight_timer_get_duty(void) { | ||
| 155 | return s_duty; | ||
| 156 | } | ||
| 145 | 157 | ||
| 146 | // ChibiOS - Map GPT timer onto Software PWM | 158 | // ChibiOS - Map GPT timer onto Software PWM |
| 147 | static void gptTimerCallback(GPTDriver *gptp) { | 159 | static void gptTimerCallback(GPTDriver *gptp) { |
