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