]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/zer09/lights.c
Remove more commented out MCUs
[qmk_firmware.git] / users / zer09 / lights.c
1 #include "lights.h"
2
3 static bool active_key_pos[50] = {};
4 static uint8_t led_dim = 0;
5
6 volatile led_key rbw_led_keys[RBW] = {
7     [RBW_LCTL] = {DEFAULT, 21, true},  [RBW_LCAP] = {DEFAULT, 24, false},
8     [RBW_LSPR] = {DEFAULT, 23, true},  [RBW_RCTL] = {DEFAULT, 48, true},
9     [RBW_RCAP] = {DEFAULT, 45, false}, [RBW_RALT] = {DEFAULT, 46, true},
10     [RBW_SCRL] = {DEFAULT, 42, true}};
11
12 /* Pressed led color. */
13 const uint32_t _PC[3] = {0xFF, 0x00, 0x00};
14
15 /* Layer color. */
16 const uint8_t _LC[5][3] = {[_BL] = {0x00, 0x00, 0x00},
17                            [_UL] = {0x00, 0x00, 0xFF},
18                            [_VL] = {0xFF, 0xFF, 0x00},
19                            [_DL] = {0x00, 0xFF, 0x00},
20                            [_AL] = {0xFF, 0x00, 0x00}};
21
22 /* Color table by sine wave */
23 const uint8_t _LIGHTS[360] = {
24     0,   0,   0,   0,   0,   1,   1,   2,   2,   3,   4,   5,   6,   7,   8,
25     11,  12,  9,   13,  15,  17,  18,  20,  22,  24,  26,  28,  30,  32,  35,
26     37,  39,  42,  44,  47,  49,  52,  55,  58,  60,  63,  66,  69,  72,  75,
27     78,  81,  85,  88,  91,  94,  97,  101, 104, 107, 111, 114, 117, 121, 124,
28     127, 131, 134, 137, 141, 144, 147, 150, 154, 157, 160, 163, 167, 170, 173,
29     176, 179, 182, 185, 188, 191, 194, 197, 200, 202, 205, 208, 210, 213, 215,
30     217, 220, 222, 224, 226, 229, 231, 232, 234, 236, 238, 239, 241, 242, 244,
31     245, 246, 248, 249, 250, 251, 251, 252, 253, 253, 254, 254, 255, 255, 255,
32     255, 255, 255, 255, 254, 254, 253, 253, 252, 251, 251, 250, 249, 248, 246,
33     245, 244, 242, 241, 239, 238, 236, 234, 232, 231, 229, 226, 224, 222, 220,
34     217, 215, 213, 210, 208, 205, 202, 200, 197, 194, 191, 188, 185, 182, 179,
35     176, 173, 170, 167, 163, 160, 157, 154, 150, 147, 144, 141, 137, 134, 131,
36     127, 124, 121, 117, 114, 111, 107, 104, 101, 97,  94,  91,  88,  85,  81,
37     78,  75,  72,  69,  66,  63,  60,  58,  55,  52,  49,  47,  44,  42,  39,
38     37,  35,  32,  30,  28,  26,  24,  22,  20,  18,  17,  15,  13,  12,  11,
39     9,   8,   7,   6,   5,   4,   3,   2,   2,   1,   1,   0,   0,   0,   0,
40     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
41     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
42     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
43     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
44     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
45     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
46     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
47     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0};
48
49 void set_key_led(keyrecord_t *record, uint8_t lyr) {
50   static uint8_t base = 5;
51
52   uint8_t r = record->event.key.row;
53   uint8_t c = record->event.key.col;
54   uint8_t pos;
55
56   // This was the result of my soldering.
57   // Lesson of the day: always check.
58   if (r < 5) {
59     pos = r % 2 == 0 ? r * base + c : r * base + (base - (c + 1));
60   } else {
61     pos = r % 2 == 0 ? r * base + (base - (c + 1)) : r * base + c;
62   }
63
64   if (record->event.pressed) {
65     active_key_pos[pos] = true;
66     SET_LED_RGB(_PC[0], _PC[1], _PC[2], led_dim, pos);
67   } else {
68     active_key_pos[pos] = false;
69     SET_LED_RGB(_LC[lyr][0], _LC[lyr][1], _LC[lyr][2], led_dim, pos);
70   }
71 }
72
73 /* Fix for unknown reason after testin flash the eeprom handedness,
74    the leds will ligth up after boot.
75    This should be call in the set layer led.*/
76 bool is_first_run(void) {
77   static uint8_t run = 0;
78
79   if (run == 0) {
80     run++;
81     return true;
82   } else {
83     return false;
84   }
85 }
86
87 bool set_layer_led(uint8_t lyr) {
88   static uint8_t p_lyr = 0; // Previous layer.
89   static uint8_t p_dim = 0; // Previous dim.
90
91   if (p_lyr == lyr && p_dim == led_dim && !is_first_run()) {
92     return false;
93   }
94
95   p_lyr = lyr;
96   p_dim = led_dim;
97   const uint8_t r = _LC[lyr][0];
98   const uint8_t g = _LC[lyr][1];
99   const uint8_t b = _LC[lyr][2];
100   const uint8_t d = (p_lyr == _VL && p_dim < 1) ? 1 : p_dim;
101
102   for (uint8_t i = 0; i < RGBLED_NUM; i++) {
103     SET_LED_RGB(r, g, b, d, i);
104   }
105
106   return true;
107 }
108
109 bool rainbow_loop(uint8_t lyr) {
110   static uint16_t last_timer = 0;
111   static uint16_t i = 0;
112   static uint8_t r, g, b, pos;
113
114   if (timer_elapsed(last_timer) < 8) {
115     return false;
116   }
117
118   if (i >= 360) {
119     i = 0;
120   }
121
122   last_timer = timer_read();
123   r = _LIGHTS[(i + 120) % 360];
124   g = _LIGHTS[i];
125   b = _LIGHTS[(i + 240) % 360];
126
127   i++;
128
129   bool set_rbw = false;
130
131   for (uint8_t j = 0; j < RBW; j++) {
132     pos = rbw_led_keys[j].pos;
133
134     switch (rbw_led_keys[j].status) {
135     case ENABLED:
136       if (!active_key_pos[pos] || rbw_led_keys[j].forced) {
137         SET_LED_RGB(r, g, b, led_dim, pos);
138         set_rbw = true;
139       }
140
141       break;
142     case DISABLED:
143       if (!active_key_pos[pos] || rbw_led_keys[j].forced) {
144         SET_LED_RGB(_LC[lyr][0], _LC[lyr][1], _LC[lyr][2], led_dim, pos);
145         set_rbw = true;
146       }
147
148       rbw_led_keys[j].status = DEFAULT;
149       break;
150     default:
151       break;
152     }
153   }
154
155   return set_rbw;
156 }
157
158 bool led_brightness(uint16_t keycode, keyrecord_t *record) {
159   switch (keycode) {
160   case RGUP:
161     if (record->event.pressed && led_dim > 0) {
162       led_dim--;
163       eeprom_write_byte(EECONFIG_LED_DIM_LVL, led_dim);
164     }
165
166     return true;
167     break;
168   case RGDWN:
169     if (record->event.pressed && led_dim < 8) {
170       led_dim++;
171       eeprom_write_byte(EECONFIG_LED_DIM_LVL, led_dim);
172     }
173
174     return true;
175     break;
176   default:
177     return false;
178     break;
179   }
180 }
181
182 void eeprom_read_led_dim_lvl(void) {
183   led_dim = eeprom_read_byte(EECONFIG_LED_DIM_LVL);
184
185   if (led_dim > 8 || led_dim < 0) {
186     led_dim = 0;
187     eeprom_write_byte(EECONFIG_LED_DIM_LVL, led_dim);
188   }
189 }