2 * Breathing effect code for PS2AVRGB boards (ATMEGA32A)
3 * Works in conjunction with `backlight.c`.
5 * Code adapted from `quantum.c` to register with the existing TIMER1 overflow
6 * handler in `backlight.c` instead of setting up its own timer.
7 * Kenneth A. (github.com/krusli | krusli.me)
10 #ifdef BACKLIGHT_ENABLE
11 #ifdef BACKLIGHT_BREATHING
13 #include "backlight_custom.h"
15 #ifndef BREATHING_PERIOD
16 #define BREATHING_PERIOD 6
19 #define breathing_min() do {breathing_counter = 0;} while (0)
20 #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
22 // TODO make this share code with quantum.c
24 #define BREATHING_NO_HALT 0
25 #define BREATHING_HALT_OFF 1
26 #define BREATHING_HALT_ON 2
27 #define BREATHING_STEPS 128
29 static uint8_t breathing_period = BREATHING_PERIOD;
30 static uint8_t breathing_halt = BREATHING_NO_HALT;
31 static uint16_t breathing_counter = 0;
33 static bool breathing = false;
35 bool is_breathing(void) {
39 // See http://jared.geek.nz/2013/feb/linear-led-pwm
40 static uint16_t cie_lightness(uint16_t v) {
41 if (v <= 5243) // if below 8% of max
42 return v / 9; // same as dividing by 900%
44 uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
45 // to get a useful result with integer division, we shift left in the expression above
46 // and revert what we've done again after squaring.
48 if (y > 0xFFFFUL) // prevent overflow
55 void breathing_enable(void) {
57 breathing_counter = 0;
58 breathing_halt = BREATHING_NO_HALT;
59 // interrupt already registered
62 void breathing_pulse(void) {
63 if (get_backlight_level() == 0)
67 breathing_halt = BREATHING_HALT_ON;
68 // breathing_interrupt_enable();
72 void breathing_disable(void) {
74 // backlight_set(get_backlight_level());
75 b_led_set(get_backlight_level()); // custom implementation of backlight_set()
78 void breathing_self_disable(void)
80 if (get_backlight_level() == 0)
81 breathing_halt = BREATHING_HALT_OFF;
83 breathing_halt = BREATHING_HALT_ON;
86 void breathing_toggle(void) {
93 void breathing_period_set(uint8_t value)
97 breathing_period = value;
100 void breathing_period_default(void) {
101 breathing_period_set(BREATHING_PERIOD);
104 void breathing_period_inc(void)
106 breathing_period_set(breathing_period+1);
109 void breathing_period_dec(void)
111 breathing_period_set(breathing_period-1);
114 /* To generate breathing curve in python:
115 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
117 static 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};
119 // Use this before the cie_lightness function.
120 static inline uint16_t scale_backlight(uint16_t v) {
121 return v / BACKLIGHT_LEVELS * get_backlight_level();
124 void custom_breathing_handler(void) {
125 uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
126 // resetting after one period to prevent ugly reset at overflow.
127 breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
128 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
130 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
131 ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
133 // breathing_interrupt_disable();
136 setPWM(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
139 #endif // BACKLIGHT_BREATHING
140 #endif // BACKLIGHT_ENABLE