Move the 1 second delay to led_matrix_init
[jackhill/qmk/firmware.git] / quantum / led_matrix.c
CommitLineData
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
30led_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
56bool g_suspend_state = false;
57
6b74dd6d 58// Last uniform brightness level.
59uint8_t g_uniform_brightness = 0;
60
fd698c43 61// Global tick at 20 Hz
62uint32_t g_tick = 0;
63
64// Ticks since this key was last hit.
bf267060 65uint8_t g_key_hit[LED_DRIVER_LED_COUNT];
fd698c43 66
67// Ticks since any key was last hit.
68uint32_t g_any_key_hit = 0;
69
70uint32_t eeconfig_read_led_matrix(void) {
71 return eeprom_read_dword(EECONFIG_LED_MATRIX);
72}
73void eeconfig_update_led_matrix(uint32_t config_value) {
74 eeprom_update_dword(EECONFIG_LED_MATRIX, config_value);
75}
76void 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}
84void 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
93#define LED_HITS_TO_REMEMBER 8
94uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
95uint8_t g_last_led_count = 0;
96
97void 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
111void led_matrix_update_pwm_buffers(void) {
112 led_matrix_driver.flush();
113}
114
115void led_matrix_set_index_value(int index, uint8_t value) {
116 led_matrix_driver.set_value(index, value);
117}
118
119void led_matrix_set_index_value_all(uint8_t value) {
120 led_matrix_driver.set_value_all(value);
121}
122
123bool 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
152void led_matrix_set_suspend_state(bool state) {
153 g_suspend_state = state;
154}
155
156// All LEDs off
157void led_matrix_all_off(void) {
158 led_matrix_set_index_value_all(0);
159}
160
161// Uniform brightness
162void 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
170void led_matrix_custom(void) {}
171
172void 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
fd698c43 179 g_tick++;
180
181 if (g_any_key_hit < 0xFFFFFFFF) {
182 g_any_key_hit++;
183 }
184
bf267060 185 for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) {
fd698c43 186 if (g_key_hit[led] < 255) {
187 if (g_key_hit[led] == 254)
188 g_last_led_count = MAX(g_last_led_count - 1, 0);
189 g_key_hit[led]++;
190 }
191 }
192
fd698c43 193 // Ideally we would also stop sending zeros to the LED driver PWM buffers
194 // while suspended and just do a software shutdown. This is a cheap hack for now.
195 bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) ||
196 (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20));
197 uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode;
198
fd698c43 199 // this gets ticked at 20 Hz.
200 // each effect can opt to do calculations
201 // and/or request PWM buffer updates.
202 switch (effect) {
203 case LED_MATRIX_UNIFORM_BRIGHTNESS:
204 led_matrix_uniform_brightness();
205 break;
206 default:
207 led_matrix_custom();
208 break;
209 }
210
6b74dd6d 211 if (!suspend_backlight) {
fd698c43 212 led_matrix_indicators();
213 }
214
6b74dd6d 215 // Tell the LED driver to update its state
216 led_matrix_driver.flush();
fd698c43 217}
218
219void led_matrix_indicators(void) {
220 led_matrix_indicators_kb();
221 led_matrix_indicators_user();
222}
223
224__attribute__((weak))
225void led_matrix_indicators_kb(void) {}
226
227__attribute__((weak))
228void led_matrix_indicators_user(void) {}
229
230
231// void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column)
232// {
233// if (row >= MATRIX_ROWS)
234// {
235// // Special value, 255=none, 254=all
236// *index = row;
237// }
238// else
239// {
240// // This needs updated to something like
241// // uint8_t led[8], led_count;
242// // map_row_column_to_led(row,column,led,&led_count);
243// // for(uint8_t i = 0; i < led_count; i++)
244// map_row_column_to_led(row, column, index);
245// }
246// }
247
248void led_matrix_init(void) {
249 led_matrix_driver.init();
250
32116f1a 251 // Wait a second for the driver to finish initializing
252 wait_ms(1000);
fd698c43 253
254 // clear the key hits
bf267060 255 for (int led=0; led<LED_DRIVER_LED_COUNT; led++) {
fd698c43 256 g_key_hit[led] = 255;
257 }
258
fd698c43 259 if (!eeconfig_is_enabled()) {
260 dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
261 eeconfig_init();
262 eeconfig_update_led_matrix_default();
263 }
32116f1a 264
fd698c43 265 led_matrix_config.raw = eeconfig_read_led_matrix();
32116f1a 266
fd698c43 267 if (!led_matrix_config.mode) {
268 dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n");
269 eeconfig_update_led_matrix_default();
270 led_matrix_config.raw = eeconfig_read_led_matrix();
271 }
32116f1a 272
fd698c43 273 eeconfig_debug_led_matrix(); // display current eeprom values
274}
275
276// Deals with the messy details of incrementing an integer
277static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
278 int16_t new_value = value;
279 new_value += step;
280 return MIN(MAX( new_value, min), max );
281}
282
283static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
284 int16_t new_value = value;
285 new_value -= step;
286 return MIN(MAX( new_value, min), max );
287}
288
289// void *backlight_get_custom_key_value_eeprom_address(uint8_t led) {
290// // 3 bytes per value
291// return EECONFIG_LED_MATRIX + (led * 3);
292// }
293
294// void backlight_get_key_value(uint8_t led, uint8_t *value) {
295// void *address = backlight_get_custom_key_value_eeprom_address(led);
296// value = eeprom_read_byte(address);
297// }
298
299// void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
300// uint8_t led[8], led_count;
301// map_row_column_to_led(row,column,led,&led_count);
302// for(uint8_t i = 0; i < led_count; i++) {
bf267060 303// if (led[i] < LED_DRIVER_LED_COUNT) {
fd698c43 304// void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
305// eeprom_update_byte(address, value);
306// }
307// }
308// }
309
310uint32_t led_matrix_get_tick(void) {
311 return g_tick;
312}
313
314void led_matrix_toggle(void) {
315 led_matrix_config.enable ^= 1;
316 eeconfig_update_led_matrix(led_matrix_config.raw);
317}
318
319void led_matrix_enable(void) {
320 led_matrix_config.enable = 1;
321 eeconfig_update_led_matrix(led_matrix_config.raw);
322}
323
324void led_matrix_enable_noeeprom(void) {
325 led_matrix_config.enable = 1;
326}
327
328void led_matrix_disable(void) {
329 led_matrix_config.enable = 0;
330 eeconfig_update_led_matrix(led_matrix_config.raw);
331}
332
333void led_matrix_disable_noeeprom(void) {
334 led_matrix_config.enable = 0;
335}
336
337void led_matrix_step(void) {
338 led_matrix_config.mode++;
339 if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX)
340 led_matrix_config.mode = 1;
341 eeconfig_update_led_matrix(led_matrix_config.raw);
342}
343
344void led_matrix_step_reverse(void) {
345 led_matrix_config.mode--;
346 if (led_matrix_config.mode < 1)
347 led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1;
348 eeconfig_update_led_matrix(led_matrix_config.raw);
349}
350
351void led_matrix_increase_val(void) {
352 led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
353 eeconfig_update_led_matrix(led_matrix_config.raw);
354}
355
356void led_matrix_decrease_val(void) {
357 led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
358 eeconfig_update_led_matrix(led_matrix_config.raw);
359}
360
361void led_matrix_increase_speed(void) {
362 led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3);
363 eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this
364}
365
366void led_matrix_decrease_speed(void) {
367 led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3);
368 eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this
369}
370
371void led_matrix_mode(uint8_t mode, bool eeprom_write) {
372 led_matrix_config.mode = mode;
373 if (eeprom_write) {
374 eeconfig_update_led_matrix(led_matrix_config.raw);
375 }
376}
377
378uint8_t led_matrix_get_mode(void) {
379 return led_matrix_config.mode;
380}
381
bf267060 382void led_matrix_set_value_noeeprom(uint8_t val) {
fd698c43 383 led_matrix_config.val = val;
bf267060 384}
385
386void led_matrix_set_value(uint8_t val) {
387 led_matrix_set_value_noeeprom(val);
388 eeconfig_update_led_matrix(led_matrix_config.raw);
fd698c43 389}
6b74dd6d 390
391void backlight_set(uint8_t val) {
392 led_matrix_set_value(val);
393}