From 12ce7feefdd8ca2cc241ad8f48889566c0924e4e Mon Sep 17 00:00:00 2001 From: zvecr Date: Wed, 27 Nov 2024 19:18:05 +0000 Subject: [PATCH] Add generic handling to cycle LED/RGB Matrix flags --- data/constants/keycodes/keycodes_0.0.7.hjson | 0 .../keycodes/keycodes_0.0.7_lighting.hjson | 18 +++++++ data/mappings/info_config.hjson | 1 + data/schemas/keyboard.jsonschema | 5 ++ lib/python/qmk/info.py | 8 +++ quantum/keycodes.h | 8 ++- quantum/process_keycode/process_rgb_matrix.c | 14 +++++ quantum/rgb_matrix/rgb_matrix.c | 51 +++++++++++++++++++ quantum/rgb_matrix/rgb_matrix.h | 4 ++ tests/test_common/keycode_table.cpp | 2 + 10 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 data/constants/keycodes/keycodes_0.0.7.hjson create mode 100644 data/constants/keycodes/keycodes_0.0.7_lighting.hjson diff --git a/data/constants/keycodes/keycodes_0.0.7.hjson b/data/constants/keycodes/keycodes_0.0.7.hjson new file mode 100644 index 00000000000..e69de29bb2d diff --git a/data/constants/keycodes/keycodes_0.0.7_lighting.hjson b/data/constants/keycodes/keycodes_0.0.7_lighting.hjson new file mode 100644 index 00000000000..8635fe5083c --- /dev/null +++ b/data/constants/keycodes/keycodes_0.0.7_lighting.hjson @@ -0,0 +1,18 @@ +{ + "keycodes": { + "0x784D": { + "group": "rgb_matrix", + "key": "QK_RGB_MATRIX_FLAG_NEXT", + "aliases": [ + "RM_FLGN" + ] + }, + "0x784E": { + "group": "rgb_matrix", + "key": "QK_RGB_MATRIX_FLAG_PREVIOUS", + "aliases": [ + "RM_FLGP" + ] + } + } +} diff --git a/data/mappings/info_config.hjson b/data/mappings/info_config.hjson index 8d3e3c7f0ee..a2fe16a13ec 100644 --- a/data/mappings/info_config.hjson +++ b/data/mappings/info_config.hjson @@ -135,6 +135,7 @@ // RGB Matrix "RGB_MATRIX_CENTER": {"info_key": "rgb_matrix.center_point", "value_type": "array.int"}, + "RGB_MATRIX_FLAG_STEPS": {"info_key": "rgb_matrix.flag_steps", "value_type": "array.int"}, "RGB_MATRIX_HUE_STEP": {"info_key": "rgb_matrix.hue_steps", "value_type": "int"}, "RGB_MATRIX_KEYRELEASES": {"info_key": "rgb_matrix.react_on_keyup", "value_type": "flag"}, "RGB_MATRIX_LED_FLUSH_LIMIT": {"info_key": "rgb_matrix.led_flush_limit", "value_type": "int"}, diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema index ec87680fa05..3c4b1de503b 100644 --- a/data/schemas/keyboard.jsonschema +++ b/data/schemas/keyboard.jsonschema @@ -608,6 +608,11 @@ "maxItems": 2, "items": {"$ref": "qmk.definitions.v1#/unsigned_int_8"} }, + "flag_steps": { + "type": "array", + "minItems": 1, + "items": {"$ref": "qmk.definitions.v1#/unsigned_int_8"} + }, "max_brightness": {"$ref": "qmk.definitions.v1#/unsigned_int_8"}, "timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"}, "hue_steps": {"$ref": "qmk.definitions.v1#/unsigned_int"}, diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index 5dae353538a..2600196ced1 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -794,6 +794,14 @@ def _extract_led_config(info_data, keyboard): if info_data[feature].get('layout', None) and not info_data[feature].get('led_count', None): info_data[feature]['led_count'] = len(info_data[feature]['layout']) + if info_data[feature].get('layout', None) and not info_data[feature].get('flag_steps', None): + flags = {0xFF, 0} + # if only a single flag is used, assume only all+none flags + unique_flags = set(x.get('flags', 0) for x in info_data[feature]['layout']) + if len(unique_flags) > 1: + flags.update(unique_flags) + info_data[feature]['flag_steps'] = sorted(list(flags), reverse=True) + return info_data diff --git a/quantum/keycodes.h b/quantum/keycodes.h index 921dc7199f2..5bcf1405bb3 100644 --- a/quantum/keycodes.h +++ b/quantum/keycodes.h @@ -689,6 +689,8 @@ enum qk_keycode_defines { QK_RGB_MATRIX_VALUE_DOWN = 0x784A, QK_RGB_MATRIX_SPEED_UP = 0x784B, QK_RGB_MATRIX_SPEED_DOWN = 0x784C, + QK_RGB_MATRIX_FLAG_NEXT = 0x784D, + QK_RGB_MATRIX_FLAG_PREVIOUS = 0x784E, QK_BOOTLOADER = 0x7C00, QK_REBOOT = 0x7C01, QK_DEBUG_TOGGLE = 0x7C02, @@ -1378,6 +1380,8 @@ enum qk_keycode_defines { RM_VALD = QK_RGB_MATRIX_VALUE_DOWN, RM_SPDU = QK_RGB_MATRIX_SPEED_UP, RM_SPDD = QK_RGB_MATRIX_SPEED_DOWN, + RM_FLGN = QK_RGB_MATRIX_FLAG_NEXT, + RM_FLGP = QK_RGB_MATRIX_FLAG_PREVIOUS, QK_BOOT = QK_BOOTLOADER, QK_RBT = QK_REBOOT, DB_TOGG = QK_DEBUG_TOGGLE, @@ -1505,7 +1509,7 @@ enum qk_keycode_defines { #define IS_LED_MATRIX_KEYCODE(code) ((code) >= QK_LED_MATRIX_ON && (code) <= QK_LED_MATRIX_SPEED_DOWN) #define IS_UNDERGLOW_KEYCODE(code) ((code) >= QK_UNDERGLOW_TOGGLE && (code) <= QK_UNDERGLOW_SPEED_DOWN) #define IS_RGB_KEYCODE(code) ((code) >= RGB_MODE_PLAIN && (code) <= RGB_MODE_TWINKLE) -#define IS_RGB_MATRIX_KEYCODE(code) ((code) >= QK_RGB_MATRIX_ON && (code) <= QK_RGB_MATRIX_SPEED_DOWN) +#define IS_RGB_MATRIX_KEYCODE(code) ((code) >= QK_RGB_MATRIX_ON && (code) <= QK_RGB_MATRIX_FLAG_PREVIOUS) #define IS_QUANTUM_KEYCODE(code) ((code) >= QK_BOOTLOADER && (code) <= QK_LAYER_LOCK) #define IS_KB_KEYCODE(code) ((code) >= QK_KB_0 && (code) <= QK_KB_31) #define IS_USER_KEYCODE(code) ((code) >= QK_USER_0 && (code) <= QK_USER_31) @@ -1531,7 +1535,7 @@ enum qk_keycode_defines { #define LED_MATRIX_KEYCODE_RANGE QK_LED_MATRIX_ON ... QK_LED_MATRIX_SPEED_DOWN #define UNDERGLOW_KEYCODE_RANGE QK_UNDERGLOW_TOGGLE ... QK_UNDERGLOW_SPEED_DOWN #define RGB_KEYCODE_RANGE RGB_MODE_PLAIN ... RGB_MODE_TWINKLE -#define RGB_MATRIX_KEYCODE_RANGE QK_RGB_MATRIX_ON ... QK_RGB_MATRIX_SPEED_DOWN +#define RGB_MATRIX_KEYCODE_RANGE QK_RGB_MATRIX_ON ... QK_RGB_MATRIX_FLAG_PREVIOUS #define QUANTUM_KEYCODE_RANGE QK_BOOTLOADER ... QK_LAYER_LOCK #define KB_KEYCODE_RANGE QK_KB_0 ... QK_KB_31 #define USER_KEYCODE_RANGE QK_USER_0 ... QK_USER_31 diff --git a/quantum/process_keycode/process_rgb_matrix.c b/quantum/process_keycode/process_rgb_matrix.c index fd2aa1a0c73..c18212294db 100644 --- a/quantum/process_keycode/process_rgb_matrix.c +++ b/quantum/process_keycode/process_rgb_matrix.c @@ -94,6 +94,20 @@ bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { rgb_matrix_decrease_speed(); } return false; + case QK_RGB_MATRIX_FLAG_NEXT: + if (shifted) { + rgb_matrix_flags_step_reverse(); + } else { + rgb_matrix_flags_step(); + } + return false; + case QK_RGB_MATRIX_FLAG_PREVIOUS: + if (shifted) { + rgb_matrix_flags_step(); + } else { + rgb_matrix_flags_step_reverse(); + } + return false; } } diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c index 484d177546a..55acc4111bd 100644 --- a/quantum/rgb_matrix/rgb_matrix.c +++ b/quantum/rgb_matrix/rgb_matrix.c @@ -70,6 +70,13 @@ uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}}; last_hit_t g_last_hit_tracker; #endif // RGB_MATRIX_KEYREACTIVE_ENABLED +#ifndef RGB_MATRIX_FLAG_STEPS +# define RGB_MATRIX_FLAG_STEPS \ + { LED_FLAG_ALL, LED_FLAG_KEYLIGHT | LED_FLAG_MODIFIER, LED_FLAG_UNDERGLOW, LED_FLAG_NONE } +#endif +static const uint8_t rgb_matrix_flag_steps[] = RGB_MATRIX_FLAG_STEPS; +#define rgb_matrix_flag_steps_count ARRAY_SIZE(rgb_matrix_flag_steps) + // internals static bool suspend_state = false; static uint8_t rgb_last_enable = UINT8_MAX; @@ -717,3 +724,47 @@ void rgb_matrix_set_flags(led_flags_t flags) { void rgb_matrix_set_flags_noeeprom(led_flags_t flags) { rgb_matrix_set_flags_eeprom_helper(flags, false); } + +void rgb_matrix_flags_step_helper(bool write_to_eeprom) { + led_flags_t flags = rgb_matrix_get_flags(); + + uint8_t next = 0; + for (uint8_t i = 0; i < rgb_matrix_flag_steps_count; i++) { + if (rgb_matrix_flag_steps[i] == flags) { + next = i == rgb_matrix_flag_steps_count - 1 ? 0 : i + 1; + break; + } + } + + rgb_matrix_set_flags_eeprom_helper(rgb_matrix_flag_steps[next], write_to_eeprom); +} + +void rgb_matrix_flags_step_noeeprom(void) { + rgb_matrix_flags_step_helper(false); +} + +void rgb_matrix_flags_step(void) { + rgb_matrix_flags_step_helper(true); +} + +void rgb_matrix_flags_step_reverse_helper(bool write_to_eeprom) { + led_flags_t flags = rgb_matrix_get_flags(); + + uint8_t next = 0; + for (uint8_t i = 0; i < rgb_matrix_flag_steps_count; i++) { + if (rgb_matrix_flag_steps[i] == flags) { + next = i == 0 ? rgb_matrix_flag_steps_count - 1 : i - 1; + break; + } + } + + rgb_matrix_set_flags_eeprom_helper(rgb_matrix_flag_steps[next], write_to_eeprom); +} + +void rgb_matrix_flags_step_reverse_noeeprom(void) { + rgb_matrix_flags_step_reverse_helper(false); +} + +void rgb_matrix_flags_step_reverse(void) { + rgb_matrix_flags_step_reverse_helper(true); +} diff --git a/quantum/rgb_matrix/rgb_matrix.h b/quantum/rgb_matrix/rgb_matrix.h index 33f7e06a639..0b97503e438 100644 --- a/quantum/rgb_matrix/rgb_matrix.h +++ b/quantum/rgb_matrix/rgb_matrix.h @@ -212,6 +212,10 @@ void rgb_matrix_decrease_speed_noeeprom(void); led_flags_t rgb_matrix_get_flags(void); void rgb_matrix_set_flags(led_flags_t flags); void rgb_matrix_set_flags_noeeprom(led_flags_t flags); +void rgb_matrix_flags_step_noeeprom(void); +void rgb_matrix_flags_step(void); +void rgb_matrix_flags_step_reverse_noeeprom(void); +void rgb_matrix_flags_step_reverse(void); void rgb_matrix_update_pwm_buffers(void); #ifndef RGBLIGHT_ENABLE diff --git a/tests/test_common/keycode_table.cpp b/tests/test_common/keycode_table.cpp index ae1a9edcd0f..5e6f73a8204 100644 --- a/tests/test_common/keycode_table.cpp +++ b/tests/test_common/keycode_table.cpp @@ -627,6 +627,8 @@ std::map KEYCODE_ID_TABLE = { {QK_RGB_MATRIX_VALUE_DOWN, "QK_RGB_MATRIX_VALUE_DOWN"}, {QK_RGB_MATRIX_SPEED_UP, "QK_RGB_MATRIX_SPEED_UP"}, {QK_RGB_MATRIX_SPEED_DOWN, "QK_RGB_MATRIX_SPEED_DOWN"}, + {QK_RGB_MATRIX_FLAG_NEXT, "QK_RGB_MATRIX_FLAG_NEXT"}, + {QK_RGB_MATRIX_FLAG_PREVIOUS, "QK_RGB_MATRIX_FLAG_PREVIOUS"}, {QK_BOOTLOADER, "QK_BOOTLOADER"}, {QK_REBOOT, "QK_REBOOT"}, {QK_DEBUG_TOGGLE, "QK_DEBUG_TOGGLE"},