diff options
Diffstat (limited to 'quantum')
-rw-r--r-- | quantum/led_matrix.c | 404 | ||||
-rw-r--r-- | quantum/led_matrix.h | 142 | ||||
-rw-r--r-- | quantum/led_matrix_drivers.c | 147 | ||||
-rw-r--r-- | quantum/rgb_matrix.h | 12 |
4 files changed, 695 insertions, 10 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c new file mode 100644 index 000000000..9a0aa6acd --- /dev/null +++ b/quantum/led_matrix.c | |||
@@ -0,0 +1,404 @@ | |||
1 | /* Copyright 2017 Jason Williams | ||
2 | * Copyright 2017 Jack Humbert | ||
3 | * Copyright 2018 Yiancar | ||
4 | * Copyright 2019 Clueboard | ||
5 | * | ||
6 | * This program is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include <stdint.h> | ||
21 | #include <stdbool.h> | ||
22 | #include "quantum.h" | ||
23 | #include "led_matrix.h" | ||
24 | #include "progmem.h" | ||
25 | #include "config.h" | ||
26 | #include "eeprom.h" | ||
27 | #include <string.h> | ||
28 | #include <math.h> | ||
29 | |||
30 | led_config_t led_matrix_config; | ||
31 | |||
32 | #ifndef MAX | ||
33 | #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | ||
34 | #endif | ||
35 | |||
36 | #ifndef MIN | ||
37 | #define MIN(a,b) ((a) < (b)? (a): (b)) | ||
38 | #endif | ||
39 | |||
40 | #ifndef LED_DISABLE_AFTER_TIMEOUT | ||
41 | #define LED_DISABLE_AFTER_TIMEOUT 0 | ||
42 | #endif | ||
43 | |||
44 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED | ||
45 | #define LED_DISABLE_WHEN_USB_SUSPENDED false | ||
46 | #endif | ||
47 | |||
48 | #ifndef EECONFIG_LED_MATRIX | ||
49 | #define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT | ||
50 | #endif | ||
51 | |||
52 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 | ||
53 | #define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 | ||
54 | #endif | ||
55 | |||
56 | bool g_suspend_state = false; | ||
57 | |||
58 | // Global tick at 20 Hz | ||
59 | uint32_t g_tick = 0; | ||
60 | |||
61 | // Ticks since this key was last hit. | ||
62 | uint8_t g_key_hit[DRIVER_LED_TOTAL]; | ||
63 | |||
64 | // Ticks since any key was last hit. | ||
65 | uint32_t g_any_key_hit = 0; | ||
66 | |||
67 | uint32_t eeconfig_read_led_matrix(void) { | ||
68 | return eeprom_read_dword(EECONFIG_LED_MATRIX); | ||
69 | } | ||
70 | void eeconfig_update_led_matrix(uint32_t config_value) { | ||
71 | eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); | ||
72 | } | ||
73 | void eeconfig_update_led_matrix_default(void) { | ||
74 | dprintf("eeconfig_update_led_matrix_default\n"); | ||
75 | led_matrix_config.enable = 1; | ||
76 | led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; | ||
77 | led_matrix_config.val = 128; | ||
78 | led_matrix_config.speed = 0; | ||
79 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
80 | } | ||
81 | void eeconfig_debug_led_matrix(void) { | ||
82 | dprintf("led_matrix_config eprom\n"); | ||
83 | dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable); | ||
84 | dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode); | ||
85 | dprintf("led_matrix_config.val = %d\n", led_matrix_config.val); | ||
86 | dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed); | ||
87 | } | ||
88 | |||
89 | // Last led hit | ||
90 | #define LED_HITS_TO_REMEMBER 8 | ||
91 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | ||
92 | uint8_t g_last_led_count = 0; | ||
93 | |||
94 | void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { | ||
95 | led_matrix led; | ||
96 | *led_count = 0; | ||
97 | |||
98 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
99 | // map_index_to_led(i, &led); | ||
100 | led = g_leds[i]; | ||
101 | if (row == led.matrix_co.row && column == led.matrix_co.col) { | ||
102 | led_i[*led_count] = i; | ||
103 | (*led_count)++; | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | |||
108 | void led_matrix_update_pwm_buffers(void) { | ||
109 | led_matrix_driver.flush(); | ||
110 | } | ||
111 | |||
112 | void led_matrix_set_index_value(int index, uint8_t value) { | ||
113 | led_matrix_driver.set_value(index, value); | ||
114 | } | ||
115 | |||
116 | void led_matrix_set_index_value_all(uint8_t value) { | ||
117 | led_matrix_driver.set_value_all(value); | ||
118 | } | ||
119 | |||
120 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { | ||
121 | if (record->event.pressed) { | ||
122 | uint8_t led[8], led_count; | ||
123 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | ||
124 | if (led_count > 0) { | ||
125 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { | ||
126 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | ||
127 | } | ||
128 | g_last_led_hit[0] = led[0]; | ||
129 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | ||
130 | } | ||
131 | for(uint8_t i = 0; i < led_count; i++) | ||
132 | g_key_hit[led[i]] = 0; | ||
133 | g_any_key_hit = 0; | ||
134 | } else { | ||
135 | #ifdef LED_MATRIX_KEYRELEASES | ||
136 | uint8_t led[8], led_count; | ||
137 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | ||
138 | for(uint8_t i = 0; i < led_count; i++) | ||
139 | g_key_hit[led[i]] = 255; | ||
140 | |||
141 | g_any_key_hit = 255; | ||
142 | #endif | ||
143 | } | ||
144 | return true; | ||
145 | } | ||
146 | |||
147 | void led_matrix_set_suspend_state(bool state) { | ||
148 | g_suspend_state = state; | ||
149 | } | ||
150 | |||
151 | // All LEDs off | ||
152 | void led_matrix_all_off(void) { | ||
153 | led_matrix_set_index_value_all(0); | ||
154 | } | ||
155 | |||
156 | // Uniform brightness | ||
157 | void led_matrix_uniform_brightness(void) { | ||
158 | led_matrix_set_index_value_all(led_matrix_config.val); | ||
159 | } | ||
160 | |||
161 | void led_matrix_custom(void) {} | ||
162 | |||
163 | void led_matrix_task(void) { | ||
164 | #ifdef TRACK_PREVIOUS_EFFECT | ||
165 | static uint8_t toggle_enable_last = 255; | ||
166 | #endif | ||
167 | if (!led_matrix_config.enable) { | ||
168 | led_matrix_all_off(); | ||
169 | led_matrix_indicators(); | ||
170 | #ifdef TRACK_PREVIOUS_EFFECT | ||
171 | toggle_enable_last = led_matrix_config.enable; | ||
172 | #endif | ||
173 | return; | ||
174 | } | ||
175 | |||
176 | // delay 1 second before driving LEDs or doing anything else | ||
177 | // FIXME: Can't we use wait_ms() here? | ||
178 | static uint8_t startup_tick = 0; | ||
179 | if (startup_tick < 20) { | ||
180 | startup_tick++; | ||
181 | return; | ||
182 | } | ||
183 | |||
184 | g_tick++; | ||
185 | |||
186 | if (g_any_key_hit < 0xFFFFFFFF) { | ||
187 | g_any_key_hit++; | ||
188 | } | ||
189 | |||
190 | for (int led = 0; led < DRIVER_LED_TOTAL; led++) { | ||
191 | if (g_key_hit[led] < 255) { | ||
192 | if (g_key_hit[led] == 254) | ||
193 | g_last_led_count = MAX(g_last_led_count - 1, 0); | ||
194 | g_key_hit[led]++; | ||
195 | } | ||
196 | } | ||
197 | |||
198 | // Factory default magic value | ||
199 | if (led_matrix_config.mode == 255) { | ||
200 | led_matrix_uniform_brightness(); | ||
201 | return; | ||
202 | } | ||
203 | |||
204 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | ||
205 | // while suspended and just do a software shutdown. This is a cheap hack for now. | ||
206 | bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || | ||
207 | (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20)); | ||
208 | uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode; | ||
209 | |||
210 | #ifdef TRACK_PREVIOUS_EFFECT | ||
211 | // Keep track of the effect used last time, | ||
212 | // detect change in effect, so each effect can | ||
213 | // have an optional initialization. | ||
214 | |||
215 | static uint8_t effect_last = 255; | ||
216 | bool initialize = (effect != effect_last) || (led_matrix_config.enable != toggle_enable_last); | ||
217 | effect_last = effect; | ||
218 | toggle_enable_last = led_matrix_config.enable; | ||
219 | #endif | ||
220 | |||
221 | // this gets ticked at 20 Hz. | ||
222 | // each effect can opt to do calculations | ||
223 | // and/or request PWM buffer updates. | ||
224 | switch (effect) { | ||
225 | case LED_MATRIX_UNIFORM_BRIGHTNESS: | ||
226 | led_matrix_uniform_brightness(); | ||
227 | break; | ||
228 | default: | ||
229 | led_matrix_custom(); | ||
230 | break; | ||
231 | } | ||
232 | |||
233 | if (! suspend_backlight) { | ||
234 | led_matrix_indicators(); | ||
235 | } | ||
236 | |||
237 | } | ||
238 | |||
239 | void led_matrix_indicators(void) { | ||
240 | led_matrix_indicators_kb(); | ||
241 | led_matrix_indicators_user(); | ||
242 | } | ||
243 | |||
244 | __attribute__((weak)) | ||
245 | void led_matrix_indicators_kb(void) {} | ||
246 | |||
247 | __attribute__((weak)) | ||
248 | void led_matrix_indicators_user(void) {} | ||
249 | |||
250 | |||
251 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) | ||
252 | // { | ||
253 | // if (row >= MATRIX_ROWS) | ||
254 | // { | ||
255 | // // Special value, 255=none, 254=all | ||
256 | // *index = row; | ||
257 | // } | ||
258 | // else | ||
259 | // { | ||
260 | // // This needs updated to something like | ||
261 | // // uint8_t led[8], led_count; | ||
262 | // // map_row_column_to_led(row,column,led,&led_count); | ||
263 | // // for(uint8_t i = 0; i < led_count; i++) | ||
264 | // map_row_column_to_led(row, column, index); | ||
265 | // } | ||
266 | // } | ||
267 | |||
268 | void led_matrix_init(void) { | ||
269 | led_matrix_driver.init(); | ||
270 | |||
271 | // TODO: put the 1 second startup delay here? | ||
272 | |||
273 | // clear the key hits | ||
274 | for (int led=0; led<DRIVER_LED_TOTAL; led++) { | ||
275 | g_key_hit[led] = 255; | ||
276 | } | ||
277 | |||
278 | |||
279 | if (!eeconfig_is_enabled()) { | ||
280 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); | ||
281 | eeconfig_init(); | ||
282 | eeconfig_update_led_matrix_default(); | ||
283 | } | ||
284 | led_matrix_config.raw = eeconfig_read_led_matrix(); | ||
285 | if (!led_matrix_config.mode) { | ||
286 | dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n"); | ||
287 | eeconfig_update_led_matrix_default(); | ||
288 | led_matrix_config.raw = eeconfig_read_led_matrix(); | ||
289 | } | ||
290 | eeconfig_debug_led_matrix(); // display current eeprom values | ||
291 | } | ||
292 | |||
293 | // Deals with the messy details of incrementing an integer | ||
294 | static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { | ||
295 | int16_t new_value = value; | ||
296 | new_value += step; | ||
297 | return MIN(MAX( new_value, min), max ); | ||
298 | } | ||
299 | |||
300 | static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { | ||
301 | int16_t new_value = value; | ||
302 | new_value -= step; | ||
303 | return MIN(MAX( new_value, min), max ); | ||
304 | } | ||
305 | |||
306 | // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) { | ||
307 | // // 3 bytes per value | ||
308 | // return EECONFIG_LED_MATRIX + (led * 3); | ||
309 | // } | ||
310 | |||
311 | // void backlight_get_key_value(uint8_t led, uint8_t *value) { | ||
312 | // void *address = backlight_get_custom_key_value_eeprom_address(led); | ||
313 | // value = eeprom_read_byte(address); | ||
314 | // } | ||
315 | |||
316 | // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) { | ||
317 | // uint8_t led[8], led_count; | ||
318 | // map_row_column_to_led(row,column,led,&led_count); | ||
319 | // for(uint8_t i = 0; i < led_count; i++) { | ||
320 | // if (led[i] < DRIVER_LED_TOTAL) { | ||
321 | // void *address = backlight_get_custom_key_value_eeprom_address(led[i]); | ||
322 | // eeprom_update_byte(address, value); | ||
323 | // } | ||
324 | // } | ||
325 | // } | ||
326 | |||
327 | uint32_t led_matrix_get_tick(void) { | ||
328 | return g_tick; | ||
329 | } | ||
330 | |||
331 | void led_matrix_toggle(void) { | ||
332 | led_matrix_config.enable ^= 1; | ||
333 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
334 | } | ||
335 | |||
336 | void led_matrix_enable(void) { | ||
337 | led_matrix_config.enable = 1; | ||
338 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
339 | } | ||
340 | |||
341 | void led_matrix_enable_noeeprom(void) { | ||
342 | led_matrix_config.enable = 1; | ||
343 | } | ||
344 | |||
345 | void led_matrix_disable(void) { | ||
346 | led_matrix_config.enable = 0; | ||
347 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
348 | } | ||
349 | |||
350 | void led_matrix_disable_noeeprom(void) { | ||
351 | led_matrix_config.enable = 0; | ||
352 | } | ||
353 | |||
354 | void led_matrix_step(void) { | ||
355 | led_matrix_config.mode++; | ||
356 | if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX) | ||
357 | led_matrix_config.mode = 1; | ||
358 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
359 | } | ||
360 | |||
361 | void led_matrix_step_reverse(void) { | ||
362 | led_matrix_config.mode--; | ||
363 | if (led_matrix_config.mode < 1) | ||
364 | led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1; | ||
365 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
366 | } | ||
367 | |||
368 | void led_matrix_increase_val(void) { | ||
369 | led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); | ||
370 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
371 | } | ||
372 | |||
373 | void led_matrix_decrease_val(void) { | ||
374 | led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); | ||
375 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
376 | } | ||
377 | |||
378 | void led_matrix_increase_speed(void) { | ||
379 | led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3); | ||
380 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this | ||
381 | } | ||
382 | |||
383 | void led_matrix_decrease_speed(void) { | ||
384 | led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3); | ||
385 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this | ||
386 | } | ||
387 | |||
388 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { | ||
389 | led_matrix_config.mode = mode; | ||
390 | if (eeprom_write) { | ||
391 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
392 | } | ||
393 | } | ||
394 | |||
395 | uint8_t led_matrix_get_mode(void) { | ||
396 | return led_matrix_config.mode; | ||
397 | } | ||
398 | |||
399 | void led_matrix_set_value(uint8_t val, bool eeprom_write) { | ||
400 | led_matrix_config.val = val; | ||
401 | if (eeprom_write) { | ||
402 | eeconfig_update_led_matrix(led_matrix_config.raw); | ||
403 | } | ||
404 | } | ||
diff --git a/quantum/led_matrix.h b/quantum/led_matrix.h new file mode 100644 index 000000000..20f2e73c6 --- /dev/null +++ b/quantum/led_matrix.h | |||
@@ -0,0 +1,142 @@ | |||
1 | /* Copyright 2017 Jason Williams | ||
2 | * Copyright 2017 Jack Humbert | ||
3 | * Copyright 2018 Yiancar | ||
4 | * Copyright 2019 Clueboard | ||
5 | * | ||
6 | * This program is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #ifndef LED_MATRIX_H | ||
21 | #define LED_MATRIX_H | ||
22 | |||
23 | |||
24 | typedef struct Point { | ||
25 | uint8_t x; | ||
26 | uint8_t y; | ||
27 | } __attribute__((packed)) Point; | ||
28 | |||
29 | typedef struct led_matrix { | ||
30 | union { | ||
31 | uint8_t raw; | ||
32 | struct { | ||
33 | uint8_t row:4; // 16 max | ||
34 | uint8_t col:4; // 16 max | ||
35 | }; | ||
36 | } matrix_co; | ||
37 | Point point; | ||
38 | uint8_t modifier:1; | ||
39 | } __attribute__((packed)) led_matrix; | ||
40 | |||
41 | extern const led_matrix g_leds[DRIVER_LED_TOTAL]; | ||
42 | |||
43 | typedef struct { | ||
44 | uint8_t index; | ||
45 | uint8_t value; | ||
46 | } led_indicator; | ||
47 | |||
48 | typedef union { | ||
49 | uint32_t raw; | ||
50 | struct { | ||
51 | bool enable :1; | ||
52 | uint8_t mode :6; | ||
53 | uint8_t hue :8; // Unused by led_matrix | ||
54 | uint8_t sat :8; // Unused by led_matrix | ||
55 | uint8_t val :8; | ||
56 | uint8_t speed :8;//EECONFIG needs to be increased to support this | ||
57 | }; | ||
58 | } led_config_t; | ||
59 | |||
60 | enum led_matrix_effects { | ||
61 | LED_MATRIX_UNIFORM_BRIGHTNESS = 1, | ||
62 | // All new effects go above this line | ||
63 | LED_MATRIX_EFFECT_MAX | ||
64 | }; | ||
65 | |||
66 | void led_matrix_set_index_value(int index, uint8_t value); | ||
67 | void led_matrix_set_index_value_all(uint8_t value); | ||
68 | |||
69 | // This runs after another backlight effect and replaces | ||
70 | // colors already set | ||
71 | void led_matrix_indicators(void); | ||
72 | void led_matrix_indicators_kb(void); | ||
73 | void led_matrix_indicators_user(void); | ||
74 | |||
75 | void led_matrix_init(void); | ||
76 | void led_matrix_setup_drivers(void); | ||
77 | |||
78 | void led_matrix_set_suspend_state(bool state); | ||
79 | void led_matrix_set_indicator_state(uint8_t state); | ||
80 | |||
81 | void led_matrix_task(void); | ||
82 | |||
83 | // This should not be called from an interrupt | ||
84 | // (eg. from a timer interrupt). | ||
85 | // Call this while idle (in between matrix scans). | ||
86 | // If the buffer is dirty, it will update the driver with the buffer. | ||
87 | void led_matrix_update_pwm_buffers(void); | ||
88 | |||
89 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record); | ||
90 | |||
91 | uint32_t led_matrix_get_tick(void); | ||
92 | |||
93 | void led_matrix_toggle(void); | ||
94 | void led_matrix_enable(void); | ||
95 | void led_matrix_enable_noeeprom(void); | ||
96 | void led_matrix_disable(void); | ||
97 | void led_matrix_disable_noeeprom(void); | ||
98 | void led_matrix_step(void); | ||
99 | void led_matrix_step_reverse(void); | ||
100 | void led_matrix_increase_val(void); | ||
101 | void led_matrix_decrease_val(void); | ||
102 | void led_matrix_increase_speed(void); | ||
103 | void led_matrix_decrease_speed(void); | ||
104 | void led_matrix_mode(uint8_t mode, bool eeprom_write); | ||
105 | void led_matrix_mode_noeeprom(uint8_t mode); | ||
106 | uint8_t led_matrix_get_mode(void); | ||
107 | void led_matrix_set_value(uint8_t mode, bool eeprom_write); | ||
108 | |||
109 | #ifndef BACKLIGHT_ENABLE | ||
110 | #define backlight_toggle() backlight_matrix_toggle() | ||
111 | #define backlight_enable() backlight_matrix_enable() | ||
112 | #define backlight_enable_noeeprom() backlight_matrix_enable_noeeprom() | ||
113 | #define backlight_disable() backlight_matrix_disable() | ||
114 | #define backlight_disable_noeeprom() backlight_matrix_disable_noeeprom() | ||
115 | #define backlight_step() backlight_matrix_step() | ||
116 | #define backlight_set_value(val) backlight_matrix_set_value(val) | ||
117 | #define backlight_set_value_noeeprom(val) backlight_matrix_set_value_noeeprom(val) | ||
118 | #define backlight_step_reverse() backlight_matrix_step_reverse() | ||
119 | #define backlight_increase_val() backlight_matrix_increase_val() | ||
120 | #define backlight_decrease_val() backlight_matrix_decrease_val() | ||
121 | #define backlight_increase_speed() backlight_matrix_increase_speed() | ||
122 | #define backlight_decrease_speed() backlight_matrix_decrease_speed() | ||
123 | #define backlight_mode(mode) backlight_matrix_mode(mode) | ||
124 | #define backlight_mode_noeeprom(mode) backlight_matrix_mode_noeeprom(mode) | ||
125 | #define backlight_get_mode() backlight_matrix_get_mode() | ||
126 | #endif | ||
127 | |||
128 | typedef struct { | ||
129 | /* Perform any initialisation required for the other driver functions to work. */ | ||
130 | void (*init)(void); | ||
131 | |||
132 | /* Set the brightness of a single LED in the buffer. */ | ||
133 | void (*set_value)(int index, uint8_t value); | ||
134 | /* Set the brightness of all LEDS on the keyboard in the buffer. */ | ||
135 | void (*set_value_all)(uint8_t value); | ||
136 | /* Flush any buffered changes to the hardware. */ | ||
137 | void (*flush)(void); | ||
138 | } led_matrix_driver_t; | ||
139 | |||
140 | extern const led_matrix_driver_t led_matrix_driver; | ||
141 | |||
142 | #endif | ||
diff --git a/quantum/led_matrix_drivers.c b/quantum/led_matrix_drivers.c new file mode 100644 index 000000000..f00f4f366 --- /dev/null +++ b/quantum/led_matrix_drivers.c | |||
@@ -0,0 +1,147 @@ | |||
1 | /* Copyright 2018 Clueboard | ||
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 | #include <stdint.h> | ||
18 | #include <stdbool.h> | ||
19 | #include "quantum.h" | ||
20 | #include "led_matrix.h" | ||
21 | |||
22 | /* Each driver needs to define a struct: | ||
23 | * | ||
24 | * const led_matrix_driver_t led_matrix_driver; | ||
25 | * | ||
26 | * All members must be provided. Keyboard custom drivers must define this | ||
27 | * in their own files. | ||
28 | */ | ||
29 | |||
30 | #if defined(IS31FL3731) || defined(IS31FL3733) | ||
31 | |||
32 | #if defined(IS31FL3731) | ||
33 | #include "is31fl3731-simple.h" | ||
34 | #endif | ||
35 | |||
36 | #include "i2c_master.h" | ||
37 | |||
38 | static void init(void) { | ||
39 | i2c_init(); | ||
40 | #ifdef IS31FL3731 | ||
41 | #ifdef LED_DRIVER_ADDR_1 | ||
42 | IS31FL3731_init(DRIVER_ADDR_1); | ||
43 | #endif | ||
44 | #ifdef LED_DRIVER_ADDR_2 | ||
45 | IS31FL3731_init(DRIVER_ADDR_2); | ||
46 | #endif | ||
47 | #ifdef LED_DRIVER_ADDR_3 | ||
48 | IS31FL3731_init(DRIVER_ADDR_3); | ||
49 | #endif | ||
50 | #ifdef LED_DRIVER_ADDR_4 | ||
51 | IS31FL3731_init(DRIVER_ADDR_4); | ||
52 | #endif | ||
53 | #else | ||
54 | #ifdef LED_DRIVER_ADDR_1 | ||
55 | IS31FL3733_init(DRIVER_ADDR_1); | ||
56 | #endif | ||
57 | #ifdef LED_DRIVER_ADDR_2 | ||
58 | IS31FL3733_init(DRIVER_ADDR_2); | ||
59 | #endif | ||
60 | #ifdef LED_DRIVER_ADDR_3 | ||
61 | IS31FL3733_init(DRIVER_ADDR_3); | ||
62 | #endif | ||
63 | #ifdef LED_DRIVER_ADDR_4 | ||
64 | IS31FL3733_init(DRIVER_ADDR_4); | ||
65 | #endif | ||
66 | #endif | ||
67 | for (int index = 0; index < DRIVER_LED_TOTAL; index++) { | ||
68 | #ifdef IS31FL3731 | ||
69 | IS31FL3731_set_led_control_register(index, true); | ||
70 | #else | ||
71 | IS31FL3733_set_led_control_register(index, true); | ||
72 | #endif | ||
73 | } | ||
74 | // This actually updates the LED drivers | ||
75 | #ifdef IS31FL3731 | ||
76 | #ifdef LED_DRIVER_ADDR_1 | ||
77 | IS31FL3731_update_led_control_registers(DRIVER_ADDR_1); | ||
78 | #endif | ||
79 | #ifdef LED_DRIVER_ADDR_2 | ||
80 | IS31FL3731_update_led_control_registers(DRIVER_ADDR_2); | ||
81 | #endif | ||
82 | #ifdef LED_DRIVER_ADDR_3 | ||
83 | IS31FL3731_update_led_control_registers(DRIVER_ADDR_3); | ||
84 | #endif | ||
85 | #ifdef LED_DRIVER_ADDR_4 | ||
86 | IS31FL3731_update_led_control_registers(DRIVER_ADDR_4); | ||
87 | #endif | ||
88 | #else | ||
89 | #ifdef LED_DRIVER_ADDR_1 | ||
90 | IS31FL3733_update_led_control_registers(DRIVER_ADDR_1); | ||
91 | #endif | ||
92 | #ifdef LED_DRIVER_ADDR_2 | ||
93 | IS31FL3733_update_led_control_registers(DRIVER_ADDR_2); | ||
94 | #endif | ||
95 | #ifdef LED_DRIVER_ADDR_3 | ||
96 | IS31FL3733_update_led_control_registers(DRIVER_ADDR_3); | ||
97 | #endif | ||
98 | #ifdef LED_DRIVER_ADDR_4 | ||
99 | IS31FL3733_update_led_control_registers(DRIVER_ADDR_4); | ||
100 | #endif | ||
101 | #endif | ||
102 | } | ||
103 | |||
104 | static void flush(void) { | ||
105 | #ifdef IS31FL3731 | ||
106 | #ifdef LED_DRIVER_ADDR_1 | ||
107 | IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1); | ||
108 | #endif | ||
109 | #ifdef LED_DRIVER_ADDR_2 | ||
110 | IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2); | ||
111 | #endif | ||
112 | #ifdef LED_DRIVER_ADDR_3 | ||
113 | IS31FL3731_update_pwm_buffers(DRIVER_ADDR_3); | ||
114 | #endif | ||
115 | #ifdef LED_DRIVER_ADDR_4 | ||
116 | IS31FL3731_update_pwm_buffers(DRIVER_ADDR_4); | ||
117 | #endif | ||
118 | #else | ||
119 | #ifdef LED_DRIVER_ADDR_1 | ||
120 | IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1); | ||
121 | #endif | ||
122 | #ifdef LED_DRIVER_ADDR_2 | ||
123 | IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2); | ||
124 | #endif | ||
125 | #ifdef LED_DRIVER_ADDR_3 | ||
126 | IS31FL3733_update_pwm_buffers(DRIVER_ADDR_3); | ||
127 | #endif | ||
128 | #ifdef LED_DRIVER_ADDR_4 | ||
129 | IS31FL3733_update_pwm_buffers(DRIVER_ADDR_4); | ||
130 | #endif | ||
131 | #endif | ||
132 | } | ||
133 | |||
134 | const led_matrix_driver_t led_matrix_driver = { | ||
135 | .init = init, | ||
136 | .flush = flush, | ||
137 | #ifdef IS31FL3731 | ||
138 | .set_value = IS31FL3731_set_value, | ||
139 | .set_value_all = IS31FL3731_set_value_all, | ||
140 | #else | ||
141 | .set_value = IS31FL3733_set_value, | ||
142 | .set_value_all = IS31FL3733_set_value_all, | ||
143 | #endif | ||
144 | }; | ||
145 | |||
146 | |||
147 | #endif | ||
diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h index e43532d11..b64ddec07 100644 --- a/quantum/rgb_matrix.h +++ b/quantum/rgb_matrix.h | |||
@@ -50,25 +50,17 @@ typedef struct rgb_led { | |||
50 | 50 | ||
51 | extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; | 51 | extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; |
52 | 52 | ||
53 | typedef struct | ||
54 | { | ||
55 | HSV color; | ||
56 | uint8_t index; | ||
57 | } rgb_indicator; | ||
58 | |||
59 | typedef union { | 53 | typedef union { |
60 | uint32_t raw; | 54 | uint32_t raw; |
61 | struct { | 55 | struct { |
62 | bool enable :1; | 56 | bool enable :1; |
63 | uint8_t mode :6; | 57 | uint8_t mode :6; |
64 | uint16_t hue :9; | ||
65 | uint8_t sat :8; | ||
66 | uint8_t val :8; | 58 | uint8_t val :8; |
67 | uint8_t speed :8;//EECONFIG needs to be increased to support this | 59 | uint8_t speed :8;//EECONFIG needs to be increased to support this |
68 | }; | 60 | }; |
69 | } rgb_config_t; | 61 | } led_config_t; |
70 | 62 | ||
71 | enum rgb_matrix_effects { | 63 | enum _matrix_effects { |
72 | RGB_MATRIX_SOLID_COLOR = 1, | 64 | RGB_MATRIX_SOLID_COLOR = 1, |
73 | #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS | 65 | #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS |
74 | RGB_MATRIX_ALPHAS_MODS, | 66 | RGB_MATRIX_ALPHAS_MODS, |