typing_heatmap_anim.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_LED_MATRIX_TYPING_HEATMAP)
  2. LED_MATRIX_EFFECT(TYPING_HEATMAP)
  3. # ifdef LED_MATRIX_CUSTOM_EFFECT_IMPLS
  4. # ifndef LED_MATRIX_TYPING_HEATMAP_INCREASE_STEP
  5. # define LED_MATRIX_TYPING_HEATMAP_INCREASE_STEP 32
  6. # endif
  7. # ifndef LED_MATRIX_TYPING_HEATMAP_DECREASE_DELAY_MS
  8. # define LED_MATRIX_TYPING_HEATMAP_DECREASE_DELAY_MS 25
  9. # endif
  10. # ifndef LED_MATRIX_TYPING_HEATMAP_SPREAD
  11. # define LED_MATRIX_TYPING_HEATMAP_SPREAD 40
  12. # endif
  13. # ifndef LED_MATRIX_TYPING_HEATMAP_AREA_LIMIT
  14. # define LED_MATRIX_TYPING_HEATMAP_AREA_LIMIT 16
  15. # endif
  16. void process_led_matrix_typing_heatmap(uint8_t row, uint8_t col) {
  17. # ifdef LED_MATRIX_TYPING_HEATMAP_SLIM
  18. // Limit effect to pressed keys
  19. g_led_frame_buffer[row][col] = qadd8(g_led_frame_buffer[row][col], LED_MATRIX_TYPING_HEATMAP_INCREASE_STEP);
  20. # else
  21. if (g_led_config.matrix_co[row][col] == NO_LED) { // skip as pressed key doesn't have an led position
  22. return;
  23. }
  24. for (uint8_t i_row = 0; i_row < MATRIX_ROWS; i_row++) {
  25. for (uint8_t i_col = 0; i_col < MATRIX_COLS; i_col++) {
  26. if (g_led_config.matrix_co[i_row][i_col] == NO_LED) { // skip as target key doesn't have an led position
  27. continue;
  28. }
  29. if (i_row == row && i_col == col) {
  30. g_led_frame_buffer[row][col] = qadd8(g_led_frame_buffer[row][col], LED_MATRIX_TYPING_HEATMAP_INCREASE_STEP);
  31. } else {
  32. # define LED_DISTANCE(led_a, led_b) sqrt16(((int32_t)(led_a.x - led_b.x) * (int32_t)(led_a.x - led_b.x)) + ((int32_t)(led_a.y - led_b.y) * (int32_t)(led_a.y - led_b.y)))
  33. uint8_t distance = LED_DISTANCE(g_led_config.point[g_led_config.matrix_co[row][col]], g_led_config.point[g_led_config.matrix_co[i_row][i_col]]);
  34. # undef LED_DISTANCE
  35. if (distance <= LED_MATRIX_TYPING_HEATMAP_SPREAD) {
  36. uint8_t amount = qsub8(LED_MATRIX_TYPING_HEATMAP_SPREAD, distance);
  37. if (amount > LED_MATRIX_TYPING_HEATMAP_AREA_LIMIT) {
  38. amount = LED_MATRIX_TYPING_HEATMAP_AREA_LIMIT;
  39. }
  40. g_led_frame_buffer[i_row][i_col] = qadd8(g_led_frame_buffer[i_row][i_col], amount);
  41. }
  42. }
  43. }
  44. }
  45. # endif
  46. }
  47. // A timer to track the last time we decremented all heatmap values.
  48. static uint16_t heatmap_decrease_timer;
  49. // Whether we should decrement the heatmap values during the next update.
  50. static bool decrease_heatmap_values;
  51. bool TYPING_HEATMAP(effect_params_t* params) {
  52. LED_MATRIX_USE_LIMITS(led_min, led_max);
  53. if (params->init) {
  54. led_matrix_set_value_all(0);
  55. memset(g_led_frame_buffer, 0, sizeof g_led_frame_buffer);
  56. }
  57. // The heatmap animation might run in several iterations depending on
  58. // `LED_MATRIX_LED_PROCESS_LIMIT`, therefore we only want to update the
  59. // timer when the animation starts.
  60. if (params->iter == 0) {
  61. decrease_heatmap_values = timer_elapsed(heatmap_decrease_timer) >= LED_MATRIX_TYPING_HEATMAP_DECREASE_DELAY_MS;
  62. // Restart the timer if we are going to decrease the heatmap this frame.
  63. if (decrease_heatmap_values) {
  64. heatmap_decrease_timer = timer_read();
  65. }
  66. }
  67. // Render heatmap & decrease
  68. uint8_t count = 0;
  69. for (uint8_t row = 0; row < MATRIX_ROWS && count < LED_MATRIX_LED_PROCESS_LIMIT; row++) {
  70. for (uint8_t col = 0; col < MATRIX_COLS && LED_MATRIX_LED_PROCESS_LIMIT; col++) {
  71. uint8_t val = g_led_frame_buffer[row][col];
  72. bool processed = false;
  73. uint8_t led[LED_HITS_TO_REMEMBER];
  74. uint8_t led_count = led_matrix_map_row_column_to_led(row, col, led);
  75. for (uint8_t index = 0; index < led_count; index++) {
  76. uint8_t led_index = led[index];
  77. if (led_index >= led_min && led_index < led_max) {
  78. count++;
  79. if (!HAS_ANY_FLAGS(g_led_config.flags[led_index], params->flags)) {
  80. continue;
  81. }
  82. uint8_t value = scale8(led_matrix_eeconfig.val, val);
  83. led_matrix_set_value(led_index, value);
  84. processed = true;
  85. }
  86. }
  87. if (processed && decrease_heatmap_values) {
  88. g_led_frame_buffer[row][col] = qsub8(val, 1);
  89. }
  90. }
  91. }
  92. return led_matrix_check_finished_leds(led_max);
  93. }
  94. # endif // LED_MATRIX_CUSTOM_EFFECT_IMPLS
  95. #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_LED_MATRIX_TYPING_HEATMAP)