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.c132
1 files changed, 62 insertions, 70 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c
index eb523990a..c3538e94d 100644
--- a/quantum/led_matrix.c
+++ b/quantum/led_matrix.c
@@ -27,7 +27,7 @@
27#include <string.h> 27#include <string.h>
28#include <math.h> 28#include <math.h>
29 29
30led_config_t led_matrix_config; 30led_eeconfig_t led_matrix_eeconfig;
31 31
32#ifndef MAX 32#ifndef MAX
33# define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) 33# define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
@@ -70,40 +70,32 @@ void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EEC
70 70
71void eeconfig_update_led_matrix_default(void) { 71void eeconfig_update_led_matrix_default(void) {
72 dprintf("eeconfig_update_led_matrix_default\n"); 72 dprintf("eeconfig_update_led_matrix_default\n");
73 led_matrix_config.enable = 1; 73 led_matrix_eeconfig.enable = 1;
74 led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; 74 led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS;
75 led_matrix_config.val = 128; 75 led_matrix_eeconfig.val = 128;
76 led_matrix_config.speed = 0; 76 led_matrix_eeconfig.speed = 0;
77 eeconfig_update_led_matrix(led_matrix_config.raw); 77 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
78} 78}
79 79
80void eeconfig_debug_led_matrix(void) { 80void eeconfig_debug_led_matrix(void) {
81 dprintf("led_matrix_config eeprom\n"); 81 dprintf("led_matrix_eeconfig eeprom\n");
82 dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable); 82 dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
83 dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode); 83 dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
84 dprintf("led_matrix_config.val = %d\n", led_matrix_config.val); 84 dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
85 dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed); 85 dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
86} 86}
87 87
88// Last led hit
89#ifndef LED_HITS_TO_REMEMBER
90# define LED_HITS_TO_REMEMBER 8
91#endif
92uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; 88uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
93uint8_t g_last_led_count = 0; 89uint8_t g_last_led_count = 0;
94 90
95void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { 91uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
96 led_matrix led; 92 uint8_t led_count = 0;
97 *led_count = 0; 93 uint8_t led_index = g_led_config.matrix_co[row][column];
98 94 if (led_index != NO_LED) {
99 for (uint8_t i = 0; i < LED_DRIVER_LED_COUNT; i++) { 95 led_i[led_count] = led_index;
100 // map_index_to_led(i, &led); 96 led_count++;
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 } 97 }
98 return led_count;
107} 99}
108 100
109void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } 101void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
@@ -114,8 +106,8 @@ void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value
114 106
115bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { 107bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
116 if (record->event.pressed) { 108 if (record->event.pressed) {
117 uint8_t led[8], led_count; 109 uint8_t led[8];
118 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); 110 uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
119 if (led_count > 0) { 111 if (led_count > 0) {
120 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { 112 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
121 g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; 113 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
@@ -127,8 +119,8 @@ bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
127 g_any_key_hit = 0; 119 g_any_key_hit = 0;
128 } else { 120 } else {
129#ifdef LED_MATRIX_KEYRELEASES 121#ifdef LED_MATRIX_KEYRELEASES
130 uint8_t led[8], led_count; 122 uint8_t led[8];
131 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); 123 uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
132 for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255; 124 for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255;
133 125
134 g_any_key_hit = 255; 126 g_any_key_hit = 255;
@@ -143,12 +135,12 @@ void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
143void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } 135void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); }
144 136
145// Uniform brightness 137// Uniform brightness
146void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_config.val); } 138void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); }
147 139
148void led_matrix_custom(void) {} 140void led_matrix_custom(void) {}
149 141
150void led_matrix_task(void) { 142void led_matrix_task(void) {
151 if (!led_matrix_config.enable) { 143 if (!led_matrix_eeconfig.enable) {
152 led_matrix_all_off(); 144 led_matrix_all_off();
153 led_matrix_indicators(); 145 led_matrix_indicators();
154 return; 146 return;
@@ -170,7 +162,7 @@ void led_matrix_task(void) {
170 // Ideally we would also stop sending zeros to the LED driver PWM buffers 162 // 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. 163 // while suspended and just do a software shutdown. This is a cheap hack for now.
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)); 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));
173 uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode; 165 uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode;
174 166
175 // this gets ticked at 20 Hz. 167 // this gets ticked at 20 Hz.
176 // each effect can opt to do calculations 168 // each effect can opt to do calculations
@@ -211,8 +203,8 @@ __attribute__((weak)) void led_matrix_indicators_user(void) {}
211// else 203// else
212// { 204// {
213// // This needs updated to something like 205// // This needs updated to something like
214// // uint8_t led[8], led_count; 206// // uint8_t led[8];
215// // map_row_column_to_led(row,column,led,&led_count); 207// // uint8_t led_count = map_row_column_to_led(row, column, led);
216// // for(uint8_t i = 0; i < led_count; i++) 208// // for(uint8_t i = 0; i < led_count; i++)
217// map_row_column_to_led(row, column, index); 209// map_row_column_to_led(row, column, index);
218// } 210// }
@@ -235,12 +227,12 @@ void led_matrix_init(void) {
235 eeconfig_update_led_matrix_default(); 227 eeconfig_update_led_matrix_default();
236 } 228 }
237 229
238 led_matrix_config.raw = eeconfig_read_led_matrix(); 230 led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
239 231
240 if (!led_matrix_config.mode) { 232 if (!led_matrix_eeconfig.mode) {
241 dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n"); 233 dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
242 eeconfig_update_led_matrix_default(); 234 eeconfig_update_led_matrix_default();
243 led_matrix_config.raw = eeconfig_read_led_matrix(); 235 led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
244 } 236 }
245 237
246 eeconfig_debug_led_matrix(); // display current eeprom values 238 eeconfig_debug_led_matrix(); // display current eeprom values
@@ -270,8 +262,8 @@ static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max)
270// } 262// }
271 263
272// void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) { 264// void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
273// uint8_t led[8], led_count; 265// uint8_t led[8];
274// map_row_column_to_led(row,column,led,&led_count); 266// uint8_t led_count = map_row_column_to_led(row, column, led);
275// for(uint8_t i = 0; i < led_count; i++) { 267// for(uint8_t i = 0; i < led_count; i++) {
276// if (led[i] < LED_DRIVER_LED_COUNT) { 268// if (led[i] < LED_DRIVER_LED_COUNT) {
277// void *address = backlight_get_custom_key_value_eeprom_address(led[i]); 269// void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
@@ -283,74 +275,74 @@ static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max)
283uint32_t led_matrix_get_tick(void) { return g_tick; } 275uint32_t led_matrix_get_tick(void) { return g_tick; }
284 276
285void led_matrix_toggle(void) { 277void led_matrix_toggle(void) {
286 led_matrix_config.enable ^= 1; 278 led_matrix_eeconfig.enable ^= 1;
287 eeconfig_update_led_matrix(led_matrix_config.raw); 279 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
288} 280}
289 281
290void led_matrix_enable(void) { 282void led_matrix_enable(void) {
291 led_matrix_config.enable = 1; 283 led_matrix_eeconfig.enable = 1;
292 eeconfig_update_led_matrix(led_matrix_config.raw); 284 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
293} 285}
294 286
295void led_matrix_enable_noeeprom(void) { led_matrix_config.enable = 1; } 287void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; }
296 288
297void led_matrix_disable(void) { 289void led_matrix_disable(void) {
298 led_matrix_config.enable = 0; 290 led_matrix_eeconfig.enable = 0;
299 eeconfig_update_led_matrix(led_matrix_config.raw); 291 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
300} 292}
301 293
302void led_matrix_disable_noeeprom(void) { led_matrix_config.enable = 0; } 294void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; }
303 295
304void led_matrix_step(void) { 296void led_matrix_step(void) {
305 led_matrix_config.mode++; 297 led_matrix_eeconfig.mode++;
306 if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX) { 298 if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) {
307 led_matrix_config.mode = 1; 299 led_matrix_eeconfig.mode = 1;
308 } 300 }
309 eeconfig_update_led_matrix(led_matrix_config.raw); 301 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
310} 302}
311 303
312void led_matrix_step_reverse(void) { 304void led_matrix_step_reverse(void) {
313 led_matrix_config.mode--; 305 led_matrix_eeconfig.mode--;
314 if (led_matrix_config.mode < 1) { 306 if (led_matrix_eeconfig.mode < 1) {
315 led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1; 307 led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
316 } 308 }
317 eeconfig_update_led_matrix(led_matrix_config.raw); 309 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
318} 310}
319 311
320void led_matrix_increase_val(void) { 312void led_matrix_increase_val(void) {
321 led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); 313 led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
322 eeconfig_update_led_matrix(led_matrix_config.raw); 314 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
323} 315}
324 316
325void led_matrix_decrease_val(void) { 317void led_matrix_decrease_val(void) {
326 led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); 318 led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
327 eeconfig_update_led_matrix(led_matrix_config.raw); 319 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
328} 320}
329 321
330void led_matrix_increase_speed(void) { 322void led_matrix_increase_speed(void) {
331 led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3); 323 led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3);
332 eeconfig_update_led_matrix(led_matrix_config.raw); // EECONFIG needs to be increased to support this 324 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
333} 325}
334 326
335void led_matrix_decrease_speed(void) { 327void led_matrix_decrease_speed(void) {
336 led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3); 328 led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3);
337 eeconfig_update_led_matrix(led_matrix_config.raw); // EECONFIG needs to be increased to support this 329 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
338} 330}
339 331
340void led_matrix_mode(uint8_t mode, bool eeprom_write) { 332void led_matrix_mode(uint8_t mode, bool eeprom_write) {
341 led_matrix_config.mode = mode; 333 led_matrix_eeconfig.mode = mode;
342 if (eeprom_write) { 334 if (eeprom_write) {
343 eeconfig_update_led_matrix(led_matrix_config.raw); 335 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
344 } 336 }
345} 337}
346 338
347uint8_t led_matrix_get_mode(void) { return led_matrix_config.mode; } 339uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
348 340
349void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_config.val = val; } 341void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; }
350 342
351void led_matrix_set_value(uint8_t val) { 343void led_matrix_set_value(uint8_t val) {
352 led_matrix_set_value_noeeprom(val); 344 led_matrix_set_value_noeeprom(val);
353 eeconfig_update_led_matrix(led_matrix_config.raw); 345 eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
354} 346}
355 347
356void backlight_set(uint8_t val) { led_matrix_set_value(val); } 348void backlight_set(uint8_t val) { led_matrix_set_value(val); }