diff options
Diffstat (limited to 'quantum/led_matrix.c')
-rw-r--r-- | quantum/led_matrix.c | 135 |
1 files changed, 50 insertions, 85 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c index 8ef8abe71..5c24c797a 100644 --- a/quantum/led_matrix.c +++ b/quantum/led_matrix.c | |||
@@ -30,27 +30,27 @@ | |||
30 | led_config_t led_matrix_config; | 30 | led_config_t led_matrix_config; |
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)) |
34 | #endif | 34 | #endif |
35 | 35 | ||
36 | #ifndef MIN | 36 | #ifndef MIN |
37 | #define MIN(a,b) ((a) < (b)? (a): (b)) | 37 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
38 | #endif | 38 | #endif |
39 | 39 | ||
40 | #ifndef LED_DISABLE_AFTER_TIMEOUT | 40 | #ifndef LED_DISABLE_AFTER_TIMEOUT |
41 | #define LED_DISABLE_AFTER_TIMEOUT 0 | 41 | # define LED_DISABLE_AFTER_TIMEOUT 0 |
42 | #endif | 42 | #endif |
43 | 43 | ||
44 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED | 44 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED |
45 | #define LED_DISABLE_WHEN_USB_SUSPENDED false | 45 | # define LED_DISABLE_WHEN_USB_SUSPENDED false |
46 | #endif | 46 | #endif |
47 | 47 | ||
48 | #ifndef EECONFIG_LED_MATRIX | 48 | #ifndef EECONFIG_LED_MATRIX |
49 | #define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT | 49 | # define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT |
50 | #endif | 50 | #endif |
51 | 51 | ||
52 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 | 52 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 |
53 | #define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 | 53 | # define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 |
54 | #endif | 54 | #endif |
55 | 55 | ||
56 | bool g_suspend_state = false; | 56 | bool g_suspend_state = false; |
@@ -64,37 +64,33 @@ uint8_t g_key_hit[LED_DRIVER_LED_COUNT]; | |||
64 | // Ticks since any key was last hit. | 64 | // Ticks since any key was last hit. |
65 | uint32_t g_any_key_hit = 0; | 65 | uint32_t g_any_key_hit = 0; |
66 | 66 | ||
67 | uint32_t eeconfig_read_led_matrix(void) { | 67 | uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); } |
68 | return eeprom_read_dword(EECONFIG_LED_MATRIX); | ||
69 | } | ||
70 | 68 | ||
71 | void eeconfig_update_led_matrix(uint32_t config_value) { | 69 | void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); } |
72 | eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); | ||
73 | } | ||
74 | 70 | ||
75 | void eeconfig_update_led_matrix_default(void) { | 71 | void eeconfig_update_led_matrix_default(void) { |
76 | dprintf("eeconfig_update_led_matrix_default\n"); | 72 | dprintf("eeconfig_update_led_matrix_default\n"); |
77 | led_matrix_config.enable = 1; | 73 | led_matrix_config.enable = 1; |
78 | led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; | 74 | led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; |
79 | led_matrix_config.val = 128; | 75 | led_matrix_config.val = 128; |
80 | led_matrix_config.speed = 0; | 76 | led_matrix_config.speed = 0; |
81 | eeconfig_update_led_matrix(led_matrix_config.raw); | 77 | eeconfig_update_led_matrix(led_matrix_config.raw); |
82 | } | 78 | } |
83 | 79 | ||
84 | void eeconfig_debug_led_matrix(void) { | 80 | void eeconfig_debug_led_matrix(void) { |
85 | dprintf("led_matrix_config eeprom\n"); | 81 | dprintf("led_matrix_config eeprom\n"); |
86 | dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable); | 82 | dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable); |
87 | dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode); | 83 | dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode); |
88 | dprintf("led_matrix_config.val = %d\n", led_matrix_config.val); | 84 | dprintf("led_matrix_config.val = %d\n", led_matrix_config.val); |
89 | dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed); | 85 | dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed); |
90 | } | 86 | } |
91 | 87 | ||
92 | // Last led hit | 88 | // Last led hit |
93 | #ifndef LED_HITS_TO_REMEMBER | 89 | #ifndef LED_HITS_TO_REMEMBER |
94 | #define LED_HITS_TO_REMEMBER 8 | 90 | # define LED_HITS_TO_REMEMBER 8 |
95 | #endif | 91 | #endif |
96 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | 92 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; |
97 | uint8_t g_last_led_count = 0; | 93 | uint8_t g_last_led_count = 0; |
98 | 94 | ||
99 | void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { | 95 | void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { |
100 | led_matrix led; | 96 | led_matrix led; |
@@ -110,17 +106,11 @@ void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t | |||
110 | } | 106 | } |
111 | } | 107 | } |
112 | 108 | ||
113 | void led_matrix_update_pwm_buffers(void) { | 109 | void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } |
114 | led_matrix_driver.flush(); | ||
115 | } | ||
116 | 110 | ||
117 | void led_matrix_set_index_value(int index, uint8_t value) { | 111 | void led_matrix_set_index_value(int index, uint8_t value) { led_matrix_driver.set_value(index, value); } |
118 | led_matrix_driver.set_value(index, value); | ||
119 | } | ||
120 | 112 | ||
121 | void led_matrix_set_index_value_all(uint8_t value) { | 113 | void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value_all(value); } |
122 | led_matrix_driver.set_value_all(value); | ||
123 | } | ||
124 | 114 | ||
125 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { | 115 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { |
126 | if (record->event.pressed) { | 116 | if (record->event.pressed) { |
@@ -131,37 +121,29 @@ bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { | |||
131 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | 121 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; |
132 | } | 122 | } |
133 | g_last_led_hit[0] = led[0]; | 123 | g_last_led_hit[0] = led[0]; |
134 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | 124 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); |
135 | } | 125 | } |
136 | for(uint8_t i = 0; i < led_count; i++) | 126 | for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 0; |
137 | g_key_hit[led[i]] = 0; | ||
138 | g_any_key_hit = 0; | 127 | g_any_key_hit = 0; |
139 | } else { | 128 | } else { |
140 | #ifdef LED_MATRIX_KEYRELEASES | 129 | #ifdef LED_MATRIX_KEYRELEASES |
141 | uint8_t led[8], led_count; | 130 | uint8_t led[8], led_count; |
142 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | 131 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); |
143 | for(uint8_t i = 0; i < led_count; i++) | 132 | for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255; |
144 | g_key_hit[led[i]] = 255; | ||
145 | 133 | ||
146 | g_any_key_hit = 255; | 134 | g_any_key_hit = 255; |
147 | #endif | 135 | #endif |
148 | } | 136 | } |
149 | return true; | 137 | return true; |
150 | } | 138 | } |
151 | 139 | ||
152 | void led_matrix_set_suspend_state(bool state) { | 140 | void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; } |
153 | g_suspend_state = state; | ||
154 | } | ||
155 | 141 | ||
156 | // All LEDs off | 142 | // All LEDs off |
157 | void led_matrix_all_off(void) { | 143 | void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } |
158 | led_matrix_set_index_value_all(0); | ||
159 | } | ||
160 | 144 | ||
161 | // Uniform brightness | 145 | // Uniform brightness |
162 | void led_matrix_uniform_brightness(void) { | 146 | void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_config.val); } |
163 | led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_config.val); | ||
164 | } | ||
165 | 147 | ||
166 | void led_matrix_custom(void) {} | 148 | void led_matrix_custom(void) {} |
167 | 149 | ||
@@ -180,17 +162,15 @@ void led_matrix_task(void) { | |||
180 | 162 | ||
181 | for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) { | 163 | for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) { |
182 | if (g_key_hit[led] < 255) { | 164 | if (g_key_hit[led] < 255) { |
183 | if (g_key_hit[led] == 254) | 165 | if (g_key_hit[led] == 254) g_last_led_count = MAX(g_last_led_count - 1, 0); |
184 | g_last_led_count = MAX(g_last_led_count - 1, 0); | ||
185 | g_key_hit[led]++; | 166 | g_key_hit[led]++; |
186 | } | 167 | } |
187 | } | 168 | } |
188 | 169 | ||
189 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | 170 | // Ideally we would also stop sending zeros to the LED driver PWM buffers |
190 | // while suspended and just do a software shutdown. This is a cheap hack for now. | 171 | // while suspended and just do a software shutdown. This is a cheap hack for now. |
191 | bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || | 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)); |
192 | (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; |
193 | uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode; | ||
194 | 174 | ||
195 | // this gets ticked at 20 Hz. | 175 | // this gets ticked at 20 Hz. |
196 | // each effect can opt to do calculations | 176 | // each effect can opt to do calculations |
@@ -217,12 +197,9 @@ void led_matrix_indicators(void) { | |||
217 | led_matrix_indicators_user(); | 197 | led_matrix_indicators_user(); |
218 | } | 198 | } |
219 | 199 | ||
220 | __attribute__((weak)) | 200 | __attribute__((weak)) void led_matrix_indicators_kb(void) {} |
221 | void led_matrix_indicators_kb(void) {} | ||
222 | |||
223 | __attribute__((weak)) | ||
224 | void led_matrix_indicators_user(void) {} | ||
225 | 201 | ||
202 | __attribute__((weak)) void led_matrix_indicators_user(void) {} | ||
226 | 203 | ||
227 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) | 204 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) |
228 | // { | 205 | // { |
@@ -248,7 +225,7 @@ void led_matrix_init(void) { | |||
248 | wait_ms(500); | 225 | wait_ms(500); |
249 | 226 | ||
250 | // clear the key hits | 227 | // clear the key hits |
251 | for (int led=0; led<LED_DRIVER_LED_COUNT; led++) { | 228 | for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) { |
252 | g_key_hit[led] = 255; | 229 | g_key_hit[led] = 255; |
253 | } | 230 | } |
254 | 231 | ||
@@ -266,7 +243,7 @@ void led_matrix_init(void) { | |||
266 | led_matrix_config.raw = eeconfig_read_led_matrix(); | 243 | led_matrix_config.raw = eeconfig_read_led_matrix(); |
267 | } | 244 | } |
268 | 245 | ||
269 | eeconfig_debug_led_matrix(); // display current eeprom values | 246 | eeconfig_debug_led_matrix(); // display current eeprom values |
270 | } | 247 | } |
271 | 248 | ||
272 | // Deals with the messy details of incrementing an integer | 249 | // Deals with the messy details of incrementing an integer |
@@ -303,32 +280,26 @@ static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) | |||
303 | // } | 280 | // } |
304 | // } | 281 | // } |
305 | 282 | ||
306 | uint32_t led_matrix_get_tick(void) { | 283 | uint32_t led_matrix_get_tick(void) { return g_tick; } |
307 | return g_tick; | ||
308 | } | ||
309 | 284 | ||
310 | void led_matrix_toggle(void) { | 285 | void led_matrix_toggle(void) { |
311 | led_matrix_config.enable ^= 1; | 286 | led_matrix_config.enable ^= 1; |
312 | eeconfig_update_led_matrix(led_matrix_config.raw); | 287 | eeconfig_update_led_matrix(led_matrix_config.raw); |
313 | } | 288 | } |
314 | 289 | ||
315 | void led_matrix_enable(void) { | 290 | void led_matrix_enable(void) { |
316 | led_matrix_config.enable = 1; | 291 | led_matrix_config.enable = 1; |
317 | eeconfig_update_led_matrix(led_matrix_config.raw); | 292 | eeconfig_update_led_matrix(led_matrix_config.raw); |
318 | } | 293 | } |
319 | 294 | ||
320 | void led_matrix_enable_noeeprom(void) { | 295 | void led_matrix_enable_noeeprom(void) { led_matrix_config.enable = 1; } |
321 | led_matrix_config.enable = 1; | ||
322 | } | ||
323 | 296 | ||
324 | void led_matrix_disable(void) { | 297 | void led_matrix_disable(void) { |
325 | led_matrix_config.enable = 0; | 298 | led_matrix_config.enable = 0; |
326 | eeconfig_update_led_matrix(led_matrix_config.raw); | 299 | eeconfig_update_led_matrix(led_matrix_config.raw); |
327 | } | 300 | } |
328 | 301 | ||
329 | void led_matrix_disable_noeeprom(void) { | 302 | void led_matrix_disable_noeeprom(void) { led_matrix_config.enable = 0; } |
330 | led_matrix_config.enable = 0; | ||
331 | } | ||
332 | 303 | ||
333 | void led_matrix_step(void) { | 304 | void led_matrix_step(void) { |
334 | led_matrix_config.mode++; | 305 | led_matrix_config.mode++; |
@@ -358,12 +329,12 @@ void led_matrix_decrease_val(void) { | |||
358 | 329 | ||
359 | void led_matrix_increase_speed(void) { | 330 | void led_matrix_increase_speed(void) { |
360 | led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3); | 331 | led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3); |
361 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this | 332 | eeconfig_update_led_matrix(led_matrix_config.raw); // EECONFIG needs to be increased to support this |
362 | } | 333 | } |
363 | 334 | ||
364 | void led_matrix_decrease_speed(void) { | 335 | void led_matrix_decrease_speed(void) { |
365 | led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3); | 336 | led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3); |
366 | eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this | 337 | eeconfig_update_led_matrix(led_matrix_config.raw); // EECONFIG needs to be increased to support this |
367 | } | 338 | } |
368 | 339 | ||
369 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { | 340 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { |
@@ -373,19 +344,13 @@ void led_matrix_mode(uint8_t mode, bool eeprom_write) { | |||
373 | } | 344 | } |
374 | } | 345 | } |
375 | 346 | ||
376 | uint8_t led_matrix_get_mode(void) { | 347 | uint8_t led_matrix_get_mode(void) { return led_matrix_config.mode; } |
377 | return led_matrix_config.mode; | ||
378 | } | ||
379 | 348 | ||
380 | void led_matrix_set_value_noeeprom(uint8_t val) { | 349 | void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_config.val = val; } |
381 | led_matrix_config.val = val; | ||
382 | } | ||
383 | 350 | ||
384 | void led_matrix_set_value(uint8_t val) { | 351 | void led_matrix_set_value(uint8_t val) { |
385 | led_matrix_set_value_noeeprom(val); | 352 | led_matrix_set_value_noeeprom(val); |
386 | eeconfig_update_led_matrix(led_matrix_config.raw); | 353 | eeconfig_update_led_matrix(led_matrix_config.raw); |
387 | } | 354 | } |
388 | 355 | ||
389 | void backlight_set(uint8_t val) { | 356 | void backlight_set(uint8_t val) { led_matrix_set_value(val); } |
390 | led_matrix_set_value(val); | ||
391 | } | ||