]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgb_matrix_animations/digital_rain_anim.h
RGB Matrix Overhaul (#5372)
[qmk_firmware.git] / quantum / rgb_matrix_animations / digital_rain_anim.h
1 #pragma once
2 #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
3
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
9 bool rgb_matrix_digital_rain(effect_params_t* params) {
10   // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
11   const uint8_t drop_ticks           = 28;
12   const uint8_t pure_green_intensity = 0xd0;
13   const uint8_t max_brightness_boost = 0xc0;
14   const uint8_t max_intensity        = 0xff;
15
16   static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}};
17   static uint8_t drop = 0;
18
19   if (params->init) {
20     rgb_matrix_set_color_all(0, 0, 0);
21     memset(map, 0, sizeof map);
22     drop = 0;
23   }
24   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
25     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
26       if (row == 0 && drop == 0 && rand() < RAND_MAX / RGB_DIGITAL_RAIN_DROPS) {
27         // top row, pixels have just fallen and we're
28         // making a new rain drop in this column
29         map[col][row] = max_intensity;
30       }
31       else if (map[col][row] > 0 && map[col][row] < max_intensity) {
32         // neither fully bright nor dark, decay it
33         map[col][row]--;
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
39       // TODO: multiple leds are supported mapped to the same row/column
40       if (led_count > 0) {
41         if (map[col][row] > pure_green_intensity) {
42           const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost * (map[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity));
43           rgb_matrix_set_color(led[0], boost, max_intensity, boost);
44         }
45         else {
46           const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity);
47           rgb_matrix_set_color(led[0], 0, green, 0);
48         }
49       }
50     }
51   }
52   if (++drop > drop_ticks) {
53     // reset drop timer
54     drop = 0;
55     for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) {
56       for (uint8_t col = 0; col < MATRIX_COLS; col++) {
57         // if ths is on the bottom row and bright allow decay
58         if (row == MATRIX_ROWS - 1 && map[col][row] == max_intensity) {
59           map[col][row]--;
60         }
61         // check if the pixel above is bright
62         if (map[col][row - 1] == max_intensity) {
63           // allow old bright pixel to decay
64           map[col][row - 1]--;
65           // make this pixel bright
66           map[col][row] = max_intensity;
67         }
68       }
69     }
70   }
71   return false;
72 }
73
74 #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN