aboutsummaryrefslogtreecommitdiff
path: root/quantum/backlight/backlight_avr.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/backlight/backlight_avr.c')
-rw-r--r--quantum/backlight/backlight_avr.c55
1 files changed, 26 insertions, 29 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index 59050e250..e3fe56aba 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -237,8 +237,8 @@ static uint16_t cie_lightness(uint16_t v) {
237 return v / 9; // Same as dividing by 900% 237 return v / 9; // Same as dividing by 900%
238 } else { 238 } else {
239 // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math. 239 // 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 + ICRx / 6) << 5) / (ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max) 240 uint32_t y = (((uint32_t)v + ICRx / 6) << 5) / (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) 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)
242 242
243 if (out > ICRx) // Avoid overflows 243 if (out > ICRx) // Avoid overflows
244 { 244 {
@@ -297,7 +297,7 @@ static uint16_t breathing_counter = 0;
297 297
298static uint8_t breath_scale_counter = 1; 298static uint8_t breath_scale_counter = 1;
299/* Run the breathing loop at ~120Hz*/ 299/* Run the breathing loop at ~120Hz*/
300const uint8_t breathing_ISR_frequency = 120; 300const uint8_t breathing_ISR_frequency = 120;
301static uint16_t breathing_freq_scale_factor = 2; 301static uint16_t breathing_freq_scale_factor = 2;
302 302
303# ifdef BACKLIGHT_PWM_TIMER 303# ifdef BACKLIGHT_PWM_TIMER
@@ -327,14 +327,14 @@ bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
327 } while (0) 327 } while (0)
328# endif 328# endif
329 329
330# define breathing_min() \ 330# define breathing_min() \
331 do { \ 331 do { \
332 breathing_counter = 0; \ 332 breathing_counter = 0; \
333 } while (0) 333 } while (0)
334# define breathing_max() \ 334# define breathing_max() \
335 do { \ 335 do { \
336 breathing_counter = breathing_period * breathing_ISR_frequency / 2; \ 336 breathing_counter = breathing_period * breathing_ISR_frequency / 2; \
337 } while (0) 337 } while (0)
338 338
339void breathing_enable(void) { 339void breathing_enable(void) {
340 breathing_counter = 0; 340 breathing_counter = 0;
@@ -385,18 +385,15 @@ ISR(TIMERx_OVF_vect)
385{ 385{
386 386
387 // Only run this ISR at ~120 Hz 387 // Only run this ISR at ~120 Hz
388 if(breath_scale_counter++ == breathing_freq_scale_factor) 388 if (breath_scale_counter++ == breathing_freq_scale_factor) {
389 {
390 breath_scale_counter = 1; 389 breath_scale_counter = 1;
391 } 390 } else {
392 else
393 {
394 return; 391 return;
395 } 392 }
396 uint16_t interval = (uint16_t)breathing_period * breathing_ISR_frequency / BREATHING_STEPS; 393 uint16_t interval = (uint16_t)breathing_period * breathing_ISR_frequency / BREATHING_STEPS;
397 // resetting after one period to prevent ugly reset at overflow. 394 // resetting after one period to prevent ugly reset at overflow.
398 breathing_counter = (breathing_counter + 1) % (breathing_period * breathing_ISR_frequency); 395 breathing_counter = (breathing_counter + 1) % (breathing_period * breathing_ISR_frequency);
399 uint8_t index = breathing_counter / interval % BREATHING_STEPS; 396 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
400 397
401 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) { 398 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
402 breathing_interrupt_disable(); 399 breathing_interrupt_disable();
@@ -435,21 +432,21 @@ void backlight_init_ports(void) {
435 */ 432 */
436 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010; 433 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
437 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; 434 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
438# endif 435#endif
439 436
440# ifdef BACKLIGHT_CUSTOM_RESOLUTION 437#ifdef BACKLIGHT_CUSTOM_RESOLUTION
441# if (BACKLIGHT_CUSTOM_RESOLUTION > 0xFFFF || BACKLIGHT_CUSTOM_RESOLUTION < 1) 438# if (BACKLIGHT_CUSTOM_RESOLUTION > 0xFFFF || BACKLIGHT_CUSTOM_RESOLUTION < 1)
442# error "This out of range of the timer capabilities" 439# error "This out of range of the timer capabilities"
443# elif (BACKLIGHT_CUSTOM_RESOLUTION < 0xFF) 440# elif (BACKLIGHT_CUSTOM_RESOLUTION < 0xFF)
444# warning "Resolution lower than 0xFF isn't recommended" 441# warning "Resolution lower than 0xFF isn't recommended"
445# endif 442# endif
446# ifdef BACKLIGHT_BREATHING 443# ifdef BACKLIGHT_BREATHING
447 breathing_freq_scale_factor = F_CPU / BACKLIGHT_CUSTOM_RESOLUTION / 120; 444 breathing_freq_scale_factor = F_CPU / BACKLIGHT_CUSTOM_RESOLUTION / 120;
448# endif 445# endif
449 ICRx = BACKLIGHT_CUSTOM_RESOLUTION; 446 ICRx = BACKLIGHT_CUSTOM_RESOLUTION;
450# else 447#else
451 ICRx = TIMER_TOP; 448 ICRx = TIMER_TOP;
452# endif 449#endif
453 450
454 backlight_init(); 451 backlight_init();
455#ifdef BACKLIGHT_BREATHING 452#ifdef BACKLIGHT_BREATHING