fd698c43 |
1 | /* Copyright 2017 Jason Williams |
2 | * Copyright 2017 Jack Humbert |
3 | * Copyright 2018 Yiancar |
4 | * Copyright 2019 Clueboard |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include <stdint.h> |
21 | #include <stdbool.h> |
22 | #include "quantum.h" |
c5221fa1 |
23 | #include "ledmatrix.h" |
fd698c43 |
24 | #include "progmem.h" |
25 | #include "config.h" |
26 | #include "eeprom.h" |
27 | #include <string.h> |
28 | #include <math.h> |
29 | |
30 | led_config_t led_matrix_config; |
31 | |
32 | #ifndef MAX |
33 | #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) |
34 | #endif |
35 | |
36 | #ifndef MIN |
37 | #define MIN(a,b) ((a) < (b)? (a): (b)) |
38 | #endif |
39 | |
40 | #ifndef LED_DISABLE_AFTER_TIMEOUT |
41 | #define LED_DISABLE_AFTER_TIMEOUT 0 |
42 | #endif |
43 | |
44 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED |
45 | #define LED_DISABLE_WHEN_USB_SUSPENDED false |
46 | #endif |
47 | |
48 | #ifndef EECONFIG_LED_MATRIX |
49 | #define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT |
50 | #endif |
51 | |
52 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 |
53 | #define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 |
54 | #endif |
55 | |
56 | bool g_suspend_state = false; |
57 | |
58 | // Global tick at 20 Hz |
59 | uint32_t g_tick = 0; |
60 | |
61 | // Ticks since this key was last hit. |
bf267060 |
62 | uint8_t g_key_hit[LED_DRIVER_LED_COUNT]; |
fd698c43 |
63 | |
64 | // Ticks since any key was last hit. |
65 | uint32_t g_any_key_hit = 0; |
66 | |
67 | uint32_t eeconfig_read_led_matrix(void) { |
68 | return eeprom_read_dword(EECONFIG_LED_MATRIX); |
69 | } |
c080a3e7 |
70 | |
fd698c43 |
71 | void eeconfig_update_led_matrix(uint32_t config_value) { |
72 | eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); |
73 | } |
c080a3e7 |
74 | |
fd698c43 |
75 | void eeconfig_update_led_matrix_default(void) { |
76 | dprintf("eeconfig_update_led_matrix_default\n"); |
77 | led_matrix_config.enable = 1; |
78 | led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; |
79 | led_matrix_config.val = 128; |
80 | led_matrix_config.speed = 0; |
81 | eeconfig_update_led_matrix(led_matrix_config.raw); |
82 | } |
c080a3e7 |
83 | |
fd698c43 |
84 | void eeconfig_debug_led_matrix(void) { |
32116f1a |
85 | dprintf("led_matrix_config eeprom\n"); |
fd698c43 |
86 | dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable); |
87 | dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode); |
88 | dprintf("led_matrix_config.val = %d\n", led_matrix_config.val); |
89 | dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed); |
90 | } |
91 | |
92 | // Last led hit |
c080a3e7 |
93 | #ifndef LED_HITS_TO_REMEMBER |
94 | #define LED_HITS_TO_REMEMBER 8 |
95 | #endif |
fd698c43 |
96 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; |
97 | uint8_t g_last_led_count = 0; |
98 | |
99 | void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { |
100 | led_matrix led; |
101 | *led_count = 0; |
102 | |
bf267060 |
103 | for (uint8_t i = 0; i < LED_DRIVER_LED_COUNT; i++) { |
fd698c43 |
104 | // map_index_to_led(i, &led); |
105 | led = g_leds[i]; |
106 | if (row == led.matrix_co.row && column == led.matrix_co.col) { |
107 | led_i[*led_count] = i; |
108 | (*led_count)++; |
109 | } |
110 | } |
111 | } |
112 | |
113 | void led_matrix_update_pwm_buffers(void) { |
114 | led_matrix_driver.flush(); |
115 | } |
116 | |
117 | void led_matrix_set_index_value(int index, uint8_t value) { |
118 | led_matrix_driver.set_value(index, value); |
119 | } |
120 | |
121 | void led_matrix_set_index_value_all(uint8_t value) { |
122 | led_matrix_driver.set_value_all(value); |
123 | } |
124 | |
125 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { |
126 | if (record->event.pressed) { |
127 | uint8_t led[8], led_count; |
128 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); |
129 | if (led_count > 0) { |
130 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { |
131 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; |
132 | } |
133 | g_last_led_hit[0] = led[0]; |
134 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); |
135 | } |
136 | for(uint8_t i = 0; i < led_count; i++) |
137 | g_key_hit[led[i]] = 0; |
138 | g_any_key_hit = 0; |
139 | } else { |
140 | #ifdef LED_MATRIX_KEYRELEASES |
141 | uint8_t led[8], led_count; |
142 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); |
143 | for(uint8_t i = 0; i < led_count; i++) |
144 | g_key_hit[led[i]] = 255; |
145 | |
146 | g_any_key_hit = 255; |
147 | #endif |
148 | } |
149 | return true; |
150 | } |
151 | |
152 | void led_matrix_set_suspend_state(bool state) { |
153 | g_suspend_state = state; |
154 | } |
155 | |
156 | // All LEDs off |
157 | void led_matrix_all_off(void) { |
158 | led_matrix_set_index_value_all(0); |
159 | } |
160 | |
161 | // Uniform brightness |
162 | void led_matrix_uniform_brightness(void) { |
c080a3e7 |
163 | led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_config.val); |
fd698c43 |
164 | } |
165 | |
166 | void led_matrix_custom(void) {} |
167 | |
168 | void led_matrix_task(void) { |
6b74dd6d |
169 | if (!led_matrix_config.enable) { |
170 | led_matrix_all_off(); |
171 | led_matrix_indicators(); |
172 | return; |
fd698c43 |
173 | } |
174 | |
fd698c43 |
175 | g_tick++; |
176 | |
177 | if (g_any_key_hit < 0xFFFFFFFF) { |
178 | g_any_key_hit++; |
179 | } |
180 | |
bf267060 |
181 | for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) { |
fd698c43 |
182 | if (g_key_hit[led] < 255) { |
183 | if (g_key_hit[led] == 254) |
184 | g_last_led_count = MAX(g_last_led_count - 1, 0); |
185 | g_key_hit[led]++; |
186 | } |
187 | } |
188 | |
fd698c43 |
189 | // Ideally we would also stop sending zeros to the LED driver PWM buffers |
190 | // while suspended and just do a software shutdown. This is a cheap hack for now. |
191 | bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || |
192 | (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20)); |
193 | uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode; |
194 | |
fd698c43 |
195 | // this gets ticked at 20 Hz. |
196 | // each effect can opt to do calculations |
197 | // and/or request PWM buffer updates. |
198 | switch (effect) { |
199 | case LED_MATRIX_UNIFORM_BRIGHTNESS: |
200 | led_matrix_uniform_brightness(); |
201 | break; |
202 | default: |
203 | led_matrix_custom(); |
204 | break; |
205 | } |
206 | |
6b74dd6d |
207 | if (!suspend_backlight) { |
fd698c43 |
208 | led_matrix_indicators(); |
209 | } |
210 | |
6b74dd6d |
211 | // Tell the LED driver to update its state |
212 | led_matrix_driver.flush(); |
fd698c43 |
213 | } |
214 | |
215 | void led_matrix_indicators(void) { |
216 | led_matrix_indicators_kb(); |
217 | led_matrix_indicators_user(); |
218 | } |
219 | |
220 | __attribute__((weak)) |
221 | void led_matrix_indicators_kb(void) {} |
222 | |
223 | __attribute__((weak)) |
224 | void led_matrix_indicators_user(void) {} |
225 | |
226 | |
227 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) |
228 | // { |
229 | // if (row >= MATRIX_ROWS) |
230 | // { |
231 | // // Special value, 255=none, 254=all |
232 | // *index = row; |
233 | // } |
234 | // else |
235 | // { |
236 | // // This needs updated to something like |
237 | // // uint8_t led[8], led_count; |
238 | // // map_row_column_to_led(row,column,led,&led_count); |
239 | // // for(uint8_t i = 0; i < led_count; i++) |
240 | // map_row_column_to_led(row, column, index); |
241 | // } |
242 | // } |
243 | |
244 | void led_matrix_init(void) { |
c080a3e7 |
245 | led_matrix_driver.init(); |
fd698c43 |
246 | |
c080a3e7 |
247 | // Wait half a second for the driver to finish initializing |
248 | wait_ms(500); |
fd698c43 |
249 | |
c080a3e7 |
250 | // clear the key hits |
251 | for (int led=0; led<LED_DRIVER_LED_COUNT; led++) { |
252 | g_key_hit[led] = 255; |
253 | } |
fd698c43 |
254 | |
c080a3e7 |
255 | if (!eeconfig_is_enabled()) { |
256 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); |
257 | eeconfig_init(); |
258 | eeconfig_update_led_matrix_default(); |
259 | } |
32116f1a |
260 | |
c080a3e7 |
261 | led_matrix_config.raw = eeconfig_read_led_matrix(); |
32116f1a |
262 | |
c080a3e7 |
263 | if (!led_matrix_config.mode) { |
264 | dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n"); |
265 | eeconfig_update_led_matrix_default(); |
266 | led_matrix_config.raw = eeconfig_read_led_matrix(); |
267 | } |
32116f1a |
268 | |
c080a3e7 |
269 | eeconfig_debug_led_matrix(); // display current eeprom values |
fd698c43 |
270 | } |
271 | |
272 | // Deals with the messy details of incrementing an integer |
273 | static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { |
274 | int16_t new_value = value; |
275 | new_value += step; |
c080a3e7 |
276 | return MIN(MAX(new_value, min), max); |
fd698c43 |
277 | } |
278 | |
279 | static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { |
280 | int16_t new_value = value; |
281 | new_value -= step; |
c080a3e7 |
282 | return MIN(MAX(new_value, min), max); |
fd698c43 |
283 | } |
284 | |
285 | // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) { |
286 | // // 3 bytes per value |
287 | // return EECONFIG_LED_MATRIX + (led * 3); |
288 | // } |
289 | |
290 | // void backlight_get_key_value(uint8_t led, uint8_t *value) { |
291 | // void *address = backlight_get_custom_key_value_eeprom_address(led); |
292 | // value = eeprom_read_byte(address); |
293 | // } |
294 | |
295 | // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) { |
296 | // uint8_t led[8], led_count; |
297 | // map_row_column_to_led(row,column,led,&led_count); |
298 | // for(uint8_t i = 0; i < led_count; i++) { |
bf267060 |
299 | // if (led[i] < LED_DRIVER_LED_COUNT) { |
fd698c43 |
300 | // void *address = backlight_get_custom_key_value_eeprom_address(led[i]); |
301 | // eeprom_update_byte(address, value); |
302 | // } |
303 | // } |
304 | // } |
305 | |
306 | uint32_t led_matrix_get_tick(void) { |
307 | return g_tick; |
308 | } |
309 | |
310 | void led_matrix_toggle(void) { |
c080a3e7 |
311 | led_matrix_config.enable ^= 1; |
fd698c43 |
312 | eeconfig_update_led_matrix(led_matrix_config.raw); |
313 | } |
314 | |
315 | void led_matrix_enable(void) { |
c080a3e7 |
316 | led_matrix_config.enable = 1; |
fd698c43 |
317 | eeconfig_update_led_matrix(led_matrix_config.raw); |
318 | } |
319 | |
320 | void led_matrix_enable_noeeprom(void) { |
c080a3e7 |
321 | led_matrix_config.enable = 1; |
fd698c43 |
322 | } |
323 | |
324 | void led_matrix_disable(void) { |
c080a3e7 |
325 | led_matrix_config.enable = 0; |
fd698c43 |
326 | eeconfig_update_led_matrix(led_matrix_config.raw); |
327 | } |
328 | |
329 | void led_matrix_disable_noeeprom(void) { |
c080a3e7 |
330 | led_matrix_config.enable = 0; |
fd698c43 |
331 | } |
332 | |
333 | void led_matrix_step(void) { |
334 | led_matrix_config.mode++; |
c080a3e7 |
335 | if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX) { |
fd698c43 |
336 | led_matrix_config.mode = 1; |
c080a3e7 |
337 | } |
fd698c43 |
338 | eeconfig_update_led_matrix(led_matrix_config.raw); |
339 | } |
340 | |
341 | void led_matrix_step_reverse(void) { |
342 | led_matrix_config.mode--; |
c080a3e7 |
343 | if (led_matrix_config.mode < 1) { |
fd698c43 |
344 | led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1; |
c080a3e7 |
345 | } |
fd698c43 |
346 | eeconfig_update_led_matrix(led_matrix_config.raw); |
347 | } |
348 | |
349 | void led_matrix_increase_val(void) { |
350 | led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); |
351 | eeconfig_update_led_matrix(led_matrix_config.raw); |
352 | } |
353 | |
354 | void led_matrix_decrease_val(void) { |
355 | led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); |
356 | eeconfig_update_led_matrix(led_matrix_config.raw); |
357 | } |
358 | |
359 | void led_matrix_increase_speed(void) { |
360 | led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3); |
361 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this |
362 | } |
363 | |
364 | void led_matrix_decrease_speed(void) { |
365 | led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3); |
366 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this |
367 | } |
368 | |
369 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { |
370 | led_matrix_config.mode = mode; |
371 | if (eeprom_write) { |
372 | eeconfig_update_led_matrix(led_matrix_config.raw); |
373 | } |
374 | } |
375 | |
376 | uint8_t led_matrix_get_mode(void) { |
377 | return led_matrix_config.mode; |
378 | } |
379 | |
bf267060 |
380 | void led_matrix_set_value_noeeprom(uint8_t val) { |
fd698c43 |
381 | led_matrix_config.val = val; |
bf267060 |
382 | } |
383 | |
384 | void led_matrix_set_value(uint8_t val) { |
385 | led_matrix_set_value_noeeprom(val); |
386 | eeconfig_update_led_matrix(led_matrix_config.raw); |
fd698c43 |
387 | } |
6b74dd6d |
388 | |
389 | void backlight_set(uint8_t val) { |
390 | led_matrix_set_value(val); |
391 | } |