summaryrefslogtreecommitdiff
path: root/quantum/backlight
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2022-02-12 10:29:31 -0800
committerGitHub <noreply@github.com>2022-02-12 18:29:31 +0000
commit63646e8906e062d1c1de3925cba70c4e3426a855 (patch)
tree4e91648b77b838e1125cf86331d7e84bde6d07a9 /quantum/backlight
parentafcdd7079c774dec2aa4b7f2d08adf8b7310919b (diff)
Format code according to conventions (#16322)
Diffstat (limited to 'quantum/backlight')
-rw-r--r--quantum/backlight/backlight.c54
-rw-r--r--quantum/backlight/backlight.h2
-rw-r--r--quantum/backlight/backlight_avr.c50
-rw-r--r--quantum/backlight/backlight_chibios.c16
-rw-r--r--quantum/backlight/backlight_driver_common.c8
-rw-r--r--quantum/backlight/backlight_software.c12
-rw-r--r--quantum/backlight/backlight_timer.c32
7 files changed, 117 insertions, 57 deletions
diff --git a/quantum/backlight/backlight.c b/quantum/backlight/backlight.c
index dfb98419e6..52ec086bb0 100644
--- a/quantum/backlight/backlight.c
+++ b/quantum/backlight/backlight.c
@@ -95,10 +95,10 @@ void backlight_toggle(void) {
95 * FIXME: needs doc 95 * FIXME: needs doc
96 */ 96 */
97void backlight_enable(void) { 97void backlight_enable(void) {
98 if (backlight_config.enable) return; // do nothing if backlight is already on 98 if (backlight_config.enable) return; // do nothing if backlight is already on
99 99
100 backlight_config.enable = true; 100 backlight_config.enable = true;
101 if (backlight_config.raw == 1) // enabled but level == 0 101 if (backlight_config.raw == 1) // enabled but level == 0
102 backlight_config.level = 1; 102 backlight_config.level = 1;
103 eeconfig_update_backlight(backlight_config.raw); 103 eeconfig_update_backlight(backlight_config.raw);
104 dprintf("backlight enable\n"); 104 dprintf("backlight enable\n");
@@ -110,7 +110,7 @@ void backlight_enable(void) {
110 * FIXME: needs doc 110 * FIXME: needs doc
111 */ 111 */
112void backlight_disable(void) { 112void backlight_disable(void) {
113 if (!backlight_config.enable) return; // do nothing if backlight is already off 113 if (!backlight_config.enable) return; // do nothing if backlight is already off
114 114
115 backlight_config.enable = false; 115 backlight_config.enable = false;
116 eeconfig_update_backlight(backlight_config.raw); 116 eeconfig_update_backlight(backlight_config.raw);
@@ -122,7 +122,9 @@ void backlight_disable(void) {
122 * 122 *
123 * FIXME: needs doc 123 * FIXME: needs doc
124 */ 124 */
125bool is_backlight_enabled(void) { return backlight_config.enable; } 125bool is_backlight_enabled(void) {
126 return backlight_config.enable;
127}
126 128
127/** \brief Backlight step through levels 129/** \brief Backlight step through levels
128 * 130 *
@@ -158,11 +160,17 @@ void backlight_level(uint8_t level) {
158 eeconfig_update_backlight(backlight_config.raw); 160 eeconfig_update_backlight(backlight_config.raw);
159} 161}
160 162
161uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); } 163uint8_t eeconfig_read_backlight(void) {
164 return eeprom_read_byte(EECONFIG_BACKLIGHT);
165}
162 166
163void eeconfig_update_backlight(uint8_t val) { eeprom_update_byte(EECONFIG_BACKLIGHT, val); } 167void eeconfig_update_backlight(uint8_t val) {
168 eeprom_update_byte(EECONFIG_BACKLIGHT, val);
169}
164 170
165void eeconfig_update_backlight_current(void) { eeconfig_update_backlight(backlight_config.raw); } 171void eeconfig_update_backlight_current(void) {
172 eeconfig_update_backlight(backlight_config.raw);
173}
166 174
167void eeconfig_update_backlight_default(void) { 175void eeconfig_update_backlight_default(void) {
168 backlight_config.enable = 1; 176 backlight_config.enable = 1;
@@ -179,7 +187,9 @@ void eeconfig_update_backlight_default(void) {
179 * 187 *
180 * FIXME: needs doc 188 * FIXME: needs doc
181 */ 189 */
182uint8_t get_backlight_level(void) { return backlight_config.level; } 190uint8_t get_backlight_level(void) {
191 return backlight_config.level;
192}
183 193
184#ifdef BACKLIGHT_BREATHING 194#ifdef BACKLIGHT_BREATHING
185/** \brief Backlight breathing toggle 195/** \brief Backlight breathing toggle
@@ -200,7 +210,7 @@ void backlight_toggle_breathing(void) {
200 * FIXME: needs doc 210 * FIXME: needs doc
201 */ 211 */
202void backlight_enable_breathing(void) { 212void backlight_enable_breathing(void) {
203 if (backlight_config.breathing) return; // do nothing if breathing is already on 213 if (backlight_config.breathing) return; // do nothing if breathing is already on
204 214
205 backlight_config.breathing = true; 215 backlight_config.breathing = true;
206 eeconfig_update_backlight(backlight_config.raw); 216 eeconfig_update_backlight(backlight_config.raw);
@@ -213,7 +223,7 @@ void backlight_enable_breathing(void) {
213 * FIXME: needs doc 223 * FIXME: needs doc
214 */ 224 */
215void backlight_disable_breathing(void) { 225void backlight_disable_breathing(void) {
216 if (!backlight_config.breathing) return; // do nothing if breathing is already off 226 if (!backlight_config.breathing) return; // do nothing if breathing is already off
217 227
218 backlight_config.breathing = false; 228 backlight_config.breathing = false;
219 eeconfig_update_backlight(backlight_config.raw); 229 eeconfig_update_backlight(backlight_config.raw);
@@ -225,18 +235,30 @@ void backlight_disable_breathing(void) {
225 * 235 *
226 * FIXME: needs doc 236 * FIXME: needs doc
227 */ 237 */
228bool is_backlight_breathing(void) { return backlight_config.breathing; } 238bool is_backlight_breathing(void) {
239 return backlight_config.breathing;
240}
229 241
230// following are marked as weak purely for backwards compatibility 242// following are marked as weak purely for backwards compatibility
231__attribute__((weak)) void breathing_period_set(uint8_t value) { breathing_period = value ? value : 1; } 243__attribute__((weak)) void breathing_period_set(uint8_t value) {
244 breathing_period = value ? value : 1;
245}
232 246
233__attribute__((weak)) uint8_t get_breathing_period(void) { return breathing_period; } 247__attribute__((weak)) uint8_t get_breathing_period(void) {
248 return breathing_period;
249}
234 250
235__attribute__((weak)) void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); } 251__attribute__((weak)) void breathing_period_default(void) {
252 breathing_period_set(BREATHING_PERIOD);
253}
236 254
237__attribute__((weak)) void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); } 255__attribute__((weak)) void breathing_period_inc(void) {
256 breathing_period_set(breathing_period + 1);
257}
238 258
239__attribute__((weak)) void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); } 259__attribute__((weak)) void breathing_period_dec(void) {
260 breathing_period_set(breathing_period - 1);
261}
240 262
241__attribute__((weak)) void breathing_toggle(void) { 263__attribute__((weak)) void breathing_toggle(void) {
242 if (is_breathing()) 264 if (is_breathing())
diff --git a/quantum/backlight/backlight.h b/quantum/backlight/backlight.h
index c30c70fd62..ff9c8de420 100644
--- a/quantum/backlight/backlight.h
+++ b/quantum/backlight/backlight.h
@@ -39,7 +39,7 @@ typedef union {
39 struct { 39 struct {
40 bool enable : 1; 40 bool enable : 1;
41 bool breathing : 1; 41 bool breathing : 1;
42 uint8_t reserved : 1; // Reserved for possible future backlight modes 42 uint8_t reserved : 1; // Reserved for possible future backlight modes
43 uint8_t level : 5; 43 uint8_t level : 5;
44 }; 44 };
45} backlight_config_t; 45} backlight_config_t;
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index 9c972ae02e..f3a0252270 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -136,7 +136,7 @@
136# define TCCRxB TCCR1B 136# define TCCRxB TCCR1B
137# define TIMERx_COMPA_vect TIMER1_COMPA_vect 137# define TIMERx_COMPA_vect TIMER1_COMPA_vect
138# define TIMERx_OVF_vect TIMER1_OVF_vect 138# define TIMERx_OVF_vect TIMER1_OVF_vect
139# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register 139# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
140# define TIMSKx TIMSK 140# define TIMSKx TIMSK
141# else 141# else
142# define TIMSKx TIMSK1 142# define TIMSKx TIMSK1
@@ -166,7 +166,7 @@ error("Please set 'BACKLIGHT_DRIVER = custom' within rules.mk")
166error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk") 166error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk")
167#endif 167#endif
168 168
169#ifndef BACKLIGHT_PWM_TIMER // pwm through software 169#ifndef BACKLIGHT_PWM_TIMER // pwm through software
170 170
171static inline void enable_pwm(void) { 171static inline void enable_pwm(void) {
172# if BACKLIGHT_ON_STATE == 1 172# if BACKLIGHT_ON_STATE == 1
@@ -203,7 +203,9 @@ static inline void disable_pwm(void) {
203// or F_CPU/BACKLIGHT_CUSTOM_RESOLUTION if used. 203// or F_CPU/BACKLIGHT_CUSTOM_RESOLUTION if used.
204 204
205// Triggered when the counter reaches the OCRx value 205// Triggered when the counter reaches the OCRx value
206ISR(TIMERx_COMPA_vect) { backlight_pins_off(); } 206ISR(TIMERx_COMPA_vect) {
207 backlight_pins_off();
208}
207 209
208// Triggered when the counter reaches the TOP value 210// Triggered when the counter reaches the TOP value
209// this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz 211// this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz
@@ -232,15 +234,15 @@ ISR(TIMERx_OVF_vect) {
232 234
233// See http://jared.geek.nz/2013/feb/linear-led-pwm 235// See http://jared.geek.nz/2013/feb/linear-led-pwm
234static uint16_t cie_lightness(uint16_t v) { 236static uint16_t cie_lightness(uint16_t v) {
235 if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max 237 if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max
236 { 238 {
237 return v / 9; // Same as dividing by 900% 239 return v / 9; // Same as dividing by 900%
238 } else { 240 } else {
239 // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math. 241 // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math.
240 uint32_t y = (((uint32_t)v + (uint32_t)ICRx / 6) << 5) / ((uint32_t)ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max) 242 uint32_t y = (((uint32_t)v + (uint32_t)ICRx / 6) << 5) / ((uint32_t)ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max)
241 uint32_t out = (y * y * y * ICRx) >> 15; // Cube it and undo the bit-shifting. (which is now three times as much due to the cubing) 243 uint32_t out = (y * y * y * ICRx) >> 15; // Cube it and undo the bit-shifting. (which is now three times as much due to the cubing)
242 244
243 if (out > ICRx) // Avoid overflows 245 if (out > ICRx) // Avoid overflows
244 { 246 {
245 out = ICRx; 247 out = ICRx;
246 } 248 }
@@ -249,10 +251,14 @@ static uint16_t cie_lightness(uint16_t v) {
249} 251}
250 252
251// rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val. 253// rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
252static uint32_t rescale_limit_val(uint32_t val) { return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256; } 254static uint32_t rescale_limit_val(uint32_t val) {
255 return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256;
256}
253 257
254// range for val is [0..ICRx]. PWM pin is high while the timer count is below val. 258// range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
255static inline void set_pwm(uint16_t val) { OCRxx = val; } 259static inline void set_pwm(uint16_t val) {
260 OCRxx = val;
261}
256 262
257void backlight_set(uint8_t level) { 263void backlight_set(uint8_t level) {
258 if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; 264 if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
@@ -303,7 +309,9 @@ static uint16_t breathing_freq_scale_factor = 2;
303# ifdef BACKLIGHT_PWM_TIMER 309# ifdef BACKLIGHT_PWM_TIMER
304static bool breathing = false; 310static bool breathing = false;
305 311
306bool is_breathing(void) { return breathing; } 312bool is_breathing(void) {
313 return breathing;
314}
307 315
308# define breathing_interrupt_enable() \ 316# define breathing_interrupt_enable() \
309 do { \ 317 do { \
@@ -315,7 +323,9 @@ bool is_breathing(void) { return breathing; }
315 } while (0) 323 } while (0)
316# else 324# else
317 325
318bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); } 326bool is_breathing(void) {
327 return !!(TIMSKx & _BV(TOIEx));
328}
319 329
320# define breathing_interrupt_enable() \ 330# define breathing_interrupt_enable() \
321 do { \ 331 do { \
@@ -370,7 +380,9 @@ void breathing_self_disable(void) {
370static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {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}; 380static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {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};
371 381
372// Use this before the cie_lightness function. 382// Use this before the cie_lightness function.
373static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); } 383static inline uint16_t scale_backlight(uint16_t v) {
384 return v / BACKLIGHT_LEVELS * get_backlight_level();
385}
374 386
375# ifdef BACKLIGHT_PWM_TIMER 387# ifdef BACKLIGHT_PWM_TIMER
376void breathing_task(void) 388void breathing_task(void)
@@ -403,7 +415,7 @@ ISR(TIMERx_OVF_vect)
403 set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255)))); 415 set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255))));
404} 416}
405 417
406#endif // BACKLIGHT_BREATHING 418#endif // BACKLIGHT_BREATHING
407 419
408void backlight_init_ports(void) { 420void backlight_init_ports(void) {
409 // Setup backlight pin as output and output to on state. 421 // Setup backlight pin as output and output to on state.
@@ -415,10 +427,10 @@ void backlight_init_ports(void) {
415 427
416#ifdef BACKLIGHT_PWM_TIMER 428#ifdef BACKLIGHT_PWM_TIMER
417 // TimerX setup, Fast PWM mode count to TOP set in ICRx 429 // TimerX setup, Fast PWM mode count to TOP set in ICRx
418 TCCRxA = _BV(WGM11); // = 0b00000010; 430 TCCRxA = _BV(WGM11); // = 0b00000010;
419 // clock select clk/1 431 // clock select clk/1
420 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; 432 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
421#else // hardware PWM 433#else // hardware PWM
422 // Pin PB7 = OCR1C (Timer 1, Channel C) 434 // Pin PB7 = OCR1C (Timer 1, Channel C)
423 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 435 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
424 // (i.e. start high, go low when counter matches.) 436 // (i.e. start high, go low when counter matches.)
@@ -430,8 +442,8 @@ void backlight_init_ports(void) {
430 "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]." 442 "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
431 "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)." 443 "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
432 */ 444 */
433 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010; 445 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
434 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; 446 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
435#endif 447#endif
436 448
437#ifdef BACKLIGHT_CUSTOM_RESOLUTION 449#ifdef BACKLIGHT_CUSTOM_RESOLUTION
diff --git a/quantum/backlight/backlight_chibios.c b/quantum/backlight/backlight_chibios.c
index 7c6edd10d6..e8f9e70f78 100644
--- a/quantum/backlight/backlight_chibios.c
+++ b/quantum/backlight/backlight_chibios.c
@@ -53,14 +53,14 @@ static PWMConfig pwmCFG = {0xFFFF, /* PWM clock frequency */
53 53
54// See http://jared.geek.nz/2013/feb/linear-led-pwm 54// See http://jared.geek.nz/2013/feb/linear-led-pwm
55static uint16_t cie_lightness(uint16_t v) { 55static uint16_t cie_lightness(uint16_t v) {
56 if (v <= 5243) // if below 8% of max 56 if (v <= 5243) // if below 8% of max
57 return v / 9; // same as dividing by 900% 57 return v / 9; // same as dividing by 900%
58 else { 58 else {
59 uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare 59 uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
60 // to get a useful result with integer division, we shift left in the expression above 60 // to get a useful result with integer division, we shift left in the expression above
61 // and revert what we've done again after squaring. 61 // and revert what we've done again after squaring.
62 y = y * y * y >> 8; 62 y = y * y * y >> 8;
63 if (y > 0xFFFFUL) // prevent overflow 63 if (y > 0xFFFFUL) // prevent overflow
64 return 0xFFFFU; 64 return 0xFFFFU;
65 else 65 else
66 return (uint16_t)y; 66 return (uint16_t)y;
@@ -117,7 +117,9 @@ static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0,
117 117
118void breathing_callback(PWMDriver *pwmp); 118void breathing_callback(PWMDriver *pwmp);
119 119
120bool is_breathing(void) { return pwmCFG.callback != NULL; } 120bool is_breathing(void) {
121 return pwmCFG.callback != NULL;
122}
121 123
122void breathing_enable(void) { 124void breathing_enable(void) {
123 pwmCFG.callback = breathing_callback; 125 pwmCFG.callback = breathing_callback;
@@ -133,7 +135,9 @@ void breathing_disable(void) {
133} 135}
134 136
135// Use this before the cie_lightness function. 137// Use this before the cie_lightness function.
136static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); } 138static inline uint16_t scale_backlight(uint16_t v) {
139 return v / BACKLIGHT_LEVELS * get_backlight_level();
140}
137 141
138void breathing_callback(PWMDriver *pwmp) { 142void breathing_callback(PWMDriver *pwmp) {
139 uint8_t breathing_period = get_breathing_period(); 143 uint8_t breathing_period = get_breathing_period();
diff --git a/quantum/backlight/backlight_driver_common.c b/quantum/backlight/backlight_driver_common.c
index 270a43c67e..e4c2e90b5f 100644
--- a/quantum/backlight/backlight_driver_common.c
+++ b/quantum/backlight/backlight_driver_common.c
@@ -44,6 +44,10 @@ void backlight_pins_init(void) {
44 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);) 44 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);)
45} 45}
46 46
47void backlight_pins_on(void) { FOR_EACH_LED(backlight_on(backlight_pin);) } 47void backlight_pins_on(void) {
48 FOR_EACH_LED(backlight_on(backlight_pin);)
49}
48 50
49void backlight_pins_off(void) { FOR_EACH_LED(backlight_off(backlight_pin);) } 51void backlight_pins_off(void) {
52 FOR_EACH_LED(backlight_off(backlight_pin);)
53}
diff --git a/quantum/backlight/backlight_software.c b/quantum/backlight/backlight_software.c
index 709304f559..3d412cab52 100644
--- a/quantum/backlight/backlight_software.c
+++ b/quantum/backlight/backlight_software.c
@@ -30,11 +30,17 @@ static const uint16_t backlight_duty_table[] = {
30 30
31// clang-format on 31// clang-format on
32 32
33static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } 33static uint8_t scale_backlight(uint8_t v) {
34 return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS;
35}
34 36
35void backlight_init_ports(void) { backlight_pins_init(); } 37void backlight_init_ports(void) {
38 backlight_pins_init();
39}
36 40
37void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; } 41void backlight_set(uint8_t level) {
42 s_duty_pattern = backlight_duty_table[scale_backlight(level)];
43}
38 44
39void backlight_task(void) { 45void backlight_task(void) {
40 static uint8_t backlight_tick = 0; 46 static uint8_t backlight_tick = 0;
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
16static uint16_t cie_lightness(uint16_t v) { 16static 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
64static void backlight_timer_cmp(void) { backlight_pins_off(); } 64static void backlight_timer_cmp(void) {
65 backlight_pins_off();
66}
65 67
66void backlight_task(void) {} 68void backlight_task(void) {}
67 69
@@ -77,7 +79,9 @@ static uint16_t breathing_counter = 0;
77static 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}; 79static 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.
80static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); } 82static inline uint16_t scale_backlight(uint16_t v) {
83 return v / BACKLIGHT_LEVELS * get_backlight_level();
84}
81 85
82void breathing_task(void) { 86void 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
94bool is_breathing(void) { return breathing; } 98bool is_breathing(void) {
99 return breathing;
100}
95 101
96void breathing_enable(void) { 102void breathing_enable(void) {
97 breathing_counter = 0; 103 breathing_counter = 0;
98 breathing = true; 104 breathing = true;
99} 105}
100void breathing_disable(void) { breathing = false; } 106void breathing_disable(void) {
107 breathing = false;
108}
101 109
102void breathing_pulse(void) { 110void 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
143static void backlight_timer_set_duty(uint16_t duty) { s_duty = duty; } 151static void backlight_timer_set_duty(uint16_t duty) {
144static uint16_t backlight_timer_get_duty(void) { return s_duty; } 152 s_duty = duty;
153}
154static 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
147static void gptTimerCallback(GPTDriver *gptp) { 159static void gptTimerCallback(GPTDriver *gptp) {