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" |
23 | #include "led_matrix.h" |
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 | |
6b74dd6d |
58 | // Last uniform brightness level. |
59 | uint8_t g_uniform_brightness = 0; |
60 | |
fd698c43 |
61 | // Global tick at 20 Hz |
62 | uint32_t g_tick = 0; |
63 | |
64 | // Ticks since this key was last hit. |
bf267060 |
65 | uint8_t g_key_hit[LED_DRIVER_LED_COUNT]; |
fd698c43 |
66 | |
67 | // Ticks since any key was last hit. |
68 | uint32_t g_any_key_hit = 0; |
69 | |
70 | uint32_t eeconfig_read_led_matrix(void) { |
71 | return eeprom_read_dword(EECONFIG_LED_MATRIX); |
72 | } |
73 | void eeconfig_update_led_matrix(uint32_t config_value) { |
74 | eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); |
75 | } |
76 | void eeconfig_update_led_matrix_default(void) { |
77 | dprintf("eeconfig_update_led_matrix_default\n"); |
78 | led_matrix_config.enable = 1; |
79 | led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; |
80 | led_matrix_config.val = 128; |
81 | led_matrix_config.speed = 0; |
82 | eeconfig_update_led_matrix(led_matrix_config.raw); |
83 | } |
84 | void eeconfig_debug_led_matrix(void) { |
85 | dprintf("led_matrix_config eprom\n"); |
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 |
93 | #define LED_HITS_TO_REMEMBER 8 |
94 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; |
95 | uint8_t g_last_led_count = 0; |
96 | |
97 | void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { |
98 | led_matrix led; |
99 | *led_count = 0; |
100 | |
bf267060 |
101 | for (uint8_t i = 0; i < LED_DRIVER_LED_COUNT; i++) { |
fd698c43 |
102 | // map_index_to_led(i, &led); |
103 | led = g_leds[i]; |
104 | if (row == led.matrix_co.row && column == led.matrix_co.col) { |
105 | led_i[*led_count] = i; |
106 | (*led_count)++; |
107 | } |
108 | } |
109 | } |
110 | |
111 | void led_matrix_update_pwm_buffers(void) { |
112 | led_matrix_driver.flush(); |
113 | } |
114 | |
115 | void led_matrix_set_index_value(int index, uint8_t value) { |
116 | led_matrix_driver.set_value(index, value); |
117 | } |
118 | |
119 | void led_matrix_set_index_value_all(uint8_t value) { |
120 | led_matrix_driver.set_value_all(value); |
121 | } |
122 | |
123 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { |
6b74dd6d |
124 | /* FIXME: Why you comment out skully? |
fd698c43 |
125 | if (record->event.pressed) { |
126 | uint8_t led[8], led_count; |
127 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); |
128 | if (led_count > 0) { |
129 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { |
130 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; |
131 | } |
132 | g_last_led_hit[0] = led[0]; |
133 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); |
134 | } |
135 | for(uint8_t i = 0; i < led_count; i++) |
136 | g_key_hit[led[i]] = 0; |
137 | g_any_key_hit = 0; |
138 | } else { |
139 | #ifdef LED_MATRIX_KEYRELEASES |
140 | uint8_t led[8], led_count; |
141 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); |
142 | for(uint8_t i = 0; i < led_count; i++) |
143 | g_key_hit[led[i]] = 255; |
144 | |
145 | g_any_key_hit = 255; |
146 | #endif |
147 | } |
6b74dd6d |
148 | */ |
fd698c43 |
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) { |
6b74dd6d |
163 | uint8_t current_brightness = (LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS) * led_matrix_config.val; |
164 | if (current_brightness != g_uniform_brightness) { |
165 | g_uniform_brightness = current_brightness; |
166 | led_matrix_set_index_value_all(current_brightness); |
167 | } |
fd698c43 |
168 | } |
169 | |
170 | void led_matrix_custom(void) {} |
171 | |
172 | void led_matrix_task(void) { |
6b74dd6d |
173 | if (!led_matrix_config.enable) { |
174 | led_matrix_all_off(); |
175 | led_matrix_indicators(); |
176 | return; |
fd698c43 |
177 | } |
178 | |
179 | // delay 1 second before driving LEDs or doing anything else |
180 | // FIXME: Can't we use wait_ms() here? |
181 | static uint8_t startup_tick = 0; |
182 | if (startup_tick < 20) { |
183 | startup_tick++; |
184 | return; |
185 | } |
186 | |
187 | g_tick++; |
188 | |
189 | if (g_any_key_hit < 0xFFFFFFFF) { |
190 | g_any_key_hit++; |
191 | } |
192 | |
6b74dd6d |
193 | /* FIXME: WHY YOU COMMENT OUT?! |
bf267060 |
194 | for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) { |
fd698c43 |
195 | if (g_key_hit[led] < 255) { |
196 | if (g_key_hit[led] == 254) |
197 | g_last_led_count = MAX(g_last_led_count - 1, 0); |
198 | g_key_hit[led]++; |
199 | } |
200 | } |
201 | |
202 | // Factory default magic value |
203 | if (led_matrix_config.mode == 255) { |
204 | led_matrix_uniform_brightness(); |
205 | return; |
206 | } |
6b74dd6d |
207 | */ |
fd698c43 |
208 | // Ideally we would also stop sending zeros to the LED driver PWM buffers |
209 | // while suspended and just do a software shutdown. This is a cheap hack for now. |
210 | bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || |
211 | (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20)); |
212 | uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode; |
213 | |
fd698c43 |
214 | // this gets ticked at 20 Hz. |
215 | // each effect can opt to do calculations |
216 | // and/or request PWM buffer updates. |
217 | switch (effect) { |
218 | case LED_MATRIX_UNIFORM_BRIGHTNESS: |
219 | led_matrix_uniform_brightness(); |
220 | break; |
221 | default: |
222 | led_matrix_custom(); |
223 | break; |
224 | } |
225 | |
6b74dd6d |
226 | if (!suspend_backlight) { |
fd698c43 |
227 | led_matrix_indicators(); |
228 | } |
229 | |
6b74dd6d |
230 | // Tell the LED driver to update its state |
231 | led_matrix_driver.flush(); |
fd698c43 |
232 | } |
233 | |
234 | void led_matrix_indicators(void) { |
235 | led_matrix_indicators_kb(); |
236 | led_matrix_indicators_user(); |
237 | } |
238 | |
239 | __attribute__((weak)) |
240 | void led_matrix_indicators_kb(void) {} |
241 | |
242 | __attribute__((weak)) |
243 | void led_matrix_indicators_user(void) {} |
244 | |
245 | |
246 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) |
247 | // { |
248 | // if (row >= MATRIX_ROWS) |
249 | // { |
250 | // // Special value, 255=none, 254=all |
251 | // *index = row; |
252 | // } |
253 | // else |
254 | // { |
255 | // // This needs updated to something like |
256 | // // uint8_t led[8], led_count; |
257 | // // map_row_column_to_led(row,column,led,&led_count); |
258 | // // for(uint8_t i = 0; i < led_count; i++) |
259 | // map_row_column_to_led(row, column, index); |
260 | // } |
261 | // } |
262 | |
263 | void led_matrix_init(void) { |
264 | led_matrix_driver.init(); |
265 | |
266 | // TODO: put the 1 second startup delay here? |
267 | |
268 | // clear the key hits |
bf267060 |
269 | for (int led=0; led<LED_DRIVER_LED_COUNT; led++) { |
fd698c43 |
270 | g_key_hit[led] = 255; |
271 | } |
272 | |
273 | |
274 | if (!eeconfig_is_enabled()) { |
275 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); |
276 | eeconfig_init(); |
277 | eeconfig_update_led_matrix_default(); |
278 | } |
279 | led_matrix_config.raw = eeconfig_read_led_matrix(); |
280 | if (!led_matrix_config.mode) { |
281 | dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n"); |
282 | eeconfig_update_led_matrix_default(); |
283 | led_matrix_config.raw = eeconfig_read_led_matrix(); |
284 | } |
285 | eeconfig_debug_led_matrix(); // display current eeprom values |
286 | } |
287 | |
288 | // Deals with the messy details of incrementing an integer |
289 | static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { |
290 | int16_t new_value = value; |
291 | new_value += step; |
292 | return MIN(MAX( new_value, min), max ); |
293 | } |
294 | |
295 | static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { |
296 | int16_t new_value = value; |
297 | new_value -= step; |
298 | return MIN(MAX( new_value, min), max ); |
299 | } |
300 | |
301 | // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) { |
302 | // // 3 bytes per value |
303 | // return EECONFIG_LED_MATRIX + (led * 3); |
304 | // } |
305 | |
306 | // void backlight_get_key_value(uint8_t led, uint8_t *value) { |
307 | // void *address = backlight_get_custom_key_value_eeprom_address(led); |
308 | // value = eeprom_read_byte(address); |
309 | // } |
310 | |
311 | // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) { |
312 | // uint8_t led[8], led_count; |
313 | // map_row_column_to_led(row,column,led,&led_count); |
314 | // for(uint8_t i = 0; i < led_count; i++) { |
bf267060 |
315 | // if (led[i] < LED_DRIVER_LED_COUNT) { |
fd698c43 |
316 | // void *address = backlight_get_custom_key_value_eeprom_address(led[i]); |
317 | // eeprom_update_byte(address, value); |
318 | // } |
319 | // } |
320 | // } |
321 | |
322 | uint32_t led_matrix_get_tick(void) { |
323 | return g_tick; |
324 | } |
325 | |
326 | void led_matrix_toggle(void) { |
327 | led_matrix_config.enable ^= 1; |
328 | eeconfig_update_led_matrix(led_matrix_config.raw); |
329 | } |
330 | |
331 | void led_matrix_enable(void) { |
332 | led_matrix_config.enable = 1; |
333 | eeconfig_update_led_matrix(led_matrix_config.raw); |
334 | } |
335 | |
336 | void led_matrix_enable_noeeprom(void) { |
337 | led_matrix_config.enable = 1; |
338 | } |
339 | |
340 | void led_matrix_disable(void) { |
341 | led_matrix_config.enable = 0; |
342 | eeconfig_update_led_matrix(led_matrix_config.raw); |
343 | } |
344 | |
345 | void led_matrix_disable_noeeprom(void) { |
346 | led_matrix_config.enable = 0; |
347 | } |
348 | |
349 | void led_matrix_step(void) { |
350 | led_matrix_config.mode++; |
351 | if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX) |
352 | led_matrix_config.mode = 1; |
353 | eeconfig_update_led_matrix(led_matrix_config.raw); |
354 | } |
355 | |
356 | void led_matrix_step_reverse(void) { |
357 | led_matrix_config.mode--; |
358 | if (led_matrix_config.mode < 1) |
359 | led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1; |
360 | eeconfig_update_led_matrix(led_matrix_config.raw); |
361 | } |
362 | |
363 | void led_matrix_increase_val(void) { |
364 | led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); |
365 | eeconfig_update_led_matrix(led_matrix_config.raw); |
366 | } |
367 | |
368 | void led_matrix_decrease_val(void) { |
369 | led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); |
370 | eeconfig_update_led_matrix(led_matrix_config.raw); |
371 | } |
372 | |
373 | void led_matrix_increase_speed(void) { |
374 | led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3); |
375 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this |
376 | } |
377 | |
378 | void led_matrix_decrease_speed(void) { |
379 | led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3); |
380 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this |
381 | } |
382 | |
383 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { |
384 | led_matrix_config.mode = mode; |
385 | if (eeprom_write) { |
386 | eeconfig_update_led_matrix(led_matrix_config.raw); |
387 | } |
388 | } |
389 | |
390 | uint8_t led_matrix_get_mode(void) { |
391 | return led_matrix_config.mode; |
392 | } |
393 | |
bf267060 |
394 | void led_matrix_set_value_noeeprom(uint8_t val) { |
fd698c43 |
395 | led_matrix_config.val = val; |
bf267060 |
396 | } |
397 | |
398 | void led_matrix_set_value(uint8_t val) { |
399 | led_matrix_set_value_noeeprom(val); |
400 | eeconfig_update_led_matrix(led_matrix_config.raw); |
fd698c43 |
401 | } |
6b74dd6d |
402 | |
403 | void backlight_set(uint8_t val) { |
404 | led_matrix_set_value(val); |
405 | } |