diff options
| author | Ryan <fauxpark@gmail.com> | 2021-04-20 17:38:44 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-20 17:38:44 +1000 |
| commit | cb19c0906e215a46a8e9461af3be022f2ebcb263 (patch) | |
| tree | 5f9d8b877aaf3bdf1456b45cf90d3962e2804577 /quantum | |
| parent | b1f48da5ee09a2b132d631e9f40f9b248eb941aa (diff) | |
| download | qmk_firmware-cb19c0906e215a46a8e9461af3be022f2ebcb263.tar.gz qmk_firmware-cb19c0906e215a46a8e9461af3be022f2ebcb263.zip | |
LED Matrix: Reactive effect buffers & advanced indicators (#12588)
Diffstat (limited to 'quantum')
| -rw-r--r-- | quantum/led_matrix.c | 125 | ||||
| -rw-r--r-- | quantum/led_matrix.h | 10 |
2 files changed, 107 insertions, 28 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c index c40e5bd7d..69600c498 100644 --- a/quantum/led_matrix.c +++ b/quantum/led_matrix.c | |||
| @@ -27,14 +27,6 @@ | |||
| 27 | 27 | ||
| 28 | #include <lib/lib8tion/lib8tion.h> | 28 | #include <lib/lib8tion/lib8tion.h> |
| 29 | 29 | ||
| 30 | #ifndef MAX | ||
| 31 | # define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #ifndef MIN | ||
| 35 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT) | 30 | #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT) |
| 39 | # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL) | 31 | # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL) |
| 40 | #endif | 32 | #endif |
| @@ -76,6 +68,12 @@ | |||
| 76 | bool g_suspend_state = false; | 68 | bool g_suspend_state = false; |
| 77 | led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr | 69 | led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr |
| 78 | uint32_t g_led_timer; | 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 | ||
| 79 | 77 | ||
| 80 | // internals | 78 | // internals |
| 81 | static uint8_t led_last_enable = UINT8_MAX; | 79 | static uint8_t led_last_enable = UINT8_MAX; |
| @@ -88,6 +86,9 @@ static uint32_t led_anykey_timer; | |||
| 88 | 86 | ||
| 89 | // double buffers | 87 | // double buffers |
| 90 | static uint32_t led_timer_buffer; | 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 | ||
| 91 | 92 | ||
| 92 | void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); } | 93 | void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); } |
| 93 | 94 | ||
| @@ -112,9 +113,6 @@ void eeconfig_debug_led_matrix(void) { | |||
| 112 | dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags); | 113 | dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags); |
| 113 | } | 114 | } |
| 114 | 115 | ||
| 115 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | ||
| 116 | uint8_t g_last_led_count = 0; | ||
| 117 | |||
| 118 | __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; } | 116 | __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; } |
| 119 | 117 | ||
| 120 | uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { | 118 | uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { |
| @@ -150,22 +148,42 @@ void process_led_matrix(uint8_t row, uint8_t col, bool pressed) { | |||
| 150 | led_anykey_timer = 0; | 148 | led_anykey_timer = 0; |
| 151 | #endif // LED_DISABLE_TIMEOUT > 0 | 149 | #endif // LED_DISABLE_TIMEOUT > 0 |
| 152 | 150 | ||
| 153 | if (pressed) { | 151 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED |
| 154 | uint8_t led[8]; | 152 | uint8_t led[LED_HITS_TO_REMEMBER]; |
| 155 | uint8_t led_count = led_matrix_map_row_column_to_led(row, col, led); | 153 | uint8_t led_count = 0; |
| 156 | if (led_count > 0) { | 154 | |
| 157 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { | 155 | # if defined(LED_MATRIX_KEYRELEASES) |
| 158 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | 156 | if (!pressed) |
| 159 | } | 157 | # elif defined(LED_MATRIX_KEYPRESSES) |
| 160 | g_last_led_hit[0] = led[0]; | 158 | if (pressed) |
| 161 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | 159 | # endif // defined(LED_MATRIX_KEYRELEASES) |
| 162 | } | 160 | { |
| 163 | } else { | 161 | led_count = led_matrix_map_row_column_to_led(row, col, led); |
| 164 | #ifdef LED_MATRIX_KEYRELEASES | 162 | } |
| 165 | uint8_t led[8]; | 163 | |
| 166 | uint8_t led_count = led_matrix_map_row_column_to_led(row, .col, led); | 164 | if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) { |
| 167 | #endif | 165 | memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count); |
| 166 | memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
| 167 | memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit | ||
| 168 | memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
| 169 | last_hit_buffer.count--; | ||
| 170 | } | ||
| 171 | |||
| 172 | for (uint8_t i = 0; i < led_count; i++) { | ||
| 173 | uint8_t index = last_hit_buffer.count; | ||
| 174 | last_hit_buffer.x[index] = g_led_config.point[led[i]].x; | ||
| 175 | last_hit_buffer.y[index] = g_led_config.point[led[i]].y; | ||
| 176 | last_hit_buffer.index[index] = led[i]; | ||
| 177 | last_hit_buffer.tick[index] = 0; | ||
| 178 | last_hit_buffer.count++; | ||
| 179 | } | ||
| 180 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 181 | |||
| 182 | #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP) | ||
| 183 | if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) { | ||
| 184 | process_led_matrix_typing_heatmap(row, col); | ||
| 168 | } | 185 | } |
| 186 | #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP) | ||
| 169 | } | 187 | } |
| 170 | 188 | ||
| 171 | static bool led_matrix_none(effect_params_t *params) { | 189 | static bool led_matrix_none(effect_params_t *params) { |
| @@ -189,9 +207,9 @@ static bool led_matrix_uniform_brightness(effect_params_t *params) { | |||
| 189 | } | 207 | } |
| 190 | 208 | ||
| 191 | static void led_task_timers(void) { | 209 | static void led_task_timers(void) { |
| 192 | #if LED_DISABLE_TIMEOUT > 0 | 210 | #if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0 |
| 193 | uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer); | 211 | uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer); |
| 194 | #endif // LED_DISABLE_TIMEOUT > 0 | 212 | #endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0 |
| 195 | led_timer_buffer = sync_timer_read32(); | 213 | led_timer_buffer = sync_timer_read32(); |
| 196 | 214 | ||
| 197 | // Update double buffer timers | 215 | // Update double buffer timers |
| @@ -204,6 +222,18 @@ static void led_task_timers(void) { | |||
| 204 | } | 222 | } |
| 205 | } | 223 | } |
| 206 | #endif // LED_DISABLE_TIMEOUT > 0 | 224 | #endif // LED_DISABLE_TIMEOUT > 0 |
| 225 | |||
| 226 | // Update double buffer last hit timers | ||
| 227 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 228 | uint8_t count = last_hit_buffer.count; | ||
| 229 | for (uint8_t i = 0; i < count; ++i) { | ||
| 230 | if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) { | ||
| 231 | last_hit_buffer.count--; | ||
| 232 | continue; | ||
| 233 | } | ||
| 234 | last_hit_buffer.tick[i] += deltaTime; | ||
| 235 | } | ||
| 236 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 207 | } | 237 | } |
| 208 | 238 | ||
| 209 | static void led_task_sync(void) { | 239 | static void led_task_sync(void) { |
| @@ -217,6 +247,9 @@ static void led_task_start(void) { | |||
| 217 | 247 | ||
| 218 | // update double buffers | 248 | // update double buffers |
| 219 | g_led_timer = led_timer_buffer; | 249 | g_led_timer = led_timer_buffer; |
| 250 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 251 | g_last_hit_tracker = last_hit_buffer; | ||
| 252 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 220 | 253 | ||
| 221 | // next task | 254 | // next task |
| 222 | led_task_state = RENDERING; | 255 | led_task_state = RENDERING; |
| @@ -235,6 +268,7 @@ static void led_task_render(uint8_t effect) { | |||
| 235 | switch (effect) { | 268 | switch (effect) { |
| 236 | case LED_MATRIX_NONE: | 269 | case LED_MATRIX_NONE: |
| 237 | rendering = led_matrix_none(&led_effect_params); | 270 | rendering = led_matrix_none(&led_effect_params); |
| 271 | break; | ||
| 238 | case LED_MATRIX_UNIFORM_BRIGHTNESS: | 272 | case LED_MATRIX_UNIFORM_BRIGHTNESS: |
| 239 | rendering = led_matrix_uniform_brightness(&led_effect_params); | 273 | rendering = led_matrix_uniform_brightness(&led_effect_params); |
| 240 | break; | 274 | break; |
| @@ -288,6 +322,7 @@ void led_matrix_task(void) { | |||
| 288 | led_task_render(effect); | 322 | led_task_render(effect); |
| 289 | if (effect) { | 323 | if (effect) { |
| 290 | led_matrix_indicators(); | 324 | led_matrix_indicators(); |
| 325 | led_matrix_indicators_advanced(&led_effect_params); | ||
| 291 | } | 326 | } |
| 292 | break; | 327 | break; |
| 293 | case FLUSHING: | 328 | case FLUSHING: |
| @@ -308,9 +343,43 @@ __attribute__((weak)) void led_matrix_indicators_kb(void) {} | |||
| 308 | 343 | ||
| 309 | __attribute__((weak)) void led_matrix_indicators_user(void) {} | 344 | __attribute__((weak)) void led_matrix_indicators_user(void) {} |
| 310 | 345 | ||
| 346 | void led_matrix_indicators_advanced(effect_params_t *params) { | ||
| 347 | /* special handling is needed for "params->iter", since it's already been incremented. | ||
| 348 | * Could move the invocations to led_task_render, but then it's missing a few checks | ||
| 349 | * and not sure which would be better. Otherwise, this should be called from | ||
| 350 | * led_task_render, right before the iter++ line. | ||
| 351 | */ | ||
| 352 | #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL | ||
| 353 | uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1); | ||
| 354 | uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; | ||
| 355 | if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL; | ||
| 356 | #else | ||
| 357 | uint8_t min = 0; | ||
| 358 | uint8_t max = DRIVER_LED_TOTAL; | ||
| 359 | #endif | ||
| 360 | led_matrix_indicators_advanced_kb(min, max); | ||
| 361 | led_matrix_indicators_advanced_user(min, max); | ||
| 362 | } | ||
| 363 | |||
| 364 | __attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {} | ||
| 365 | |||
| 366 | __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {} | ||
| 367 | |||
| 311 | void led_matrix_init(void) { | 368 | void led_matrix_init(void) { |
| 312 | led_matrix_driver.init(); | 369 | led_matrix_driver.init(); |
| 313 | 370 | ||
| 371 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 372 | g_last_hit_tracker.count = 0; | ||
| 373 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { | ||
| 374 | g_last_hit_tracker.tick[i] = UINT16_MAX; | ||
| 375 | } | ||
| 376 | |||
| 377 | last_hit_buffer.count = 0; | ||
| 378 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { | ||
| 379 | last_hit_buffer.tick[i] = UINT16_MAX; | ||
| 380 | } | ||
| 381 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 382 | |||
| 314 | if (!eeconfig_is_enabled()) { | 383 | if (!eeconfig_is_enabled()) { |
| 315 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); | 384 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); |
| 316 | eeconfig_init(); | 385 | eeconfig_init(); |
diff --git a/quantum/led_matrix.h b/quantum/led_matrix.h index 7fb1c953a..f35bbe209 100644 --- a/quantum/led_matrix.h +++ b/quantum/led_matrix.h | |||
| @@ -79,6 +79,10 @@ void led_matrix_indicators(void); | |||
| 79 | void led_matrix_indicators_kb(void); | 79 | void led_matrix_indicators_kb(void); |
| 80 | void led_matrix_indicators_user(void); | 80 | void led_matrix_indicators_user(void); |
| 81 | 81 | ||
| 82 | void led_matrix_indicators_advanced(effect_params_t *params); | ||
| 83 | void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max); | ||
| 84 | void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max); | ||
| 85 | |||
| 82 | void led_matrix_init(void); | 86 | void led_matrix_init(void); |
| 83 | 87 | ||
| 84 | void led_matrix_set_suspend_state(bool state); | 88 | void led_matrix_set_suspend_state(bool state); |
| @@ -133,3 +137,9 @@ extern led_eeconfig_t led_matrix_eeconfig; | |||
| 133 | extern bool g_suspend_state; | 137 | extern bool g_suspend_state; |
| 134 | extern uint32_t g_led_timer; | 138 | extern uint32_t g_led_timer; |
| 135 | extern led_config_t g_led_config; | 139 | extern led_config_t g_led_config; |
| 140 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 141 | extern last_hit_t g_last_hit_tracker; | ||
| 142 | #endif | ||
| 143 | #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS | ||
| 144 | extern uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS]; | ||
| 145 | #endif | ||
