diff options
Diffstat (limited to 'quantum/led_matrix.c')
-rw-r--r-- | quantum/led_matrix.c | 573 |
1 files changed, 383 insertions, 190 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c index 4f1f06c7a..72eb5190b 100644 --- a/quantum/led_matrix.c +++ b/quantum/led_matrix.c | |||
@@ -17,79 +17,111 @@ | |||
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" |
26 | #include "eeprom.h" | 23 | #include "eeprom.h" |
27 | #include <string.h> | 24 | #include <string.h> |
28 | #include <math.h> | 25 | #include <math.h> |
26 | #include "led_tables.h" | ||
29 | 27 | ||
30 | led_eeconfig_t led_matrix_eeconfig; | 28 | #include <lib/lib8tion/lib8tion.h> |
31 | 29 | ||
32 | #ifndef MAX | 30 | #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT) |
33 | # define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | 31 | # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL) |
34 | #endif | 32 | #endif |
35 | 33 | ||
36 | #ifndef MIN | 34 | #ifndef LED_DISABLE_TIMEOUT |
37 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) | 35 | # define LED_DISABLE_TIMEOUT 0 |
38 | #endif | ||
39 | |||
40 | #ifndef LED_DISABLE_AFTER_TIMEOUT | ||
41 | # define LED_DISABLE_AFTER_TIMEOUT 0 | ||
42 | #endif | 36 | #endif |
43 | 37 | ||
44 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED | 38 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED |
45 | # define LED_DISABLE_WHEN_USB_SUSPENDED false | 39 | # define LED_DISABLE_WHEN_USB_SUSPENDED false |
46 | #endif | 40 | #endif |
47 | 41 | ||
48 | #ifndef EECONFIG_LED_MATRIX | 42 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX |
49 | # define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT | 43 | # undef LED_MATRIX_MAXIMUM_BRIGHTNESS |
44 | # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX | ||
50 | #endif | 45 | #endif |
51 | 46 | ||
52 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 | 47 | #if !defined(LED_MATRIX_VAL_STEP) |
53 | # define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 | 48 | # define LED_MATRIX_VAL_STEP 8 |
54 | #endif | 49 | #endif |
55 | 50 | ||
56 | bool g_suspend_state = false; | 51 | #if !defined(LED_MATRIX_SPD_STEP) |
52 | # define LED_MATRIX_SPD_STEP 16 | ||
53 | #endif | ||
57 | 54 | ||
58 | // Global tick at 20 Hz | 55 | #if !defined(LED_MATRIX_STARTUP_MODE) |
59 | uint32_t g_tick = 0; | 56 | # define LED_MATRIX_STARTUP_MODE LED_MATRIX_UNIFORM_BRIGHTNESS |
57 | #endif | ||
60 | 58 | ||
61 | // Ticks since this key was last hit. | 59 | #if !defined(LED_MATRIX_STARTUP_VAL) |
62 | uint8_t g_key_hit[DRIVER_LED_TOTAL]; | 60 | # define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS |
61 | #endif | ||
63 | 62 | ||
64 | // Ticks since any key was last hit. | 63 | #if !defined(LED_MATRIX_STARTUP_SPD) |
65 | uint32_t g_any_key_hit = 0; | 64 | # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2 |
65 | #endif | ||
66 | 66 | ||
67 | uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); } | 67 | // globals |
68 | bool g_suspend_state = false; | ||
69 | led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr | ||
70 | uint32_t g_led_timer; | ||
71 | #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS | ||
72 | uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}}; | ||
73 | #endif // LED_MATRIX_FRAMEBUFFER_EFFECTS | ||
74 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
75 | last_hit_t g_last_hit_tracker; | ||
76 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
77 | |||
78 | // internals | ||
79 | static uint8_t led_last_enable = UINT8_MAX; | ||
80 | static uint8_t led_last_effect = UINT8_MAX; | ||
81 | static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false}; | ||
82 | static led_task_states led_task_state = SYNCING; | ||
83 | #if LED_DISABLE_TIMEOUT > 0 | ||
84 | static uint32_t led_anykey_timer; | ||
85 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
86 | |||
87 | // double buffers | ||
88 | static uint32_t led_timer_buffer; | ||
89 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
90 | static last_hit_t last_hit_buffer; | ||
91 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
92 | |||
93 | // split led matrix | ||
94 | #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT) | ||
95 | const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; | ||
96 | #endif | ||
68 | 97 | ||
69 | void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); } | 98 | void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); } |
99 | |||
100 | void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); } | ||
70 | 101 | ||
71 | void eeconfig_update_led_matrix_default(void) { | 102 | void eeconfig_update_led_matrix_default(void) { |
72 | dprintf("eeconfig_update_led_matrix_default\n"); | 103 | dprintf("eeconfig_update_led_matrix_default\n"); |
73 | led_matrix_eeconfig.enable = 1; | 104 | led_matrix_eeconfig.enable = 1; |
74 | led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; | 105 | led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE; |
75 | led_matrix_eeconfig.val = 128; | 106 | led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL; |
76 | led_matrix_eeconfig.speed = 0; | 107 | led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD; |
77 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 108 | led_matrix_eeconfig.flags = LED_FLAG_ALL; |
109 | eeconfig_update_led_matrix(); | ||
78 | } | 110 | } |
79 | 111 | ||
80 | void eeconfig_debug_led_matrix(void) { | 112 | void eeconfig_debug_led_matrix(void) { |
81 | dprintf("led_matrix_eeconfig eeprom\n"); | 113 | dprintf("led_matrix_eeconfig EEPROM\n"); |
82 | dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable); | 114 | dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable); |
83 | dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode); | 115 | dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode); |
84 | dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val); | 116 | dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val); |
85 | dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed); | 117 | dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed); |
118 | dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags); | ||
86 | } | 119 | } |
87 | 120 | ||
88 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | 121 | __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; } |
89 | uint8_t g_last_led_count = 0; | ||
90 | 122 | ||
91 | uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { | 123 | uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { |
92 | uint8_t led_count = 0; | 124 | uint8_t led_count = led_matrix_map_row_column_to_led_kb(row, column, led_i); |
93 | uint8_t led_index = g_led_config.matrix_co[row][column]; | 125 | uint8_t led_index = g_led_config.matrix_co[row][column]; |
94 | if (led_index != NO_LED) { | 126 | if (led_index != NO_LED) { |
95 | led_i[led_count] = led_index; | 127 | led_i[led_count] = led_index; |
@@ -100,88 +132,227 @@ uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { | |||
100 | 132 | ||
101 | void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } | 133 | void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } |
102 | 134 | ||
103 | void led_matrix_set_index_value(int index, uint8_t value) { led_matrix_driver.set_value(index, value); } | 135 | void led_matrix_set_value(int index, uint8_t value) { |
104 | 136 | #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT) | |
105 | void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value_all(value); } | 137 | if (!is_keyboard_left() && index >= k_led_matrix_split[0]) |
106 | 138 | # ifdef USE_CIE1931_CURVE | |
107 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { | 139 | led_matrix_driver.set_value(index - k_led_matrix_split[0], pgm_read_byte(&CIE1931_CURVE[value])); |
108 | if (record->event.pressed) { | 140 | # else |
109 | uint8_t led[8]; | 141 | led_matrix_driver.set_value(index - k_led_matrix_split[0], value); |
110 | uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led); | 142 | # endif |
111 | if (led_count > 0) { | 143 | else if (is_keyboard_left() && index < k_led_matrix_split[0]) |
112 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { | 144 | #endif |
113 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | 145 | #ifdef USE_CIE1931_CURVE |
114 | } | 146 | led_matrix_driver.set_value(index, pgm_read_byte(&CIE1931_CURVE[value])); |
115 | g_last_led_hit[0] = led[0]; | 147 | #else |
116 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | 148 | led_matrix_driver.set_value(index, value); |
117 | } | 149 | #endif |
118 | for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 0; | 150 | } |
119 | g_any_key_hit = 0; | ||
120 | } else { | ||
121 | #ifdef LED_MATRIX_KEYRELEASES | ||
122 | uint8_t led[8]; | ||
123 | uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led); | ||
124 | for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255; | ||
125 | 151 | ||
126 | g_any_key_hit = 255; | 152 | void led_matrix_set_value_all(uint8_t value) { |
153 | #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT) | ||
154 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) led_matrix_set_value(i, value); | ||
155 | #else | ||
156 | # ifdef USE_CIE1931_CURVE | ||
157 | led_matrix_driver.set_value_all(pgm_read_byte(&CIE1931_CURVE[value])); | ||
158 | # else | ||
159 | led_matrix_driver.set_value_all(value); | ||
160 | # endif | ||
127 | #endif | 161 | #endif |
128 | } | ||
129 | return true; | ||
130 | } | 162 | } |
131 | 163 | ||
132 | void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; } | 164 | void process_led_matrix(uint8_t row, uint8_t col, bool pressed) { |
165 | #ifndef LED_MATRIX_SPLIT | ||
166 | if (!is_keyboard_master()) return; | ||
167 | #endif | ||
168 | #if LED_DISABLE_TIMEOUT > 0 | ||
169 | led_anykey_timer = 0; | ||
170 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
133 | 171 | ||
134 | // All LEDs off | 172 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED |
135 | void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } | 173 | uint8_t led[LED_HITS_TO_REMEMBER]; |
174 | uint8_t led_count = 0; | ||
136 | 175 | ||
137 | // Uniform brightness | 176 | # if defined(LED_MATRIX_KEYRELEASES) |
138 | void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); } | 177 | if (!pressed) |
178 | # elif defined(LED_MATRIX_KEYPRESSES) | ||
179 | if (pressed) | ||
180 | # endif // defined(LED_MATRIX_KEYRELEASES) | ||
181 | { | ||
182 | led_count = led_matrix_map_row_column_to_led(row, col, led); | ||
183 | } | ||
139 | 184 | ||
140 | void led_matrix_custom(void) {} | 185 | if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) { |
186 | memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
187 | memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
188 | memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit | ||
189 | memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
190 | last_hit_buffer.count--; | ||
191 | } | ||
141 | 192 | ||
142 | void led_matrix_task(void) { | 193 | for (uint8_t i = 0; i < led_count; i++) { |
143 | if (!led_matrix_eeconfig.enable) { | 194 | uint8_t index = last_hit_buffer.count; |
144 | led_matrix_all_off(); | 195 | last_hit_buffer.x[index] = g_led_config.point[led[i]].x; |
145 | led_matrix_indicators(); | 196 | last_hit_buffer.y[index] = g_led_config.point[led[i]].y; |
146 | return; | 197 | last_hit_buffer.index[index] = led[i]; |
198 | last_hit_buffer.tick[index] = 0; | ||
199 | last_hit_buffer.count++; | ||
147 | } | 200 | } |
201 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
148 | 202 | ||
149 | g_tick++; | 203 | #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP) |
204 | if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) { | ||
205 | process_led_matrix_typing_heatmap(row, col); | ||
206 | } | ||
207 | #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP) | ||
208 | } | ||
150 | 209 | ||
151 | if (g_any_key_hit < 0xFFFFFFFF) { | 210 | static bool led_matrix_none(effect_params_t *params) { |
152 | g_any_key_hit++; | 211 | if (!params->init) { |
212 | return false; | ||
153 | } | 213 | } |
154 | 214 | ||
155 | for (int led = 0; led < DRIVER_LED_TOTAL; led++) { | 215 | led_matrix_set_value_all(0); |
156 | if (g_key_hit[led] < 255) { | 216 | return false; |
157 | if (g_key_hit[led] == 254) g_last_led_count = MAX(g_last_led_count - 1, 0); | 217 | } |
158 | g_key_hit[led]++; | 218 | |
219 | static bool led_matrix_uniform_brightness(effect_params_t *params) { | ||
220 | LED_MATRIX_USE_LIMITS(led_min, led_max); | ||
221 | |||
222 | uint8_t val = led_matrix_eeconfig.val; | ||
223 | for (uint8_t i = led_min; i < led_max; i++) { | ||
224 | LED_MATRIX_TEST_LED_FLAGS(); | ||
225 | led_matrix_set_value(i, val); | ||
226 | } | ||
227 | return led_max < DRIVER_LED_TOTAL; | ||
228 | } | ||
229 | |||
230 | static void led_task_timers(void) { | ||
231 | #if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0 | ||
232 | uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer); | ||
233 | #endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0 | ||
234 | led_timer_buffer = sync_timer_read32(); | ||
235 | |||
236 | // Update double buffer timers | ||
237 | #if LED_DISABLE_TIMEOUT > 0 | ||
238 | if (led_anykey_timer < UINT32_MAX) { | ||
239 | if (UINT32_MAX - deltaTime < led_anykey_timer) { | ||
240 | led_anykey_timer = UINT32_MAX; | ||
241 | } else { | ||
242 | led_anykey_timer += deltaTime; | ||
243 | } | ||
244 | } | ||
245 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
246 | |||
247 | // Update double buffer last hit timers | ||
248 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
249 | uint8_t count = last_hit_buffer.count; | ||
250 | for (uint8_t i = 0; i < count; ++i) { | ||
251 | if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) { | ||
252 | last_hit_buffer.count--; | ||
253 | continue; | ||
159 | } | 254 | } |
255 | last_hit_buffer.tick[i] += deltaTime; | ||
160 | } | 256 | } |
257 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
258 | } | ||
161 | 259 | ||
162 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | 260 | static void led_task_sync(void) { |
163 | // while suspended and just do a software shutdown. This is a cheap hack for now. | 261 | // next task |
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)); | 262 | if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING; |
165 | uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode; | 263 | } |
264 | |||
265 | static void led_task_start(void) { | ||
266 | // reset iter | ||
267 | led_effect_params.iter = 0; | ||
268 | |||
269 | // update double buffers | ||
270 | g_led_timer = led_timer_buffer; | ||
271 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
272 | g_last_hit_tracker = last_hit_buffer; | ||
273 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
274 | |||
275 | // next task | ||
276 | led_task_state = RENDERING; | ||
277 | } | ||
278 | |||
279 | static void led_task_render(uint8_t effect) { | ||
280 | bool rendering = false; | ||
281 | led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable); | ||
282 | if (led_effect_params.flags != led_matrix_eeconfig.flags) { | ||
283 | led_effect_params.flags = led_matrix_eeconfig.flags; | ||
284 | led_matrix_set_value_all(0); | ||
285 | } | ||
166 | 286 | ||
167 | // this gets ticked at 20 Hz. | ||
168 | // each effect can opt to do calculations | 287 | // each effect can opt to do calculations |
169 | // and/or request PWM buffer updates. | 288 | // and/or request PWM buffer updates. |
170 | switch (effect) { | 289 | switch (effect) { |
171 | case LED_MATRIX_UNIFORM_BRIGHTNESS: | 290 | case LED_MATRIX_NONE: |
172 | led_matrix_uniform_brightness(); | 291 | rendering = led_matrix_none(&led_effect_params); |
173 | break; | 292 | break; |
174 | default: | 293 | case LED_MATRIX_UNIFORM_BRIGHTNESS: |
175 | led_matrix_custom(); | 294 | rendering = led_matrix_uniform_brightness(&led_effect_params); |
176 | break; | 295 | break; |
177 | } | 296 | } |
178 | 297 | ||
179 | if (!suspend_backlight) { | 298 | led_effect_params.iter++; |
180 | led_matrix_indicators(); | 299 | |
300 | // next task | ||
301 | if (!rendering) { | ||
302 | led_task_state = FLUSHING; | ||
303 | if (!led_effect_params.init && effect == LED_MATRIX_NONE) { | ||
304 | // We only need to flush once if we are LED_MATRIX_NONE | ||
305 | led_task_state = SYNCING; | ||
306 | } | ||
181 | } | 307 | } |
308 | } | ||
182 | 309 | ||
183 | // Tell the LED driver to update its state | 310 | static void led_task_flush(uint8_t effect) { |
184 | led_matrix_driver.flush(); | 311 | // update last trackers after the first full render so we can init over several frames |
312 | led_last_effect = effect; | ||
313 | led_last_enable = led_matrix_eeconfig.enable; | ||
314 | |||
315 | // update pwm buffers | ||
316 | led_matrix_update_pwm_buffers(); | ||
317 | |||
318 | // next task | ||
319 | led_task_state = SYNCING; | ||
320 | } | ||
321 | |||
322 | void led_matrix_task(void) { | ||
323 | led_task_timers(); | ||
324 | |||
325 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | ||
326 | // while suspended and just do a software shutdown. This is a cheap hack for now. | ||
327 | bool suspend_backlight = | ||
328 | #if LED_DISABLE_WHEN_USB_SUSPENDED == true | ||
329 | g_suspend_state || | ||
330 | #endif // LED_DISABLE_WHEN_USB_SUSPENDED == true | ||
331 | #if LED_DISABLE_TIMEOUT > 0 | ||
332 | (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) || | ||
333 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
334 | false; | ||
335 | |||
336 | uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode; | ||
337 | |||
338 | switch (led_task_state) { | ||
339 | case STARTING: | ||
340 | led_task_start(); | ||
341 | break; | ||
342 | case RENDERING: | ||
343 | led_task_render(effect); | ||
344 | if (effect) { | ||
345 | led_matrix_indicators(); | ||
346 | led_matrix_indicators_advanced(&led_effect_params); | ||
347 | } | ||
348 | break; | ||
349 | case FLUSHING: | ||
350 | led_task_flush(effect); | ||
351 | break; | ||
352 | case SYNCING: | ||
353 | led_task_sync(); | ||
354 | break; | ||
355 | } | ||
185 | } | 356 | } |
186 | 357 | ||
187 | void led_matrix_indicators(void) { | 358 | void led_matrix_indicators(void) { |
@@ -193,33 +364,42 @@ __attribute__((weak)) void led_matrix_indicators_kb(void) {} | |||
193 | 364 | ||
194 | __attribute__((weak)) void led_matrix_indicators_user(void) {} | 365 | __attribute__((weak)) void led_matrix_indicators_user(void) {} |
195 | 366 | ||
196 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) | 367 | void led_matrix_indicators_advanced(effect_params_t *params) { |
197 | // { | 368 | /* special handling is needed for "params->iter", since it's already been incremented. |
198 | // if (row >= MATRIX_ROWS) | 369 | * Could move the invocations to led_task_render, but then it's missing a few checks |
199 | // { | 370 | * and not sure which would be better. Otherwise, this should be called from |
200 | // // Special value, 255=none, 254=all | 371 | * led_task_render, right before the iter++ line. |
201 | // *index = row; | 372 | */ |
202 | // } | 373 | #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL |
203 | // else | 374 | uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1); |
204 | // { | 375 | uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; |
205 | // // This needs updated to something like | 376 | if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL; |
206 | // // uint8_t led[8]; | 377 | #else |
207 | // // uint8_t led_count = map_row_column_to_led(row, column, led); | 378 | uint8_t min = 0; |
208 | // // for(uint8_t i = 0; i < led_count; i++) | 379 | uint8_t max = DRIVER_LED_TOTAL; |
209 | // map_row_column_to_led(row, column, index); | 380 | #endif |
210 | // } | 381 | led_matrix_indicators_advanced_kb(min, max); |
211 | // } | 382 | led_matrix_indicators_advanced_user(min, max); |
383 | } | ||
384 | |||
385 | __attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {} | ||
386 | |||
387 | __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {} | ||
212 | 388 | ||
213 | void led_matrix_init(void) { | 389 | void led_matrix_init(void) { |
214 | led_matrix_driver.init(); | 390 | led_matrix_driver.init(); |
215 | 391 | ||
216 | // Wait half a second for the driver to finish initializing | 392 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED |
217 | wait_ms(500); | 393 | g_last_hit_tracker.count = 0; |
394 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { | ||
395 | g_last_hit_tracker.tick[i] = UINT16_MAX; | ||
396 | } | ||
218 | 397 | ||
219 | // clear the key hits | 398 | last_hit_buffer.count = 0; |
220 | for (int led = 0; led < DRIVER_LED_TOTAL; led++) { | 399 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { |
221 | g_key_hit[led] = 255; | 400 | last_hit_buffer.tick[i] = UINT16_MAX; |
222 | } | 401 | } |
402 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
223 | 403 | ||
224 | if (!eeconfig_is_enabled()) { | 404 | if (!eeconfig_is_enabled()) { |
225 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); | 405 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); |
@@ -227,122 +407,135 @@ void led_matrix_init(void) { | |||
227 | eeconfig_update_led_matrix_default(); | 407 | eeconfig_update_led_matrix_default(); |
228 | } | 408 | } |
229 | 409 | ||
230 | led_matrix_eeconfig.raw = eeconfig_read_led_matrix(); | 410 | eeconfig_read_led_matrix(); |
231 | |||
232 | if (!led_matrix_eeconfig.mode) { | 411 | if (!led_matrix_eeconfig.mode) { |
233 | dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); | 412 | dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); |
234 | eeconfig_update_led_matrix_default(); | 413 | eeconfig_update_led_matrix_default(); |
235 | led_matrix_eeconfig.raw = eeconfig_read_led_matrix(); | ||
236 | } | 414 | } |
237 | |||
238 | eeconfig_debug_led_matrix(); // display current eeprom values | 415 | eeconfig_debug_led_matrix(); // display current eeprom values |
239 | } | 416 | } |
240 | 417 | ||
241 | // Deals with the messy details of incrementing an integer | 418 | void led_matrix_set_suspend_state(bool state) { |
242 | static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { | 419 | if (LED_DISABLE_WHEN_USB_SUSPENDED && state) { |
243 | int16_t new_value = value; | 420 | led_matrix_set_value_all(0); // turn off all LEDs when suspending |
244 | new_value += step; | 421 | } |
245 | return MIN(MAX(new_value, min), max); | 422 | g_suspend_state = state; |
246 | } | 423 | } |
247 | 424 | ||
248 | static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { | 425 | bool led_matrix_get_suspend_state(void) { return g_suspend_state; } |
249 | int16_t new_value = value; | ||
250 | new_value -= step; | ||
251 | return MIN(MAX(new_value, min), max); | ||
252 | } | ||
253 | 426 | ||
254 | // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) { | 427 | void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) { |
255 | // // 3 bytes per value | ||
256 | // return EECONFIG_LED_MATRIX + (led * 3); | ||
257 | // } | ||
258 | |||
259 | // void backlight_get_key_value(uint8_t led, uint8_t *value) { | ||
260 | // void *address = backlight_get_custom_key_value_eeprom_address(led); | ||
261 | // value = eeprom_read_byte(address); | ||
262 | // } | ||
263 | |||
264 | // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) { | ||
265 | // uint8_t led[8]; | ||
266 | // uint8_t led_count = map_row_column_to_led(row, column, led); | ||
267 | // for(uint8_t i = 0; i < led_count; i++) { | ||
268 | // if (led[i] < DRIVER_LED_TOTAL) { | ||
269 | // void *address = backlight_get_custom_key_value_eeprom_address(led[i]); | ||
270 | // eeprom_update_byte(address, value); | ||
271 | // } | ||
272 | // } | ||
273 | // } | ||
274 | |||
275 | uint32_t led_matrix_get_tick(void) { return g_tick; } | ||
276 | |||
277 | void led_matrix_toggle(void) { | ||
278 | led_matrix_eeconfig.enable ^= 1; | 428 | led_matrix_eeconfig.enable ^= 1; |
279 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 429 | led_task_state = STARTING; |
430 | if (write_to_eeprom) { | ||
431 | eeconfig_update_led_matrix(); | ||
432 | } | ||
433 | dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable); | ||
280 | } | 434 | } |
435 | void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); } | ||
436 | void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); } | ||
281 | 437 | ||
282 | void led_matrix_enable(void) { | 438 | void led_matrix_enable(void) { |
283 | led_matrix_eeconfig.enable = 1; | 439 | led_matrix_enable_noeeprom(); |
284 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 440 | eeconfig_update_led_matrix(); |
285 | } | 441 | } |
286 | 442 | ||
287 | void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; } | 443 | void led_matrix_enable_noeeprom(void) { |
444 | if (!led_matrix_eeconfig.enable) led_task_state = STARTING; | ||
445 | led_matrix_eeconfig.enable = 1; | ||
446 | } | ||
288 | 447 | ||
289 | void led_matrix_disable(void) { | 448 | void led_matrix_disable(void) { |
449 | led_matrix_disable_noeeprom(); | ||
450 | eeconfig_update_led_matrix(); | ||
451 | } | ||
452 | |||
453 | void led_matrix_disable_noeeprom(void) { | ||
454 | if (led_matrix_eeconfig.enable) led_task_state = STARTING; | ||
290 | led_matrix_eeconfig.enable = 0; | 455 | led_matrix_eeconfig.enable = 0; |
291 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | ||
292 | } | 456 | } |
293 | 457 | ||
294 | void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; } | 458 | uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; } |
295 | 459 | ||
296 | void led_matrix_step(void) { | 460 | void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { |
297 | led_matrix_eeconfig.mode++; | 461 | if (!led_matrix_eeconfig.enable) { |
298 | if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) { | 462 | return; |
299 | led_matrix_eeconfig.mode = 1; | ||
300 | } | 463 | } |
301 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 464 | if (mode < 1) { |
302 | } | 465 | led_matrix_eeconfig.mode = 1; |
303 | 466 | } else if (mode >= LED_MATRIX_EFFECT_MAX) { | |
304 | void led_matrix_step_reverse(void) { | ||
305 | led_matrix_eeconfig.mode--; | ||
306 | if (led_matrix_eeconfig.mode < 1) { | ||
307 | led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1; | 467 | led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1; |
468 | } else { | ||
469 | led_matrix_eeconfig.mode = mode; | ||
470 | } | ||
471 | led_task_state = STARTING; | ||
472 | if (write_to_eeprom) { | ||
473 | eeconfig_update_led_matrix(); | ||
308 | } | 474 | } |
309 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 475 | dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode); |
310 | } | 476 | } |
477 | void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); } | ||
478 | void led_matrix_mode(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, true); } | ||
311 | 479 | ||
312 | void led_matrix_increase_val(void) { | 480 | uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; } |
313 | led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); | ||
314 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | ||
315 | } | ||
316 | 481 | ||
317 | void led_matrix_decrease_val(void) { | 482 | void led_matrix_step_helper(bool write_to_eeprom) { |
318 | led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); | 483 | uint8_t mode = led_matrix_eeconfig.mode + 1; |
319 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 484 | led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom); |
320 | } | 485 | } |
486 | void led_matrix_step_noeeprom(void) { led_matrix_step_helper(false); } | ||
487 | void led_matrix_step(void) { led_matrix_step_helper(true); } | ||
321 | 488 | ||
322 | void led_matrix_increase_speed(void) { | 489 | void led_matrix_step_reverse_helper(bool write_to_eeprom) { |
323 | led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3); | 490 | uint8_t mode = led_matrix_eeconfig.mode - 1; |
324 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this | 491 | led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom); |
325 | } | 492 | } |
493 | void led_matrix_step_reverse_noeeprom(void) { led_matrix_step_reverse_helper(false); } | ||
494 | void led_matrix_step_reverse(void) { led_matrix_step_reverse_helper(true); } | ||
326 | 495 | ||
327 | void led_matrix_decrease_speed(void) { | 496 | void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) { |
328 | led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3); | 497 | if (!led_matrix_eeconfig.enable) { |
329 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this | 498 | return; |
499 | } | ||
500 | led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val; | ||
501 | if (write_to_eeprom) { | ||
502 | eeconfig_update_led_matrix(); | ||
503 | } | ||
504 | dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val); | ||
330 | } | 505 | } |
506 | void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); } | ||
507 | void led_matrix_set_val(uint8_t val) { led_matrix_set_val_eeprom_helper(val, true); } | ||
331 | 508 | ||
332 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { | 509 | uint8_t led_matrix_get_val(void) { return led_matrix_eeconfig.val; } |
333 | led_matrix_eeconfig.mode = mode; | 510 | |
334 | if (eeprom_write) { | 511 | void led_matrix_increase_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); } |
335 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 512 | void led_matrix_increase_val_noeeprom(void) { led_matrix_increase_val_helper(false); } |
513 | void led_matrix_increase_val(void) { led_matrix_increase_val_helper(true); } | ||
514 | |||
515 | void led_matrix_decrease_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); } | ||
516 | void led_matrix_decrease_val_noeeprom(void) { led_matrix_decrease_val_helper(false); } | ||
517 | void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); } | ||
518 | |||
519 | void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) { | ||
520 | led_matrix_eeconfig.speed = speed; | ||
521 | if (write_to_eeprom) { | ||
522 | eeconfig_update_led_matrix(); | ||
336 | } | 523 | } |
524 | dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed); | ||
337 | } | 525 | } |
526 | void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); } | ||
527 | void led_matrix_set_speed(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, true); } | ||
338 | 528 | ||
339 | uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; } | 529 | uint8_t led_matrix_get_speed(void) { return led_matrix_eeconfig.speed; } |
340 | 530 | ||
341 | void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; } | 531 | void led_matrix_increase_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qadd8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); } |
532 | void led_matrix_increase_speed_noeeprom(void) { led_matrix_increase_speed_helper(false); } | ||
533 | void led_matrix_increase_speed(void) { led_matrix_increase_speed_helper(true); } | ||
342 | 534 | ||
343 | void led_matrix_set_value(uint8_t val) { | 535 | void led_matrix_decrease_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qsub8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); } |
344 | led_matrix_set_value_noeeprom(val); | 536 | void led_matrix_decrease_speed_noeeprom(void) { led_matrix_decrease_speed_helper(false); } |
345 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 537 | void led_matrix_decrease_speed(void) { led_matrix_decrease_speed_helper(true); } |
346 | } | 538 | |
539 | led_flags_t led_matrix_get_flags(void) { return led_matrix_eeconfig.flags; } | ||
347 | 540 | ||
348 | void backlight_set(uint8_t val) { led_matrix_set_value(val); } | 541 | void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; } |