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.c82
1 files changed, 48 insertions, 34 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c
index 4f1f06c7a..ceb236809 100644
--- a/quantum/led_matrix.c
+++ b/quantum/led_matrix.c
@@ -17,9 +17,6 @@
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */ 18 */
19 19
20#include <stdint.h>
21#include <stdbool.h>
22#include "quantum.h"
23#include "led_matrix.h" 20#include "led_matrix.h"
24#include "progmem.h" 21#include "progmem.h"
25#include "config.h" 22#include "config.h"
@@ -37,20 +34,41 @@ led_eeconfig_t led_matrix_eeconfig;
37# define MIN(a, b) ((a) < (b) ? (a) : (b)) 34# define MIN(a, b) ((a) < (b) ? (a) : (b))
38#endif 35#endif
39 36
40#ifndef LED_DISABLE_AFTER_TIMEOUT 37#if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT)
41# define LED_DISABLE_AFTER_TIMEOUT 0 38# define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL)
39#endif
40
41#ifndef LED_DISABLE_TIMEOUT
42# define LED_DISABLE_TIMEOUT 0
42#endif 43#endif
43 44
44#ifndef LED_DISABLE_WHEN_USB_SUSPENDED 45#ifndef LED_DISABLE_WHEN_USB_SUSPENDED
45# define LED_DISABLE_WHEN_USB_SUSPENDED false 46# define LED_DISABLE_WHEN_USB_SUSPENDED false
46#endif 47#endif
47 48
48#ifndef EECONFIG_LED_MATRIX 49#if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
49# define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT 50# undef LED_MATRIX_MAXIMUM_BRIGHTNESS
51# define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
52#endif
53
54#if !defined(LED_MATRIX_VAL_STEP)
55# define LED_MATRIX_VAL_STEP 8
56#endif
57
58#if !defined(LED_MATRIX_SPD_STEP)
59# define LED_MATRIX_SPD_STEP 16
50#endif 60#endif
51 61
52#if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 62#if !defined(LED_MATRIX_STARTUP_MODE)
53# define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 63# define LED_MATRIX_STARTUP_MODE LED_MATRIX_UNIFORM_BRIGHTNESS
64#endif
65
66#if !defined(LED_MATRIX_STARTUP_VAL)
67# define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
68#endif
69
70#if !defined(LED_MATRIX_STARTUP_SPD)
71# define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
54#endif 72#endif
55 73
56bool g_suspend_state = false; 74bool g_suspend_state = false;
@@ -64,21 +82,21 @@ uint8_t g_key_hit[DRIVER_LED_TOTAL];
64// Ticks since any key was last hit. 82// Ticks since any key was last hit.
65uint32_t g_any_key_hit = 0; 83uint32_t g_any_key_hit = 0;
66 84
67uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); } 85void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
68 86
69void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); } 87void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
70 88
71void eeconfig_update_led_matrix_default(void) { 89void eeconfig_update_led_matrix_default(void) {
72 dprintf("eeconfig_update_led_matrix_default\n"); 90 dprintf("eeconfig_update_led_matrix_default\n");
73 led_matrix_eeconfig.enable = 1; 91 led_matrix_eeconfig.enable = 1;
74 led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; 92 led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE;
75 led_matrix_eeconfig.val = 128; 93 led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL;
76 led_matrix_eeconfig.speed = 0; 94 led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD;
77 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 95 eeconfig_update_led_matrix();
78} 96}
79 97
80void eeconfig_debug_led_matrix(void) { 98void eeconfig_debug_led_matrix(void) {
81 dprintf("led_matrix_eeconfig eeprom\n"); 99 dprintf("led_matrix_eeconfig EEPROM\n");
82 dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable); 100 dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
83 dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode); 101 dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
84 dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val); 102 dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
@@ -135,7 +153,7 @@ void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
135void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } 153void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); }
136 154
137// Uniform brightness 155// Uniform brightness
138void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); } 156void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(led_matrix_eeconfig.val); }
139 157
140void led_matrix_custom(void) {} 158void led_matrix_custom(void) {}
141 159
@@ -161,7 +179,7 @@ void led_matrix_task(void) {
161 179
162 // Ideally we would also stop sending zeros to the LED driver PWM buffers 180 // Ideally we would also stop sending zeros to the LED driver PWM buffers
163 // while suspended and just do a software shutdown. This is a cheap hack for now. 181 // while suspended and just do a software shutdown. This is a cheap hack for now.
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)); 182 bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || (LED_DISABLE_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_TIMEOUT));
165 uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode; 183 uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode;
166 184
167 // this gets ticked at 20 Hz. 185 // this gets ticked at 20 Hz.
@@ -227,12 +245,10 @@ void led_matrix_init(void) {
227 eeconfig_update_led_matrix_default(); 245 eeconfig_update_led_matrix_default();
228 } 246 }
229 247
230 led_matrix_eeconfig.raw = eeconfig_read_led_matrix(); 248 eeconfig_read_led_matrix();
231
232 if (!led_matrix_eeconfig.mode) { 249 if (!led_matrix_eeconfig.mode) {
233 dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); 250 dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
234 eeconfig_update_led_matrix_default(); 251 eeconfig_update_led_matrix_default();
235 led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
236 } 252 }
237 253
238 eeconfig_debug_led_matrix(); // display current eeprom values 254 eeconfig_debug_led_matrix(); // display current eeprom values
@@ -276,19 +292,19 @@ uint32_t led_matrix_get_tick(void) { return g_tick; }
276 292
277void led_matrix_toggle(void) { 293void led_matrix_toggle(void) {
278 led_matrix_eeconfig.enable ^= 1; 294 led_matrix_eeconfig.enable ^= 1;
279 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 295 eeconfig_update_led_matrix();
280} 296}
281 297
282void led_matrix_enable(void) { 298void led_matrix_enable(void) {
283 led_matrix_eeconfig.enable = 1; 299 led_matrix_eeconfig.enable = 1;
284 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 300 eeconfig_update_led_matrix();
285} 301}
286 302
287void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; } 303void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; }
288 304
289void led_matrix_disable(void) { 305void led_matrix_disable(void) {
290 led_matrix_eeconfig.enable = 0; 306 led_matrix_eeconfig.enable = 0;
291 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 307 eeconfig_update_led_matrix();
292} 308}
293 309
294void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; } 310void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; }
@@ -298,7 +314,7 @@ void led_matrix_step(void) {
298 if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) { 314 if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) {
299 led_matrix_eeconfig.mode = 1; 315 led_matrix_eeconfig.mode = 1;
300 } 316 }
301 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 317 eeconfig_update_led_matrix();
302} 318}
303 319
304void led_matrix_step_reverse(void) { 320void led_matrix_step_reverse(void) {
@@ -306,33 +322,33 @@ void led_matrix_step_reverse(void) {
306 if (led_matrix_eeconfig.mode < 1) { 322 if (led_matrix_eeconfig.mode < 1) {
307 led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1; 323 led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
308 } 324 }
309 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 325 eeconfig_update_led_matrix();
310} 326}
311 327
312void led_matrix_increase_val(void) { 328void led_matrix_increase_val(void) {
313 led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); 329 led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
314 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 330 eeconfig_update_led_matrix();
315} 331}
316 332
317void led_matrix_decrease_val(void) { 333void led_matrix_decrease_val(void) {
318 led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); 334 led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
319 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 335 eeconfig_update_led_matrix();
320} 336}
321 337
322void led_matrix_increase_speed(void) { 338void led_matrix_increase_speed(void) {
323 led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3); 339 led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3);
324 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this 340 eeconfig_update_led_matrix(); // EECONFIG needs to be increased to support this
325} 341}
326 342
327void led_matrix_decrease_speed(void) { 343void led_matrix_decrease_speed(void) {
328 led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3); 344 led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3);
329 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this 345 eeconfig_update_led_matrix(); // EECONFIG needs to be increased to support this
330} 346}
331 347
332void led_matrix_mode(uint8_t mode, bool eeprom_write) { 348void led_matrix_mode(uint8_t mode, bool eeprom_write) {
333 led_matrix_eeconfig.mode = mode; 349 led_matrix_eeconfig.mode = mode;
334 if (eeprom_write) { 350 if (eeprom_write) {
335 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 351 eeconfig_update_led_matrix();
336 } 352 }
337} 353}
338 354
@@ -342,7 +358,5 @@ void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val;
342 358
343void led_matrix_set_value(uint8_t val) { 359void led_matrix_set_value(uint8_t val) {
344 led_matrix_set_value_noeeprom(val); 360 led_matrix_set_value_noeeprom(val);
345 eeconfig_update_led_matrix(led_matrix_eeconfig.raw); 361 eeconfig_update_led_matrix();
346} 362}
347
348void backlight_set(uint8_t val) { led_matrix_set_value(val); }