Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Add user config sync
Browse files Browse the repository at this point in the history
  • Loading branch information
richardbaptist committed Aug 6, 2023
1 parent 9e5057d commit e033ad5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
2 changes: 2 additions & 0 deletions keyboards/crkbd/keymaps/rpbaptist/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#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
Expand Down
1 change: 0 additions & 1 deletion keyboards/crkbd/keymaps/rpbaptist/oled.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
6 changes: 1 addition & 5 deletions keyboards/crkbd/keymaps/rpbaptist/oled.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
45 changes: 41 additions & 4 deletions keyboards/crkbd/keymaps/rpbaptist/rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -241,13 +248,40 @@ 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) {
has_ran_yet = true;
startup_user();
}
matrix_scan_rgb();

// Update config to slave
user_transport_update();
}

void eeconfig_init_user(void) {
Expand All @@ -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);
Expand Down
20 changes: 1 addition & 19 deletions keyboards/crkbd/keymaps/rpbaptist/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#include "quantum.h"
#include "transactions.h"

typedef union {
uint32_t raw;
Expand All @@ -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);
2 changes: 2 additions & 0 deletions keyboards/crkbd/keymaps/rpbaptist/rpbaptist.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#pragma once
#include QMK_KEYBOARD_H

#include "quantum.h"

#if defined(RGB_MATRIX_ENABLE)
# include "rgb.h"
#endif
Expand Down

0 comments on commit e033ad5

Please sign in to comment.