Skip to content

Instantly share code, notes, and snippets.

@drashna
Created August 6, 2022 23:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drashna/4f12ea060e37e752e725f009fc1e7297 to your computer and use it in GitHub Desktop.
Save drashna/4f12ea060e37e752e725f009fc1e7297 to your computer and use it in GitHub Desktop.
transport sync
typedef union {
uint32_t raw;
struct {
bool rgb_matrix_ledmap_active :1;
};
} user_runtime_config_t;
user_runtime_config_t user_state;
void user_sync_a_slave_handler(uint8_t in_buflen, const void* in_data, uint8_t out_buflen, void* out_data) {
// if buffer length matches size of data structure (simple error checking)
if (in_buflen == sizeof(user_state)) {
// copy data from master into local data structure
memcpy(&user_state, in_data, in_buflen);
}
}
void keyboard_post_init_user(void) {
// register sync handler
transaction_register_rpc(USER_SYNC_A, user_sync_a_slave_handler);
}
void housekeeping_task_user(void) {
if (is_keyboard_master()) {
// update values
// copy local variable to sync data structure
user_state.rgb_matrix_ledmap_active = (local_variable);
// sync values
static uint32_t last_sync = 0;
static user_runtime_config_t last_user_state = 0;
static bool needs_sync = false;
// if value is different, then needs syncing
if (memcmp(&user_state, &last_user_state, sizeof(user_state))) {
needs_sync = true;
// copy local user state to verify changes
memcpy(&last_user_state, &user_state, sizeof(user_state));
}
// Send to slave every 250ms regardless of state change
if (timer_elapsed32(last_sync) > 250) {
needs_sync = true;
}
// if it needs syncing:
if (needs_sync) {
// send user_data stuct over to slave
if(transaction_rpc_send(USER_SYNC_A, sizeof(user_state), &user_state)) {
// reset sync checks
last_sync = timer_read32();
needs_sync = false;
}
}
} else { // not master:
// copy value from data structure to local variable
(local_variable) = user_state.rgb_matrix_ledmap_active;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment