aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/led_matrix.c132
-rw-r--r--quantum/led_matrix.h42
-rw-r--r--quantum/led_matrix_types.h69
-rw-r--r--quantum/rgb_matrix_types.h16
4 files changed, 153 insertions, 106 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); }
diff --git a/quantum/led_matrix.h b/quantum/led_matrix.h
index 7dcdf1d48..85bae43c1 100644
--- a/quantum/led_matrix.h
+++ b/quantum/led_matrix.h
@@ -19,46 +19,12 @@
19 19
20#pragma once 20#pragma once
21 21
22#include "led_matrix_types.h"
23
22#ifndef BACKLIGHT_ENABLE 24#ifndef BACKLIGHT_ENABLE
23# error You must define BACKLIGHT_ENABLE with LED_MATRIX_ENABLE 25# error You must define BACKLIGHT_ENABLE with LED_MATRIX_ENABLE
24#endif 26#endif
25 27
26typedef struct Point {
27 uint8_t x;
28 uint8_t y;
29} __attribute__((packed)) Point;
30
31typedef struct led_matrix {
32 union {
33 uint8_t raw;
34 struct {
35 uint8_t row : 4; // 16 max
36 uint8_t col : 4; // 16 max
37 };
38 } matrix_co;
39 Point point;
40 uint8_t modifier : 1;
41} __attribute__((packed)) led_matrix;
42
43extern const led_matrix g_leds[LED_DRIVER_LED_COUNT];
44
45typedef struct {
46 uint8_t index;
47 uint8_t value;
48} led_indicator;
49
50typedef union {
51 uint32_t raw;
52 struct {
53 bool enable : 1;
54 uint8_t mode : 6;
55 uint8_t hue : 8; // Unused by led_matrix
56 uint8_t sat : 8; // Unused by led_matrix
57 uint8_t val : 8;
58 uint8_t speed : 8; // EECONFIG needs to be increased to support this
59 };
60} led_config_t;
61
62enum led_matrix_effects { 28enum led_matrix_effects {
63 LED_MATRIX_UNIFORM_BRIGHTNESS = 1, 29 LED_MATRIX_UNIFORM_BRIGHTNESS = 1,
64 // All new effects go above this line 30 // All new effects go above this line
@@ -122,3 +88,7 @@ typedef struct {
122} led_matrix_driver_t; 88} led_matrix_driver_t;
123 89
124extern const led_matrix_driver_t led_matrix_driver; 90extern const led_matrix_driver_t led_matrix_driver;
91
92extern led_eeconfig_t led_matrix_eeconfig;
93
94extern led_config_t g_led_config;
diff --git a/quantum/led_matrix_types.h b/quantum/led_matrix_types.h
new file mode 100644
index 000000000..2602bf2bf
--- /dev/null
+++ b/quantum/led_matrix_types.h
@@ -0,0 +1,69 @@
1/* Copyright 2021
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#include <stdint.h>
20#include <stdbool.h>
21
22#if defined(__GNUC__)
23# define PACKED __attribute__((__packed__))
24#else
25# define PACKED
26#endif
27
28#if defined(_MSC_VER)
29# pragma pack(push, 1)
30#endif
31
32// Last led hit
33#ifndef LED_HITS_TO_REMEMBER
34# define LED_HITS_TO_REMEMBER 8
35#endif // LED_HITS_TO_REMEMBER
36
37typedef struct PACKED {
38 uint8_t x;
39 uint8_t y;
40} point_t;
41
42#define LED_FLAG_ALL 0xFF
43#define LED_FLAG_NONE 0x00
44#define LED_FLAG_MODIFIER 0x01
45#define LED_FLAG_KEYLIGHT 0x04
46#define LED_FLAG_INDICATOR 0x08
47
48#define NO_LED 255
49
50typedef struct PACKED {
51 uint8_t matrix_co[MATRIX_ROWS][MATRIX_COLS];
52 point_t point[LED_DRIVER_LED_COUNT];
53 uint8_t flags[LED_DRIVER_LED_COUNT];
54} led_config_t;
55
56typedef union {
57 uint32_t raw;
58 struct PACKED {
59 uint8_t enable : 2;
60 uint8_t mode : 6;
61 uint16_t reserved;
62 uint8_t val;
63 uint8_t speed; // EECONFIG needs to be increased to support this
64 };
65} led_eeconfig_t;
66
67#if defined(_MSC_VER)
68# pragma pack(pop)
69#endif
diff --git a/quantum/rgb_matrix_types.h b/quantum/rgb_matrix_types.h
index f7ebec1d5..7b8171fb2 100644
--- a/quantum/rgb_matrix_types.h
+++ b/quantum/rgb_matrix_types.h
@@ -1,3 +1,19 @@
1/* Copyright 2021
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
1#pragma once 17#pragma once
2 18
3#include <stdint.h> 19#include <stdint.h>