aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/config_common.h351
-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/quantum.h90
-rw-r--r--quantum/rgb_matrix_types.h16
6 files changed, 160 insertions, 540 deletions
diff --git a/quantum/config_common.h b/quantum/config_common.h
index bfaf7389e..fa1ff2a5f 100644
--- a/quantum/config_common.h
+++ b/quantum/config_common.h
@@ -16,357 +16,14 @@
16 16
17#pragma once 17#pragma once
18 18
19#ifndef __ASSEMBLER__
20# include "pin_defs.h"
21#endif
22
19/* diode directions */ 23/* diode directions */
20#define COL2ROW 0 24#define COL2ROW 0
21#define ROW2COL 1 25#define ROW2COL 1
22 26
23// useful for direct pin mapping
24#define NO_PIN (pin_t)(~0)
25
26#ifdef __AVR__
27# ifndef __ASSEMBLER__
28# include <avr/io.h>
29# endif
30# define PORT_SHIFTER 4 // this may be 4 for all AVR chips
31
32// If you want to add more to this list, reference the PINx definitions in these header
33// files: https://github.com/vancegroup-mirrors/avr-libc/tree/master/avr-libc/include/avr
34
35# if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)
36# define ADDRESS_BASE 0x00
37# define PINB_ADDRESS 0x3
38# define PINC_ADDRESS 0x6
39# define PIND_ADDRESS 0x9
40# define PINE_ADDRESS 0xC
41# define PINF_ADDRESS 0xF
42# elif defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
43# define ADDRESS_BASE 0x00
44# define PINB_ADDRESS 0x3
45# define PINC_ADDRESS 0x6
46# define PIND_ADDRESS 0x9
47# elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
48# define ADDRESS_BASE 0x00
49# define PINA_ADDRESS 0x0
50# define PINB_ADDRESS 0x3
51# define PINC_ADDRESS 0x6
52# define PIND_ADDRESS 0x9
53# define PINE_ADDRESS 0xC
54# define PINF_ADDRESS 0xF
55# elif defined(__AVR_ATmega32A__)
56# define ADDRESS_BASE 0x10
57# define PIND_ADDRESS 0x0
58# define PINC_ADDRESS 0x3
59# define PINB_ADDRESS 0x6
60# define PINA_ADDRESS 0x9
61# elif defined(__AVR_ATtiny85__)
62# define ADDRESS_BASE 0x10
63# define PINB_ADDRESS 0x6
64# else
65# error "Pins are not defined"
66# endif
67
68/* I/O pins */
69# define PINDEF(port, pin) ((PIN##port##_ADDRESS << PORT_SHIFTER) | pin)
70
71# ifdef PORTA
72# define A0 PINDEF(A, 0)
73# define A1 PINDEF(A, 1)
74# define A2 PINDEF(A, 2)
75# define A3 PINDEF(A, 3)
76# define A4 PINDEF(A, 4)
77# define A5 PINDEF(A, 5)
78# define A6 PINDEF(A, 6)
79# define A7 PINDEF(A, 7)
80# endif
81# ifdef PORTB
82# define B0 PINDEF(B, 0)
83# define B1 PINDEF(B, 1)
84# define B2 PINDEF(B, 2)
85# define B3 PINDEF(B, 3)
86# define B4 PINDEF(B, 4)
87# define B5 PINDEF(B, 5)
88# define B6 PINDEF(B, 6)
89# define B7 PINDEF(B, 7)
90# endif
91# ifdef PORTC
92# define C0 PINDEF(C, 0)
93# define C1 PINDEF(C, 1)
94# define C2 PINDEF(C, 2)
95# define C3 PINDEF(C, 3)
96# define C4 PINDEF(C, 4)
97# define C5 PINDEF(C, 5)
98# define C6 PINDEF(C, 6)
99# define C7 PINDEF(C, 7)
100# endif
101# ifdef PORTD
102# define D0 PINDEF(D, 0)
103# define D1 PINDEF(D, 1)
104# define D2 PINDEF(D, 2)
105# define D3 PINDEF(D, 3)
106# define D4 PINDEF(D, 4)
107# define D5 PINDEF(D, 5)
108# define D6 PINDEF(D, 6)
109# define D7 PINDEF(D, 7)
110# endif
111# ifdef PORTE
112# define E0 PINDEF(E, 0)
113# define E1 PINDEF(E, 1)
114# define E2 PINDEF(E, 2)
115# define E3 PINDEF(E, 3)
116# define E4 PINDEF(E, 4)
117# define E5 PINDEF(E, 5)
118# define E6 PINDEF(E, 6)
119# define E7 PINDEF(E, 7)
120# endif
121# ifdef PORTF
122# define F0 PINDEF(F, 0)
123# define F1 PINDEF(F, 1)
124# define F2 PINDEF(F, 2)
125# define F3 PINDEF(F, 3)
126# define F4 PINDEF(F, 4)
127# define F5 PINDEF(F, 5)
128# define F6 PINDEF(F, 6)
129# define F7 PINDEF(F, 7)
130# endif
131
132# ifndef __ASSEMBLER__
133# define _PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + ((p) >> PORT_SHIFTER) + (offset))
134// Port X Input Pins Address
135# define PINx_ADDRESS(p) _PIN_ADDRESS(p, 0)
136// Port X Data Direction Register, 0:input 1:output
137# define DDRx_ADDRESS(p) _PIN_ADDRESS(p, 1)
138// Port X Data Register
139# define PORTx_ADDRESS(p) _PIN_ADDRESS(p, 2)
140# endif
141
142#elif defined(PROTOCOL_CHIBIOS)
143// Defines mapping for Proton C replacement
144# ifdef CONVERT_TO_PROTON_C
145// Left side (front)
146# define D3 PAL_LINE(GPIOA, 9)
147# define D2 PAL_LINE(GPIOA, 10)
148// GND
149// GND
150# define D1 PAL_LINE(GPIOB, 7)
151# define D0 PAL_LINE(GPIOB, 6)
152# define D4 PAL_LINE(GPIOB, 5)
153# define C6 PAL_LINE(GPIOB, 4)
154# define D7 PAL_LINE(GPIOB, 3)
155# define E6 PAL_LINE(GPIOB, 2)
156# define B4 PAL_LINE(GPIOB, 1)
157# define B5 PAL_LINE(GPIOB, 0)
158
159// Right side (front)
160// RAW
161// GND
162// RESET
163// VCC
164# define F4 PAL_LINE(GPIOA, 2)
165# define F5 PAL_LINE(GPIOA, 1)
166# define F6 PAL_LINE(GPIOA, 0)
167# define F7 PAL_LINE(GPIOB, 8)
168# define B1 PAL_LINE(GPIOB, 13)
169# define B3 PAL_LINE(GPIOB, 14)
170# define B2 PAL_LINE(GPIOB, 15)
171# define B6 PAL_LINE(GPIOB, 9)
172
173// LEDs (only D5/C13 uses an actual LED)
174# ifdef CONVERT_TO_PROTON_C_RXLED
175# define D5 PAL_LINE(GPIOC, 14)
176# define B0 PAL_LINE(GPIOC, 13)
177# else
178# define D5 PAL_LINE(GPIOC, 13)
179# define B0 PAL_LINE(GPIOC, 14)
180# endif
181# else
182# define A0 PAL_LINE(GPIOA, 0)
183# define A1 PAL_LINE(GPIOA, 1)
184# define A2 PAL_LINE(GPIOA, 2)
185# define A3 PAL_LINE(GPIOA, 3)
186# define A4 PAL_LINE(GPIOA, 4)
187# define A5 PAL_LINE(GPIOA, 5)
188# define A6 PAL_LINE(GPIOA, 6)
189# define A7 PAL_LINE(GPIOA, 7)
190# define A8 PAL_LINE(GPIOA, 8)
191# define A9 PAL_LINE(GPIOA, 9)
192# define A10 PAL_LINE(GPIOA, 10)
193# define A11 PAL_LINE(GPIOA, 11)
194# define A12 PAL_LINE(GPIOA, 12)
195# define A13 PAL_LINE(GPIOA, 13)
196# define A14 PAL_LINE(GPIOA, 14)
197# define A15 PAL_LINE(GPIOA, 15)
198# define B0 PAL_LINE(GPIOB, 0)
199# define B1 PAL_LINE(GPIOB, 1)
200# define B2 PAL_LINE(GPIOB, 2)
201# define B3 PAL_LINE(GPIOB, 3)
202# define B4 PAL_LINE(GPIOB, 4)
203# define B5 PAL_LINE(GPIOB, 5)
204# define B6 PAL_LINE(GPIOB, 6)
205# define B7 PAL_LINE(GPIOB, 7)
206# define B8 PAL_LINE(GPIOB, 8)
207# define B9 PAL_LINE(GPIOB, 9)
208# define B10 PAL_LINE(GPIOB, 10)
209# define B11 PAL_LINE(GPIOB, 11)
210# define B12 PAL_LINE(GPIOB, 12)
211# define B13 PAL_LINE(GPIOB, 13)
212# define B14 PAL_LINE(GPIOB, 14)
213# define B15 PAL_LINE(GPIOB, 15)
214# define B16 PAL_LINE(GPIOB, 16)
215# define B17 PAL_LINE(GPIOB, 17)
216# define B18 PAL_LINE(GPIOB, 18)
217# define B19 PAL_LINE(GPIOB, 19)
218# define C0 PAL_LINE(GPIOC, 0)
219# define C1 PAL_LINE(GPIOC, 1)
220# define C2 PAL_LINE(GPIOC, 2)
221# define C3 PAL_LINE(GPIOC, 3)
222# define C4 PAL_LINE(GPIOC, 4)
223# define C5 PAL_LINE(GPIOC, 5)
224# define C6 PAL_LINE(GPIOC, 6)
225# define C7 PAL_LINE(GPIOC, 7)
226# define C8 PAL_LINE(GPIOC, 8)
227# define C9 PAL_LINE(GPIOC, 9)
228# define C10 PAL_LINE(GPIOC, 10)
229# define C11 PAL_LINE(GPIOC, 11)
230# define C12 PAL_LINE(GPIOC, 12)
231# define C13 PAL_LINE(GPIOC, 13)
232# define C14 PAL_LINE(GPIOC, 14)
233# define C15 PAL_LINE(GPIOC, 15)
234# define D0 PAL_LINE(GPIOD, 0)
235# define D1 PAL_LINE(GPIOD, 1)
236# define D2 PAL_LINE(GPIOD, 2)
237# define D3 PAL_LINE(GPIOD, 3)
238# define D4 PAL_LINE(GPIOD, 4)
239# define D5 PAL_LINE(GPIOD, 5)
240# define D6 PAL_LINE(GPIOD, 6)
241# define D7 PAL_LINE(GPIOD, 7)
242# define D8 PAL_LINE(GPIOD, 8)
243# define D9 PAL_LINE(GPIOD, 9)
244# define D10 PAL_LINE(GPIOD, 10)
245# define D11 PAL_LINE(GPIOD, 11)
246# define D12 PAL_LINE(GPIOD, 12)
247# define D13 PAL_LINE(GPIOD, 13)
248# define D14 PAL_LINE(GPIOD, 14)
249# define D15 PAL_LINE(GPIOD, 15)
250# define E0 PAL_LINE(GPIOE, 0)
251# define E1 PAL_LINE(GPIOE, 1)
252# define E2 PAL_LINE(GPIOE, 2)
253# define E3 PAL_LINE(GPIOE, 3)
254# define E4 PAL_LINE(GPIOE, 4)
255# define E5 PAL_LINE(GPIOE, 5)
256# define E6 PAL_LINE(GPIOE, 6)
257# define E7 PAL_LINE(GPIOE, 7)
258# define E8 PAL_LINE(GPIOE, 8)
259# define E9 PAL_LINE(GPIOE, 9)
260# define E10 PAL_LINE(GPIOE, 10)
261# define E11 PAL_LINE(GPIOE, 11)
262# define E12 PAL_LINE(GPIOE, 12)
263# define E13 PAL_LINE(GPIOE, 13)
264# define E14 PAL_LINE(GPIOE, 14)
265# define E15 PAL_LINE(GPIOE, 15)
266# define F0 PAL_LINE(GPIOF, 0)
267# define F1 PAL_LINE(GPIOF, 1)
268# define F2 PAL_LINE(GPIOF, 2)
269# define F3 PAL_LINE(GPIOF, 3)
270# define F4 PAL_LINE(GPIOF, 4)
271# define F5 PAL_LINE(GPIOF, 5)
272# define F6 PAL_LINE(GPIOF, 6)
273# define F7 PAL_LINE(GPIOF, 7)
274# define F8 PAL_LINE(GPIOF, 8)
275# define F9 PAL_LINE(GPIOF, 9)
276# define F10 PAL_LINE(GPIOF, 10)
277# define F11 PAL_LINE(GPIOF, 11)
278# define F12 PAL_LINE(GPIOF, 12)
279# define F13 PAL_LINE(GPIOF, 13)
280# define F14 PAL_LINE(GPIOF, 14)
281# define F15 PAL_LINE(GPIOF, 15)
282# define G0 PAL_LINE(GPIOG, 0)
283# define G1 PAL_LINE(GPIOG, 1)
284# define G2 PAL_LINE(GPIOG, 2)
285# define G3 PAL_LINE(GPIOG, 3)
286# define G4 PAL_LINE(GPIOG, 4)
287# define G5 PAL_LINE(GPIOG, 5)
288# define G6 PAL_LINE(GPIOG, 6)
289# define G7 PAL_LINE(GPIOG, 7)
290# define G8 PAL_LINE(GPIOG, 8)
291# define G9 PAL_LINE(GPIOG, 9)
292# define G10 PAL_LINE(GPIOG, 10)
293# define G11 PAL_LINE(GPIOG, 11)
294# define G12 PAL_LINE(GPIOG, 12)
295# define G13 PAL_LINE(GPIOG, 13)
296# define G14 PAL_LINE(GPIOG, 14)
297# define G15 PAL_LINE(GPIOG, 15)
298# define H0 PAL_LINE(GPIOH, 0)
299# define H1 PAL_LINE(GPIOH, 1)
300# define H2 PAL_LINE(GPIOH, 2)
301# define H3 PAL_LINE(GPIOH, 3)
302# define H4 PAL_LINE(GPIOH, 4)
303# define H5 PAL_LINE(GPIOH, 5)
304# define H6 PAL_LINE(GPIOH, 6)
305# define H7 PAL_LINE(GPIOH, 7)
306# define H8 PAL_LINE(GPIOH, 8)
307# define H9 PAL_LINE(GPIOH, 9)
308# define H10 PAL_LINE(GPIOH, 10)
309# define H11 PAL_LINE(GPIOH, 11)
310# define H12 PAL_LINE(GPIOH, 12)
311# define H13 PAL_LINE(GPIOH, 13)
312# define H14 PAL_LINE(GPIOH, 14)
313# define H15 PAL_LINE(GPIOH, 15)
314# define I0 PAL_LINE(GPIOI, 0)
315# define I1 PAL_LINE(GPIOI, 1)
316# define I2 PAL_LINE(GPIOI, 2)
317# define I3 PAL_LINE(GPIOI, 3)
318# define I4 PAL_LINE(GPIOI, 4)
319# define I5 PAL_LINE(GPIOI, 5)
320# define I6 PAL_LINE(GPIOI, 6)
321# define I7 PAL_LINE(GPIOI, 7)
322# define I8 PAL_LINE(GPIOI, 8)
323# define I9 PAL_LINE(GPIOI, 9)
324# define I10 PAL_LINE(GPIOI, 10)
325# define I11 PAL_LINE(GPIOI, 11)
326# define I12 PAL_LINE(GPIOI, 12)
327# define I13 PAL_LINE(GPIOI, 13)
328# define I14 PAL_LINE(GPIOI, 14)
329# define I15 PAL_LINE(GPIOI, 15)
330# define J0 PAL_LINE(GPIOJ, 0)
331# define J1 PAL_LINE(GPIOJ, 1)
332# define J2 PAL_LINE(GPIOJ, 2)
333# define J3 PAL_LINE(GPIOJ, 3)
334# define J4 PAL_LINE(GPIOJ, 4)
335# define J5 PAL_LINE(GPIOJ, 5)
336# define J6 PAL_LINE(GPIOJ, 6)
337# define J7 PAL_LINE(GPIOJ, 7)
338# define J8 PAL_LINE(GPIOJ, 8)
339# define J9 PAL_LINE(GPIOJ, 9)
340# define J10 PAL_LINE(GPIOJ, 10)
341# define J11 PAL_LINE(GPIOJ, 11)
342# define J12 PAL_LINE(GPIOJ, 12)
343# define J13 PAL_LINE(GPIOJ, 13)
344# define J14 PAL_LINE(GPIOJ, 14)
345# define J15 PAL_LINE(GPIOJ, 15)
346// Keyboards can `#define KEYBOARD_REQUIRES_GPIOK` if they need to access GPIO-K pins. These conflict with a whole
347// bunch of layout definitions, so it's intentionally left out unless absolutely required -- in that case, the
348// keyboard designer should use a different symbol when defining their layout macros.
349# ifdef KEYBOARD_REQUIRES_GPIOK
350# define K0 PAL_LINE(GPIOK, 0)
351# define K1 PAL_LINE(GPIOK, 1)
352# define K2 PAL_LINE(GPIOK, 2)
353# define K3 PAL_LINE(GPIOK, 3)
354# define K4 PAL_LINE(GPIOK, 4)
355# define K5 PAL_LINE(GPIOK, 5)
356# define K6 PAL_LINE(GPIOK, 6)
357# define K7 PAL_LINE(GPIOK, 7)
358# define K8 PAL_LINE(GPIOK, 8)
359# define K9 PAL_LINE(GPIOK, 9)
360# define K10 PAL_LINE(GPIOK, 10)
361# define K11 PAL_LINE(GPIOK, 11)
362# define K12 PAL_LINE(GPIOK, 12)
363# define K13 PAL_LINE(GPIOK, 13)
364# define K14 PAL_LINE(GPIOK, 14)
365# define K15 PAL_LINE(GPIOK, 15)
366# endif
367# endif
368#endif
369
370#define API_SYSEX_MAX_SIZE 32 27#define API_SYSEX_MAX_SIZE 32
371 28
372#include "song_list.h" 29#include "song_list.h"
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/quantum.h b/quantum/quantum.h
index b7bf5be31..36a983d57 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -55,6 +55,8 @@
55#include "timer.h" 55#include "timer.h"
56#include "sync_timer.h" 56#include "sync_timer.h"
57#include "config_common.h" 57#include "config_common.h"
58#include "gpio.h"
59#include "atomic_util.h"
58#include "led.h" 60#include "led.h"
59#include "action_util.h" 61#include "action_util.h"
60#include "action_tapping.h" 62#include "action_tapping.h"
@@ -199,20 +201,6 @@ extern layer_state_t layer_state;
199 201
200// Function substitutions to ease GPIO manipulation 202// Function substitutions to ease GPIO manipulation
201#if defined(__AVR__) 203#if defined(__AVR__)
202typedef uint8_t pin_t;
203
204# define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
205# define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
206# define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
207# define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
208
209# define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
210# define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
211# define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
212
213# define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
214
215# define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF))
216 204
217/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal. 205/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal.
218 * But here's more margin to make it two clocks. */ 206 * But here's more margin to make it two clocks. */
@@ -221,25 +209,8 @@ typedef uint8_t pin_t;
221# endif 209# endif
222# define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) 210# define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
223 211
224#elif defined(PROTOCOL_CHIBIOS) 212#elif defined(__ARMEL__) || defined(__ARMEB__)
225typedef ioline_t pin_t;
226
227# define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
228# define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
229# define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
230# define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
231
232# define writePinHigh(pin) palSetLine(pin)
233# define writePinLow(pin) palClearLine(pin)
234# define writePin(pin, level) ((level) ? (writePinHigh(pin)) : (writePinLow(pin)))
235
236# define readPin(pin) palReadLine(pin)
237 213
238# define togglePin(pin) palToggleLine(pin)
239
240#endif
241
242#if defined(__ARMEL__) || defined(__ARMEB__)
243/* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus 214/* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus
244 * to which the GPIO is connected. 215 * to which the GPIO is connected.
245 * The connected buses differ depending on the various series of MCUs. 216 * The connected buses differ depending on the various series of MCUs.
@@ -258,63 +229,8 @@ typedef ioline_t pin_t;
258# endif 229# endif
259# endif 230# endif
260# define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) 231# define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
261#endif
262
263// Atomic macro to help make GPIO and other controls atomic.
264#ifdef IGNORE_ATOMIC_BLOCK
265/* do nothing atomic macro */
266# define ATOMIC_BLOCK for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0)
267# define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK
268# define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK
269
270#elif defined(__AVR__)
271/* atomic macro for AVR */
272# include <util/atomic.h>
273
274# define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
275# define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
276
277#elif defined(PROTOCOL_CHIBIOS) || defined(PROTOCOL_ARM_ATSAM)
278/* atomic macro for ChibiOS / ARM ATSAM */
279# if defined(PROTOCOL_ARM_ATSAM)
280# include "arm_atsam_protocol.h"
281# endif
282
283static __inline__ uint8_t __interrupt_disable__(void) {
284# if defined(PROTOCOL_CHIBIOS)
285 chSysLock();
286# endif
287# if defined(PROTOCOL_ARM_ATSAM)
288 __disable_irq();
289# endif
290 return 1;
291}
292
293static __inline__ void __interrupt_enable__(const uint8_t *__s) {
294# if defined(PROTOCOL_CHIBIOS)
295 chSysUnlock();
296# endif
297# if defined(PROTOCOL_ARM_ATSAM)
298 __enable_irq();
299# endif
300 __asm__ volatile("" ::: "memory");
301 (void)__s;
302}
303
304# define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0)
305# define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0
306
307# define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE dose not implement")
308# define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
309
310/* Other platform */
311#else
312
313# define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE dose not implement")
314# define ATOMIC_BLOCK_FORCEON _Static_assert(0, "ATOMIC_BLOCK_FORCEON dose not implement")
315 232
316#endif 233#endif
317
318#define SEND_STRING(string) send_string_P(PSTR(string)) 234#define SEND_STRING(string) send_string_P(PSTR(string))
319#define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval) 235#define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval)
320 236
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>