From 5ac8a005b9b05e5f96a153165da553208feb3116 Mon Sep 17 00:00:00 2001 From: era Date: Tue, 24 Dec 2024 09:32:23 +0900 Subject: [PATCH 1/2] Add Custom UI code Add Custom UI code --- .../era/sirind/tomak79s/keymaps/via/config.h | 8 + .../era/sirind/tomak79s/keymaps/via/keymap.c | 267 +++++++++++++++++- 2 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 keyboards/era/sirind/tomak79s/keymaps/via/config.h diff --git a/keyboards/era/sirind/tomak79s/keymaps/via/config.h b/keyboards/era/sirind/tomak79s/keymaps/via/config.h new file mode 100644 index 0000000000..a0276d0a9d --- /dev/null +++ b/keyboards/era/sirind/tomak79s/keymaps/via/config.h @@ -0,0 +1,8 @@ +// Copyright 2024 Hyojin Bak (@eerraa) +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +/* Sync configuration */ +#define TOMAK_CONFIG_SYNC +#define SPLIT_TRANSACTION_IDS_KB RPC_ID_KB_CONFIG_SYNC \ No newline at end of file diff --git a/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c b/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c index 9ac7334d48..e2547181da 100644 --- a/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c +++ b/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c @@ -2,6 +2,9 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include QMK_KEYBOARD_H +#include "quantum.h" +#include "eeprom.h" +#include "transactions.h" const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { @@ -21,4 +24,266 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KC_TRNS, KC_TRNS, RM_NEXT, RM_VALD, RM_HUED, RM_SATD, RM_SPDD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS ) -}; \ No newline at end of file +}; + +typedef union { + uint32_t raw; + struct { + bool indicator_toggle:1; // | byte + bool indicator_override:1; // | byte + bool per_key_toggle:1; // | byte + HSV indicator_hsv; // 3 bytes + } __attribute__((packed)); // total 4 bytes +} tomak_config_t; + +tomak_config_t g_tomak_config; + +static void read_tomak_config_from_eeprom(tomak_config_t* config) { + config->raw = eeconfig_read_kb() & 0xffffffff; +} + +static void write_tomak_config_to_eeprom(tomak_config_t* config) { + eeconfig_update_kb(config->raw); +} + +void eeconfig_init_kb(void) { + g_tomak_config.raw = 0; + g_tomak_config.indicator_toggle = true; + g_tomak_config.indicator_override = false; + g_tomak_config.indicator_hsv.h = 255; + g_tomak_config.indicator_hsv.s = 255; + g_tomak_config.indicator_hsv.v = 255; + g_tomak_config.per_key_toggle = true; + write_tomak_config_to_eeprom(&g_tomak_config); + eeconfig_init_user(); +} + +void matrix_init_kb(void) { + read_tomak_config_from_eeprom(&g_tomak_config); + matrix_init_user(); +} + +bool rgb_matrix_indicators_kb(void) { + if (!rgb_matrix_indicators_user()) { + return false; + } + + if (g_tomak_config.indicator_toggle) { + RGB rgb_caps = hsv_to_rgb( (HSV){ .h = g_tomak_config.indicator_hsv.h, + .s = g_tomak_config.indicator_hsv.s, + .v = g_tomak_config.indicator_hsv.v } ); + if (host_keyboard_led_state().caps_lock) { + for (uint8_t i = 93; i <= 101; ++i) { + rgb_matrix_set_color(i, rgb_caps.r, rgb_caps.g, rgb_caps.b); + } + } else if (g_tomak_config.indicator_override) { + for (uint8_t i = 93; i <= 101; ++i) { + rgb_matrix_set_color(i, 0, 0, 0); + } + } + } + + if (g_tomak_config.per_key_toggle == false) { + for (uint8_t i = 0; i <= 92; ++i) { + rgb_matrix_set_color(i, 0, 0, 0); + } + } + + return true; +} + +#ifdef TOMAK_CONFIG_SYNC +void tomak_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { + if (initiator2target_buffer_size == sizeof(g_tomak_config)) { + memcpy(&g_tomak_config, initiator2target_buffer, sizeof(g_tomak_config)); + } +} + +void keyboard_post_init_kb(void) { + + transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, tomak_config_sync_handler); + keyboard_post_init_user(); +} + +void housekeeping_task_kb(void) { + if (is_keyboard_master()) { + // Keep track of the last state, so that we can tell if we need to propagate to slave. + static tomak_config_t last_tomak_config = {0}; + static uint32_t last_sync = 0; + bool needs_sync = false; + + // Check if the state values are different. + if (memcmp(&g_tomak_config, &last_tomak_config, sizeof(g_tomak_config))) { + needs_sync = true; + memcpy(&last_tomak_config, &g_tomak_config, sizeof(g_tomak_config)); + } + // Send to slave every 500ms regardless of state change. + if (timer_elapsed32(last_sync) > 500) { + needs_sync = true; + } + + // Perform the sync if requested. + if (needs_sync) { + if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(g_tomak_config), &g_tomak_config)) { + last_sync = timer_read32(); + } + } + } + // No need to invoke the user-specific callback, as it's been called + // already. +} +#endif + +#ifdef VIA_ENABLE +// via value id declaration +enum tomak_custom_value_id { + id_custom_indicator_toggle = 0, + id_custom_indicator_override, + id_custom_indicator_brightness, + id_custom_indicator_color, + id_custom_per_key_toggle +}; + +// function declaration +void via_tomak_config_set_value( uint8_t *data ); +void via_tomak_config_get_value( uint8_t *data ); +void _set_color(HSV *color, uint8_t *data); +void _get_color(HSV *color, uint8_t *data); + +void via_init_kb(void) +{ + // If the EEPROM has the magic, the data is good. + // OK to load from EEPROM + if (via_eeprom_is_valid()) { + read_tomak_config_from_eeprom(&g_tomak_config); + } else { + write_tomak_config_to_eeprom(&g_tomak_config); + // DO NOT set EEPROM valid here, let caller do this + } +} + +// Some helpers for setting/getting HSV +void _set_color( HSV *color, uint8_t *data ) +{ + color->h = data[0]; + color->s = data[1]; +} + +void _get_color( HSV *color, uint8_t *data ) +{ + data[0] = color->h; + data[1] = color->s; +} + +void via_tomak_config_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_custom_indicator_toggle: + { + *value_data = g_tomak_config.indicator_toggle; + break; + } + case id_custom_indicator_override: + { + *value_data = g_tomak_config.indicator_override; + break; + } + case id_custom_indicator_brightness: + { + *value_data = g_tomak_config.indicator_hsv.v; + break; + } + case id_custom_indicator_color: + { + _get_color( &(g_tomak_config.indicator_hsv), value_data ); + break; + } + case id_custom_per_key_toggle: + { + *value_data = g_tomak_config.per_key_toggle; + break; + } + } +} + +void via_tomak_config_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_custom_indicator_toggle: + { + g_tomak_config.indicator_toggle = (bool) *value_data; + break; + } + case id_custom_indicator_override: + { + g_tomak_config.indicator_override = (bool) *value_data; + break; + } + case id_custom_indicator_brightness: + { + g_tomak_config.indicator_hsv.v = *value_data; + break; + } + case id_custom_indicator_color: + { + _set_color( &(g_tomak_config.indicator_hsv), value_data ); + break; + } + case id_custom_per_key_toggle: + { + g_tomak_config.per_key_toggle = (bool) *value_data; + break; + } + } +} + +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]); + uint8_t *channel_id = &(data[1]); + uint8_t *value_id_and_data = &(data[2]); + + if ( *channel_id == id_custom_channel ) { + switch ( *command_id ) + { + case id_custom_set_value: + { + via_tomak_config_set_value(value_id_and_data); + break; + } + case id_custom_get_value: + { + via_tomak_config_get_value(value_id_and_data); + break; + } + case id_custom_save: + { + write_tomak_config_to_eeprom(&g_tomak_config); + break; + } + default: + { + // Unhandled message. + *command_id = id_unhandled; + break; + } + } + return; + } + + // Return the unhandled state + *command_id = id_unhandled; + +} +#endif \ No newline at end of file From 343839379cbd67b8b4af2df9a3c215b153f68ddb Mon Sep 17 00:00:00 2001 From: era Date: Tue, 24 Dec 2024 09:50:48 +0900 Subject: [PATCH 2/2] Fix Linker Error Fix Linker Error --- keyboards/era/sirind/tomak79s/keymaps/via/keymap.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c b/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c index e2547181da..3a2c6f904b 100644 --- a/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c +++ b/keyboards/era/sirind/tomak79s/keymaps/via/keymap.c @@ -63,11 +63,7 @@ void matrix_init_kb(void) { matrix_init_user(); } -bool rgb_matrix_indicators_kb(void) { - if (!rgb_matrix_indicators_user()) { - return false; - } - +bool rgb_matrix_indicators_user(void) { if (g_tomak_config.indicator_toggle) { RGB rgb_caps = hsv_to_rgb( (HSV){ .h = g_tomak_config.indicator_hsv.h, .s = g_tomak_config.indicator_hsv.s, @@ -89,7 +85,7 @@ bool rgb_matrix_indicators_kb(void) { } } - return true; + return false; } #ifdef TOMAK_CONFIG_SYNC