typing_heatmap_anim.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  2. RGB_MATRIX_EFFECT(TYPING_HEATMAP)
  3. # ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  4. # ifndef RGB_MATRIX_TYPING_HEATMAP_DECREASE_DELAY_MS
  5. # define RGB_MATRIX_TYPING_HEATMAP_DECREASE_DELAY_MS 25
  6. # endif
  7. # ifndef RGB_MATRIX_TYPING_HEATMAP_SPREAD
  8. # define RGB_MATRIX_TYPING_HEATMAP_SPREAD 40
  9. # endif
  10. # ifndef RGB_MATRIX_TYPING_HEATMAP_AREA_LIMIT
  11. # define RGB_MATRIX_TYPING_HEATMAP_AREA_LIMIT 16
  12. # endif
  13. void process_rgb_matrix_typing_heatmap(uint8_t row, uint8_t col) {
  14. # ifdef RGB_MATRIX_TYPING_HEATMAP_SLIM
  15. // Limit effect to pressed keys
  16. g_rgb_frame_buffer[row][col] = qadd8(g_rgb_frame_buffer[row][col], 32);
  17. # else
  18. for (uint8_t i_row = 0; i_row < MATRIX_ROWS; i_row++) {
  19. for (uint8_t i_col = 0; i_col < MATRIX_COLS; i_col++) {
  20. if (i_row == row && i_col == col) {
  21. g_rgb_frame_buffer[row][col] = qadd8(g_rgb_frame_buffer[row][col], 32);
  22. } else {
  23. # define LED_DISTANCE(led_a, led_b) sqrt16(((int8_t)(led_a.x - led_b.x) * (int8_t)(led_a.x - led_b.x)) + ((int8_t)(led_a.y - led_b.y) * (int8_t)(led_a.y - led_b.y)))
  24. 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]]);
  25. # undef LED_DISTANCE
  26. if (distance <= RGB_MATRIX_TYPING_HEATMAP_SPREAD) {
  27. uint8_t amount = qsub8(RGB_MATRIX_TYPING_HEATMAP_SPREAD, distance);
  28. if (amount > RGB_MATRIX_TYPING_HEATMAP_AREA_LIMIT) {
  29. amount = RGB_MATRIX_TYPING_HEATMAP_AREA_LIMIT;
  30. }
  31. g_rgb_frame_buffer[i_row][i_col] = qadd8(g_rgb_frame_buffer[i_row][i_col], amount);
  32. }
  33. }
  34. }
  35. }
  36. # endif
  37. }
  38. // A timer to track the last time we decremented all heatmap values.
  39. static uint16_t heatmap_decrease_timer;
  40. // Whether we should decrement the heatmap values during the next update.
  41. static bool decrease_heatmap_values;
  42. bool TYPING_HEATMAP(effect_params_t* params) {
  43. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  44. if (params->init) {
  45. rgb_matrix_set_color_all(0, 0, 0);
  46. memset(g_rgb_frame_buffer, 0, sizeof g_rgb_frame_buffer);
  47. }
  48. // The heatmap animation might run in several iterations depending on
  49. // `RGB_MATRIX_LED_PROCESS_LIMIT`, therefore we only want to update the
  50. // timer when the animation starts.
  51. if (params->iter == 0) {
  52. decrease_heatmap_values = timer_elapsed(heatmap_decrease_timer) >= RGB_MATRIX_TYPING_HEATMAP_DECREASE_DELAY_MS;
  53. // Restart the timer if we are going to decrease the heatmap this frame.
  54. if (decrease_heatmap_values) {
  55. heatmap_decrease_timer = timer_read();
  56. }
  57. }
  58. // Render heatmap & decrease
  59. uint8_t count = 0;
  60. for (uint8_t row = 0; row < MATRIX_ROWS && count < RGB_MATRIX_LED_PROCESS_LIMIT; row++) {
  61. for (uint8_t col = 0; col < MATRIX_COLS && RGB_MATRIX_LED_PROCESS_LIMIT; col++) {
  62. if (g_led_config.matrix_co[row][col] >= led_min && g_led_config.matrix_co[row][col] < led_max) {
  63. count++;
  64. uint8_t val = g_rgb_frame_buffer[row][col];
  65. if (!HAS_ANY_FLAGS(g_led_config.flags[g_led_config.matrix_co[row][col]], params->flags)) continue;
  66. HSV hsv = {170 - qsub8(val, 85), rgb_matrix_config.hsv.s, scale8((qadd8(170, val) - 170) * 3, rgb_matrix_config.hsv.v)};
  67. RGB rgb = rgb_matrix_hsv_to_rgb(hsv);
  68. rgb_matrix_set_color(g_led_config.matrix_co[row][col], rgb.r, rgb.g, rgb.b);
  69. if (decrease_heatmap_values) {
  70. g_rgb_frame_buffer[row][col] = qsub8(val, 1);
  71. }
  72. }
  73. }
  74. }
  75. return rgb_matrix_check_finished_leds(led_max);
  76. }
  77. # endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  78. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)