From 4d9f2ac7bc8b53123bbd1178f4677d46eebd9015 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Fri, 13 Aug 2021 01:17:52 +0200 Subject: [PATCH 1/2] Clearer RGB flags, do not depend on lib8tion --- .../gmmk/pro/ansi/keymaps/andrebrait/keymap.c | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c b/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c index febca3b97414..63c182e56228 100644 --- a/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c +++ b/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c @@ -21,9 +21,6 @@ along with this program. If not, see . #ifndef RGB_CONFIRMATION_BLINKING_TIME #define RGB_CONFIRMATION_BLINKING_TIME 2000 // 2 seconds #endif - #if RGB_CONFIRMATION_BLINKING_TIME > 0 - #include - #endif // RGB_CONFIRMATION_BLINKING_TIME > 0 #endif // RGB_MATRIX_ENABLE // clang-format off @@ -83,6 +80,10 @@ bool encoder_update_user(uint8_t index, bool clockwise) { #ifdef RGB_MATRIX_ENABLE +/* Renaming those to make the purpose on this keymap clearer */ +#define LED_FLAG_CAPS LED_FLAG_NONE +#define LED_FLAG_EFFECTS LED_FLAG_INDICATOR + static void set_rgb_caps_leds(void); #if RGB_CONFIRMATION_BLINKING_TIME > 0 @@ -90,6 +91,8 @@ static uint16_t effect_started_time = 0; static uint8_t r_effect = 0x0, g_effect = 0x0, b_effect = 0x0; static void start_effects(void); +/* The higher this is, the slower the blinking will be */ +#define TIME_SELECTED_BIT 8 #define effect_red() r_effect = 0xFF, g_effect = 0x0, b_effect = 0x0 #define effect_green() r_effect = 0x0, g_effect = 0xFF, b_effect = 0x0 #endif // RGB_CONFIRMATION_BLINKING_TIME > 0 @@ -98,10 +101,10 @@ bool led_update_user(led_t led_state) { if (led_state.caps_lock) { if (!rgb_matrix_is_enabled()) { /* Turn ON the RGB Matrix for CAPS LOCK */ - rgb_matrix_set_flags(LED_FLAG_NONE); + rgb_matrix_set_flags(LED_FLAG_CAPS); rgb_matrix_enable(); } - } else if (rgb_matrix_get_flags() == LED_FLAG_NONE) { + } else if (rgb_matrix_get_flags() == LED_FLAG_CAPS) { /* RGB Matrix was only ON because of CAPS LOCK. Turn it OFF. */ rgb_matrix_set_flags(LED_FLAG_ALL); rgb_matrix_disable(); @@ -167,9 +170,9 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { if (rgb_matrix_is_enabled()) { switch (rgb_matrix_get_flags()) { #if RGB_CONFIRMATION_BLINKING_TIME > 0 - case LED_FLAG_INDICATOR: + case LED_FLAG_EFFECTS: #endif - case LED_FLAG_NONE: + case LED_FLAG_CAPS: /* Turned ON because of EFFECTS or CAPS, is actually OFF */ /* Change to LED_FLAG_ALL to signal it's really ON */ rgb_matrix_set_flags(LED_FLAG_ALL); @@ -180,15 +183,15 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { /* Is actually ON */ #if RGB_CONFIRMATION_BLINKING_TIME > 0 if (effect_started_time > 0) { - /* Change to LED_FLAG_INDICATOR to signal EFFECTS */ - rgb_matrix_set_flags(LED_FLAG_INDICATOR); + /* Signal EFFECTS */ + rgb_matrix_set_flags(LED_FLAG_EFFECTS); /* Will be re-enabled by the processing of the toggle */ rgb_matrix_disable_noeeprom(); } else #endif if (host_keyboard_led_state().caps_lock) { - /* Change to LED_FLAG_NONE to signal CAPS */ - rgb_matrix_set_flags(LED_FLAG_NONE); + /* Signal CAPS */ + rgb_matrix_set_flags(LED_FLAG_CAPS); /* Will be re-enabled by the processing of the toggle */ rgb_matrix_disable_noeeprom(); } @@ -204,12 +207,11 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { void rgb_matrix_indicators_user() { #if RGB_CONFIRMATION_BLINKING_TIME > 0 - if (effect_started_time != 0) { + if (effect_started_time > 0) { /* Render blinking EFFECTS */ uint16_t deltaTime = sync_timer_elapsed(effect_started_time); if (deltaTime <= RGB_CONFIRMATION_BLINKING_TIME) { - uint16_t time = scale16by8(deltaTime, UINT8_MAX / 2); - uint8_t led_state = sin8(time) >> 7; + uint8_t led_state = ((~deltaTime) >> TIME_SELECTED_BIT) & 0x01; uint8_t val_r = led_state * r_effect; uint8_t val_g = led_state * g_effect; uint8_t val_b = led_state * b_effect; @@ -221,11 +223,11 @@ void rgb_matrix_indicators_user() { } else { /* EFFECTS duration is finished */ effect_started_time = 0; - if (rgb_matrix_get_flags() == LED_FLAG_INDICATOR) { + if (rgb_matrix_get_flags() == LED_FLAG_EFFECTS) { /* It was turned ON because of EFFECTS */ if (host_keyboard_led_state().caps_lock) { /* CAPS is still ON. Demote to CAPS */ - rgb_matrix_set_flags(LED_FLAG_NONE); + rgb_matrix_set_flags(LED_FLAG_CAPS); } else { /* There is nothing else keeping RGB enabled. Reset flags and turn if off. */ rgb_matrix_set_flags(LED_FLAG_ALL); @@ -235,7 +237,7 @@ void rgb_matrix_indicators_user() { } } #endif // RGB_CONFIRMATION_BLINKING_TIME > 0 - if (rgb_matrix_get_flags() == LED_FLAG_NONE) { + if (rgb_matrix_get_flags() == LED_FLAG_CAPS) { rgb_matrix_set_color_all(0x0, 0x0, 0x0); } if (host_keyboard_led_state().caps_lock) { @@ -248,11 +250,11 @@ static void start_effects() { effect_started_time = sync_timer_read(); if (!rgb_matrix_is_enabled()) { /* Turn it ON, signal the cause (EFFECTS) */ - rgb_matrix_set_flags(LED_FLAG_INDICATOR); + rgb_matrix_set_flags(LED_FLAG_EFFECTS); rgb_matrix_enable_noeeprom(); - } else if (rgb_matrix_get_flags() == LED_FLAG_NONE) { + } else if (rgb_matrix_get_flags() == LED_FLAG_CAPS) { /* It's already ON, promote the cause from CAPS to EFFECTS */ - rgb_matrix_set_flags(LED_FLAG_INDICATOR); + rgb_matrix_set_flags(LED_FLAG_EFFECTS); } } #endif // RGB_CONFIRMATION_BLINKING_TIME > 0 From 0f53b2bfb1262b70aa63951d73994424cdf488fb Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Fri, 13 Aug 2021 10:54:21 +0200 Subject: [PATCH 2/2] Validate TIME_SELECTED_BIT --- keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c b/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c index 63c182e56228..b80164043680 100644 --- a/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c +++ b/keyboards/gmmk/pro/ansi/keymaps/andrebrait/keymap.c @@ -92,7 +92,12 @@ static uint8_t r_effect = 0x0, g_effect = 0x0, b_effect = 0x0; static void start_effects(void); /* The higher this is, the slower the blinking will be */ -#define TIME_SELECTED_BIT 8 +#ifndef TIME_SELECTED_BIT + #define TIME_SELECTED_BIT 8 +#endif +#if TIME_SELECTED_BIT < 0 || TIME_SELECTED_BIT >= 16 + #error "TIME_SELECTED_BIT must be a positive integer smaller than 16" +#endif #define effect_red() r_effect = 0xFF, g_effect = 0x0, b_effect = 0x0 #define effect_green() r_effect = 0x0, g_effect = 0xFF, b_effect = 0x0 #endif // RGB_CONFIRMATION_BLINKING_TIME > 0