digital_rain_anim.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_DIGITAL_RAIN)
  2. RGB_MATRIX_EFFECT(DIGITAL_RAIN)
  3. # ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  4. # ifndef RGB_DIGITAL_RAIN_DROPS
  5. // lower the number for denser effect/wider keyboard
  6. # define RGB_DIGITAL_RAIN_DROPS 24
  7. # endif
  8. bool DIGITAL_RAIN(effect_params_t* params) {
  9. // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
  10. const uint8_t drop_ticks = 28;
  11. const uint8_t pure_green_intensity = (((uint16_t)rgb_matrix_config.hsv.v) * 3) >> 2;
  12. const uint8_t max_brightness_boost = (((uint16_t)rgb_matrix_config.hsv.v) * 3) >> 2;
  13. const uint8_t max_intensity = rgb_matrix_config.hsv.v;
  14. const uint8_t decay_ticks = 0xff / max_intensity;
  15. static uint8_t drop = 0;
  16. static uint8_t decay = 0;
  17. if (params->init) {
  18. rgb_matrix_set_color_all(0, 0, 0);
  19. memset(g_rgb_frame_buffer, 0, sizeof(g_rgb_frame_buffer));
  20. drop = 0;
  21. }
  22. decay++;
  23. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  24. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  25. if (row == 0 && drop == 0 && rand() < RAND_MAX / RGB_DIGITAL_RAIN_DROPS) {
  26. // top row, pixels have just fallen and we're
  27. // making a new rain drop in this column
  28. g_rgb_frame_buffer[row][col] = max_intensity;
  29. } else if (g_rgb_frame_buffer[row][col] > 0 && g_rgb_frame_buffer[row][col] < max_intensity) {
  30. // neither fully bright nor dark, decay it
  31. if (decay == decay_ticks) {
  32. g_rgb_frame_buffer[row][col]--;
  33. }
  34. }
  35. // set the pixel colour
  36. uint8_t led[LED_HITS_TO_REMEMBER];
  37. uint8_t led_count = rgb_matrix_map_row_column_to_led(row, col, led);
  38. // TODO: multiple leds are supported mapped to the same row/column
  39. if (led_count > 0) {
  40. if (g_rgb_frame_buffer[row][col] > pure_green_intensity) {
  41. const uint8_t boost = (uint8_t)((uint16_t)max_brightness_boost * (g_rgb_frame_buffer[row][col] - pure_green_intensity) / (max_intensity - pure_green_intensity));
  42. rgb_matrix_set_color(led[0], boost, max_intensity, boost);
  43. } else {
  44. const uint8_t green = (uint8_t)((uint16_t)max_intensity * g_rgb_frame_buffer[row][col] / pure_green_intensity);
  45. rgb_matrix_set_color(led[0], 0, green, 0);
  46. }
  47. }
  48. }
  49. }
  50. if (decay == decay_ticks) {
  51. decay = 0;
  52. }
  53. if (++drop > drop_ticks) {
  54. // reset drop timer
  55. drop = 0;
  56. for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) {
  57. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  58. // if ths is on the bottom row and bright allow decay
  59. if (row == MATRIX_ROWS - 1 && g_rgb_frame_buffer[row][col] == max_intensity) {
  60. g_rgb_frame_buffer[row][col]--;
  61. }
  62. // check if the pixel above is bright
  63. if (g_rgb_frame_buffer[row - 1][col] >= max_intensity) { // Note: can be larger than max_intensity if val was recently decreased
  64. // allow old bright pixel to decay
  65. g_rgb_frame_buffer[row - 1][col] = max_intensity - 1;
  66. // make this pixel bright
  67. g_rgb_frame_buffer[row][col] = max_intensity;
  68. }
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. # endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  75. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(ENABLE_RGB_MATRIX_DIGITAL_RAIN)