From e033ad5638c91d661277160cc0f6050bc8d34880 Mon Sep 17 00:00:00 2001 From: Richard Baptist Date: Sun, 6 Aug 2023 13:59:28 +0200 Subject: [PATCH] Add user config sync --- keyboards/crkbd/keymaps/rpbaptist/config.h | 2 + keyboards/crkbd/keymaps/rpbaptist/oled.c | 1 - keyboards/crkbd/keymaps/rpbaptist/oled.h | 6 +-- keyboards/crkbd/keymaps/rpbaptist/rgb.c | 45 +++++++++++++++++-- keyboards/crkbd/keymaps/rpbaptist/rgb.h | 20 +-------- keyboards/crkbd/keymaps/rpbaptist/rpbaptist.h | 2 + 6 files changed, 47 insertions(+), 29 deletions(-) diff --git a/keyboards/crkbd/keymaps/rpbaptist/config.h b/keyboards/crkbd/keymaps/rpbaptist/config.h index e7ad58790319..e2756de86fc3 100644 --- a/keyboards/crkbd/keymaps/rpbaptist/config.h +++ b/keyboards/crkbd/keymaps/rpbaptist/config.h @@ -32,6 +32,8 @@ along with this program. If not, see . #define SPLIT_LAYER_STATE_ENABLE #ifdef RGB_MATRIX_ENABLE +# define SPLIT_TRANSACTION_IDS_USER USER_CONFIG_SYNC + # define RGB_MATRIX_LED_PROCESS_LIMIT (RGB_MATRIX_LED_COUNT + 4) / 5 // limits the number of LEDs to process in an animation per task run (increases keyboard responsiveness) # define IDLE_TIMEOUT 60000 * 4 # define GAMING_IDLE_TIMEOUT 10000 diff --git a/keyboards/crkbd/keymaps/rpbaptist/oled.c b/keyboards/crkbd/keymaps/rpbaptist/oled.c index 91f1d517b2d1..a2cc43c37365 100644 --- a/keyboards/crkbd/keymaps/rpbaptist/oled.c +++ b/keyboards/crkbd/keymaps/rpbaptist/oled.c @@ -2,7 +2,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "rpbaptist.h" -#include "oled.h" oled_rotation_t oled_init_user(oled_rotation_t rotation) { if (is_keyboard_master()) { diff --git a/keyboards/crkbd/keymaps/rpbaptist/oled.h b/keyboards/crkbd/keymaps/rpbaptist/oled.h index 5a8781f56251..d4a73992e0ed 100644 --- a/keyboards/crkbd/keymaps/rpbaptist/oled.h +++ b/keyboards/crkbd/keymaps/rpbaptist/oled.h @@ -3,10 +3,6 @@ #pragma once -#include "quantum.h" #include "oled_driver.h" -oled_rotation_t oled_init_user(oled_rotation_t rotation); -void render_crkbd_logo(void); -void render_status(void); -bool oled_task_user(void); +void render_crkbd_logo(void); diff --git a/keyboards/crkbd/keymaps/rpbaptist/rgb.c b/keyboards/crkbd/keymaps/rpbaptist/rgb.c index 0d45c081bea4..7f87d0a9eaa4 100644 --- a/keyboards/crkbd/keymaps/rpbaptist/rgb.c +++ b/keyboards/crkbd/keymaps/rpbaptist/rgb.c @@ -2,10 +2,17 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "rpbaptist.h" -#include "rgb_matrix.h" + +uint32_t transport_user_config = 0; + +void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { + if (initiator2target_buffer_size == sizeof(transport_user_config)) { + memcpy(&transport_user_config, initiator2target_buffer, initiator2target_buffer_size); + } +} #ifdef OLED_ENABLE -const char *rgb_matrix_anim_oled_text(uint8_t mode) { +const char* rgb_matrix_anim_oled_text(uint8_t mode) { switch (mode) { case RGB_MATRIX_TYPING_HEATMAP: return PSTR("Heat "); @@ -45,7 +52,7 @@ void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t led_ } } -user_config_t user_config; +extern user_config_t user_config; bool rgb_matrix_in_idle(void) { return (user_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == user_config.rgb_matrix_idle_mode); @@ -241,6 +248,30 @@ void matrix_scan_rgb(void) { } } +void user_transport_sync(void) { + // Keep track of the last state, so that we can tell if we need to propagate to slave + static uint32_t last_config = 0, last_sync = 0; + + // Check if the state values are different + // or if sync timer elapsed + if (memcmp(&user_config, &last_config, sizeof(transport_user_config)) || (timer_elapsed32(last_sync) > 250)) { + memcpy(&last_config, &user_config, sizeof(transport_user_config)); + + if (transaction_rpc_send(USER_CONFIG_SYNC, sizeof(transport_user_config), &transport_user_config)) { + last_sync = timer_read32(); + } + } +} + +void user_transport_update(void) { + if (is_keyboard_master()) { + transport_user_config = user_config.raw; + user_transport_sync(); + } else { + user_config.raw = transport_user_config; + } +} + void housekeeping_task_user(void) { static bool has_ran_yet; if (!has_ran_yet) { @@ -248,6 +279,9 @@ void housekeeping_task_user(void) { startup_user(); } matrix_scan_rgb(); + + // Update config to slave + user_transport_update(); } void eeconfig_init_user(void) { @@ -265,9 +299,12 @@ void eeconfig_init_user(void) { void keyboard_post_init_user(void) { set_single_persistent_default_layer(_COLEMAKDH); rgb_matrix_set_defaults(); + + // Register user config sync + transaction_register_rpc(USER_CONFIG_SYNC, user_config_sync); } -bool process_record_user_rgb_matrix(uint16_t keycode, keyrecord_t *record) { +bool process_record_user_rgb_matrix(uint16_t keycode, keyrecord_t* record) { if (user_config.rgb_matrix_idle_anim) { if (rgb_matrix_get_mode() == user_config.rgb_matrix_idle_mode) { rgb_matrix_update_current_mode(user_config.rgb_matrix_active_mode); diff --git a/keyboards/crkbd/keymaps/rpbaptist/rgb.h b/keyboards/crkbd/keymaps/rpbaptist/rgb.h index 2712bf805a8e..91c25f5c2872 100644 --- a/keyboards/crkbd/keymaps/rpbaptist/rgb.h +++ b/keyboards/crkbd/keymaps/rpbaptist/rgb.h @@ -3,7 +3,7 @@ #pragma once -#include "quantum.h" +#include "transactions.h" typedef union { uint32_t raw; @@ -23,24 +23,6 @@ extern user_config_t user_config; _Static_assert(sizeof(user_config_t) == sizeof(uint32_t), "Userspace EECONFIG out of spec."); const char *rgb_matrix_anim_oled_text(uint8_t mode); -void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t led_min, uint8_t led_max); -void rgb_matrix_turn_off_underglow(void); -uint8_t rgb_matrix_speed_for_mode(uint8_t mode); -bool rgb_matrix_mode_active(uint8_t mode); -void rgb_matrix_update_current_mode(uint8_t mode); -void rgb_matrix_update_dynamic_mode(uint8_t mode); -void rgb_matrix_update_mode(uint8_t mode); -uint8_t get_rgb_matrix_active_mode(void); -void rgb_matrix_toggle_active_mode(void); -uint8_t get_rgb_matrix_idle_mode(void); -void rgb_matrix_toggle_simple_passive_mode(void); -void rgb_matrix_toggle_color_passive_mode(void); -void rgb_matrix_toggle_underglow_layer_indicator(void); -void rgb_matrix_toggle_idle_animation_change(void); void rgb_matrix_set_gaming_defaults(void); void rgb_matrix_set_typing_defaults(void); -void rgb_matrix_set_defaults(void); -void matrix_scan_rgb(void); -void eeconfig_init_user(void); -void keyboard_post_init_user(void); bool process_record_user_rgb_matrix(uint16_t keycode, keyrecord_t *record); diff --git a/keyboards/crkbd/keymaps/rpbaptist/rpbaptist.h b/keyboards/crkbd/keymaps/rpbaptist/rpbaptist.h index 932a997ccc97..630ab5df49c1 100644 --- a/keyboards/crkbd/keymaps/rpbaptist/rpbaptist.h +++ b/keyboards/crkbd/keymaps/rpbaptist/rpbaptist.h @@ -4,6 +4,8 @@ #pragma once #include QMK_KEYBOARD_H +#include "quantum.h" + #if defined(RGB_MATRIX_ENABLE) # include "rgb.h" #endif