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