qmk-keychron-q3-colemak-dh/quantum/rgb_matrix/animations/digital_rain_anim.h

76 lines
3.2 KiB
C
Raw Normal View History

#if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_DIGITAL_RAIN)
RGB_MATRIX_EFFECT(DIGITAL_RAIN)
2019-08-30 20:19:03 +02:00
# ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
2019-08-30 20:19:03 +02:00
# ifndef RGB_DIGITAL_RAIN_DROPS
// lower the number for denser effect/wider keyboard
# define RGB_DIGITAL_RAIN_DROPS 24
# endif
bool DIGITAL_RAIN(effect_params_t* params) {
2019-08-30 20:19:03 +02:00
// algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
const uint8_t drop_ticks = 28;
const uint8_t pure_green_intensity = 0xd0;
const uint8_t max_brightness_boost = 0xc0;
const uint8_t max_intensity = 0xff;
2019-08-30 20:19:03 +02:00
static uint8_t drop = 0;
2019-08-30 20:19:03 +02:00
if (params->init) {
rgb_matrix_set_color_all(0, 0, 0);
2020-05-30 22:14:59 +02:00
memset(g_rgb_frame_buffer, 0, sizeof(g_rgb_frame_buffer));
2019-08-30 20:19:03 +02:00
drop = 0;
}
2019-08-30 20:19:03 +02:00
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
if (row == 0 && drop == 0 && rand() < RAND_MAX / RGB_DIGITAL_RAIN_DROPS) {
// top row, pixels have just fallen and we're
// making a new rain drop in this column
2020-05-30 22:14:59 +02:00
g_rgb_frame_buffer[row][col] = max_intensity;
} else if (g_rgb_frame_buffer[row][col] > 0 && g_rgb_frame_buffer[row][col] < max_intensity) {
2019-08-30 20:19:03 +02:00
// neither fully bright nor dark, decay it
2020-05-30 22:14:59 +02:00
g_rgb_frame_buffer[row][col]--;
2019-08-30 20:19:03 +02:00
}
// set the pixel colour
uint8_t led[LED_HITS_TO_REMEMBER];
uint8_t led_count = rgb_matrix_map_row_column_to_led(row, col, led);
2019-08-30 20:19:03 +02:00
// TODO: multiple leds are supported mapped to the same row/column
if (led_count > 0) {
2020-05-30 22:14:59 +02:00
if (g_rgb_frame_buffer[row][col] > pure_green_intensity) {
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));
2019-08-30 20:19:03 +02:00
rgb_matrix_set_color(led[0], boost, max_intensity, boost);
} else {
2020-05-30 22:14:59 +02:00
const uint8_t green = (uint8_t)((uint16_t)max_intensity * g_rgb_frame_buffer[row][col] / pure_green_intensity);
2019-08-30 20:19:03 +02:00
rgb_matrix_set_color(led[0], 0, green, 0);
}
}
}
}
2019-08-30 20:19:03 +02:00
if (++drop > drop_ticks) {
// reset drop timer
drop = 0;
for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) {
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
// if ths is on the bottom row and bright allow decay
2020-05-30 22:14:59 +02:00
if (row == MATRIX_ROWS - 1 && g_rgb_frame_buffer[row][col] == max_intensity) {
g_rgb_frame_buffer[row][col]--;
2019-08-30 20:19:03 +02:00
}
// check if the pixel above is bright
2020-05-30 22:14:59 +02:00
if (g_rgb_frame_buffer[row - 1][col] == max_intensity) {
2019-08-30 20:19:03 +02:00
// allow old bright pixel to decay
2020-05-30 22:14:59 +02:00
g_rgb_frame_buffer[row - 1][col]--;
2019-08-30 20:19:03 +02:00
// make this pixel bright
2020-05-30 22:14:59 +02:00
g_rgb_frame_buffer[row][col] = max_intensity;
2019-08-30 20:19:03 +02:00
}
}
}
}
2019-08-30 20:19:03 +02:00
return false;
}
2019-08-30 20:19:03 +02:00
# endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS
#endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(ENABLE_RGB_MATRIX_DIGITAL_RAIN)