diff options
-rw-r--r-- | quantum/backlight/backlight.c | 20 | ||||
-rw-r--r-- | quantum/backlight/backlight.h | 2 | ||||
-rw-r--r-- | quantum/rgblight.c | 20 | ||||
-rw-r--r-- | quantum/rgblight.h | 6 | ||||
-rw-r--r-- | quantum/via.c | 185 | ||||
-rw-r--r-- | quantum/via.h | 67 |
6 files changed, 262 insertions, 38 deletions
diff --git a/quantum/backlight/backlight.c b/quantum/backlight/backlight.c index e57b31d103..4aa74667d9 100644 --- a/quantum/backlight/backlight.c +++ b/quantum/backlight/backlight.c @@ -130,18 +130,30 @@ void backlight_step(void) { backlight_set(backlight_config.level); } -/** \brief Backlight set level +/** \brief Backlight set level without EEPROM update * - * FIXME: needs doc */ -void backlight_level(uint8_t level) { +void backlight_level_noeeprom(uint8_t level) { if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; backlight_config.level = level; backlight_config.enable = !!backlight_config.level; - eeconfig_update_backlight(backlight_config.raw); backlight_set(backlight_config.level); } +/** \brief Backlight set level + * + * FIXME: needs doc + */ +void backlight_level(uint8_t level) { + backlight_level_noeeprom(level); + eeconfig_update_backlight(backlight_config.raw); +} + +/** \brief Update current backlight state to EEPROM + * + */ +void eeconfig_update_backlight_current(void) { eeconfig_update_backlight(backlight_config.raw); } + /** \brief Get backlight level * * FIXME: needs doc diff --git a/quantum/backlight/backlight.h b/quantum/backlight/backlight.h index 9f0a5e81d7..08acf942ff 100644 --- a/quantum/backlight/backlight.h +++ b/quantum/backlight/backlight.h @@ -48,8 +48,10 @@ bool is_backlight_enabled(void); void backlight_step(void); void backlight_increase(void); void backlight_decrease(void); +void backlight_level_noeeprom(uint8_t level); void backlight_level(uint8_t level); uint8_t get_backlight_level(void); +void eeconfig_update_backlight_current(void); // implementation specific void backlight_init_ports(void); diff --git a/quantum/rgblight.c b/quantum/rgblight.c index e0ed30879b..643a9eaaf2 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -162,6 +162,10 @@ void eeconfig_update_rgblight(uint32_t val) { #endif } +void eeconfig_update_rgblight_current(void) { + eeconfig_update_rgblight(rgblight_config.raw); +} + void eeconfig_update_rgblight_default(void) { rgblight_config.enable = 1; rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT; @@ -503,6 +507,22 @@ void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_ee void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, false); } +uint8_t rgblight_get_speed(void) { return rgblight_config.speed; } + +void rgblight_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) { + rgblight_config.speed = speed; + if (write_to_eeprom) { + eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this + dprintf("rgblight set speed [EEPROM]: %u\n", rgblight_config.speed); + } else { + dprintf("rgblight set speed [NOEEPROM]: %u\n", rgblight_config.speed); + } +} + +void rgblight_set_speed(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, true); } + +void rgblight_set_speed_noeeprom(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, false); } + uint8_t rgblight_get_hue(void) { return rgblight_config.hue; } uint8_t rgblight_get_sat(void) { return rgblight_config.sat; } diff --git a/quantum/rgblight.h b/quantum/rgblight.h index e3aa098e4d..f6746e50ff 100644 --- a/quantum/rgblight.h +++ b/quantum/rgblight.h @@ -233,6 +233,11 @@ void rgblight_decrease_speed(void); void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val); void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val); +/* effect speed */ +uint8_t rgblight_get_speed(void); +void rgblight_set_speed(uint8_t speed); +void rgblight_set_speed_noeeprom(uint8_t speed); + /* query */ uint8_t rgblight_get_mode(void); uint8_t rgblight_get_hue(void); @@ -245,6 +250,7 @@ uint32_t rgblight_read_dword(void); void rgblight_update_dword(uint32_t dword); uint32_t eeconfig_read_rgblight(void); void eeconfig_update_rgblight(uint32_t val); +void eeconfig_update_rgblight_current(void); void eeconfig_update_rgblight_default(void); void eeconfig_debug_rgblight(void); diff --git a/quantum/via.c b/quantum/via.c index 64b05324a1..2a47b9ee94 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -22,14 +22,42 @@ # error "DYNAMIC_KEYMAP_ENABLE is not enabled" #endif +// If VIA_CUSTOM_LIGHTING_ENABLE is not defined, then VIA_QMK_BACKLIGHT_ENABLE is set +// if BACKLIGHT_ENABLE is set, so handling of QMK Backlight values happens here by default. +// if VIA_CUSTOM_LIGHTING_ENABLE is defined, then VIA_QMK_BACKLIGHT_ENABLE must be explicitly +// set in keyboard-level config.h, so handling of QMK Backlight values happens here +#if defined(BACKLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) +# define VIA_QMK_BACKLIGHT_ENABLE +#endif + +// If VIA_CUSTOM_LIGHTING_ENABLE is not defined, then VIA_QMK_RGBLIGHT_ENABLE is set +// if RGBLIGHT_ENABLE is set, so handling of QMK RGBLIGHT values happens here by default. +// If VIA_CUSTOM_LIGHTING_ENABLE is defined, then VIA_QMK_RGBLIGHT_ENABLE must be explicitly +// set in keyboard-level config.h, so handling of QMK RGBLIGHT values happens here +#if defined(RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) +# define VIA_QMK_RGBLIGHT_ENABLE +#endif + #include "quantum.h" #include "via.h" + #include "raw_hid.h" #include "dynamic_keymap.h" #include "tmk_core/common/eeprom.h" #include "version.h" // for QMK_BUILDDATE used in EEPROM magic +// Forward declare some helpers. +#if defined(VIA_QMK_BACKLIGHT_ENABLE) +void via_qmk_backlight_set_value(uint8_t *data); +void via_qmk_backlight_get_value(uint8_t *data); +#endif + +#if defined(VIA_QMK_RGBLIGHT_ENABLE) +void via_qmk_rgblight_set_value(uint8_t *data); +void via_qmk_rgblight_get_value(uint8_t *data); +#endif + // Can be called in an overriding via_init_kb() to test if keyboard level code usage of // EEPROM is invalid and use/save defaults. bool via_eeprom_is_valid(void) @@ -211,7 +239,7 @@ __attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) { // This gives the keyboard code level the ability to handle the command // specifically. // -// raw_hid_send() is called at the end, with the same buffer, which was +// raw_hid_send() is called at the end, with the same buffer, which was // possibly modified with returned values. void raw_hid_receive( uint8_t *data, uint8_t length ) { @@ -313,11 +341,52 @@ void raw_hid_receive( uint8_t *data, uint8_t length ) dynamic_keymap_reset(); break; } - case id_backlight_config_set_value: - case id_backlight_config_get_value: - case id_backlight_config_save: - { + case id_lighting_set_value: { +#if defined(VIA_QMK_BACKLIGHT_ENABLE) + via_qmk_backlight_set_value(command_data); +#endif +#if defined(VIA_QMK_RGBLIGHT_ENABLE) + via_qmk_rgblight_set_value(command_data); +#endif +#if defined(VIA_CUSTOM_LIGHTING_ENABLE) + raw_hid_receive_kb(data, length); +#endif +#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) + // Return the unhandled state + *command_id = id_unhandled; +#endif + break; + } + case id_lighting_get_value: { +#if defined(VIA_QMK_BACKLIGHT_ENABLE) + via_qmk_backlight_get_value(command_data); +#endif +#if defined(VIA_QMK_RGBLIGHT_ENABLE) + via_qmk_rgblight_get_value(command_data); +#endif +#if defined(VIA_CUSTOM_LIGHTING_ENABLE) + raw_hid_receive_kb(data, length); +#endif +#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) + // Return the unhandled state + *command_id = id_unhandled; +#endif + break; + } + case id_lighting_save: { +#if defined(VIA_QMK_BACKLIGHT_ENABLE) + eeconfig_update_backlight_current(); +#endif +#if defined(VIA_QMK_RGBLIGHT_ENABLE) + eeconfig_update_rgblight_current(); +#endif +#if defined(VIA_CUSTOM_LIGHTING_ENABLE) raw_hid_receive_kb(data, length); +#endif +#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) + // Return the unhandled state + *command_id = id_unhandled; +#endif break; } case id_dynamic_keymap_macro_get_count: @@ -398,3 +467,109 @@ void raw_hid_receive( uint8_t *data, uint8_t length ) // (i.e. returning state to the host, or the unhandled state). raw_hid_send( data, length ); } + +#if defined(VIA_QMK_BACKLIGHT_ENABLE) + +# if BACKLIGHT_LEVELS == 0 +# error BACKLIGHT_LEVELS == 0 +# endif + +void via_qmk_backlight_get_value(uint8_t *data) { + uint8_t *value_id = &(data[0]); + uint8_t *value_data = &(data[1]); + switch (*value_id) { + case id_qmk_backlight_brightness: { + // level / BACKLIGHT_LEVELS * 255 + value_data[0] = ((uint16_t)get_backlight_level()) * 255 / BACKLIGHT_LEVELS; + break; + } + case id_qmk_backlight_effect: { +# ifdef BACKLIGHT_BREATHING + value_data[0] = is_backlight_breathing() ? 1 : 0; +# else + value_data[0] = 0; +# endif + break; + } + } +} + +void via_qmk_backlight_set_value(uint8_t *data) { + uint8_t *value_id = &(data[0]); + uint8_t *value_data = &(data[1]); + switch (*value_id) { + case id_qmk_backlight_brightness: { + // level / 255 * BACKLIGHT_LEVELS + backlight_level_noeeprom(((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255); + break; + } + case id_qmk_backlight_effect: { +# ifdef BACKLIGHT_BREATHING + if (value_data[0] == 0) { + backlight_disable_breathing(); + } else { + backlight_enable_breathing(); + } +# endif + break; + } + } +} + +#endif // #if defined(VIA_QMK_BACKLIGHT_ENABLE) + +#if defined(VIA_QMK_RGBLIGHT_ENABLE) + +void via_qmk_rgblight_get_value(uint8_t *data) { + uint8_t *value_id = &(data[0]); + uint8_t *value_data = &(data[1]); + switch (*value_id) { + case id_qmk_rgblight_brightness: { + value_data[0] = rgblight_get_val(); + break; + } + case id_qmk_rgblight_effect: { + value_data[0] = rgblight_get_mode(); + break; + } + case id_qmk_rgblight_effect_speed: { + value_data[0] = rgblight_get_speed(); + break; + } + case id_qmk_rgblight_color: { + value_data[0] = rgblight_get_hue(); + value_data[1] = rgblight_get_sat(); + break; + } + } +} + +void via_qmk_rgblight_set_value(uint8_t *data) { + uint8_t *value_id = &(data[0]); + uint8_t *value_data = &(data[1]); + switch (*value_id) { + case id_qmk_rgblight_brightness: { + rgblight_sethsv_noeeprom(rgblight_get_hue(), rgblight_get_sat(), value_data[0]); + break; + } + case id_qmk_rgblight_effect: { + rgblight_mode_noeeprom(value_data[0]); + if (value_data[0] == 0) { + rgblight_disable_noeeprom(); + } else { + rgblight_enable_noeeprom(); + } + break; + } + case id_qmk_rgblight_effect_speed: { + rgblight_set_speed_noeeprom(value_data[0]); + break; + } + case id_qmk_rgblight_color: { + rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgblight_get_val()); + break; + } + } +} + +#endif // #if defined(VIA_QMK_RGBLIGHT_ENABLE) diff --git a/quantum/via.h b/quantum/via.h index f9a8017b24..aaa01705e3 100644 --- a/quantum/via.h +++ b/quantum/via.h @@ -47,38 +47,48 @@ #endif // This is changed only when the command IDs change, -// so VIA Configurator can detect compatible firmware. +// so VIA Configurator can detect compatible firmware. #define VIA_PROTOCOL_VERSION 0x0009 -enum via_command_id -{ - id_get_protocol_version = 0x01, // always 0x01 - id_get_keyboard_value, - id_set_keyboard_value, - id_dynamic_keymap_get_keycode, - id_dynamic_keymap_set_keycode, - id_dynamic_keymap_reset, - id_backlight_config_set_value, - id_backlight_config_get_value, - id_backlight_config_save, - id_eeprom_reset, - id_bootloader_jump, - id_dynamic_keymap_macro_get_count, - id_dynamic_keymap_macro_get_buffer_size, - id_dynamic_keymap_macro_get_buffer, - id_dynamic_keymap_macro_set_buffer, - id_dynamic_keymap_macro_reset, - id_dynamic_keymap_get_layer_count, - id_dynamic_keymap_get_buffer, - id_dynamic_keymap_set_buffer, - id_unhandled = 0xFF, +enum via_command_id { + id_get_protocol_version = 0x01, // always 0x01 + id_get_keyboard_value = 0x02, + id_set_keyboard_value = 0x03, + id_dynamic_keymap_get_keycode = 0x04, + id_dynamic_keymap_set_keycode = 0x05, + id_dynamic_keymap_reset = 0x06, + id_lighting_set_value = 0x07, + id_lighting_get_value = 0x08, + id_lighting_save = 0x09, + id_eeprom_reset = 0x0A, + id_bootloader_jump = 0x0B, + id_dynamic_keymap_macro_get_count = 0x0C, + id_dynamic_keymap_macro_get_buffer_size = 0x0D, + id_dynamic_keymap_macro_get_buffer = 0x0E, + id_dynamic_keymap_macro_set_buffer = 0x0F, + id_dynamic_keymap_macro_reset = 0x10, + id_dynamic_keymap_get_layer_count = 0x11, + id_dynamic_keymap_get_buffer = 0x12, + id_dynamic_keymap_set_buffer = 0x13, + id_unhandled = 0xFF, }; -enum via_keyboard_value_id -{ - id_uptime = 0x01, - id_layout_options, - id_switch_matrix_state +enum via_keyboard_value_id { + id_uptime = 0x01, // + id_layout_options = 0x02, + id_switch_matrix_state = 0x03 +}; + +enum via_lighting_value { + // QMK BACKLIGHT + id_qmk_backlight_brightness = 0x09, + id_qmk_backlight_effect = 0x0A, + + // QMK RGBLIGHT + id_qmk_rgblight_brightness = 0x80, + id_qmk_rgblight_effect = 0x81, + id_qmk_rgblight_effect_speed = 0x82, + id_qmk_rgblight_color = 0x83, }; // Can't use SAFE_RANGE here, it might change if someone adds @@ -148,4 +158,3 @@ void via_set_layout_options(uint32_t value); // Called by QMK core to process VIA-specific keycodes. bool process_record_via(uint16_t keycode, keyrecord_t *record); - |