From ce99f98bb5217ede628cfbf7e20924346b4279da Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 13 Apr 2021 19:51:03 +1000 Subject: [PATCH] LED Matrix: suspend code (#12509) --- quantum/led_matrix.c | 57 +++++++++++++++++-------------- quantum/led_matrix.h | 7 ++-- tmk_core/common/avr/suspend.c | 3 ++ tmk_core/common/chibios/suspend.c | 6 ++++ tmk_core/common/keyboard.c | 3 ++ 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c index e1337645561..5258b3acfdc 100644 --- a/quantum/led_matrix.c +++ b/quantum/led_matrix.c @@ -27,8 +27,6 @@ #include -led_eeconfig_t led_matrix_eeconfig; - #ifndef MAX # define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) #endif @@ -74,7 +72,9 @@ led_eeconfig_t led_matrix_eeconfig; # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2 #endif -bool g_suspend_state = false; +// globals +bool g_suspend_state = false; +led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr // Global tick at 20 Hz uint32_t g_tick = 0; @@ -139,10 +139,10 @@ void led_matrix_set_value_all(uint8_t value) { #endif } -bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { - if (record->event.pressed) { +void process_led_matrix(uint8_t row, uint8_t col, bool pressed) { + if (pressed) { uint8_t led[8]; - uint8_t led_count = led_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led); + uint8_t led_count = led_matrix_map_row_column_to_led(row, col, led); 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]; @@ -155,35 +155,24 @@ bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { } else { #ifdef LED_MATRIX_KEYRELEASES uint8_t led[8]; - uint8_t led_count = led_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led); + uint8_t led_count = led_matrix_map_row_column_to_led(row, .col, led); for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255; g_any_key_hit = 255; #endif } - return true; } -void led_matrix_set_suspend_state(bool state) { - if (LED_DISABLE_WHEN_USB_SUSPENDED && state) { - led_matrix_set_value_all(0); // turn off all LEDs when suspending - } - g_suspend_state = state; -} - -bool led_matrix_get_suspend_state(void) { return g_suspend_state; } - -// All LEDs off -void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } +static void led_matrix_none(void) { led_matrix_set_value_all(0); } // Uniform brightness -void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(led_matrix_eeconfig.val); } +void led_matrix_uniform_brightness(void) { led_matrix_set_value_all(led_matrix_eeconfig.val); } void led_matrix_custom(void) {} void led_matrix_task(void) { if (!led_matrix_eeconfig.enable) { - led_matrix_all_off(); + led_matrix_none(); led_matrix_indicators(); return; } @@ -203,13 +192,23 @@ void led_matrix_task(void) { // 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 && LED_DISABLE_WHEN_USB_SUSPENDED) || (LED_DISABLE_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_TIMEOUT)); - uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode; + bool suspend_backlight = +#if LED_DISABLE_WHEN_USB_SUSPENDED == true + g_suspend_state || +#endif // LED_DISABLE_WHEN_USB_SUSPENDED == true +#if LED_DISABLE_TIMEOUT > 0 + (g_any_key_hit > (uint32_t)LED_DISABLE_TIMEOUT) || +#endif // LED_DISABLE_TIMEOUT > 0 + false; + + uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode; // this gets ticked at 20 Hz. // each effect can opt to do calculations // and/or request PWM buffer updates. switch (effect) { + case LED_MATRIX_NONE: + led_matrix_none(); case LED_MATRIX_UNIFORM_BRIGHTNESS: led_matrix_uniform_brightness(); break; @@ -218,7 +217,7 @@ void led_matrix_task(void) { break; } - if (!suspend_backlight) { + if (effect) { led_matrix_indicators(); } @@ -257,10 +256,18 @@ void led_matrix_init(void) { dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); eeconfig_update_led_matrix_default(); } - eeconfig_debug_led_matrix(); // display current eeprom values } +void led_matrix_set_suspend_state(bool state) { + if (LED_DISABLE_WHEN_USB_SUSPENDED && state) { + led_matrix_set_value_all(0); // turn off all LEDs when suspending + } + g_suspend_state = state; +} + +bool led_matrix_get_suspend_state(void) { return g_suspend_state; } + void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) { led_matrix_eeconfig.enable ^= 1; if (write_to_eeprom) { diff --git a/quantum/led_matrix.h b/quantum/led_matrix.h index ba8f0279a6e..48c9483b2d7 100644 --- a/quantum/led_matrix.h +++ b/quantum/led_matrix.h @@ -48,8 +48,11 @@ #endif enum led_matrix_effects { - LED_MATRIX_UNIFORM_BRIGHTNESS = 1, + LED_MATRIX_NONE = 0, + + LED_MATRIX_UNIFORM_BRIGHTNESS, // All new effects go above this line + LED_MATRIX_EFFECT_MAX }; @@ -63,7 +66,7 @@ uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *l void led_matrix_set_value(int index, uint8_t value); void led_matrix_set_value_all(uint8_t value); -bool process_led_matrix(uint16_t keycode, keyrecord_t *record); +void process_led_matrix(uint8_t row, uint8_t col, bool pressed); void led_matrix_task(void); diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index d52c8ac4105..96b19a77fd7 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -28,6 +28,9 @@ # include "rgblight.h" #endif +#ifdef LED_MATRIX_ENABLE +# include "led_matrix.h" +#endif #ifdef RGB_MATRIX_ENABLE # include "rgb_matrix.h" #endif diff --git a/tmk_core/common/chibios/suspend.c b/tmk_core/common/chibios/suspend.c index 17f024cabac..b3949185e95 100644 --- a/tmk_core/common/chibios/suspend.c +++ b/tmk_core/common/chibios/suspend.c @@ -24,6 +24,9 @@ # include "rgblight.h" #endif +#ifdef LED_MATRIX_ENABLE +# include "led_matrix.h" +#endif #ifdef RGB_MATRIX_ENABLE # include "rgb_matrix.h" #endif @@ -57,6 +60,9 @@ void suspend_power_down(void) { backlight_set(0); #endif +#ifdef LED_MATRIX_ENABLE + led_matrix_task(); +#endif #ifdef RGB_MATRIX_ENABLE rgb_matrix_task(); #endif diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index e473806b210..132affe7a8f 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -330,6 +330,9 @@ void keyboard_init(void) { * This is differnet than keycode events as no layer processing, or filtering occurs. */ void switch_events(uint8_t row, uint8_t col, bool pressed) { +#if defined(LED_MATRIX_ENABLE) + process_led_matrix(row, col, pressed); +#endif #if defined(RGB_MATRIX_ENABLE) process_rgb_matrix(row, col, pressed); #endif