From 60eae7335b711e8d4be7d48ad2290141e04debe7 Mon Sep 17 00:00:00 2001 From: XScorpion2 Date: Tue, 7 May 2019 21:17:00 -0500 Subject: [PATCH] RGB Matrix Typing Heatmap (#5786) * RGB Matrix Typing Heatmap Effect * Fixing file name and effect init --- docs/feature_rgb_matrix.md | 4 + quantum/rgb_matrix.c | 21 ++++++ quantum/rgb_matrix.h | 5 ++ .../rgb_matrix_animations/digital_rain_anim.h | 33 ++++---- .../typing_heatmap_anim.h | 75 +++++++++++++++++++ 5 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 quantum/rgb_matrix_animations/typing_heatmap_anim.h diff --git a/docs/feature_rgb_matrix.md b/docs/feature_rgb_matrix.md index 5eb9d5536..1e4341467 100644 --- a/docs/feature_rgb_matrix.md +++ b/docs/feature_rgb_matrix.md @@ -201,7 +201,10 @@ enum rgb_matrix_effects { RGB_MATRIX_RAINBOW_PINWHEELS, // Full dual gradients spinning two halfs of keyboard RGB_MATRIX_RAINDROPS, // Randomly changes a single key's hue RGB_MATRIX_JELLYBEAN_RAINDROPS, // Randomly changes a single key's hue and saturation +#if define(RGB_MATRIX_FRAMEBUFFER_EFFECTS) + RGB_MATRIX_TYPING_HEATMAP, // How hot is your WPM! RGB_MATRIX_DIGITAL_RAIN, // That famous computer simulation +#endif #if defined(RGB_MATRIX_KEYPRESSES) || defined(RGB_MATRIX_KEYRELEASES) RGB_MATRIX_SOLID_REACTIVE_SIMPLE, // Pulses keys hit to hue & value then fades value out RGB_MATRIX_SOLID_REACTIVE, // Static single hue, pulses keys hit to shifted hue then fades to current hue @@ -237,6 +240,7 @@ You can disable a single effect by defining `DISABLE_[EFFECT_NAME]` in your `con |`#define DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS` |Disables `RGB_MATRIX_RAINBOW_PINWHEELS` | |`#define DISABLE_RGB_MATRIX_RAINDROPS` |Disables `RGB_MATRIX_RAINDROPS` | |`#define DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS` |Disables `RGB_MATRIX_JELLYBEAN_RAINDROPS` | +|`#define DISABLE_RGB_MATRIX_TYPING_HEATMAP` |Disables `RGB_MATRIX_TYPING_HEATMAP` | |`#define DISABLE_RGB_MATRIX_DIGITAL_RAIN` |Disables `RGB_MATRIX_DIGITAL_RAIN` | |`#define DISABLE_RGB_MATRIX_SOLID_REACTIVE` |Disables `RGB_MATRIX_SOLID_REACTIVE` | |`#define DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE` |Disables `RGB_MATRIX_SOLID_REACTIVE_SIMPLE` | diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c index edbcee9cd..92a94df80 100644 --- a/quantum/rgb_matrix.c +++ b/quantum/rgb_matrix.c @@ -38,6 +38,7 @@ #include "rgb_matrix_animations/rainbow_pinwheels_anim.h" #include "rgb_matrix_animations/rainbow_moving_chevron_anim.h" #include "rgb_matrix_animations/jellybean_raindrops_anim.h" +#include "rgb_matrix_animations/typing_heatmap_anim.h" #include "rgb_matrix_animations/digital_rain_anim.h" #include "rgb_matrix_animations/solid_reactive_simple_anim.h" #include "rgb_matrix_animations/solid_reactive_anim.h" @@ -111,6 +112,10 @@ rgb_config_t rgb_matrix_config; rgb_counters_t g_rgb_counters; static uint32_t rgb_counters_buffer; +#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS +uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}}; +#endif + #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED last_hit_t g_last_hit_tracker; static last_hit_t last_hit_buffer; @@ -206,6 +211,13 @@ bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { last_hit_buffer.count++; } #endif // RGB_MATRIX_KEYREACTIVE_ENABLED + +#if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP) + if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) { + process_rgb_matrix_typing_heatmap(record); + } +#endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP) + return true; } @@ -370,11 +382,20 @@ static void rgb_task_render(uint8_t effect) { rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms break; #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS + +#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS +#ifndef DISABLE_RGB_MATRIX_TYPING_HEATMAP + case RGB_MATRIX_TYPING_HEATMAP: + rendering = rgb_matrix_typing_heatmap(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_TYPING_HEATMAP #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN case RGB_MATRIX_DIGITAL_RAIN: rendering = rgb_matrix_digital_rain(&rgb_effect_params); // Max 9ms Avg 8ms | this is expensive, fix it break; #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN +#endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS + #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE case RGB_MATRIX_SOLID_REACTIVE_SIMPLE: diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h index 365a92bbf..add0715d9 100644 --- a/quantum/rgb_matrix.h +++ b/quantum/rgb_matrix.h @@ -101,9 +101,14 @@ enum rgb_matrix_effects { #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS RGB_MATRIX_JELLYBEAN_RAINDROPS, #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS +#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS +#ifndef DISABLE_RGB_MATRIX_TYPING_HEATMAP + RGB_MATRIX_TYPING_HEATMAP, +#endif // DISABLE_RGB_MATRIX_TYPING_HEATMAP #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN RGB_MATRIX_DIGITAL_RAIN, #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN +#endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE RGB_MATRIX_SOLID_REACTIVE_SIMPLE, diff --git a/quantum/rgb_matrix_animations/digital_rain_anim.h b/quantum/rgb_matrix_animations/digital_rain_anim.h index 4ba3c1c87..6ccba392a 100644 --- a/quantum/rgb_matrix_animations/digital_rain_anim.h +++ b/quantum/rgb_matrix_animations/digital_rain_anim.h @@ -1,11 +1,13 @@ #pragma once -#ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN +#if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_DIGITAL_RAIN) #ifndef RGB_DIGITAL_RAIN_DROPS // lower the number for denser effect/wider keyboard #define RGB_DIGITAL_RAIN_DROPS 24 #endif +extern uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS]; + bool rgb_matrix_digital_rain(effect_params_t* params) { // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain const uint8_t drop_ticks = 28; @@ -13,24 +15,24 @@ bool rgb_matrix_digital_rain(effect_params_t* params) { 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 (params->init) { rgb_matrix_set_color_all(0, 0, 0); - memset(map, 0, sizeof map); + memset(rgb_frame_buffer, 0, sizeof rgb_frame_buffer); 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 / RGB_DIGITAL_RAIN_DROPS) { // top row, pixels have just fallen and we're // making a new rain drop in this column - map[col][row] = max_intensity; + rgb_frame_buffer[col][row] = max_intensity; } - else if (map[col][row] > 0 && map[col][row] < max_intensity) { + else if (rgb_frame_buffer[col][row] > 0 && rgb_frame_buffer[col][row] < max_intensity) { // neither fully bright nor dark, decay it - map[col][row]--; + rgb_frame_buffer[col][row]--; } // set the pixel colour uint8_t led[LED_HITS_TO_REMEMBER]; @@ -38,32 +40,33 @@ bool rgb_matrix_digital_rain(effect_params_t* params) { // TODO: multiple leds are supported mapped to the same row/column if (led_count > 0) { - 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)); + if (rgb_frame_buffer[col][row] > pure_green_intensity) { + const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost * (rgb_frame_buffer[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity)); rgb_matrix_set_color(led[0], boost, max_intensity, boost); } else { - const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity); + const uint8_t green = (uint8_t) ((uint16_t) max_intensity * rgb_frame_buffer[col][row] / pure_green_intensity); rgb_matrix_set_color(led[0], 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]--; + if (row == MATRIX_ROWS - 1 && rgb_frame_buffer[col][row] == max_intensity) { + rgb_frame_buffer[col][row]--; } // check if the pixel above is bright - if (map[col][row - 1] == max_intensity) { + if (rgb_frame_buffer[col][row - 1] == max_intensity) { // allow old bright pixel to decay - map[col][row - 1]--; + rgb_frame_buffer[col][row - 1]--; // make this pixel bright - map[col][row] = max_intensity; + rgb_frame_buffer[col][row] = max_intensity; } } } @@ -71,4 +74,4 @@ bool rgb_matrix_digital_rain(effect_params_t* params) { return false; } -#endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN +#endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_DIGITAL_RAIN) diff --git a/quantum/rgb_matrix_animations/typing_heatmap_anim.h b/quantum/rgb_matrix_animations/typing_heatmap_anim.h new file mode 100644 index 000000000..aade53fcc --- /dev/null +++ b/quantum/rgb_matrix_animations/typing_heatmap_anim.h @@ -0,0 +1,75 @@ +#pragma once +#if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP) + +extern rgb_config_t rgb_matrix_config; +extern uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS]; + +void process_rgb_matrix_typing_heatmap(keyrecord_t *record) { + uint8_t row = record->event.key.row; + uint8_t col = record->event.key.col; + uint8_t m_row = row - 1; + uint8_t p_row = row + 1; + uint8_t m_col = col - 1; + uint8_t p_col = col + 1; + + if (m_col < col) + rgb_frame_buffer[row][m_col] = qadd8(rgb_frame_buffer[row][m_col], 16); + rgb_frame_buffer[row][col] = qadd8(rgb_frame_buffer[row][col], 32); + if (p_col < MATRIX_COLS) + rgb_frame_buffer[row][p_col] = qadd8(rgb_frame_buffer[row][p_col], 16); + + if (p_row < MATRIX_ROWS) { + if (m_col < col) + rgb_frame_buffer[p_row][m_col] = qadd8(rgb_frame_buffer[p_row][m_col], 13); + rgb_frame_buffer[p_row][col] = qadd8(rgb_frame_buffer[p_row][col], 16); + if (p_col < MATRIX_COLS) + rgb_frame_buffer[p_row][p_col] = qadd8(rgb_frame_buffer[p_row][p_col], 13); + } + + if (m_row < row) { + if (m_col < col) + rgb_frame_buffer[m_row][m_col] = qadd8(rgb_frame_buffer[m_row][m_col], 13); + rgb_frame_buffer[m_row][col] = qadd8(rgb_frame_buffer[m_row][col], 16); + if (p_col < MATRIX_COLS) + rgb_frame_buffer[m_row][p_col] = qadd8(rgb_frame_buffer[m_row][p_col], 13); + } +} + +bool rgb_matrix_typing_heatmap(effect_params_t* params) { + // Modified version of RGB_MATRIX_USE_LIMITS to work off of matrix row / col size + uint8_t led_min = RGB_MATRIX_LED_PROCESS_LIMIT * params->iter; + uint8_t led_max = led_min + RGB_MATRIX_LED_PROCESS_LIMIT; + if (led_max > sizeof(rgb_frame_buffer)) + led_max = sizeof(rgb_frame_buffer); + + if (params->init) { + rgb_matrix_set_color_all(0, 0, 0); + memset(rgb_frame_buffer, 0, sizeof rgb_frame_buffer); + } + + // Render heatmap & decrease + for (int i = led_min; i < led_max; i++) { + uint8_t row = i % MATRIX_ROWS; + uint8_t col = i / MATRIX_ROWS; + uint8_t val = rgb_frame_buffer[row][col]; + + // 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); + for (uint8_t j = 0; j < led_count; ++j) + { + if (!HAS_ANY_FLAGS(g_led_config.flags[led[j]], params->flags)) + continue; + + HSV hsv = { 170 - qsub8(val, 85), rgb_matrix_config.sat, scale8((qadd8(170, val) - 170) * 3, rgb_matrix_config.val) }; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(led[j], rgb.r, rgb.g, rgb.b); + } + + rgb_frame_buffer[row][col] = qsub8(val, 1); + } + + return led_max < sizeof(rgb_frame_buffer); +} + +#endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)