71416b1b45da44b453fd344b3b913649aa1a18c5
[jackhill/qmk/firmware.git] / keyboards / skog / breathing_custom.h
1 /**
2 * Breathing effect code for PS2AVRGB boards (ATMEGA32A)
3 * Works in conjunction with `backlight.c`.
4 *
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)
8 */
9
10 #ifdef BACKLIGHT_ENABLE
11 #ifdef BACKLIGHT_BREATHING
12
13 #include "backlight_custom.h"
14
15 #ifndef BREATHING_PERIOD
16 #define BREATHING_PERIOD 6
17 #endif
18
19 #define breathing_min() do {breathing_counter = 0;} while (0)
20 #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
21
22 // TODO make this share code with quantum.c
23
24 #define BREATHING_NO_HALT 0
25 #define BREATHING_HALT_OFF 1
26 #define BREATHING_HALT_ON 2
27 #define BREATHING_STEPS 128
28
29 static uint8_t breathing_period = BREATHING_PERIOD;
30 static uint8_t breathing_halt = BREATHING_NO_HALT;
31 static uint16_t breathing_counter = 0;
32
33 static bool breathing = false;
34
35 bool is_breathing(void) {
36 return breathing;
37 }
38
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%
43 else {
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.
47 y = y * y * y >> 8;
48 if (y > 0xFFFFUL) // prevent overflow
49 return 0xFFFFU;
50 else
51 return (uint16_t) y;
52 }
53 }
54
55 void breathing_enable(void) {
56 breathing = true;
57 breathing_counter = 0;
58 breathing_halt = BREATHING_NO_HALT;
59 // interrupt already registered
60 }
61
62 void breathing_pulse(void) {
63 if (get_backlight_level() == 0)
64 breathing_min();
65 else
66 breathing_max();
67 breathing_halt = BREATHING_HALT_ON;
68 // breathing_interrupt_enable();
69 breathing = true;
70 }
71
72 void breathing_disable(void) {
73 breathing = false;
74 // backlight_set(get_backlight_level());
75 b_led_set(get_backlight_level()); // custom implementation of backlight_set()
76 }
77
78 void breathing_self_disable(void)
79 {
80 if (get_backlight_level() == 0)
81 breathing_halt = BREATHING_HALT_OFF;
82 else
83 breathing_halt = BREATHING_HALT_ON;
84 }
85
86 void breathing_toggle(void) {
87 if (is_breathing())
88 breathing_disable();
89 else
90 breathing_enable();
91 }
92
93 void breathing_period_set(uint8_t value)
94 {
95 if (!value)
96 value = 1;
97 breathing_period = value;
98 }
99
100 void breathing_period_default(void) {
101 breathing_period_set(BREATHING_PERIOD);
102 }
103
104 void breathing_period_inc(void)
105 {
106 breathing_period_set(breathing_period+1);
107 }
108
109 void breathing_period_dec(void)
110 {
111 breathing_period_set(breathing_period-1);
112 }
113
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)]
116 */
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};
118
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();
122 }
123
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;
129
130 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
131 ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
132 {
133 // breathing_interrupt_disable();
134 }
135
136 setPWM(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
137 }
138
139 #endif // BACKLIGHT_BREATHING
140 #endif // BACKLIGHT_ENABLE