aboutsummaryrefslogtreecommitdiff
path: root/quantum/led_matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/led_matrix.c')
-rw-r--r--quantum/led_matrix.c572
1 files changed, 382 insertions, 190 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c
index 4f1f06c7a..3674c9b97 100644
--- a/quantum/led_matrix.c
+++ b/quantum/led_matrix.c
@@ -17,79 +17,111 @@
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */ 18 */
19 19
20#include <stdint.h>
21#include <stdbool.h>
22#include "quantum.h"
23#include "led_matrix.h" 20#include "led_matrix.h"
24#include "progmem.h" 21#include "progmem.h"
25#include "config.h" 22#include "config.h"
26#include "eeprom.h" 23#include "eeprom.h"
27#include <string.h> 24#include <string.h>
28#include <math.h> 25#include <math.h>
26#include "led_tables.h"
29 27
30led_eeconfig_t led_matrix_eeconfig; 28#include <lib/lib8tion/lib8tion.h>
31 29
32#ifndef MAX 30#if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT)
33# define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) 31# define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL)
34#endif 32#endif
35 33
36#ifndef MIN 34#ifndef LED_DISABLE_TIMEOUT
37# define MIN(a, b) ((a) < (b) ? (a) : (b)) 35# define LED_DISABLE_TIMEOUT 0
38#endif 36#endif
39 37
40#ifndef LED_DISABLE_AFTER_TIMEOUT 38#if LED_DISABLE_WHEN_USB_SUSPENDED == false
41# define LED_DISABLE_AFTER_TIMEOUT 0 39# undef LED_DISABLE_WHEN_USB_SUSPENDED
42#endif 40#endif
43 41
44#ifndef LED_DISABLE_WHEN_USB_SUSPENDED 42#if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
45# define LED_DISABLE_WHEN_USB_SUSPENDED false 43# undef LED_MATRIX_MAXIMUM_BRIGHTNESS
44# define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
46#endif 45#endif
47 46
48#ifndef EECONFIG_LED_MATRIX 47#if !defined(LED_MATRIX_VAL_STEP)
49# define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT 48# define LED_MATRIX_VAL_STEP 8
50#endif 49#endif
51 50
52#if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 51#if !defined(LED_MATRIX_SPD_STEP)
53# define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 52# define LED_MATRIX_SPD_STEP 16
54#endif 53#endif
55 54
56bool g_suspend_state = false; 55#if !defined(LED_MATRIX_STARTUP_MODE)
56# define LED_MATRIX_STARTUP_MODE LED_MATRIX_UNIFORM_BRIGHTNESS
57#endif
57 58
58// Global tick at 20 Hz 59#if !defined(LED_MATRIX_STARTUP_VAL)
59uint32_t g_tick = 0; 60# define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
61#endif
60 62
61// Ticks since this key was last hit. 63#if !defined(LED_MATRIX_STARTUP_SPD)
62uint8_t g_key_hit[DRIVER_LED_TOTAL]; 64# define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
65#endif
63 66
64// Ticks since any key was last hit. 67// globals
65uint32_t g_any_key_hit = 0; 68led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
69uint32_t g_led_timer;
70#ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
71uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
72#endif // LED_MATRIX_FRAMEBUFFER_EFFECTS
73#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
74last_hit_t g_last_hit_tracker;
75#endif // LED_MATRIX_KEYREACTIVE_ENABLED
76
77// internals
78static bool suspend_state = false;
79static uint8_t led_last_enable = UINT8_MAX;
80static uint8_t led_last_effect = UINT8_MAX;
81static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false};
82static led_task_states led_task_state = SYNCING;
83#if LED_DISABLE_TIMEOUT > 0
84static uint32_t led_anykey_timer;
85#endif // LED_DISABLE_TIMEOUT > 0
86
87// double buffers
88static uint32_t led_timer_buffer;
89#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
90static last_hit_t last_hit_buffer;
91#endif // LED_MATRIX_KEYREACTIVE_ENABLED
92
93// split led matrix
94#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
95const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
96#endif
66 97
67uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); } 98void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
68 99
69void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); } 100void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
70 101
71void eeconfig_update_led_matrix_default(void) { 102void eeconfig_update_led_matrix_default(void) {
72 dprintf("eeconfig_update_led_matrix_default\n"); 103 dprintf("eeconfig_update_led_matrix_default\n");
73 led_matrix_eeconfig.enable = 1; 104 led_matrix_eeconfig.enable = 1;
74 led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; 105 led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE;
75 led_matrix_eeconfig.val = 128; 106 led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL;
76 led_matrix_eeconfig.speed = 0; 107 led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD;
77 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 108 led_matrix_eeconfig.flags = LED_FLAG_ALL;
109 eeconfig_update_led_matrix();
78} 110}
79 111
80void eeconfig_debug_led_matrix(void) { 112void eeconfig_debug_led_matrix(void) {
81 dprintf("led_matrix_eeconfig eeprom\n"); 113 dprintf("led_matrix_eeconfig EEPROM\n");
82 dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable); 114 dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
83 dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode); 115 dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
84 dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val); 116 dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
85 dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed); 117 dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
118 dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags);
86} 119}
87 120
88uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; 121__attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
89uint8_t g_last_led_count = 0;
90 122
91uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { 123uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
92 uint8_t led_count = 0; 124 uint8_t led_count = led_matrix_map_row_column_to_led_kb(row, column, led_i);
93 uint8_t led_index = g_led_config.matrix_co[row][column]; 125 uint8_t led_index = g_led_config.matrix_co[row][column];
94 if (led_index != NO_LED) { 126 if (led_index != NO_LED) {
95 led_i[led_count] = led_index; 127 led_i[led_count] = led_index;
@@ -100,88 +132,224 @@ uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
100 132
101void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } 133void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
102 134
103void led_matrix_set_index_value(int index, uint8_t value) { led_matrix_driver.set_value(index, value); } 135void led_matrix_set_value(int index, uint8_t value) {
104 136#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
105void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value_all(value); } 137 if (!is_keyboard_left() && index >= k_led_matrix_split[0])
106 138# ifdef USE_CIE1931_CURVE
107bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { 139 led_matrix_driver.set_value(index - k_led_matrix_split[0], pgm_read_byte(&CIE1931_CURVE[value]));
108 if (record->event.pressed) { 140# else
109 uint8_t led[8]; 141 led_matrix_driver.set_value(index - k_led_matrix_split[0], value);
110 uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led); 142# endif
111 if (led_count > 0) { 143 else if (is_keyboard_left() && index < k_led_matrix_split[0])
112 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { 144#endif
113 g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; 145#ifdef USE_CIE1931_CURVE
114 } 146 led_matrix_driver.set_value(index, pgm_read_byte(&CIE1931_CURVE[value]));
115 g_last_led_hit[0] = led[0]; 147#else
116 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); 148 led_matrix_driver.set_value(index, value);
117 } 149#endif
118 for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 0; 150}
119 g_any_key_hit = 0;
120 } else {
121#ifdef LED_MATRIX_KEYRELEASES
122 uint8_t led[8];
123 uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
124 for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255;
125 151
126 g_any_key_hit = 255; 152void led_matrix_set_value_all(uint8_t value) {
153#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
154 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) led_matrix_set_value(i, value);
155#else
156# ifdef USE_CIE1931_CURVE
157 led_matrix_driver.set_value_all(pgm_read_byte(&CIE1931_CURVE[value]));
158# else
159 led_matrix_driver.set_value_all(value);
160# endif
127#endif 161#endif
128 }
129 return true;
130} 162}
131 163
132void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; } 164void process_led_matrix(uint8_t row, uint8_t col, bool pressed) {
165#ifndef LED_MATRIX_SPLIT
166 if (!is_keyboard_master()) return;
167#endif
168#if LED_DISABLE_TIMEOUT > 0
169 led_anykey_timer = 0;
170#endif // LED_DISABLE_TIMEOUT > 0
133 171
134// All LEDs off 172#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
135void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } 173 uint8_t led[LED_HITS_TO_REMEMBER];
174 uint8_t led_count = 0;
136 175
137// Uniform brightness 176# if defined(LED_MATRIX_KEYRELEASES)
138void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); } 177 if (!pressed)
178# elif defined(LED_MATRIX_KEYPRESSES)
179 if (pressed)
180# endif // defined(LED_MATRIX_KEYRELEASES)
181 {
182 led_count = led_matrix_map_row_column_to_led(row, col, led);
183 }
139 184
140void led_matrix_custom(void) {} 185 if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
186 memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
187 memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
188 memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
189 memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
190 last_hit_buffer.count--;
191 }
141 192
142void led_matrix_task(void) { 193 for (uint8_t i = 0; i < led_count; i++) {
143 if (!led_matrix_eeconfig.enable) { 194 uint8_t index = last_hit_buffer.count;
144 led_matrix_all_off(); 195 last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
145 led_matrix_indicators(); 196 last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
146 return; 197 last_hit_buffer.index[index] = led[i];
198 last_hit_buffer.tick[index] = 0;
199 last_hit_buffer.count++;
147 } 200 }
201#endif // LED_MATRIX_KEYREACTIVE_ENABLED
148 202
149 g_tick++; 203#if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP)
204 if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) {
205 process_led_matrix_typing_heatmap(row, col);
206 }
207#endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP)
208}
150 209
151 if (g_any_key_hit < 0xFFFFFFFF) { 210static bool led_matrix_none(effect_params_t *params) {
152 g_any_key_hit++; 211 if (!params->init) {
212 return false;
153 } 213 }
154 214
155 for (int led = 0; led < DRIVER_LED_TOTAL; led++) { 215 led_matrix_set_value_all(0);
156 if (g_key_hit[led] < 255) { 216 return false;
157 if (g_key_hit[led] == 254) g_last_led_count = MAX(g_last_led_count - 1, 0); 217}
158 g_key_hit[led]++; 218
219static bool led_matrix_uniform_brightness(effect_params_t *params) {
220 LED_MATRIX_USE_LIMITS(led_min, led_max);
221
222 uint8_t val = led_matrix_eeconfig.val;
223 for (uint8_t i = led_min; i < led_max; i++) {
224 LED_MATRIX_TEST_LED_FLAGS();
225 led_matrix_set_value(i, val);
226 }
227 return led_max < DRIVER_LED_TOTAL;
228}
229
230static void led_task_timers(void) {
231#if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0
232 uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer);
233#endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0
234 led_timer_buffer = sync_timer_read32();
235
236 // Update double buffer timers
237#if LED_DISABLE_TIMEOUT > 0
238 if (led_anykey_timer < UINT32_MAX) {
239 if (UINT32_MAX - deltaTime < led_anykey_timer) {
240 led_anykey_timer = UINT32_MAX;
241 } else {
242 led_anykey_timer += deltaTime;
243 }
244 }
245#endif // LED_DISABLE_TIMEOUT > 0
246
247 // Update double buffer last hit timers
248#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
249 uint8_t count = last_hit_buffer.count;
250 for (uint8_t i = 0; i < count; ++i) {
251 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
252 last_hit_buffer.count--;
253 continue;
159 } 254 }
255 last_hit_buffer.tick[i] += deltaTime;
160 } 256 }
257#endif // LED_MATRIX_KEYREACTIVE_ENABLED
258}
161 259
162 // Ideally we would also stop sending zeros to the LED driver PWM buffers 260static void led_task_sync(void) {
163 // while suspended and just do a software shutdown. This is a cheap hack for now. 261 // next task
164 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)); 262 if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING;
165 uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode; 263}
264
265static void led_task_start(void) {
266 // reset iter
267 led_effect_params.iter = 0;
268
269 // update double buffers
270 g_led_timer = led_timer_buffer;
271#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
272 g_last_hit_tracker = last_hit_buffer;
273#endif // LED_MATRIX_KEYREACTIVE_ENABLED
274
275 // next task
276 led_task_state = RENDERING;
277}
278
279static void led_task_render(uint8_t effect) {
280 bool rendering = false;
281 led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable);
282 if (led_effect_params.flags != led_matrix_eeconfig.flags) {
283 led_effect_params.flags = led_matrix_eeconfig.flags;
284 led_matrix_set_value_all(0);
285 }
166 286
167 // this gets ticked at 20 Hz.
168 // each effect can opt to do calculations 287 // each effect can opt to do calculations
169 // and/or request PWM buffer updates. 288 // and/or request PWM buffer updates.
170 switch (effect) { 289 switch (effect) {
171 case LED_MATRIX_UNIFORM_BRIGHTNESS: 290 case LED_MATRIX_NONE:
172 led_matrix_uniform_brightness(); 291 rendering = led_matrix_none(&led_effect_params);
173 break; 292 break;
174 default: 293 case LED_MATRIX_UNIFORM_BRIGHTNESS:
175 led_matrix_custom(); 294 rendering = led_matrix_uniform_brightness(&led_effect_params);
176 break; 295 break;
177 } 296 }
178 297
179 if (!suspend_backlight) { 298 led_effect_params.iter++;
180 led_matrix_indicators(); 299
300 // next task
301 if (!rendering) {
302 led_task_state = FLUSHING;
303 if (!led_effect_params.init && effect == LED_MATRIX_NONE) {
304 // We only need to flush once if we are LED_MATRIX_NONE
305 led_task_state = SYNCING;
306 }
181 } 307 }
308}
309
310static void led_task_flush(uint8_t effect) {
311 // update last trackers after the first full render so we can init over several frames
312 led_last_effect = effect;
313 led_last_enable = led_matrix_eeconfig.enable;
314
315 // update pwm buffers
316 led_matrix_update_pwm_buffers();
317
318 // next task
319 led_task_state = SYNCING;
320}
321
322void led_matrix_task(void) {
323 led_task_timers();
324
325 // Ideally we would also stop sending zeros to the LED driver PWM buffers
326 // while suspended and just do a software shutdown. This is a cheap hack for now.
327 bool suspend_backlight = suspend_state ||
328#if LED_DISABLE_TIMEOUT > 0
329 (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) ||
330#endif // LED_DISABLE_TIMEOUT > 0
331 false;
332
333 uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode;
182 334
183 // Tell the LED driver to update its state 335 switch (led_task_state) {
184 led_matrix_driver.flush(); 336 case STARTING:
337 led_task_start();
338 break;
339 case RENDERING:
340 led_task_render(effect);
341 if (effect) {
342 led_matrix_indicators();
343 led_matrix_indicators_advanced(&led_effect_params);
344 }
345 break;
346 case FLUSHING:
347 led_task_flush(effect);
348 break;
349 case SYNCING:
350 led_task_sync();
351 break;
352 }
185} 353}
186 354
187void led_matrix_indicators(void) { 355void led_matrix_indicators(void) {
@@ -193,33 +361,42 @@ __attribute__((weak)) void led_matrix_indicators_kb(void) {}
193 361
194__attribute__((weak)) void led_matrix_indicators_user(void) {} 362__attribute__((weak)) void led_matrix_indicators_user(void) {}
195 363
196// void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) 364void led_matrix_indicators_advanced(effect_params_t *params) {
197// { 365 /* special handling is needed for "params->iter", since it's already been incremented.
198// if (row >= MATRIX_ROWS) 366 * Could move the invocations to led_task_render, but then it's missing a few checks
199// { 367 * and not sure which would be better. Otherwise, this should be called from
200// // Special value, 255=none, 254=all 368 * led_task_render, right before the iter++ line.
201// *index = row; 369 */
202// } 370#if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL
203// else 371 uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1);
204// { 372 uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT;
205// // This needs updated to something like 373 if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL;
206// // uint8_t led[8]; 374#else
207// // uint8_t led_count = map_row_column_to_led(row, column, led); 375 uint8_t min = 0;
208// // for(uint8_t i = 0; i < led_count; i++) 376 uint8_t max = DRIVER_LED_TOTAL;
209// map_row_column_to_led(row, column, index); 377#endif
210// } 378 led_matrix_indicators_advanced_kb(min, max);
211// } 379 led_matrix_indicators_advanced_user(min, max);
380}
381
382__attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {}
383
384__attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {}
212 385
213void led_matrix_init(void) { 386void led_matrix_init(void) {
214 led_matrix_driver.init(); 387 led_matrix_driver.init();
215 388
216 // Wait half a second for the driver to finish initializing 389#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
217 wait_ms(500); 390 g_last_hit_tracker.count = 0;
391 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
392 g_last_hit_tracker.tick[i] = UINT16_MAX;
393 }
218 394
219 // clear the key hits 395 last_hit_buffer.count = 0;
220 for (int led = 0; led < DRIVER_LED_TOTAL; led++) { 396 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
221 g_key_hit[led] = 255; 397 last_hit_buffer.tick[i] = UINT16_MAX;
222 } 398 }
399#endif // LED_MATRIX_KEYREACTIVE_ENABLED
223 400
224 if (!eeconfig_is_enabled()) { 401 if (!eeconfig_is_enabled()) {
225 dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); 402 dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
@@ -227,122 +404,137 @@ void led_matrix_init(void) {
227 eeconfig_update_led_matrix_default(); 404 eeconfig_update_led_matrix_default();
228 } 405 }
229 406
230 led_matrix_eeconfig.raw = eeconfig_read_led_matrix(); 407 eeconfig_read_led_matrix();
231
232 if (!led_matrix_eeconfig.mode) { 408 if (!led_matrix_eeconfig.mode) {
233 dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); 409 dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
234 eeconfig_update_led_matrix_default(); 410 eeconfig_update_led_matrix_default();
235 led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
236 } 411 }
237
238 eeconfig_debug_led_matrix(); // display current eeprom values 412 eeconfig_debug_led_matrix(); // display current eeprom values
239} 413}
240 414
241// Deals with the messy details of incrementing an integer 415void led_matrix_set_suspend_state(bool state) {
242static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { 416#ifdef LED_DISABLE_WHEN_USB_SUSPENDED
243 int16_t new_value = value; 417 if (state) {
244 new_value += step; 418 led_matrix_set_value_all(0); // turn off all LEDs when suspending
245 return MIN(MAX(new_value, min), max); 419 }
420 suspend_state = state;
421#endif
246} 422}
247 423
248static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { 424bool led_matrix_get_suspend_state(void) { return suspend_state; }
249 int16_t new_value = value;
250 new_value -= step;
251 return MIN(MAX(new_value, min), max);
252}
253 425
254// void *backlight_get_custom_key_value_eeprom_address(uint8_t led) { 426void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
255// // 3 bytes per value
256// return EECONFIG_LED_MATRIX + (led * 3);
257// }
258
259// void backlight_get_key_value(uint8_t led, uint8_t *value) {
260// void *address = backlight_get_custom_key_value_eeprom_address(led);
261// value = eeprom_read_byte(address);
262// }
263
264// void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
265// uint8_t led[8];
266// uint8_t led_count = map_row_column_to_led(row, column, led);
267// for(uint8_t i = 0; i < led_count; i++) {
268// if (led[i] < DRIVER_LED_TOTAL) {
269// void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
270// eeprom_update_byte(address, value);
271// }
272// }
273// }
274
275uint32_t led_matrix_get_tick(void) { return g_tick; }
276
277void led_matrix_toggle(void) {
278 led_matrix_eeconfig.enable ^= 1; 427 led_matrix_eeconfig.enable ^= 1;
279 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 428 led_task_state = STARTING;
429 if (write_to_eeprom) {
430 eeconfig_update_led_matrix();
431 }
432 dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
280} 433}
434void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); }
435void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); }
281 436
282void led_matrix_enable(void) { 437void led_matrix_enable(void) {
283 led_matrix_eeconfig.enable = 1; 438 led_matrix_enable_noeeprom();
284 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 439 eeconfig_update_led_matrix();
285} 440}
286 441
287void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; } 442void led_matrix_enable_noeeprom(void) {
443 if (!led_matrix_eeconfig.enable) led_task_state = STARTING;
444 led_matrix_eeconfig.enable = 1;
445}
288 446
289void led_matrix_disable(void) { 447void led_matrix_disable(void) {
448 led_matrix_disable_noeeprom();
449 eeconfig_update_led_matrix();
450}
451
452void led_matrix_disable_noeeprom(void) {
453 if (led_matrix_eeconfig.enable) led_task_state = STARTING;
290 led_matrix_eeconfig.enable = 0; 454 led_matrix_eeconfig.enable = 0;
291 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
292} 455}
293 456
294void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; } 457uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; }
295 458
296void led_matrix_step(void) { 459void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
297 led_matrix_eeconfig.mode++; 460 if (!led_matrix_eeconfig.enable) {
298 if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) { 461 return;
299 led_matrix_eeconfig.mode = 1;
300 } 462 }
301 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 463 if (mode < 1) {
302} 464 led_matrix_eeconfig.mode = 1;
303 465 } else if (mode >= LED_MATRIX_EFFECT_MAX) {
304void led_matrix_step_reverse(void) {
305 led_matrix_eeconfig.mode--;
306 if (led_matrix_eeconfig.mode < 1) {
307 led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1; 466 led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
467 } else {
468 led_matrix_eeconfig.mode = mode;
469 }
470 led_task_state = STARTING;
471 if (write_to_eeprom) {
472 eeconfig_update_led_matrix();
308 } 473 }
309 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 474 dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode);
310} 475}
476void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); }
477void led_matrix_mode(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, true); }
311 478
312void led_matrix_increase_val(void) { 479uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
313 led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
314 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
315}
316 480
317void led_matrix_decrease_val(void) { 481void led_matrix_step_helper(bool write_to_eeprom) {
318 led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); 482 uint8_t mode = led_matrix_eeconfig.mode + 1;
319 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 483 led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
320} 484}
485void led_matrix_step_noeeprom(void) { led_matrix_step_helper(false); }
486void led_matrix_step(void) { led_matrix_step_helper(true); }
321 487
322void led_matrix_increase_speed(void) { 488void led_matrix_step_reverse_helper(bool write_to_eeprom) {
323 led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3); 489 uint8_t mode = led_matrix_eeconfig.mode - 1;
324 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this 490 led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
325} 491}
492void led_matrix_step_reverse_noeeprom(void) { led_matrix_step_reverse_helper(false); }
493void led_matrix_step_reverse(void) { led_matrix_step_reverse_helper(true); }
326 494
327void led_matrix_decrease_speed(void) { 495void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) {
328 led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3); 496 if (!led_matrix_eeconfig.enable) {
329 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this 497 return;
498 }
499 led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val;
500 if (write_to_eeprom) {
501 eeconfig_update_led_matrix();
502 }
503 dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val);
330} 504}
505void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); }
506void led_matrix_set_val(uint8_t val) { led_matrix_set_val_eeprom_helper(val, true); }
331 507
332void led_matrix_mode(uint8_t mode, bool eeprom_write) { 508uint8_t led_matrix_get_val(void) { return led_matrix_eeconfig.val; }
333 led_matrix_eeconfig.mode = mode; 509
334 if (eeprom_write) { 510void led_matrix_increase_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); }
335 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 511void led_matrix_increase_val_noeeprom(void) { led_matrix_increase_val_helper(false); }
512void led_matrix_increase_val(void) { led_matrix_increase_val_helper(true); }
513
514void led_matrix_decrease_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); }
515void led_matrix_decrease_val_noeeprom(void) { led_matrix_decrease_val_helper(false); }
516void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); }
517
518void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
519 led_matrix_eeconfig.speed = speed;
520 if (write_to_eeprom) {
521 eeconfig_update_led_matrix();
336 } 522 }
523 dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed);
337} 524}
525void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); }
526void led_matrix_set_speed(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, true); }
338 527
339uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; } 528uint8_t led_matrix_get_speed(void) { return led_matrix_eeconfig.speed; }
340 529
341void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; } 530void led_matrix_increase_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qadd8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); }
531void led_matrix_increase_speed_noeeprom(void) { led_matrix_increase_speed_helper(false); }
532void led_matrix_increase_speed(void) { led_matrix_increase_speed_helper(true); }
342 533
343void led_matrix_set_value(uint8_t val) { 534void led_matrix_decrease_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qsub8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); }
344 led_matrix_set_value_noeeprom(val); 535void led_matrix_decrease_speed_noeeprom(void) { led_matrix_decrease_speed_helper(false); }
345 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 536void led_matrix_decrease_speed(void) { led_matrix_decrease_speed_helper(true); }
346} 537
538led_flags_t led_matrix_get_flags(void) { return led_matrix_eeconfig.flags; }
347 539
348void backlight_set(uint8_t val) { led_matrix_set_value(val); } 540void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; }