1
0
Fork 0

VIA V3 - The Custom UI Update (#18222)

This commit is contained in:
Wilba 2022-11-10 07:46:44 +11:00 committed by GitHub
parent 575b0e33fa
commit bc6f8dc8b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
68 changed files with 751 additions and 886 deletions

View file

@ -22,26 +22,6 @@
# 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
#if defined(RGB_MATRIX_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
# define VIA_QMK_RGB_MATRIX_ENABLE
#endif
#include "quantum.h"
#include "via.h"
@ -51,22 +31,8 @@
#include "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
#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#if defined(RGB_MATRIX_ENABLE)
# include <lib/lib8tion/lib8tion.h>
void via_qmk_rgb_matrix_set_value(uint8_t *data);
void via_qmk_rgb_matrix_get_value(uint8_t *data);
void eeconfig_update_rgb_matrix(void);
#endif
// Can be called in an overriding via_init_kb() to test if keyboard level code usage of
@ -155,6 +121,34 @@ void via_set_layout_options(uint32_t value) {
}
}
#if defined(AUDIO_ENABLE)
float via_device_indication_song[][2] = SONG(STARTUP_SOUND);
#endif // AUDIO_ENABLE
// Used by VIA to tell a device to flash LEDs (or do something else) when that
// device becomes the active device being configured, on startup or switching
// between devices. This function will be called six times, at 200ms interval,
// with an incrementing value starting at zero. Since this function is called
// an even number of times, it can call a toggle function and leave things in
// the original state.
__attribute__((weak)) void via_set_device_indication(uint8_t value) {
#if defined(BACKLIGHT_ENABLE)
backlight_toggle();
#endif // BACKLIGHT_ENABLE
#if defined(RGBLIGHT_ENABLE)
rgblight_toggle_noeeprom();
#endif // RGBLIGHT_ENABLE
#if defined(RGB_MATRIX_ENABLE)
rgb_matrix_toggle_noeeprom();
#endif // RGB_MATRIX_ENABLE
#if defined(AUDIO_ENABLE)
if (value == 0) {
wait_ms(10);
PLAY_SONG(via_device_indication_song);
}
#endif // AUDIO_ENABLE
}
// Called by QMK core to process VIA-specific keycodes.
bool process_record_via(uint16_t keycode, keyrecord_t *record) {
// Handle macros
@ -194,24 +188,96 @@ bool process_record_via(uint16_t keycode, keyrecord_t *record) {
return true;
}
// Keyboard level code can override this to handle custom messages from VIA.
// See raw_hid_receive() implementation.
//
// via_custom_value_command() has the default handling of custom values for Core modules.
// If a keyboard is using the default Core modules, it does not need to be overridden,
// the VIA keyboard definition will have matching channel/IDs.
//
// If a keyboard has some extra custom values, then via_custom_value_command_kb() can be
// overridden to handle the extra custom values, leaving via_custom_value_command() to
// handle the custom values for Core modules.
//
// If a keyboard has custom values and code that are overlapping with Core modules,
// then via_custom_value_command() can be overridden and call the same functions
// as the default implementation, or do whatever else is required.
//
// DO NOT call raw_hid_send() in the override function.
__attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
//
// This is the default handler for "extra" custom values, i.e. keyboard-specific custom values
// that are not handled by via_custom_value_command().
__attribute__((weak)) void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value_data ]
uint8_t *command_id = &(data[0]);
*command_id = id_unhandled;
// Return the unhandled state
*command_id = id_unhandled;
}
// VIA handles received HID messages first, and will route to
// raw_hid_receive_kb() for command IDs that are not handled here.
// This gives the keyboard code level the ability to handle the command
// specifically.
// This is the default handler for custom value commands.
// It routes commands with channel IDs to command handlers as such:
//
// raw_hid_send() is called at the end, with the same buffer, which was
// possibly modified with returned values.
// id_qmk_backlight_channel -> via_qmk_backlight_command()
// id_qmk_rgblight_channel -> via_qmk_rgblight_command()
// id_qmk_rgb_matrix_channel -> via_qmk_rgb_matrix_command()
// id_qmk_audio_channel -> via_qmk_audio_command()
//
__attribute__((weak)) void via_custom_value_command(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value_data ]
uint8_t *channel_id = &(data[1]);
#if defined(BACKLIGHT_ENABLE)
if (*channel_id == id_qmk_backlight_channel) {
via_qmk_backlight_command(data, length);
return;
}
#endif // BACKLIGHT_ENABLE
#if defined(RGBLIGHT_ENABLE)
if (*channel_id == id_qmk_rgblight_channel) {
via_qmk_rgblight_command(data, length);
return;
}
#endif // RGBLIGHT_ENABLE
#if defined(RGB_MATRIX_ENABLE)
if (*channel_id == id_qmk_rgb_matrix_channel) {
via_qmk_rgb_matrix_command(data, length);
return;
}
#endif // RGBLIGHT_ENABLE
#if defined(AUDIO_ENABLE)
if (*channel_id == id_qmk_audio_channel) {
via_qmk_audio_command(data, length);
return;
}
#endif // AUDIO_ENABLE
(void)channel_id; // force use of variable
// If we haven't returned before here, then let the keyboard level code
// handle this, if it is overridden, otherwise by default, this will
// return the unhandled state.
via_custom_value_command_kb(data, length);
}
// Keyboard level code can override this, but shouldn't need to.
// Controlling custom features should be done by overriding
// via_custom_value_command_kb() instead.
__attribute__((weak)) bool via_command_kb(uint8_t *data, uint8_t length) {
return false;
}
void raw_hid_receive(uint8_t *data, uint8_t length) {
uint8_t *command_id = &(data[0]);
uint8_t *command_data = &(data[1]);
// If via_command_kb() returns true, the command was fully
// handled, including calling raw_hid_send()
if (via_command_kb(data, length)) {
return;
}
switch (*command_id) {
case id_get_protocol_version: {
command_data[0] = VIA_PROTOCOL_VERSION >> 8;
@ -237,7 +303,10 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
break;
}
case id_switch_matrix_state: {
#if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28)
// Round up to the nearest number of bytes required to hold row state.
// Multiply by number of rows to get the required size in bytes.
// Guard against this being too big for the HID message.
#if (((MATRIX_COLS + 7) / 8) * MATRIX_ROWS <= 28)
uint8_t i = 1;
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
matrix_row_t value = matrix_get_row(row);
@ -255,8 +324,18 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
#endif
break;
}
case id_firmware_version: {
uint32_t value = VIA_FIRMWARE_VERSION;
command_data[1] = (value >> 24) & 0xFF;
command_data[2] = (value >> 16) & 0xFF;
command_data[3] = (value >> 8) & 0xFF;
command_data[4] = value & 0xFF;
break;
}
default: {
raw_hid_receive_kb(data, length);
// The value ID is not known
// Return the unhandled state
*command_id = id_unhandled;
break;
}
}
@ -269,8 +348,15 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
via_set_layout_options(value);
break;
}
case id_device_indication: {
uint8_t value = command_data[1];
via_set_device_indication(value);
break;
}
default: {
raw_hid_receive_kb(data, length);
// The value ID is not known
// Return the unhandled state
*command_id = id_unhandled;
break;
}
}
@ -290,61 +376,10 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
dynamic_keymap_reset();
break;
}
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_QMK_RGB_MATRIX_ENABLE)
via_qmk_rgb_matrix_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) && !defined(VIA_QMK_RGB_MATRIX_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_QMK_RGB_MATRIX_ENABLE)
via_qmk_rgb_matrix_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) && !defined(VIA_QMK_RGB_MATRIX_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_QMK_RGB_MATRIX_ENABLE)
eeconfig_update_rgb_matrix();
#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) && !defined(VIA_QMK_RGB_MATRIX_ENABLE)
// Return the unhandled state
*command_id = id_unhandled;
#endif
case id_custom_set_value:
case id_custom_get_value:
case id_custom_save: {
via_custom_value_command(data, length);
break;
}
#ifdef VIA_EEPROM_ALLOW_RESET
@ -421,13 +456,39 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
raw_hid_send(data, length);
}
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
#if defined(BACKLIGHT_ENABLE)
void via_qmk_backlight_command(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value_data ]
uint8_t *command_id = &(data[0]);
uint8_t *value_id_and_data = &(data[2]);
switch (*command_id) {
case id_custom_set_value: {
via_qmk_backlight_set_value(value_id_and_data);
break;
}
case id_custom_get_value: {
via_qmk_backlight_get_value(value_id_and_data);
break;
}
case id_custom_save: {
via_qmk_backlight_save();
break;
}
default: {
*command_id = id_unhandled;
break;
}
}
}
# if BACKLIGHT_LEVELS == 0
# error BACKLIGHT_LEVELS == 0
# endif
void via_qmk_backlight_get_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
@ -448,6 +509,7 @@ void via_qmk_backlight_get_value(uint8_t *data) {
}
void via_qmk_backlight_set_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
@ -469,14 +531,44 @@ void via_qmk_backlight_set_value(uint8_t *data) {
}
}
#endif // #if defined(VIA_QMK_BACKLIGHT_ENABLE)
void via_qmk_backlight_save(void) {
eeconfig_update_backlight_current();
}
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
#endif // BACKLIGHT_ENABLE
#if defined(RGBLIGHT_ENABLE)
# ifndef RGBLIGHT_LIMIT_VAL
# define RGBLIGHT_LIMIT_VAL 255
# endif
void via_qmk_rgblight_command(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value_data ]
uint8_t *command_id = &(data[0]);
uint8_t *value_id_and_data = &(data[2]);
switch (*command_id) {
case id_custom_set_value: {
via_qmk_rgblight_set_value(value_id_and_data);
break;
}
case id_custom_get_value: {
via_qmk_rgblight_get_value(value_id_and_data);
break;
}
case id_custom_save: {
via_qmk_rgblight_save();
break;
}
default: {
*command_id = id_unhandled;
break;
}
}
}
void via_qmk_rgblight_get_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
@ -485,7 +577,7 @@ void via_qmk_rgblight_get_value(uint8_t *data) {
break;
}
case id_qmk_rgblight_effect: {
value_data[0] = rgblight_get_mode();
value_data[0] = rgblight_is_enabled() ? rgblight_get_mode() : 0;
break;
}
case id_qmk_rgblight_effect_speed: {
@ -501,6 +593,7 @@ void via_qmk_rgblight_get_value(uint8_t *data) {
}
void via_qmk_rgblight_set_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
@ -509,11 +602,11 @@ void via_qmk_rgblight_set_value(uint8_t *data) {
break;
}
case id_qmk_rgblight_effect: {
rgblight_mode_noeeprom(value_data[0]);
if (value_data[0] == 0) {
rgblight_disable_noeeprom();
} else {
rgblight_enable_noeeprom();
rgblight_mode_noeeprom(value_data[0]);
}
break;
}
@ -528,92 +621,168 @@ void via_qmk_rgblight_set_value(uint8_t *data) {
}
}
#endif // #if defined(VIA_QMK_RGBLIGHT_ENABLE)
void via_qmk_rgblight_save(void) {
eeconfig_update_rgblight_current();
}
#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#endif // QMK_RGBLIGHT_ENABLE
#if defined(RGB_MATRIX_ENABLE)
# 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
// VIA supports only 4 discrete values for effect speed; map these to some
// useful speed values for RGB Matrix.
enum speed_values {
RGBLIGHT_SPEED_0 = UINT8_MAX / 16, // not 0 to avoid really slow effects
RGBLIGHT_SPEED_1 = UINT8_MAX / 4,
RGBLIGHT_SPEED_2 = UINT8_MAX / 2, // matches the default value
RGBLIGHT_SPEED_3 = UINT8_MAX / 4 * 3, // UINT8_MAX is really fast
};
void via_qmk_rgb_matrix_command(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value_data ]
uint8_t *command_id = &(data[0]);
uint8_t *value_id_and_data = &(data[2]);
static uint8_t speed_from_rgblight(uint8_t rgblight_speed) {
switch (rgblight_speed) {
case 0:
return RGBLIGHT_SPEED_0;
case 1:
return RGBLIGHT_SPEED_1;
case 2:
default:
return RGBLIGHT_SPEED_2;
case 3:
return RGBLIGHT_SPEED_3;
}
}
static uint8_t speed_to_rgblight(uint8_t rgb_matrix_speed) {
if (rgb_matrix_speed < ((RGBLIGHT_SPEED_0 + RGBLIGHT_SPEED_1) / 2)) {
return 0;
} else if (rgb_matrix_speed < ((RGBLIGHT_SPEED_1 + RGBLIGHT_SPEED_2) / 2)) {
return 1;
} else if (rgb_matrix_speed < ((RGBLIGHT_SPEED_2 + RGBLIGHT_SPEED_3) / 2)) {
return 2;
} else {
return 3;
switch (*command_id) {
case id_custom_set_value: {
via_qmk_rgb_matrix_set_value(value_id_and_data);
break;
}
case id_custom_get_value: {
via_qmk_rgb_matrix_get_value(value_id_and_data);
break;
}
case id_custom_save: {
via_qmk_rgb_matrix_save();
break;
}
default: {
*command_id = id_unhandled;
break;
}
}
}
void via_qmk_rgb_matrix_get_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_rgblight_brightness:
case id_qmk_rgb_matrix_brightness: {
value_data[0] = ((uint16_t)rgb_matrix_get_val() * UINT8_MAX) / RGB_MATRIX_MAXIMUM_BRIGHTNESS;
break;
case id_qmk_rgblight_effect:
value_data[0] = rgb_matrix_get_mode();
}
case id_qmk_rgb_matrix_effect: {
value_data[0] = rgb_matrix_is_enabled() ? rgb_matrix_get_mode() : 0;
break;
case id_qmk_rgblight_effect_speed:
value_data[0] = speed_to_rgblight(rgb_matrix_get_speed());
}
case id_qmk_rgb_matrix_effect_speed: {
value_data[0] = rgb_matrix_get_speed();
break;
case id_qmk_rgblight_color:
}
case id_qmk_rgb_matrix_color: {
value_data[0] = rgb_matrix_get_hue();
value_data[1] = rgb_matrix_get_sat();
break;
}
}
}
void via_qmk_rgb_matrix_set_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_rgblight_brightness:
case id_qmk_rgb_matrix_brightness: {
rgb_matrix_sethsv_noeeprom(rgb_matrix_get_hue(), rgb_matrix_get_sat(), scale8(value_data[0], RGB_MATRIX_MAXIMUM_BRIGHTNESS));
break;
case id_qmk_rgblight_effect:
rgb_matrix_mode_noeeprom(value_data[0]);
}
case id_qmk_rgb_matrix_effect: {
if (value_data[0] == 0) {
rgb_matrix_disable_noeeprom();
} else {
rgb_matrix_enable_noeeprom();
rgb_matrix_mode_noeeprom(value_data[0]);
}
break;
case id_qmk_rgblight_effect_speed:
rgb_matrix_set_speed_noeeprom(speed_from_rgblight(value_data[0]));
}
case id_qmk_rgb_matrix_effect_speed: {
rgblight_set_speed_noeeprom(value_data[0]);
break;
case id_qmk_rgblight_color:
rgb_matrix_sethsv_noeeprom(value_data[0], value_data[1], rgb_matrix_get_val());
}
case id_qmk_rgb_matrix_color: {
rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgb_matrix_get_val());
break;
}
}
}
#endif // #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
void via_qmk_rgb_matrix_save(void) {
eeconfig_update_rgb_matrix();
}
#endif // RGB_MATRIX_ENABLE
#if defined(AUDIO_ENABLE)
extern audio_config_t audio_config;
void via_qmk_audio_command(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value_data ]
uint8_t *command_id = &(data[0]);
uint8_t *value_id_and_data = &(data[2]);
switch (*command_id) {
case id_custom_set_value: {
via_qmk_audio_set_value(value_id_and_data);
break;
}
case id_custom_get_value: {
via_qmk_audio_get_value(value_id_and_data);
break;
}
case id_custom_save: {
via_qmk_audio_save();
break;
}
default: {
*command_id = id_unhandled;
break;
}
}
}
void via_qmk_audio_get_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_audio_enable: {
value_data[0] = audio_config.enable ? 1 : 0;
break;
}
case id_qmk_audio_clicky_enable: {
value_data[0] = audio_config.clicky_enable ? 1 : 0;
break;
}
}
}
void via_qmk_audio_set_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_audio_enable: {
audio_config.enable = value_data[0] ? 1 : 0;
break;
}
case id_qmk_audio_clicky_enable: {
audio_config.clicky_enable = value_data[0] ? 1 : 0;
break;
}
}
}
void via_qmk_audio_save(void) {
eeconfig_update_audio(audio_config.raw);
}
#endif // QMK_AUDIO_ENABLE