Last active
September 1, 2022 18:06
-
-
Save drashna/63b37400e425667baec2d8d8ef05d6db to your computer and use it in GitHub Desktop.
caps word sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "transactions.h" | |
#include <string.h> | |
bool is_caps_on = false; | |
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(is_caps_on)) { | |
// copy data from master into local data structure | |
memcpy(&is_caps_on, 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 | |
is_caps_on = is_caps_word_on(); | |
// sync values | |
static uint32_t last_sync = 0; | |
static bool last_user_state = false; | |
static bool needs_sync = false; | |
// if value is different, then needs syncing | |
if (memcmp(&is_caps_on, &last_user_state, sizeof(is_caps_on))) { | |
needs_sync = true; | |
// copy local user state to verify changes | |
memcpy(&last_user_state, &is_caps_on, sizeof(is_caps_on)); | |
} | |
// 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(is_caps_on), &is_caps_on)) { | |
// reset sync checks | |
last_sync = timer_read32(); | |
needs_sync = false; | |
} | |
} | |
} else { // not master: | |
// copy value from data structure to local variable | |
if (is_caps_on) { | |
caps_word_on(); | |
} else { | |
caps_word_off(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment