diff options
| author | Joel Challis <git@zvecr.com> | 2020-03-06 12:49:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-06 12:49:45 +0000 |
| commit | 918a85d342aa608deac1650ddd0692dd1717c5e3 (patch) | |
| tree | 6de4d888cd8b6dba52ebf2fd82125e79762ad1a6 /quantum/backlight/backlight_avr.c | |
| parent | 116c0e44a1a4999c54019e48337c0e6b92a710f8 (diff) | |
| download | qmk_firmware-918a85d342aa608deac1650ddd0692dd1717c5e3.tar.gz qmk_firmware-918a85d342aa608deac1650ddd0692dd1717c5e3.zip | |
Refactor more backlight to a common location (#8292)
* Refactor more backlight to a common location
* BACKLIGHT_PIN not defined for custom backlight
* align function names
Diffstat (limited to 'quantum/backlight/backlight_avr.c')
| -rw-r--r-- | quantum/backlight/backlight_avr.c | 63 |
1 files changed, 5 insertions, 58 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c index ce6611fb5..40291d382 100644 --- a/quantum/backlight/backlight_avr.c +++ b/quantum/backlight/backlight_avr.c | |||
| @@ -164,49 +164,7 @@ error("Please set 'BACKLIGHT_DRIVER = custom' within rules.mk") | |||
| 164 | error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk") | 164 | error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk") |
| 165 | #endif | 165 | #endif |
| 166 | 166 | ||
| 167 | #ifndef BACKLIGHT_ON_STATE | 167 | #ifndef BACKLIGHT_PWM_TIMER // pwm through software |
| 168 | # define BACKLIGHT_ON_STATE 1 | ||
| 169 | #endif | ||
| 170 | |||
| 171 | void backlight_on(pin_t backlight_pin) { | ||
| 172 | #if BACKLIGHT_ON_STATE == 1 | ||
| 173 | writePinHigh(backlight_pin); | ||
| 174 | #else | ||
| 175 | writePinLow(backlight_pin); | ||
| 176 | #endif | ||
| 177 | } | ||
| 178 | |||
| 179 | void backlight_off(pin_t backlight_pin) { | ||
| 180 | #if BACKLIGHT_ON_STATE == 1 | ||
| 181 | writePinLow(backlight_pin); | ||
| 182 | #else | ||
| 183 | writePinHigh(backlight_pin); | ||
| 184 | #endif | ||
| 185 | } | ||
| 186 | |||
| 187 | #ifdef BACKLIGHT_PWM_TIMER // pwm through software | ||
| 188 | |||
| 189 | // we support multiple backlight pins | ||
| 190 | # ifndef BACKLIGHT_LED_COUNT | ||
| 191 | # define BACKLIGHT_LED_COUNT 1 | ||
| 192 | # endif | ||
| 193 | |||
| 194 | # if BACKLIGHT_LED_COUNT == 1 | ||
| 195 | # define BACKLIGHT_PIN_INIT \ | ||
| 196 | { BACKLIGHT_PIN } | ||
| 197 | # else | ||
| 198 | # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS | ||
| 199 | # endif | ||
| 200 | |||
| 201 | # define FOR_EACH_LED(x) \ | ||
| 202 | for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \ | ||
| 203 | pin_t backlight_pin = backlight_pins[i]; \ | ||
| 204 | { x } \ | ||
| 205 | } | ||
| 206 | |||
| 207 | static const pin_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT; | ||
| 208 | |||
| 209 | #else // full hardware PWM | ||
| 210 | 168 | ||
| 211 | static inline void enable_pwm(void) { | 169 | static inline void enable_pwm(void) { |
| 212 | # if BACKLIGHT_ON_STATE == 1 | 170 | # if BACKLIGHT_ON_STATE == 1 |
| @@ -224,10 +182,6 @@ static inline void disable_pwm(void) { | |||
| 224 | # endif | 182 | # endif |
| 225 | } | 183 | } |
| 226 | 184 | ||
| 227 | // we support only one backlight pin | ||
| 228 | static const pin_t backlight_pin = BACKLIGHT_PIN; | ||
| 229 | # define FOR_EACH_LED(x) x | ||
| 230 | |||
| 231 | #endif | 185 | #endif |
| 232 | 186 | ||
| 233 | #ifdef BACKLIGHT_PWM_TIMER | 187 | #ifdef BACKLIGHT_PWM_TIMER |
| @@ -246,7 +200,7 @@ static const pin_t backlight_pin = BACKLIGHT_PIN; | |||
| 246 | // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz. | 200 | // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz. |
| 247 | 201 | ||
| 248 | // Triggered when the counter reaches the OCRx value | 202 | // Triggered when the counter reaches the OCRx value |
| 249 | ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) } | 203 | ISR(TIMERx_COMPA_vect) { backlight_pins_off(); } |
| 250 | 204 | ||
| 251 | // Triggered when the counter reaches the TOP value | 205 | // Triggered when the counter reaches the TOP value |
| 252 | // this one triggers at F_CPU/65536 =~ 244 Hz | 206 | // this one triggers at F_CPU/65536 =~ 244 Hz |
| @@ -265,7 +219,7 @@ ISR(TIMERx_OVF_vect) { | |||
| 265 | // takes many computation cycles). | 219 | // takes many computation cycles). |
| 266 | // so better not turn them on while the counter TOP is very low. | 220 | // so better not turn them on while the counter TOP is very low. |
| 267 | if (OCRxx > 256) { | 221 | if (OCRxx > 256) { |
| 268 | FOR_EACH_LED(backlight_on(backlight_pin);) | 222 | backlight_pins_on(); |
| 269 | } | 223 | } |
| 270 | } | 224 | } |
| 271 | 225 | ||
| @@ -305,7 +259,7 @@ void backlight_set(uint8_t level) { | |||
| 305 | // Turn off PWM control on backlight pin | 259 | // Turn off PWM control on backlight pin |
| 306 | disable_pwm(); | 260 | disable_pwm(); |
| 307 | #endif | 261 | #endif |
| 308 | FOR_EACH_LED(backlight_off(backlight_pin);) | 262 | backlight_pins_off(); |
| 309 | } else { | 263 | } else { |
| 310 | #ifdef BACKLIGHT_PWM_TIMER | 264 | #ifdef BACKLIGHT_PWM_TIMER |
| 311 | if (!OCRxx) { | 265 | if (!OCRxx) { |
| @@ -397,13 +351,6 @@ void breathing_self_disable(void) { | |||
| 397 | breathing_halt = BREATHING_HALT_ON; | 351 | breathing_halt = BREATHING_HALT_ON; |
| 398 | } | 352 | } |
| 399 | 353 | ||
| 400 | void breathing_toggle(void) { | ||
| 401 | if (is_breathing()) | ||
| 402 | breathing_disable(); | ||
| 403 | else | ||
| 404 | breathing_enable(); | ||
| 405 | } | ||
| 406 | |||
| 407 | /* To generate breathing curve in python: | 354 | /* To generate breathing curve in python: |
| 408 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | 355 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] |
| 409 | */ | 356 | */ |
| @@ -438,7 +385,7 @@ ISR(TIMERx_OVF_vect) | |||
| 438 | 385 | ||
| 439 | void backlight_init_ports(void) { | 386 | void backlight_init_ports(void) { |
| 440 | // Setup backlight pin as output and output to on state. | 387 | // Setup backlight pin as output and output to on state. |
| 441 | FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);) | 388 | backlight_pins_init(); |
| 442 | 389 | ||
| 443 | // I could write a wall of text here to explain... but TL;DW | 390 | // I could write a wall of text here to explain... but TL;DW |
| 444 | // Go read the ATmega32u4 datasheet. | 391 | // Go read the ATmega32u4 datasheet. |
