From 4d5705ea6ceeb11cd2adc018644ec87c51af4e86 Mon Sep 17 00:00:00 2001 From: Daniel Shields Date: Mon, 3 Sep 2018 08:26:53 +0100 Subject: [PATCH] Add new RGB matrix effect digital rain aka The Matrix --- quantum/rgb_matrix.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ quantum/rgb_matrix.h | 1 + 2 files changed, 67 insertions(+) diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c index 197bc1ac5..8ad00c37d 100644 --- a/quantum/rgb_matrix.c +++ b/quantum/rgb_matrix.c @@ -510,6 +510,69 @@ void rgb_matrix_jellybean_raindrops( bool initialize ) { } } +void rgb_matrix_digital_rain( const bool initialize ) { + // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain + const uint8_t drop_ticks = 28; + const uint8_t new_drop_probability = 24; + const uint8_t pure_green_intensity = 0xd0; + const uint8_t max_brightness_boost = 0xc0; + const uint8_t max_intensity = 0xff; + + static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}}; + static uint8_t drop = 0; + + if (initialize) { + rgb_matrix_set_color_all(0, 0, 0); + memset(map, 0, sizeof map); + drop = 0; + } + 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 / new_drop_probability) { + // top row, pixels have just fallen and we're + // making a new rain drop in this column + map[col][row] = max_intensity; + } + else if (map[col][row] > 0 && map[col][row] < max_intensity) { + // neither fully bright nor dark, decay it + map[col][row]--; + } + // set the pixel colour + uint8_t led, led_count; + map_row_column_to_led(row, col, &led, &led_count); + + if (map[col][row] > pure_green_intensity) { + const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost + * (map[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity)); + rgb_matrix_set_color(led, boost, max_intensity, boost); + } + else { + const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity); + rgb_matrix_set_color(led, 0, green, 0); + } + } + } + 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 + if (row == MATRIX_ROWS - 1 && map[col][row] == max_intensity) { + map[col][row]--; + } + // check if the pixel above is bright + if (map[col][row - 1] == max_intensity) { + // allow old bright pixel to decay + map[col][row - 1]--; + // make this pixel bright + map[col][row] = max_intensity; + } + } + } + } +} + void rgb_matrix_multisplash(void) { // if (g_any_key_hit < 0xFF) { HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; @@ -685,6 +748,9 @@ void rgb_matrix_task(void) { case RGB_MATRIX_JELLYBEAN_RAINDROPS: rgb_matrix_jellybean_raindrops( initialize ); break; + case RGB_MATRIX_DIGITAL_RAIN: + rgb_matrix_digital_rain( initialize ); + break; #ifdef RGB_MATRIX_KEYPRESSES case RGB_MATRIX_SOLID_REACTIVE: rgb_matrix_solid_reactive(); diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h index b91c9fba5..046d98790 100644 --- a/quantum/rgb_matrix.h +++ b/quantum/rgb_matrix.h @@ -81,6 +81,7 @@ enum rgb_matrix_effects { RGB_MATRIX_RAINBOW_PINWHEELS, RGB_MATRIX_RAINBOW_MOVING_CHEVRON, RGB_MATRIX_JELLYBEAN_RAINDROPS, + RGB_MATRIX_DIGITAL_RAIN, #ifdef RGB_MATRIX_KEYPRESSES RGB_MATRIX_SOLID_REACTIVE, RGB_MATRIX_SPLASH,