From c98247e3dd2958bd2d8969dc75170e7e2757b895 Mon Sep 17 00:00:00 2001 From: XScorpion2 Date: Tue, 2 Apr 2019 19:24:14 -0500 Subject: RGB Matrix Overhaul (#5372) * RGB Matrix overhaul Breakout of animations to separate files Integration of optimized int based math lib Overhaul of rgb_matrix.c and animations for performance * Updating effect function api for future extensions * Combined the keypresses || keyreleases define checks into a single define so I stop forgetting it where necessary * Moving define RGB_MATRIX_KEYREACTIVE_ENABLED earlier in the include chain --- quantum/color.c | 2 + quantum/quantum.c | 14 +- quantum/rgb_matrix.c | 1185 +++++++------------- quantum/rgb_matrix.h | 152 ++- quantum/rgb_matrix_animations/alpha_mods_anim.h | 26 + quantum/rgb_matrix_animations/breathing_anim.h | 19 + quantum/rgb_matrix_animations/cycle_all_anim.h | 21 + .../rgb_matrix_animations/cycle_left_right_anim.h | 22 + quantum/rgb_matrix_animations/cycle_up_down_anim.h | 22 + quantum/rgb_matrix_animations/digital_rain_anim.h | 74 ++ quantum/rgb_matrix_animations/dual_beacon_anim.h | 24 + .../rgb_matrix_animations/gradient_up_down_anim.h | 22 + .../jellybean_raindrops_anim.h | 30 + .../rgb_matrix_animations/rainbow_beacon_anim.h | 24 + .../rainbow_moving_chevron_anim.h | 22 + .../rgb_matrix_animations/rainbow_pinwheels_anim.h | 24 + quantum/rgb_matrix_animations/raindrops_anim.h | 40 + quantum/rgb_matrix_animations/solid_color_anim.h | 14 + .../rgb_matrix_animations/solid_reactive_anim.h | 33 + .../solid_reactive_simple_anim.h | 32 + quantum/rgb_matrix_animations/solid_splash_anim.h | 42 + quantum/rgb_matrix_animations/splash_anim.h | 44 + quantum/rgb_matrix_types.h | 90 ++ 23 files changed, 1091 insertions(+), 887 deletions(-) create mode 100644 quantum/rgb_matrix_animations/alpha_mods_anim.h create mode 100644 quantum/rgb_matrix_animations/breathing_anim.h create mode 100644 quantum/rgb_matrix_animations/cycle_all_anim.h create mode 100644 quantum/rgb_matrix_animations/cycle_left_right_anim.h create mode 100644 quantum/rgb_matrix_animations/cycle_up_down_anim.h create mode 100644 quantum/rgb_matrix_animations/digital_rain_anim.h create mode 100644 quantum/rgb_matrix_animations/dual_beacon_anim.h create mode 100644 quantum/rgb_matrix_animations/gradient_up_down_anim.h create mode 100644 quantum/rgb_matrix_animations/jellybean_raindrops_anim.h create mode 100644 quantum/rgb_matrix_animations/rainbow_beacon_anim.h create mode 100644 quantum/rgb_matrix_animations/rainbow_moving_chevron_anim.h create mode 100644 quantum/rgb_matrix_animations/rainbow_pinwheels_anim.h create mode 100644 quantum/rgb_matrix_animations/raindrops_anim.h create mode 100644 quantum/rgb_matrix_animations/solid_color_anim.h create mode 100644 quantum/rgb_matrix_animations/solid_reactive_anim.h create mode 100644 quantum/rgb_matrix_animations/solid_reactive_simple_anim.h create mode 100644 quantum/rgb_matrix_animations/solid_splash_anim.h create mode 100644 quantum/rgb_matrix_animations/splash_anim.h create mode 100644 quantum/rgb_matrix_types.h (limited to 'quantum') diff --git a/quantum/color.c b/quantum/color.c index 8ede053e7..c49877592 100644 --- a/quantum/color.c +++ b/quantum/color.c @@ -78,9 +78,11 @@ RGB hsv_to_rgb( HSV hsv ) break; } +#ifdef USE_CIE1931_CURVE rgb.r = pgm_read_byte( &CIE1931_CURVE[rgb.r] ); rgb.g = pgm_read_byte( &CIE1931_CURVE[rgb.g] ); rgb.b = pgm_read_byte( &CIE1931_CURVE[rgb.b] ); +#endif return rgb; } diff --git a/quantum/quantum.c b/quantum/quantum.c index 8316d1f06..8c928441c 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -274,10 +274,10 @@ bool process_record_quantum(keyrecord_t *record) { #ifdef HAPTIC_ENABLE process_haptic(keycode, record) && #endif //HAPTIC_ENABLE - process_record_kb(keycode, record) && - #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYPRESSES) + #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYREACTIVE_ENABLED) process_rgb_matrix(keycode, record) && #endif + process_record_kb(keycode, record) && #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED) process_midi(keycode, record) && #endif @@ -1049,12 +1049,6 @@ void matrix_init_quantum() { matrix_init_kb(); } -uint8_t rgb_matrix_task_counter = 0; - -#ifndef RGB_MATRIX_SKIP_FRAMES - #define RGB_MATRIX_SKIP_FRAMES 1 -#endif - void matrix_scan_quantum() { #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE) matrix_scan_music(); @@ -1078,10 +1072,6 @@ void matrix_scan_quantum() { #ifdef RGB_MATRIX_ENABLE rgb_matrix_task(); - if (rgb_matrix_task_counter == 0) { - rgb_matrix_update_pwm_buffers(); - } - rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % (RGB_MATRIX_SKIP_FRAMES + 1)); #endif #ifdef ENCODER_ENABLE diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c index 56a97e3c7..0728e2431 100644 --- a/quantum/rgb_matrix.c +++ b/quantum/rgb_matrix.c @@ -24,62 +24,80 @@ #include #include -rgb_config_t rgb_matrix_config; +#include "lib/lib8tion/lib8tion.h" + +#include "rgb_matrix_animations/solid_color_anim.h" +#include "rgb_matrix_animations/alpha_mods_anim.h" +#include "rgb_matrix_animations/dual_beacon_anim.h" +#include "rgb_matrix_animations/gradient_up_down_anim.h" +#include "rgb_matrix_animations/raindrops_anim.h" +#include "rgb_matrix_animations/cycle_all_anim.h" +#include "rgb_matrix_animations/cycle_left_right_anim.h" +#include "rgb_matrix_animations/cycle_up_down_anim.h" +#include "rgb_matrix_animations/rainbow_beacon_anim.h" +#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/digital_rain_anim.h" +#include "rgb_matrix_animations/solid_reactive_simple_anim.h" +#include "rgb_matrix_animations/solid_reactive_anim.h" +#include "rgb_matrix_animations/splash_anim.h" +#include "rgb_matrix_animations/solid_splash_anim.h" +#include "rgb_matrix_animations/breathing_anim.h" -#ifndef MAX - #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#ifndef RGB_DISABLE_AFTER_TIMEOUT + #define RGB_DISABLE_AFTER_TIMEOUT 0 #endif -#ifndef MIN - #define MIN(a,b) ((a) < (b)? (a): (b)) +#ifndef RGB_DISABLE_WHEN_USB_SUSPENDED + #define RGB_DISABLE_WHEN_USB_SUSPENDED false #endif -#ifndef RGB_DISABLE_AFTER_TIMEOUT - #define RGB_DISABLE_AFTER_TIMEOUT 0 +#ifndef EECONFIG_RGB_MATRIX + #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT #endif -#ifndef RGB_DISABLE_WHEN_USB_SUSPENDED - #define RGB_DISABLE_WHEN_USB_SUSPENDED false +#if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX + #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS + #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX #endif -#ifndef EECONFIG_RGB_MATRIX - #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT +#if !defined(RGB_MATRIX_HUE_STEP) + #define RGB_MATRIX_HUE_STEP 8 #endif -#if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255 - #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255 +#if !defined(RGB_MATRIX_SAT_STEP) + #define RGB_MATRIX_SAT_STEP 16 #endif -#ifndef RGB_DIGITAL_RAIN_DROPS - // lower the number for denser effect/wider keyboard - #define RGB_DIGITAL_RAIN_DROPS 24 +#if !defined(RGB_MATRIX_VAL_STEP) + #define RGB_MATRIX_VAL_STEP 16 #endif -#if !defined(DISABLE_RGB_MATRIX_RAINDROPS) || !defined(DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS) || !defined(DISABLE_RGB_MATRIX_DIGITAL_RAIN) - #define TRACK_PREVIOUS_EFFECT +#if !defined(RGB_MATRIX_SPD_STEP) + #define RGB_MATRIX_SPD_STEP 16 #endif bool g_suspend_state = false; -// Global tick at 20 Hz -uint32_t g_tick = 0; - -// Ticks since this key was last hit. -uint8_t g_key_hit[DRIVER_LED_TOTAL]; +rgb_config_t rgb_matrix_config; -// Ticks since any key was last hit. -uint32_t g_any_key_hit = 0; +rgb_counters_t g_rgb_counters; +static uint32_t rgb_counters_buffer; -#ifndef PI -#define PI 3.14159265 -#endif +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED + last_hit_t g_last_hit_tracker; + static last_hit_t last_hit_buffer; +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED uint32_t eeconfig_read_rgb_matrix(void) { return eeprom_read_dword(EECONFIG_RGB_MATRIX); } + void eeconfig_update_rgb_matrix(uint32_t val) { eeprom_update_dword(EECONFIG_RGB_MATRIX, val); } + void eeconfig_update_rgb_matrix_default(void) { dprintf("eeconfig_update_rgb_matrix_default\n"); rgb_matrix_config.enable = 1; @@ -90,11 +108,12 @@ void eeconfig_update_rgb_matrix_default(void) { rgb_matrix_config.mode = RGB_MATRIX_SOLID_COLOR; #endif rgb_matrix_config.hue = 0; - rgb_matrix_config.sat = 255; + rgb_matrix_config.sat = UINT8_MAX; rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS; - rgb_matrix_config.speed = 0; + rgb_matrix_config.speed = UINT8_MAX / 2; eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } + void eeconfig_debug_rgb_matrix(void) { dprintf("rgb_matrix_config eprom\n"); dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable); @@ -105,710 +124,330 @@ void eeconfig_debug_rgb_matrix(void) { dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed); } -// Last led hit -#define LED_HITS_TO_REMEMBER 8 -uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; -uint8_t g_last_led_count = 0; - -void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { - rgb_led led; - *led_count = 0; - - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - // map_index_to_led(i, &led); - led = g_rgb_leds[i]; - if (row == led.matrix_co.row && column == led.matrix_co.col) { - led_i[*led_count] = i; - (*led_count)++; - } +uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { + // TODO: This is kinda expensive, fix this soonish + uint8_t led_count = 0; + for (uint8_t i = 0; i < DRIVER_LED_TOTAL && led_count < LED_HITS_TO_REMEMBER; i++) { + matrix_co_t matrix_co = g_rgb_leds[i].matrix_co; + if (row == matrix_co.row && column == matrix_co.col) { + led_i[led_count] = i; + led_count++; } + } + return led_count; } void rgb_matrix_update_pwm_buffers(void) { - rgb_matrix_driver.flush(); + rgb_matrix_driver.flush(); } void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) { - rgb_matrix_driver.set_color(index, red, green, blue); + rgb_matrix_driver.set_color(index, red, green, blue); } void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) { - rgb_matrix_driver.set_color_all(red, green, blue); + rgb_matrix_driver.set_color_all(red, green, blue); } bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { - if ( record->event.pressed ) { - uint8_t led[8], led_count; - map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); - if (led_count > 0) { - for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { - g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; - } - g_last_led_hit[0] = led[0]; - g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); - } - for(uint8_t i = 0; i < led_count; i++) - g_key_hit[led[i]] = 0; - g_any_key_hit = 0; - } else { - #ifdef RGB_MATRIX_KEYRELEASES - uint8_t led[8], led_count; - map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); - for(uint8_t i = 0; i < led_count; i++) - g_key_hit[led[i]] = 255; - - g_any_key_hit = 255; - #endif - } - return true; -} +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED + uint8_t led[LED_HITS_TO_REMEMBER]; + uint8_t led_count = 0; + +#if defined(RGB_MATRIX_KEYRELEASES) + if (!record->event.pressed) { + led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led); + g_rgb_counters.any_key_hit = 0; + } +#elif defined(RGB_MATRIX_KEYPRESSES) + if (record->event.pressed) { + led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led); + g_rgb_counters.any_key_hit = 0; + } +#endif // defined(RGB_MATRIX_KEYRELEASES) + + if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) { + memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count); + memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count); + memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit + memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count); + last_hit_buffer.count--; + } -void rgb_matrix_set_suspend_state(bool state) { - g_suspend_state = state; + for(uint8_t i = 0; i < led_count; i++) { + uint8_t index = last_hit_buffer.count; + last_hit_buffer.x[index] = g_rgb_leds[led[i]].point.x; + last_hit_buffer.y[index] = g_rgb_leds[led[i]].point.y; + last_hit_buffer.index[index] = led[i]; + last_hit_buffer.tick[index] = 0; + last_hit_buffer.count++; + } +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED + return true; } void rgb_matrix_test(void) { - // Mask out bits 4 and 5 - // Increase the factor to make the test animation slower (and reduce to make it faster) - uint8_t factor = 10; - switch ( (g_tick & (0b11 << factor)) >> factor ) - { - case 0: - { - rgb_matrix_set_color_all( 20, 0, 0 ); - break; - } - case 1: - { - rgb_matrix_set_color_all( 0, 20, 0 ); - break; - } - case 2: - { - rgb_matrix_set_color_all( 0, 0, 20 ); - break; - } - case 3: - { - rgb_matrix_set_color_all( 20, 20, 20 ); - break; - } + // Mask out bits 4 and 5 + // Increase the factor to make the test animation slower (and reduce to make it faster) + uint8_t factor = 10; + switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor ) + { + case 0: { + rgb_matrix_set_color_all( 20, 0, 0 ); + break; } -} - -// All LEDs off -void rgb_matrix_all_off(void) { - rgb_matrix_set_color_all( 0, 0, 0 ); -} - -// Solid color -void rgb_matrix_solid_color(void) { - HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; - RGB rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b ); -} - -void rgb_matrix_solid_reactive(void) { - // Relies on hue being 8-bit and wrapping - for ( int i=0; i 127 ) - { - deltaH -= 256; - } - else if ( deltaH < -127 ) - { - deltaH += 256; - } - // Divide delta by 4, this gives the delta per row - deltaH /= 4; - - int16_t s1 = rgb_matrix_config.sat; - int16_t s2 = rgb_matrix_config.hue; - int16_t deltaS = ( s2 - s1 ) / 4; - - HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; - RGB rgb; - Point point; - for ( int i=0; i>4); - // Relies on hue being 8-bit and wrapping - hsv.h = rgb_matrix_config.hue + ( deltaH * y ); - hsv.s = rgb_matrix_config.sat + ( deltaS * y ); - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); - } -} - -void rgb_matrix_raindrops(bool initialize) { - int16_t h1 = rgb_matrix_config.hue; - int16_t h2 = (rgb_matrix_config.hue + 180) % 360; - int16_t deltaH = h2 - h1; - deltaH /= 4; - - // Take the shortest path between hues - if ( deltaH > 127 ) - { - deltaH -= 256; + case 1: { + rgb_matrix_set_color_all( 0, 20, 0 ); + break; } - else if ( deltaH < -127 ) - { - deltaH += 256; + case 2: { + rgb_matrix_set_color_all( 0, 0, 20 ); + break; } - - int16_t s1 = rgb_matrix_config.sat; - int16_t s2 = rgb_matrix_config.sat; - int16_t deltaS = ( s2 - s1 ) / 4; - - HSV hsv; - RGB rgb; - - // Change one LED every tick, make sure speed is not 0 - uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255; - - for ( int i=0; iinit) { + return false; + } -void rgb_matrix_dual_beacon(void) { - HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; - RGB rgb; - Point point; - double cos_value = cos(g_tick * PI / 128) / 32; - double sin_value = sin(g_tick * PI / 128) / 112; - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - point = g_rgb_leds[i].point; - hsv.h = ((point.y - 32.0)* cos_value + (point.x - 112.0) * sin_value) * (180) + rgb_matrix_config.hue; - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); - } + RGB_MATRIX_USE_LIMITS(led_min, led_max); + for (uint8_t i = led_min; i < led_max; i++) { + rgb_matrix_set_color(i, 0, 0, 0); + } + return led_max < DRIVER_LED_TOTAL; } -void rgb_matrix_rainbow_beacon(void) { - HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; - RGB rgb; - Point point; - double cos_value = cos(g_tick * PI / 128); - double sin_value = sin(g_tick * PI / 128); - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - point = g_rgb_leds[i].point; - hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.y - 32.0)* cos_value + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.x - 112.0) * sin_value + rgb_matrix_config.hue; - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); - } -} +static uint8_t rgb_last_enable = UINT8_MAX; +static uint8_t rgb_last_effect = UINT8_MAX; +static effect_params_t rgb_effect_params = { 0, 0 }; +static rgb_task_states rgb_task_state = SYNCING; -void rgb_matrix_rainbow_pinwheels(void) { - HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; - RGB rgb; - Point point; - double cos_value = cos(g_tick * PI / 128); - double sin_value = sin(g_tick * PI / 128); - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - point = g_rgb_leds[i].point; - hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.y - 32.0)* cos_value + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(point.x - 112.0)) * sin_value + rgb_matrix_config.hue; - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); +static void rgb_task_timers(void) { + // Update double buffer timers + uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer); + rgb_counters_buffer = timer_read32(); + if (g_rgb_counters.any_key_hit < UINT32_MAX) { + if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) { + g_rgb_counters.any_key_hit = UINT32_MAX; + } else { + g_rgb_counters.any_key_hit += deltaTime; } -} + } -void rgb_matrix_rainbow_moving_chevron(void) { - HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; - RGB rgb; - Point point; - uint8_t r = 128; - double cos_value = cos(r * PI / 128); - double sin_value = sin(r * PI / 128); - double multiplier = (g_tick / 256.0 * 224); - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - point = g_rgb_leds[i].point; - hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(point.y - 32.0)* sin_value + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.x - multiplier) * cos_value + rgb_matrix_config.hue; - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); + // Update double buffer last hit timers +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED + uint8_t count = last_hit_buffer.count; + for (uint8_t i = 0; i < count; ++i) { + if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) { + last_hit_buffer.count--; + continue; } -} - - -void rgb_matrix_jellybean_raindrops( bool initialize ) { - HSV hsv; - RGB rgb; + last_hit_buffer.tick[i] += deltaTime; + } +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED +} + +static void rgb_task_sync(void) { + // next task + if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT) + rgb_task_state = STARTING; +} + +static void rgb_task_start(void) { + // reset iter + rgb_effect_params.iter = 0; + + // update double buffers + g_rgb_counters.tick = rgb_counters_buffer; +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED + g_last_hit_tracker = last_hit_buffer; +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED + + // next task + rgb_task_state = RENDERING; +} + +static void rgb_task_render(uint8_t effect) { + bool rendering = false; + rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable); + + // each effect can opt to do calculations + // and/or request PWM buffer updates. + switch (effect) { + case RGB_MATRIX_NONE: + rendering = rgb_matrix_none(&rgb_effect_params); + break; + + case RGB_MATRIX_SOLID_COLOR: + rendering = rgb_matrix_solid_color(&rgb_effect_params); // Max 1ms Avg 0ms + break; +#ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS + case RGB_MATRIX_ALPHAS_MODS: + rendering = rgb_matrix_alphas_mods(&rgb_effect_params); // Max 2ms Avg 1ms + break; +#endif // DISABLE_RGB_MATRIX_ALPHAS_MODS +#ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN + case RGB_MATRIX_GRADIENT_UP_DOWN: + rendering = rgb_matrix_gradient_up_down(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN +#ifndef DISABLE_RGB_MATRIX_BREATHING + case RGB_MATRIX_BREATHING: + rendering = rgb_matrix_breathing(&rgb_effect_params); // Max 1ms Avg 0ms + break; +#endif // DISABLE_RGB_MATRIX_BREATHING +#ifndef DISABLE_RGB_MATRIX_CYCLE_ALL + case RGB_MATRIX_CYCLE_ALL: + rendering = rgb_matrix_cycle_all(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_CYCLE_ALL +#ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT + case RGB_MATRIX_CYCLE_LEFT_RIGHT: + rendering = rgb_matrix_cycle_left_right(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT +#ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN + case RGB_MATRIX_CYCLE_UP_DOWN: + rendering = rgb_matrix_cycle_up_down(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN +#ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON + case RGB_MATRIX_RAINBOW_MOVING_CHEVRON: + rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON +#ifndef DISABLE_RGB_MATRIX_DUAL_BEACON + case RGB_MATRIX_DUAL_BEACON: + rendering = rgb_matrix_dual_beacon(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_DUAL_BEACON +#ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON + case RGB_MATRIX_RAINBOW_BEACON: + rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON +#ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS + case RGB_MATRIX_RAINBOW_PINWHEELS: + rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS +#ifndef DISABLE_RGB_MATRIX_RAINDROPS + case RGB_MATRIX_RAINDROPS: + rendering = rgb_matrix_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms + break; +#endif // DISABLE_RGB_MATRIX_RAINDROPS +#ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS + case RGB_MATRIX_JELLYBEAN_RAINDROPS: + rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms + break; +#endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS +#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 +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED +#ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE + case RGB_MATRIX_SOLID_REACTIVE_SIMPLE: + rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms + break; +#endif +#ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE + case RGB_MATRIX_SOLID_REACTIVE: + rendering = rgb_matrix_solid_reactive(&rgb_effect_params); // Max 4ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE +#ifndef DISABLE_RGB_MATRIX_SPLASH + case RGB_MATRIX_SPLASH: + rendering = rgb_matrix_splash(&rgb_effect_params); // Max 5ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_SPLASH +#ifndef DISABLE_RGB_MATRIX_MULTISPLASH + case RGB_MATRIX_MULTISPLASH: + rendering = rgb_matrix_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms + break; +#endif // DISABLE_RGB_MATRIX_MULTISPLASH +#ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH + case RGB_MATRIX_SOLID_SPLASH: + rendering = rgb_matrix_solid_splash(&rgb_effect_params); // Max 5ms Avg 3ms + break; +#endif // DISABLE_RGB_MATRIX_SOLID_SPLASH +#ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH + case RGB_MATRIX_SOLID_MULTISPLASH: + rendering = rgb_matrix_solid_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms + break; +#endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED - // Change one LED every tick, make sure speed is not 0 - uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255; + // Factory default magic value + case UINT8_MAX: { + rgb_matrix_test(); + rgb_task_state = FLUSHING; + } + return; + } - for ( int i=0; i 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; - } - } - } - } -} + // update pwm buffers + rgb_matrix_update_pwm_buffers(); -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 }; - RGB rgb; - rgb_led led; - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - led = g_rgb_leds[i]; - uint16_t c = 0, d = 0; - rgb_led last_led; - // if (g_last_led_count) { - for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) { - last_led = g_rgb_leds[g_last_led_hit[last_i]]; - uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2)); - uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist; - c += MIN(MAX(effect, 0), 255); - d += 255 - MIN(MAX(effect, 0), 255); - } - // } else { - // d = 255; - // } - hsv.h = (rgb_matrix_config.hue + c) % 256; - hsv.v = MAX(MIN(d, 255), 0); - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); - } - // } else { - // rgb_matrix_set_color_all( 0, 0, 0 ); - // } -} - - -void rgb_matrix_splash(void) { - g_last_led_count = MIN(g_last_led_count, 1); - rgb_matrix_multisplash(); -} - - -void rgb_matrix_solid_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 }; - RGB rgb; - rgb_led led; - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { - led = g_rgb_leds[i]; - uint16_t d = 0; - rgb_led last_led; - // if (g_last_led_count) { - for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) { - last_led = g_rgb_leds[g_last_led_hit[last_i]]; - uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2)); - uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist; - d += 255 - MIN(MAX(effect, 0), 255); - } - // } else { - // d = 255; - // } - hsv.v = MAX(MIN(d, 255), 0); - rgb = hsv_to_rgb( hsv ); - rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); - } - // } else { - // rgb_matrix_set_color_all( 0, 0, 0 ); - // } -} - - -void rgb_matrix_solid_splash(void) { - g_last_led_count = MIN(g_last_led_count, 1); - rgb_matrix_solid_multisplash(); -} - - -// Needs eeprom access that we don't have setup currently - -void rgb_matrix_custom(void) { -// HSV hsv; -// RGB rgb; -// for ( int i=0; i 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20)); - uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode; - - #ifdef TRACK_PREVIOUS_EFFECT - // Keep track of the effect used last time, - // detect change in effect, so each effect can - // have an optional initialization. - - static uint8_t effect_last = 255; - bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last); - effect_last = effect; - toggle_enable_last = rgb_matrix_config.enable; - #endif - - // this gets ticked at 20 Hz. - // each effect can opt to do calculations - // and/or request PWM buffer updates. - switch ( effect ) { - case RGB_MATRIX_SOLID_COLOR: - rgb_matrix_solid_color(); - break; - #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS - case RGB_MATRIX_ALPHAS_MODS: - rgb_matrix_alphas_mods(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON - case RGB_MATRIX_DUAL_BEACON: - rgb_matrix_dual_beacon(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN - case RGB_MATRIX_GRADIENT_UP_DOWN: - rgb_matrix_gradient_up_down(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_RAINDROPS - case RGB_MATRIX_RAINDROPS: - rgb_matrix_raindrops( initialize ); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL - case RGB_MATRIX_CYCLE_ALL: - rgb_matrix_cycle_all(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT - case RGB_MATRIX_CYCLE_LEFT_RIGHT: - rgb_matrix_cycle_left_right(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN - case RGB_MATRIX_CYCLE_UP_DOWN: - rgb_matrix_cycle_up_down(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON - case RGB_MATRIX_RAINBOW_BEACON: - rgb_matrix_rainbow_beacon(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS - case RGB_MATRIX_RAINBOW_PINWHEELS: - rgb_matrix_rainbow_pinwheels(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON - case RGB_MATRIX_RAINBOW_MOVING_CHEVRON: - rgb_matrix_rainbow_moving_chevron(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS - case RGB_MATRIX_JELLYBEAN_RAINDROPS: - rgb_matrix_jellybean_raindrops( initialize ); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN - case RGB_MATRIX_DIGITAL_RAIN: - rgb_matrix_digital_rain( initialize ); - break; - #endif - #ifdef RGB_MATRIX_KEYPRESSES - #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE - case RGB_MATRIX_SOLID_REACTIVE: - rgb_matrix_solid_reactive(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE - case RGB_MATRIX_SOLID_REACTIVE_SIMPLE: - rgb_matrix_solid_reactive_simple(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_SPLASH - case RGB_MATRIX_SPLASH: - rgb_matrix_splash(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_MULTISPLASH - case RGB_MATRIX_MULTISPLASH: - rgb_matrix_multisplash(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH - case RGB_MATRIX_SOLID_SPLASH: - rgb_matrix_solid_splash(); - break; - #endif - #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH - case RGB_MATRIX_SOLID_MULTISPLASH: - rgb_matrix_solid_multisplash(); - break; - #endif - #endif - default: - rgb_matrix_custom(); - break; - } - - if ( ! suspend_backlight ) { - rgb_matrix_indicators(); - } + rgb_task_timers(); + + // Ideally we would also stop sending zeros to the LED driver PWM buffers + // while suspended and just do a software shutdown. This is a cheap hack for now. + bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20)); + uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode; + + switch (rgb_task_state) { + case STARTING: + rgb_task_start(); + break; + case RENDERING: + rgb_task_render(effect); + break; + case FLUSHING: + rgb_task_flush(effect); + break; + case SYNCING: + rgb_task_sync(); + break; + } + if (!suspend_backlight) { + rgb_matrix_indicators(); + } } void rgb_matrix_indicators(void) { - rgb_matrix_indicators_kb(); - rgb_matrix_indicators_user(); + rgb_matrix_indicators_kb(); + rgb_matrix_indicators_user(); } __attribute__((weak)) @@ -817,103 +456,54 @@ void rgb_matrix_indicators_kb(void) {} __attribute__((weak)) void rgb_matrix_indicators_user(void) {} - -// void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column ) -// { -// if ( row >= MATRIX_ROWS ) -// { -// // Special value, 255=none, 254=all -// *index = row; -// } -// else -// { -// // This needs updated to something like -// // uint8_t led[8], led_count; -// // map_row_column_to_led(row,column,led,&led_count); -// // for(uint8_t i = 0; i < led_count; i++) -// map_row_column_to_led( row, column, index ); -// } -// } - void rgb_matrix_init(void) { rgb_matrix_driver.init(); // TODO: put the 1 second startup delay here? - // clear the key hits - for ( int led=0; ledh = eeprom_read_byte(address); -// hsv->s = eeprom_read_byte(address+1); -// hsv->v = eeprom_read_byte(address+2); -// } - -// void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv ) -// { -// uint8_t led[8], led_count; -// map_row_column_to_led(row,column,led,&led_count); -// for(uint8_t i = 0; i < led_count; i++) { -// if ( led[i] < DRIVER_LED_TOTAL ) -// { -// void *address = backlight_get_custom_key_color_eeprom_address(led[i]); -// eeprom_update_byte(address, hsv.h); -// eeprom_update_byte(address+1, hsv.s); -// eeprom_update_byte(address+2, hsv.v); -// } -// } -// } - -uint32_t rgb_matrix_get_tick(void) { - return g_tick; +void rgb_matrix_set_suspend_state(bool state) { + g_suspend_state = state; } void rgb_matrix_toggle(void) { - rgb_matrix_config.enable ^= 1; - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.enable ^= 1; + if (!rgb_matrix_config.enable) { + rgb_task_state = STARTING; + } + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_enable(void) { rgb_matrix_config.enable = 1; - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_enable_noeeprom(void) { @@ -922,7 +512,7 @@ void rgb_matrix_enable_noeeprom(void) { void rgb_matrix_disable(void) { rgb_matrix_config.enable = 0; - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_disable_noeeprom(void) { @@ -930,76 +520,79 @@ void rgb_matrix_disable_noeeprom(void) { } void rgb_matrix_step(void) { - rgb_matrix_config.mode++; - if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) - rgb_matrix_config.mode = 1; - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.mode++; + if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) + rgb_matrix_config.mode = 1; + rgb_task_state = STARTING; + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_step_reverse(void) { - rgb_matrix_config.mode--; - if (rgb_matrix_config.mode < 1) - rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1; - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.mode--; + if (rgb_matrix_config.mode < 1) + rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1; + rgb_task_state = STARTING; + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_increase_hue(void) { - rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP; + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_decrease_hue(void) { - rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP; + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_increase_sat(void) { - rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_decrease_sat(void) { - rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_increase_val(void) { - rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP); + if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) + rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS; + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_decrease_val(void) { - rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_increase_speed(void) { - rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this + rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this } void rgb_matrix_decrease_speed(void) { - rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 ); - eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this + rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP); + eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this } void rgb_matrix_mode(uint8_t mode) { - rgb_matrix_config.mode = mode; - eeconfig_update_rgb_matrix(rgb_matrix_config.raw); + rgb_matrix_config.mode = mode; + rgb_task_state = STARTING; + eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } void rgb_matrix_mode_noeeprom(uint8_t mode) { - rgb_matrix_config.mode = mode; + rgb_matrix_config.mode = mode; } uint8_t rgb_matrix_get_mode(void) { - return rgb_matrix_config.mode; + return rgb_matrix_config.mode; } void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) { - rgb_matrix_config.hue = hue; - rgb_matrix_config.sat = sat; - rgb_matrix_config.val = val; + rgb_matrix_sethsv_noeeprom(hue, sat, val); eeconfig_update_rgb_matrix(rgb_matrix_config.raw); } @@ -1007,4 +600,6 @@ void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { rgb_matrix_config.hue = hue; rgb_matrix_config.sat = sat; rgb_matrix_config.val = val; + if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) + rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS; } diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h index e6acd2d4b..855ea0323 100644 --- a/quantum/rgb_matrix.h +++ b/quantum/rgb_matrix.h @@ -21,32 +21,33 @@ #include #include +#include "rgb_matrix_types.h" #include "color.h" #include "quantum.h" #ifdef IS31FL3731 - #include "is31fl3731.h" + #include "is31fl3731.h" #elif defined (IS31FL3733) - #include "is31fl3733.h" + #include "is31fl3733.h" #endif -typedef struct Point { - uint8_t x; - uint8_t y; -} __attribute__((packed)) Point; +#ifndef RGB_MATRIX_LED_FLUSH_LIMIT + #define RGB_MATRIX_LED_FLUSH_LIMIT 16 +#endif -typedef struct rgb_led { - union { - uint8_t raw; - struct { - uint8_t row:4; // 16 max - uint8_t col:4; // 16 max - }; - } matrix_co; - Point point; - uint8_t modifier:1; -} __attribute__((packed)) rgb_led; +#ifndef RGB_MATRIX_LED_PROCESS_LIMIT + #define RGB_MATRIX_LED_PROCESS_LIMIT (DRIVER_LED_TOTAL + 4) / 5 +#endif +#if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL +#define RGB_MATRIX_USE_LIMITS(min, max) uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * params->iter; \ + uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT; \ + if (max > DRIVER_LED_TOTAL) \ + max = DRIVER_LED_TOTAL; +#else +#define RGB_MATRIX_USE_LIMITS(min, max) uint8_t min = 0; \ + uint8_t max = DRIVER_LED_TOTAL; +#endif extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; @@ -56,79 +57,73 @@ typedef struct uint8_t index; } rgb_indicator; -typedef union { - uint32_t raw; - struct { - bool enable :1; - uint8_t mode :6; - uint16_t hue :9; - uint8_t sat :8; - uint8_t val :8; - uint8_t speed :8;//EECONFIG needs to be increased to support this - }; -} rgb_config_t; - enum rgb_matrix_effects { + RGB_MATRIX_NONE = 0, RGB_MATRIX_SOLID_COLOR = 1, #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS - RGB_MATRIX_ALPHAS_MODS, -#endif -#ifndef DISABLE_RGB_MATRIX_DUAL_BEACON - RGB_MATRIX_DUAL_BEACON, -#endif + RGB_MATRIX_ALPHAS_MODS, +#endif // DISABLE_RGB_MATRIX_ALPHAS_MODS #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN - RGB_MATRIX_GRADIENT_UP_DOWN, -#endif -#ifndef DISABLE_RGB_MATRIX_RAINDROPS - RGB_MATRIX_RAINDROPS, -#endif + RGB_MATRIX_GRADIENT_UP_DOWN, +#endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN +#ifndef DISABLE_RGB_MATRIX_BREATHING + RGB_MATRIX_BREATHING, +#endif // DISABLE_RGB_MATRIX_BREATHING #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL - RGB_MATRIX_CYCLE_ALL, -#endif + RGB_MATRIX_CYCLE_ALL, +#endif // DISABLE_RGB_MATRIX_CYCLE_ALL #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT - RGB_MATRIX_CYCLE_LEFT_RIGHT, -#endif + RGB_MATRIX_CYCLE_LEFT_RIGHT, +#endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN - RGB_MATRIX_CYCLE_UP_DOWN, -#endif + RGB_MATRIX_CYCLE_UP_DOWN, +#endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN +#ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON + RGB_MATRIX_RAINBOW_MOVING_CHEVRON, +#endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON +#ifndef DISABLE_RGB_MATRIX_DUAL_BEACON + RGB_MATRIX_DUAL_BEACON, +#endif // DISABLE_RGB_MATRIX_DUAL_BEACON #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON - RGB_MATRIX_RAINBOW_BEACON, -#endif + RGB_MATRIX_RAINBOW_BEACON, +#endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS - RGB_MATRIX_RAINBOW_PINWHEELS, -#endif -#ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON - RGB_MATRIX_RAINBOW_MOVING_CHEVRON, -#endif + RGB_MATRIX_RAINBOW_PINWHEELS, +#endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS +#ifndef DISABLE_RGB_MATRIX_RAINDROPS + RGB_MATRIX_RAINDROPS, +#endif // DISABLE_RGB_MATRIX_RAINDROPS #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS - RGB_MATRIX_JELLYBEAN_RAINDROPS, -#endif + RGB_MATRIX_JELLYBEAN_RAINDROPS, +#endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN - RGB_MATRIX_DIGITAL_RAIN, -#endif -#ifdef RGB_MATRIX_KEYPRESSES - #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE - RGB_MATRIX_SOLID_REACTIVE, - #endif - #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE - RGB_MATRIX_SOLID_REACTIVE_SIMPLE, - #endif - #ifndef DISABLE_RGB_MATRIX_SPLASH - RGB_MATRIX_SPLASH, - #endif - #ifndef DISABLE_RGB_MATRIX_MULTISPLASH - RGB_MATRIX_MULTISPLASH, - #endif - #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH - RGB_MATRIX_SOLID_SPLASH, - #endif - #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH - RGB_MATRIX_SOLID_MULTISPLASH, - #endif -#endif - RGB_MATRIX_EFFECT_MAX + RGB_MATRIX_DIGITAL_RAIN, +#endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED +#ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE + RGB_MATRIX_SOLID_REACTIVE_SIMPLE, +#endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE +#ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE + RGB_MATRIX_SOLID_REACTIVE, +#endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE +#ifndef DISABLE_RGB_MATRIX_SPLASH + RGB_MATRIX_SPLASH, +#endif // DISABLE_RGB_MATRIX_SPLASH +#ifndef DISABLE_RGB_MATRIX_MULTISPLASH + RGB_MATRIX_MULTISPLASH, +#endif // DISABLE_RGB_MATRIX_MULTISPLASH +#ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH + RGB_MATRIX_SOLID_SPLASH, +#endif // DISABLE_RGB_MATRIX_SOLID_SPLASH +#ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH + RGB_MATRIX_SOLID_MULTISPLASH, +#endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED + RGB_MATRIX_EFFECT_MAX }; +uint8_t rgb_matrix_map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i); + void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ); void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ); @@ -162,8 +157,6 @@ void rgb_matrix_decrease(void); // void backlight_get_key_color( uint8_t led, HSV *hsv ); // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv ); -uint32_t rgb_matrix_get_tick(void); - void rgb_matrix_toggle(void); void rgb_matrix_enable(void); void rgb_matrix_enable_noeeprom(void); @@ -212,7 +205,6 @@ uint8_t rgb_matrix_get_mode(void); typedef struct { /* Perform any initialisation required for the other driver functions to work. */ void (*init)(void); - /* Set the colour of a single LED in the buffer. */ void (*set_color)(int index, uint8_t r, uint8_t g, uint8_t b); /* Set the colour of all LEDS on the keyboard in the buffer. */ diff --git a/quantum/rgb_matrix_animations/alpha_mods_anim.h b/quantum/rgb_matrix_animations/alpha_mods_anim.h new file mode 100644 index 000000000..cc1914d7f --- /dev/null +++ b/quantum/rgb_matrix_animations/alpha_mods_anim.h @@ -0,0 +1,26 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS + +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +// alphas = color1, mods = color2 +bool rgb_matrix_alphas_mods(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { rgb_matrix_config.hue, rgb_matrix_config.sat, rgb_matrix_config.val }; + RGB rgb1 = hsv_to_rgb(hsv); + hsv.h += rgb_matrix_config.speed; + RGB rgb2 = hsv_to_rgb(hsv); + + for (uint8_t i = led_min; i < led_max; i++) { + if (g_rgb_leds[i].modifier) { + rgb_matrix_set_color(i, rgb2.r, rgb2.g, rgb2.b); + } else { + rgb_matrix_set_color(i, rgb1.r, rgb1.g, rgb1.b); + } + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_ALPHAS_MODS diff --git a/quantum/rgb_matrix_animations/breathing_anim.h b/quantum/rgb_matrix_animations/breathing_anim.h new file mode 100644 index 000000000..fb90b66bd --- /dev/null +++ b/quantum/rgb_matrix_animations/breathing_anim.h @@ -0,0 +1,19 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_BREATHING + +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_breathing(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + uint16_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 8); + uint8_t val = scale8(abs8(sin8(time) - 128) * 2, rgb_matrix_config.val); + HSV hsv = { rgb_matrix_config.hue, rgb_matrix_config.sat, val }; + RGB rgb = hsv_to_rgb(hsv); + for (uint8_t i = led_min; i < led_max; i++) { + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_BREATHING diff --git a/quantum/rgb_matrix_animations/cycle_all_anim.h b/quantum/rgb_matrix_animations/cycle_all_anim.h new file mode 100644 index 000000000..5c18cfa0c --- /dev/null +++ b/quantum/rgb_matrix_animations/cycle_all_anim.h @@ -0,0 +1,21 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_CYCLE_ALL + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_cycle_all(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint8_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + for (uint8_t i = led_min; i < led_max; i++) { + hsv.h = time; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_CYCLE_ALL diff --git a/quantum/rgb_matrix_animations/cycle_left_right_anim.h b/quantum/rgb_matrix_animations/cycle_left_right_anim.h new file mode 100644 index 000000000..f519aeb47 --- /dev/null +++ b/quantum/rgb_matrix_animations/cycle_left_right_anim.h @@ -0,0 +1,22 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_cycle_left_right(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint8_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + hsv.h = point.x - time; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT diff --git a/quantum/rgb_matrix_animations/cycle_up_down_anim.h b/quantum/rgb_matrix_animations/cycle_up_down_anim.h new file mode 100644 index 000000000..8b91d890d --- /dev/null +++ b/quantum/rgb_matrix_animations/cycle_up_down_anim.h @@ -0,0 +1,22 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_cycle_up_down(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint8_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + hsv.h = point.y - time; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN diff --git a/quantum/rgb_matrix_animations/digital_rain_anim.h b/quantum/rgb_matrix_animations/digital_rain_anim.h new file mode 100644 index 000000000..4ba3c1c87 --- /dev/null +++ b/quantum/rgb_matrix_animations/digital_rain_anim.h @@ -0,0 +1,74 @@ +#pragma once +#ifndef 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 + +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; + 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 (params->init) { + 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 / 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; + } + 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_HITS_TO_REMEMBER]; + uint8_t led_count = rgb_matrix_map_row_column_to_led(row, col, led); + + // 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)); + 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); + 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]--; + } + // 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; + } + } + } + } + return false; +} + +#endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN diff --git a/quantum/rgb_matrix_animations/dual_beacon_anim.h b/quantum/rgb_matrix_animations/dual_beacon_anim.h new file mode 100644 index 000000000..dda315780 --- /dev/null +++ b/quantum/rgb_matrix_animations/dual_beacon_anim.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_DUAL_BEACON + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_dual_beacon(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint16_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + int8_t cos_value = cos8(time) - 128; + int8_t sin_value = sin8(time) - 128; + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + hsv.h = ((point.y - 32) * cos_value + (point.x - 112) * sin_value) / 128 + rgb_matrix_config.hue; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_DUAL_BEACON diff --git a/quantum/rgb_matrix_animations/gradient_up_down_anim.h b/quantum/rgb_matrix_animations/gradient_up_down_anim.h new file mode 100644 index 000000000..11498e22f --- /dev/null +++ b/quantum/rgb_matrix_animations/gradient_up_down_anim.h @@ -0,0 +1,22 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN + +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_gradient_up_down(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint8_t scale = scale8(64, rgb_matrix_config.speed); + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + // The y range will be 0..64, map this to 0..4 + // Relies on hue being 8-bit and wrapping + hsv.h = rgb_matrix_config.hue + scale * (point.y >> 4); + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} +#endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN diff --git a/quantum/rgb_matrix_animations/jellybean_raindrops_anim.h b/quantum/rgb_matrix_animations/jellybean_raindrops_anim.h new file mode 100644 index 000000000..01ff5c230 --- /dev/null +++ b/quantum/rgb_matrix_animations/jellybean_raindrops_anim.h @@ -0,0 +1,30 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +static void jellybean_raindrops_set_color(int i) { + HSV hsv = { rand() & 0xFF , rand() & 0xFF, rgb_matrix_config.val }; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); +} + +bool rgb_matrix_jellybean_raindrops(effect_params_t* params) { + if (!params->init) { + // Change one LED every tick, make sure speed is not 0 + if (scale16by8(g_rgb_counters.tick, qadd8(rgb_matrix_config.speed, 16)) % 5 == 0) { + jellybean_raindrops_set_color(rand() % DRIVER_LED_TOTAL); + } + return false; + } + + RGB_MATRIX_USE_LIMITS(led_min, led_max); + for (int i = led_min; i < led_max; i++) { + jellybean_raindrops_set_color(i); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS diff --git a/quantum/rgb_matrix_animations/rainbow_beacon_anim.h b/quantum/rgb_matrix_animations/rainbow_beacon_anim.h new file mode 100644 index 000000000..3c15e64ab --- /dev/null +++ b/quantum/rgb_matrix_animations/rainbow_beacon_anim.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_rainbow_beacon(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint16_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + int16_t cos_value = 2 * (cos8(time) - 128); + int16_t sin_value = 2 * (sin8(time) - 128); + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + hsv.h = ((point.y - 32) * cos_value + (point.x - 112) * sin_value) / 128 + rgb_matrix_config.hue; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON diff --git a/quantum/rgb_matrix_animations/rainbow_moving_chevron_anim.h b/quantum/rgb_matrix_animations/rainbow_moving_chevron_anim.h new file mode 100644 index 000000000..0d11d5280 --- /dev/null +++ b/quantum/rgb_matrix_animations/rainbow_moving_chevron_anim.h @@ -0,0 +1,22 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_rainbow_moving_chevron(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint8_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + hsv.h = abs8(point.y - 32) + (point.x - time) + rgb_matrix_config.hue; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON diff --git a/quantum/rgb_matrix_animations/rainbow_pinwheels_anim.h b/quantum/rgb_matrix_animations/rainbow_pinwheels_anim.h new file mode 100644 index 000000000..d7cd42cbe --- /dev/null +++ b/quantum/rgb_matrix_animations/rainbow_pinwheels_anim.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS + +extern rgb_counters_t g_rgb_counters; +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_rainbow_pinwheels(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, rgb_matrix_config.val }; + uint16_t time = scale16by8(g_rgb_counters.tick, rgb_matrix_config.speed / 4); + int16_t cos_value = 3 * (cos8(time) - 128); + int16_t sin_value = 3 * (sin8(time) - 128); + for (uint8_t i = led_min; i < led_max; i++) { + point_t point = g_rgb_leds[i].point; + hsv.h = ((point.y - 32) * cos_value + (56 - abs8(point.x - 112)) * sin_value) / 128 + rgb_matrix_config.hue; + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS diff --git a/quantum/rgb_matrix_animations/raindrops_anim.h b/quantum/rgb_matrix_animations/raindrops_anim.h new file mode 100644 index 000000000..fc721375b --- /dev/null +++ b/quantum/rgb_matrix_animations/raindrops_anim.h @@ -0,0 +1,40 @@ +#pragma once +#ifndef DISABLE_RGB_MATRIX_RAINDROPS +#include "rgb_matrix_types.h" + +extern rgb_counters_t g_rgb_counters; +extern rgb_config_t rgb_matrix_config; + +static void raindrops_set_color(int i) { + HSV hsv = { 0 , rgb_matrix_config.sat, rgb_matrix_config.val }; + + // Take the shortest path between hues + int16_t deltaH = ((rgb_matrix_config.hue + 180) % 360 - rgb_matrix_config.hue) / 4; + if (deltaH > 127) { + deltaH -= 256; + } else if (deltaH < -127) { + deltaH += 256; + } + + hsv.h = rgb_matrix_config.hue + (deltaH * (rand() & 0x03)); + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); +} + +bool rgb_matrix_raindrops(effect_params_t* params) { + if (!params->init) { + // Change one LED every tick, make sure speed is not 0 + if (scale16by8(g_rgb_counters.tick, qadd8(rgb_matrix_config.speed, 16)) % 10 == 0) { + raindrops_set_color(rand() % DRIVER_LED_TOTAL); + } + return false; + } + + RGB_MATRIX_USE_LIMITS(led_min, led_max); + for (int i = led_min; i < led_max; i++) { + raindrops_set_color(i); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_RAINDROPS diff --git a/quantum/rgb_matrix_animations/solid_color_anim.h b/quantum/rgb_matrix_animations/solid_color_anim.h new file mode 100644 index 000000000..24a197beb --- /dev/null +++ b/quantum/rgb_matrix_animations/solid_color_anim.h @@ -0,0 +1,14 @@ +#pragma once + +extern rgb_config_t rgb_matrix_config; + +bool rgb_matrix_solid_color(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { rgb_matrix_config.hue, rgb_matrix_config.sat, rgb_matrix_config.val }; + RGB rgb = hsv_to_rgb(hsv); + for (uint8_t i = led_min; i < led_max; i++) { + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} diff --git a/quantum/rgb_matrix_animations/solid_reactive_anim.h b/quantum/rgb_matrix_animations/solid_reactive_anim.h new file mode 100644 index 000000000..220e54233 --- /dev/null +++ b/quantum/rgb_matrix_animations/solid_reactive_anim.h @@ -0,0 +1,33 @@ +#pragma once +#if defined(RGB_MATRIX_KEYREACTIVE_ENABLED) +#ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE + +extern rgb_config_t rgb_matrix_config; +extern last_hit_t g_last_hit_tracker; + +bool rgb_matrix_solid_reactive(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { rgb_matrix_config.hue, 255, rgb_matrix_config.val }; + // Max tick based on speed scale ensures results from scale16by8 with rgb_matrix_config.speed are no greater than 255 + uint16_t max_tick = 65535 / rgb_matrix_config.speed; + // Relies on hue being 8-bit and wrapping + for (uint8_t i = led_min; i < led_max; i++) { + uint16_t tick = max_tick; + for(uint8_t j = 0; j < g_last_hit_tracker.count; j++) { + if (g_last_hit_tracker.index[j] == i && g_last_hit_tracker.tick[j] < tick) { + tick = g_last_hit_tracker.tick[j]; + break; + } + } + + uint16_t offset = scale16by8(tick, rgb_matrix_config.speed); + hsv.h = rgb_matrix_config.hue + qsub8(130, offset); + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON +#endif // defined(RGB_MATRIX_KEYREACTIVE_ENABLED) diff --git a/quantum/rgb_matrix_animations/solid_reactive_simple_anim.h b/quantum/rgb_matrix_animations/solid_reactive_simple_anim.h new file mode 100644 index 000000000..e84cd6939 --- /dev/null +++ b/quantum/rgb_matrix_animations/solid_reactive_simple_anim.h @@ -0,0 +1,32 @@ +#pragma once +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED +#ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE + +extern rgb_config_t rgb_matrix_config; +extern last_hit_t g_last_hit_tracker; + +bool rgb_matrix_solid_reactive_simple(effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { rgb_matrix_config.hue, rgb_matrix_config.sat, 0 }; + // Max tick based on speed scale ensures results from scale16by8 with rgb_matrix_config.speed are no greater than 255 + uint16_t max_tick = 65535 / rgb_matrix_config.speed; + for (uint8_t i = led_min; i < led_max; i++) { + uint16_t tick = max_tick; + for(uint8_t j = 0; j < g_last_hit_tracker.count; j++) { + if (g_last_hit_tracker.index[j] == i && g_last_hit_tracker.tick[j] < tick) { + tick = g_last_hit_tracker.tick[j]; + break; + } + } + + uint16_t offset = scale16by8(tick, rgb_matrix_config.speed); + hsv.v = scale8(255 - offset, rgb_matrix_config.val); + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +#endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED diff --git a/quantum/rgb_matrix_animations/solid_splash_anim.h b/quantum/rgb_matrix_animations/solid_splash_anim.h new file mode 100644 index 000000000..82ac055b8 --- /dev/null +++ b/quantum/rgb_matrix_animations/solid_splash_anim.h @@ -0,0 +1,42 @@ +#pragma once +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED +#if !defined(DISABLE_RGB_MATRIX_SOLID_SPLASH) || !defined(DISABLE_RGB_MATRIX_SOLID_MULTISPLASH) + +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; +extern last_hit_t g_last_hit_tracker; + +static bool rgb_matrix_solid_multisplash_range(uint8_t start, effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { rgb_matrix_config.hue, rgb_matrix_config.sat, 0 }; + uint8_t count = g_last_hit_tracker.count; + for (uint8_t i = led_min; i < led_max; i++) { + hsv.v = 0; + point_t point = g_rgb_leds[i].point; + for (uint8_t j = start; j < count; j++) { + int16_t dx = point.x - g_last_hit_tracker.x[j]; + int16_t dy = point.y - g_last_hit_tracker.y[j]; + uint8_t dist = sqrt16(dx * dx + dy * dy); + uint16_t effect = scale16by8(g_last_hit_tracker.tick[j], rgb_matrix_config.speed) - dist; + if (effect > 255) + effect = 255; + hsv.v = qadd8(hsv.v, 255 - effect); + } + hsv.v = scale8(hsv.v, rgb_matrix_config.val); + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +bool rgb_matrix_solid_multisplash(effect_params_t* params) { + return rgb_matrix_solid_multisplash_range(0, params); +} + +bool rgb_matrix_solid_splash(effect_params_t* params) { + return rgb_matrix_solid_multisplash_range(qsub8(g_last_hit_tracker.count, 1), params); +} + +#endif // !defined(DISABLE_RGB_MATRIX_SPLASH) && !defined(DISABLE_RGB_MATRIX_MULTISPLASH) +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED diff --git a/quantum/rgb_matrix_animations/splash_anim.h b/quantum/rgb_matrix_animations/splash_anim.h new file mode 100644 index 000000000..829d30eef --- /dev/null +++ b/quantum/rgb_matrix_animations/splash_anim.h @@ -0,0 +1,44 @@ +#pragma once +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED +#if !defined(DISABLE_RGB_MATRIX_SPLASH) || !defined(DISABLE_RGB_MATRIX_MULTISPLASH) + +extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; +extern rgb_config_t rgb_matrix_config; +extern last_hit_t g_last_hit_tracker; + +static bool rgb_matrix_multisplash_range(uint8_t start, effect_params_t* params) { + RGB_MATRIX_USE_LIMITS(led_min, led_max); + + HSV hsv = { 0, rgb_matrix_config.sat, 0 }; + uint8_t count = g_last_hit_tracker.count; + for (uint8_t i = led_min; i < led_max; i++) { + hsv.h = rgb_matrix_config.hue; + hsv.v = 0; + point_t point = g_rgb_leds[i].point; + for (uint8_t j = start; j < count; j++) { + int16_t dx = point.x - g_last_hit_tracker.x[j]; + int16_t dy = point.y - g_last_hit_tracker.y[j]; + uint8_t dist = sqrt16(dx * dx + dy * dy); + uint16_t effect = scale16by8(g_last_hit_tracker.tick[j], rgb_matrix_config.speed) - dist; + if (effect > 255) + effect = 255; + hsv.h += effect; + hsv.v = qadd8(hsv.v, 255 - effect); + } + hsv.v = scale8(hsv.v, rgb_matrix_config.val); + RGB rgb = hsv_to_rgb(hsv); + rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); + } + return led_max < DRIVER_LED_TOTAL; +} + +bool rgb_matrix_multisplash(effect_params_t* params) { + return rgb_matrix_multisplash_range(0, params); +} + +bool rgb_matrix_splash(effect_params_t* params) { + return rgb_matrix_multisplash_range(qsub8(g_last_hit_tracker.count, 1), params); +} + +#endif // !defined(DISABLE_RGB_MATRIX_SPLASH) || !defined(DISABLE_RGB_MATRIX_MULTISPLASH) +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED diff --git a/quantum/rgb_matrix_types.h b/quantum/rgb_matrix_types.h new file mode 100644 index 000000000..f7643d2b0 --- /dev/null +++ b/quantum/rgb_matrix_types.h @@ -0,0 +1,90 @@ +#pragma once + +#include +#include + +#if defined(__GNUC__) +#define PACKED __attribute__ ((__packed__)) +#else +#define PACKED +#endif + +#if defined(_MSC_VER) +#pragma pack( push, 1 ) +#endif + +#if defined(RGB_MATRIX_KEYPRESSES) || defined(RGB_MATRIX_KEYRELEASES) + #define RGB_MATRIX_KEYREACTIVE_ENABLED +#endif + +// Last led hit +#ifndef LED_HITS_TO_REMEMBER + #define LED_HITS_TO_REMEMBER 8 +#endif // LED_HITS_TO_REMEMBER + +#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED +typedef struct PACKED { + uint8_t count; + uint8_t x[LED_HITS_TO_REMEMBER]; + uint8_t y[LED_HITS_TO_REMEMBER]; + uint8_t index[LED_HITS_TO_REMEMBER]; + uint16_t tick[LED_HITS_TO_REMEMBER]; +} last_hit_t; +#endif // RGB_MATRIX_KEYREACTIVE_ENABLED + +typedef enum rgb_task_states { + STARTING, + RENDERING, + FLUSHING, + SYNCING +} rgb_task_states; + +typedef uint8_t led_flags_t; + +typedef struct PACKED { + uint8_t iter; + led_flags_t flags; + bool init; +} effect_params_t; + +typedef struct PACKED { + // Global tick at 20 Hz + uint32_t tick; + // Ticks since this key was last hit. + uint32_t any_key_hit; +} rgb_counters_t; + +typedef struct PACKED { + uint8_t x; + uint8_t y; +} point_t; + +typedef union { + uint8_t raw; + struct { + uint8_t row:4; // 16 max + uint8_t col:4; // 16 max + }; +} matrix_co_t; + +typedef struct PACKED { + matrix_co_t matrix_co; + point_t point; + uint8_t modifier:1; +} rgb_led; + +typedef union { + uint32_t raw; + struct PACKED { + bool enable :1; + uint8_t mode :7; + uint8_t hue :8; + uint8_t sat :8; + uint8_t val :8; + uint8_t speed :8;//EECONFIG needs to be increased to support this + }; +} rgb_config_t; + +#if defined(_MSC_VER) +#pragma pack( pop ) +#endif -- cgit v1.2.3-24-g4f1b