Skip to content

Commit

Permalink
Refactoring the command handlers, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
wilba committed Oct 10, 2022
1 parent 35fc1f1 commit ac6417a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 57 deletions.
63 changes: 34 additions & 29 deletions quantum/via.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "version.h" // for QMK_BUILDDATE used in EEPROM magic
#include "via_ensure_keycode.h"

#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#if defined(RGB_MATRIX_ENABLE)
# include <lib/lib8tion/lib8tion.h>
#endif

Expand Down Expand Up @@ -122,9 +122,9 @@ void via_set_layout_options(uint32_t value) {
}
}

#if defined(VIA_QMK_AUDIO_ENABLE)
#if defined(AUDIO_ENABLE)
float via_device_indication_song[][2] = SONG(STARTUP_SOUND);
#endif
#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
Expand All @@ -133,21 +133,21 @@ float via_device_indication_song[][2] = SONG(STARTUP_SOUND);
// 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(VIA_QMK_BACKLIGHT_ENABLE)
#if defined(BACKLIGHT_ENABLE)
backlight_toggle();
#endif // VIA_QMK_BACKLIGHT_ENABLE
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
#endif // BACKLIGHT_ENABLE
#if defined(RGBLIGHT_ENABLE)
rgblight_toggle_noeeprom();
#endif
#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#endif // RGBLIGHT_ENABLE
#if defined(RGB_MATRIX_ENABLE)
rgb_matrix_toggle_noeeprom();
#endif
#if defined(VIA_QMK_AUDIO_ENABLE)
#endif // RGB_MATRIX_ENABLE
#if defined(AUDIO_ENABLE)
if (value == 0) {
wait_ms(10);
PLAY_SONG(via_device_indication_song);
}
#endif
#endif // AUDIO_ENABLE
}

// Called by QMK core to process VIA-specific keycodes.
Expand Down Expand Up @@ -215,39 +215,44 @@ __attribute__((weak)) void via_custom_value_command_kb(uint8_t *data, uint8_t le
}

// This is the default handler for custom value commands.
// It routes channel VIA_QMK_BACKLIGHT_CHANNEL to via_qmk_backlight_command() and
// channel VIA_QMK_RGBLIGHT_CHANNEL to via_qmk_rgblight_command().
// It routes commands with channel IDs to command handlers as such:
//
// 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(VIA_QMK_BACKLIGHT_ENABLE)
#if defined(BACKLIGHT_ENABLE)
if (*channel_id == id_qmk_backlight_channel) {
via_qmk_backlight_command(data, length);
return;
}
#endif // VIA_QMK_BACKLIGHT_ENABLE
#endif // BACKLIGHT_ENABLE

#if defined(VIA_QMK_RGBLIGHT_ENABLE)
#if defined(RGBLIGHT_ENABLE)
if (*channel_id == id_qmk_rgblight_channel) {
via_qmk_rgblight_command(data, length);
return;
}
#endif // VIA_QMK_RGBLIGHT_ENABLE
#endif // RGBLIGHT_ENABLE

#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#if defined(RGB_MATRIX_ENABLE)
if (*channel_id == id_qmk_rgb_matrix_channel) {
via_qmk_rgb_matrix_command(data, length);
return;
}
#endif // VIA_QMK_RGBLIGHT_ENABL
#endif // RGBLIGHT_ENABL

#if defined(VIA_QMK_AUDIO_ENABLE)
#if defined(AUDIO_ENABLE)
if (*channel_id == id_qmk_audio_channel) {
via_qmk_audio_command(data, length);
return;
}
#endif // VIA_QMK_AUDIO_ENABLE
#endif // AUDIO_ENABLE

*channel_id = *channel_id; // force use of variable

Expand Down Expand Up @@ -452,7 +457,7 @@ 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 ]
Expand Down Expand Up @@ -531,9 +536,9 @@ void via_qmk_backlight_save(void) {
eeconfig_update_backlight_current();
}

#endif // #if defined(VIA_QMK_BACKLIGHT_ENABLE)
#endif // BACKLIGHT_ENABLE

#if defined(VIA_QMK_RGBLIGHT_ENABLE)
#if defined(RGBLIGHT_ENABLE)
# ifndef RGBLIGHT_LIMIT_VAL
# define RGBLIGHT_LIMIT_VAL 255
# endif
Expand Down Expand Up @@ -621,9 +626,9 @@ void via_qmk_rgblight_save(void) {
eeconfig_update_rgblight_current();
}

#endif // #if defined(VIA_QMK_RGBLIGHT_ENABLE)
#endif // QMK_RGBLIGHT_ENABLE

#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#if defined(RGB_MATRIX_ENABLE)

# if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
# undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
Expand Down Expand Up @@ -714,9 +719,9 @@ void via_qmk_rgb_matrix_save(void) {
eeconfig_update_rgb_matrix();
}

#endif // #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#endif // RGB_MATRIX_ENABLE

#if defined(VIA_QMK_AUDIO_ENABLE)
#if defined(AUDIO_ENABLE)

extern audio_config_t audio_config;

Expand Down Expand Up @@ -781,4 +786,4 @@ void via_qmk_audio_save(void) {
eeconfig_update_audio(audio_config.raw);
}

#endif // #if defined(VIA_QMK_AUDIO_ENABLE)
#endif // QMK_AUDIO_ENABLE
66 changes: 38 additions & 28 deletions quantum/via.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,6 @@
# define VIA_FIRMWARE_VERSION 0x00000000
#endif

// Enable the VIA command handlers for QMK Core features if those features
// are enabled.
#if defined(BACKLIGHT_ENABLE)
# define VIA_QMK_BACKLIGHT_ENABLE
#endif

#if defined(RGBLIGHT_ENABLE)
# define VIA_QMK_RGBLIGHT_ENABLE
#endif

#if defined(RGB_MATRIX_ENABLE)
# define VIA_QMK_RGB_MATRIX_ENABLE
#endif

#if defined(AUDIO_ENABLE)
# define VIA_QMK_AUDIO_ENABLE
#endif

enum via_command_id {
id_get_protocol_version = 0x01, // always 0x01
id_get_keyboard_value = 0x02,
Expand All @@ -113,17 +95,45 @@ enum via_command_id {
id_unhandled = 0xFF,
};

enum via_keyboard_value_id { id_uptime = 0x01, id_layout_options = 0x02, id_switch_matrix_state = 0x03, id_firmware_version = 0x04, id_device_indication = 0x05 };
enum via_keyboard_value_id {
id_uptime = 0x01,
id_layout_options = 0x02,
id_switch_matrix_state = 0x03,
id_firmware_version = 0x04,
id_device_indication = 0x05,
};

enum via_channel_id { id_custom_channel = 0, id_qmk_backlight_channel = 1, id_qmk_rgblight_channel = 2, id_qmk_rgb_matrix_channel = 3, id_qmk_audio_channel = 4 };
enum via_channel_id {
id_custom_channel = 0,
id_qmk_backlight_channel = 1,
id_qmk_rgblight_channel = 2,
id_qmk_rgb_matrix_channel = 3,
id_qmk_audio_channel = 4,
};

enum via_qmk_backlight_value { id_qmk_backlight_brightness = 1, id_qmk_backlight_effect = 2 };
enum via_qmk_backlight_value {
id_qmk_backlight_brightness = 1,
id_qmk_backlight_effect = 2,
};

enum via_qmk_rgblight_value { id_qmk_rgblight_brightness = 1, id_qmk_rgblight_effect = 2, id_qmk_rgblight_effect_speed = 3, id_qmk_rgblight_color = 4 };
enum via_qmk_rgblight_value {
id_qmk_rgblight_brightness = 1,
id_qmk_rgblight_effect = 2,
id_qmk_rgblight_effect_speed = 3,
id_qmk_rgblight_color = 4,
};

enum via_qmk_rgb_matrix_value { id_qmk_rgb_matrix_brightness = 1, id_qmk_rgb_matrix_effect = 2, id_qmk_rgb_matrix_effect_speed = 3, id_qmk_rgb_matrix_color = 4 };
enum via_qmk_rgb_matrix_value {
id_qmk_rgb_matrix_brightness = 1,
id_qmk_rgb_matrix_effect = 2,
id_qmk_rgb_matrix_effect_speed = 3,
id_qmk_rgb_matrix_color = 4,
};

enum via_qmk_audio_value { id_qmk_audio_enable = 1, id_qmk_audio_clicky_enable = 2 };
enum via_qmk_audio_value {
id_qmk_audio_enable = 1,
id_qmk_audio_clicky_enable = 2,
};

// Can't use SAFE_RANGE here, it might change if someone adds
// new values to enum quantum_keycodes.
Expand Down Expand Up @@ -196,28 +206,28 @@ void via_set_device_indication(uint8_t value);
bool process_record_via(uint16_t keycode, keyrecord_t *record);

// These are made external so that keyboard level custom value handlers can use them.
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
#if defined(BACKLIGHT_ENABLE)
void via_qmk_backlight_command(uint8_t *data, uint8_t length);
void via_qmk_backlight_set_value(uint8_t *data);
void via_qmk_backlight_get_value(uint8_t *data);
void via_qmk_backlight_save(void);
#endif

#if defined(VIA_QMK_RGBLIGHT_ENABLE)
#if defined(RGBLIGHT_ENABLE)
void via_qmk_rgblight_command(uint8_t *data, uint8_t length);
void via_qmk_rgblight_set_value(uint8_t *data);
void via_qmk_rgblight_get_value(uint8_t *data);
void via_qmk_rgblight_save(void);
#endif

#if defined(VIA_QMK_RGB_MATRIX_ENABLE)
#if defined(RGB_MATRIX_ENABLE)
void via_qmk_rgb_matrix_command(uint8_t *data, uint8_t length);
void via_qmk_rgb_matrix_set_value(uint8_t *data);
void via_qmk_rgb_matrix_get_value(uint8_t *data);
void via_qmk_rgb_matrix_save(void);
#endif

#if defined(VIA_QMK_AUDIO_ENABLE)
#if defined(AUDIO_ENABLE)
void via_qmk_audio_command(uint8_t *data, uint8_t length);
void via_qmk_audio_set_value(uint8_t *data);
void via_qmk_audio_get_value(uint8_t *data);
Expand Down

0 comments on commit ac6417a

Please sign in to comment.