diff options
Diffstat (limited to 'users/drashna/transport_sync.c')
| -rw-r--r-- | users/drashna/transport_sync.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/users/drashna/transport_sync.c b/users/drashna/transport_sync.c new file mode 100644 index 000000000..baa1f7651 --- /dev/null +++ b/users/drashna/transport_sync.c | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | #ifdef SPLIT_TRANSACTION_IDS_USER | ||
| 2 | #include "transport_sync.h" | ||
| 3 | #include "transactions.h" | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | typedef struct { | ||
| 7 | bool oled_on; | ||
| 8 | uint16_t keymap_config; | ||
| 9 | } user_runtime_config_t; | ||
| 10 | |||
| 11 | user_runtime_config_t user_state; | ||
| 12 | |||
| 13 | void user_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { | ||
| 14 | if (initiator2target_buffer_size == sizeof(user_state)) { | ||
| 15 | memcpy(&user_state, initiator2target_buffer, initiator2target_buffer_size); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | void keyboard_post_init_transport_sync(void) { | ||
| 20 | // Register keyboard state sync split transaction | ||
| 21 | transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_sync); | ||
| 22 | } | ||
| 23 | |||
| 24 | void user_state_update(void) { | ||
| 25 | if (is_keyboard_master()) { | ||
| 26 | |||
| 27 | #ifdef OLED_DRIVER_ENABLE | ||
| 28 | user_state.oled_on = is_oled_on(); | ||
| 29 | #endif | ||
| 30 | |||
| 31 | user_state.keymap_config = keymap_config.raw; | ||
| 32 | } else { | ||
| 33 | #ifdef OLED_DRIVER_ENABLE | ||
| 34 | if (user_state.oled_on) { | ||
| 35 | oled_on(); | ||
| 36 | } else { | ||
| 37 | oled_off(); | ||
| 38 | } | ||
| 39 | #endif | ||
| 40 | if (keymap_config.raw != user_state.keymap_config) { | ||
| 41 | keymap_config.raw = user_state.keymap_config; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | void user_state_sync(void) { | ||
| 47 | if (is_keyboard_master()) { | ||
| 48 | // Keep track of the last state, so that we can tell if we need to propagate to slave | ||
| 49 | static user_runtime_config_t last_user_state; | ||
| 50 | static uint32_t last_sync; | ||
| 51 | bool needs_sync = false; | ||
| 52 | |||
| 53 | // Check if the state values are different | ||
| 54 | if (memcmp(&user_state, &last_user_state, sizeof(user_state))) { | ||
| 55 | needs_sync = true; | ||
| 56 | memcpy(&last_user_state, &user_state, sizeof(user_state)); | ||
| 57 | } | ||
| 58 | |||
| 59 | // Send to slave every 500ms regardless of state change | ||
| 60 | if (timer_elapsed32(last_sync) > 250) { | ||
| 61 | needs_sync = true; | ||
| 62 | } | ||
| 63 | |||
| 64 | // Perform the sync if requested | ||
| 65 | if (needs_sync) { | ||
| 66 | if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) { | ||
| 67 | last_sync = timer_read32(); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | void housekeeping_task_user(void) { | ||
| 74 | // Update kb_state so we can send to slave | ||
| 75 | user_state_update(); | ||
| 76 | |||
| 77 | // Data sync from master to slave | ||
| 78 | user_state_sync(); | ||
| 79 | } | ||
| 80 | #endif | ||
