diff options
Diffstat (limited to 'users/drashna/transport_sync.c')
| -rw-r--r-- | users/drashna/transport_sync.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/users/drashna/transport_sync.c b/users/drashna/transport_sync.c new file mode 100644 index 000000000..8a3e6d1bf --- /dev/null +++ b/users/drashna/transport_sync.c | |||
| @@ -0,0 +1,79 @@ | |||
| 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 | # ifdef OLED_DRIVER_ENABLE | ||
| 27 | user_state.oled_on = is_oled_on(); | ||
| 28 | # endif | ||
| 29 | |||
| 30 | user_state.keymap_config = keymap_config.raw; | ||
| 31 | } else { | ||
| 32 | # ifdef OLED_DRIVER_ENABLE | ||
| 33 | if (user_state.oled_on) { | ||
| 34 | oled_on(); | ||
| 35 | } else { | ||
| 36 | oled_off(); | ||
| 37 | } | ||
| 38 | # endif | ||
| 39 | if (keymap_config.raw != user_state.keymap_config) { | ||
| 40 | keymap_config.raw = user_state.keymap_config; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | void user_state_sync(void) { | ||
| 46 | if (is_keyboard_master()) { | ||
| 47 | // Keep track of the last state, so that we can tell if we need to propagate to slave | ||
| 48 | static user_runtime_config_t last_user_state; | ||
| 49 | static uint32_t last_sync; | ||
| 50 | bool needs_sync = false; | ||
| 51 | |||
| 52 | // Check if the state values are different | ||
| 53 | if (memcmp(&user_state, &last_user_state, sizeof(user_state))) { | ||
| 54 | needs_sync = true; | ||
| 55 | memcpy(&last_user_state, &user_state, sizeof(user_state)); | ||
| 56 | } | ||
| 57 | |||
| 58 | // Send to slave every 500ms regardless of state change | ||
| 59 | if (timer_elapsed32(last_sync) > 250) { | ||
| 60 | needs_sync = true; | ||
| 61 | } | ||
| 62 | |||
| 63 | // Perform the sync if requested | ||
| 64 | if (needs_sync) { | ||
| 65 | if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) { | ||
| 66 | last_sync = timer_read32(); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | void housekeeping_task_user(void) { | ||
| 73 | // Update kb_state so we can send to slave | ||
| 74 | user_state_update(); | ||
| 75 | |||
| 76 | // Data sync from master to slave | ||
| 77 | user_state_sync(); | ||
| 78 | } | ||
| 79 | #endif | ||
