diff options
Diffstat (limited to 'users/drashna/transport_sync.c')
| -rw-r--r-- | users/drashna/transport_sync.c | 137 |
1 files changed, 123 insertions, 14 deletions
diff --git a/users/drashna/transport_sync.c b/users/drashna/transport_sync.c index 8a3e6d1bf..c3ef2eff5 100644 --- a/users/drashna/transport_sync.c +++ b/users/drashna/transport_sync.c | |||
| @@ -3,31 +3,77 @@ | |||
| 3 | # include "transactions.h" | 3 | # include "transactions.h" |
| 4 | # include <string.h> | 4 | # include <string.h> |
| 5 | 5 | ||
| 6 | # ifdef UNICODE_ENABLE | ||
| 7 | extern unicode_config_t unicode_config; | ||
| 8 | # endif | ||
| 9 | # ifdef AUDIO_ENABLE | ||
| 10 | # include "audio.h" | ||
| 11 | # endif | ||
| 12 | # if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform) | ||
| 13 | extern bool tap_toggling; | ||
| 14 | # endif | ||
| 15 | # ifdef SWAP_HANDS_ENABLE | ||
| 16 | extern bool swap_hands; | ||
| 17 | # endif | ||
| 18 | extern userspace_config_t userspace_config; | ||
| 19 | |||
| 6 | typedef struct { | 20 | typedef struct { |
| 7 | bool oled_on; | 21 | bool oled_on; |
| 8 | uint16_t keymap_config; | 22 | bool audio_enable; |
| 23 | bool audio_clicky_enable; | ||
| 24 | bool tap_toggling; | ||
| 25 | bool unicode_mode; | ||
| 26 | bool swap_hands; | ||
| 27 | uint8_t reserved :2; | ||
| 9 | } user_runtime_config_t; | 28 | } user_runtime_config_t; |
| 10 | 29 | ||
| 30 | uint16_t transport_keymap_config = 0; | ||
| 31 | uint32_t transport_userspace_config = 0; | ||
| 32 | |||
| 11 | user_runtime_config_t user_state; | 33 | user_runtime_config_t user_state; |
| 12 | 34 | ||
| 13 | void user_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { | 35 | void user_state_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)) { | 36 | if (initiator2target_buffer_size == sizeof(user_state)) { |
| 15 | memcpy(&user_state, initiator2target_buffer, initiator2target_buffer_size); | 37 | memcpy(&user_state, initiator2target_buffer, initiator2target_buffer_size); |
| 16 | } | 38 | } |
| 17 | } | 39 | } |
| 40 | void user_keymap_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { | ||
| 41 | if (initiator2target_buffer_size == sizeof(transport_keymap_config)) { | ||
| 42 | memcpy(&transport_keymap_config, initiator2target_buffer, initiator2target_buffer_size); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { | ||
| 46 | if (initiator2target_buffer_size == sizeof(transport_userspace_config)) { | ||
| 47 | memcpy(&transport_userspace_config, initiator2target_buffer, initiator2target_buffer_size); | ||
| 48 | } | ||
| 49 | } | ||
| 18 | 50 | ||
| 19 | void keyboard_post_init_transport_sync(void) { | 51 | void keyboard_post_init_transport_sync(void) { |
| 20 | // Register keyboard state sync split transaction | 52 | // Register keyboard state sync split transaction |
| 21 | transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_sync); | 53 | transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_state_sync); |
| 54 | transaction_register_rpc(RPC_ID_USER_KEYMAP_SYNC, user_keymap_sync); | ||
| 55 | transaction_register_rpc(RPC_ID_USER_CONFIG_SYNC, user_config_sync); | ||
| 22 | } | 56 | } |
| 23 | 57 | ||
| 24 | void user_state_update(void) { | 58 | void user_transport_update(void) { |
| 25 | if (is_keyboard_master()) { | 59 | if (is_keyboard_master()) { |
| 26 | # ifdef OLED_DRIVER_ENABLE | 60 | # ifdef OLED_DRIVER_ENABLE |
| 27 | user_state.oled_on = is_oled_on(); | 61 | user_state.oled_on = is_oled_on(); |
| 28 | # endif | 62 | # endif |
| 29 | 63 | ||
| 30 | user_state.keymap_config = keymap_config.raw; | 64 | transport_keymap_config = keymap_config.raw; |
| 65 | transport_userspace_config = userspace_config.raw; | ||
| 66 | # ifdef AUDIO_ENABLE | ||
| 67 | user_state.audio_enable = is_audio_on(); | ||
| 68 | user_state.audio_clicky_enable = is_clicky_on(); | ||
| 69 | # endif | ||
| 70 | # if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform) | ||
| 71 | user_state.tap_toggling = tap_toggling; | ||
| 72 | # endif | ||
| 73 | # ifdef SWAP_HANDS_ENABLE | ||
| 74 | user_state.swap_hands = swap_hands; | ||
| 75 | # endif | ||
| 76 | |||
| 31 | } else { | 77 | } else { |
| 32 | # ifdef OLED_DRIVER_ENABLE | 78 | # ifdef OLED_DRIVER_ENABLE |
| 33 | if (user_state.oled_on) { | 79 | if (user_state.oled_on) { |
| @@ -36,17 +82,43 @@ void user_state_update(void) { | |||
| 36 | oled_off(); | 82 | oled_off(); |
| 37 | } | 83 | } |
| 38 | # endif | 84 | # endif |
| 39 | if (keymap_config.raw != user_state.keymap_config) { | 85 | keymap_config.raw = transport_keymap_config; |
| 40 | keymap_config.raw = user_state.keymap_config; | 86 | userspace_config.raw = transport_userspace_config; |
| 87 | # ifdef UNICODE_ENABLE | ||
| 88 | unicode_config.input_mode = user_state.unicode_mode; | ||
| 89 | # endif | ||
| 90 | # ifdef AUDIO_ENABLE | ||
| 91 | if (user_state.audio_enable != is_audio_on()) { | ||
| 92 | if (user_state.audio_enable) { | ||
| 93 | audio_on(); | ||
| 94 | } else { | ||
| 95 | audio_off(); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | if (user_state.audio_clicky_enable != is_clicky_on()) { | ||
| 99 | if (user_state.audio_clicky_enable) { | ||
| 100 | clicky_on(); | ||
| 101 | } else { | ||
| 102 | clicky_off(); | ||
| 103 | } | ||
| 41 | } | 104 | } |
| 105 | # endif | ||
| 106 | # if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform) | ||
| 107 | tap_toggling = user_state.tap_toggling; | ||
| 108 | # endif | ||
| 109 | # ifdef SWAP_HANDS_ENABLE | ||
| 110 | swap_hands = user_state.swap_hands; | ||
| 111 | # endif | ||
| 42 | } | 112 | } |
| 43 | } | 113 | } |
| 44 | 114 | ||
| 45 | void user_state_sync(void) { | 115 | void user_transport_sync(void) { |
| 46 | if (is_keyboard_master()) { | 116 | if (is_keyboard_master()) { |
| 47 | // Keep track of the last state, so that we can tell if we need to propagate to slave | 117 | // 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; | 118 | static user_runtime_config_t last_user_state; |
| 49 | static uint32_t last_sync; | 119 | static uint16_t last_keymap = 0; |
| 120 | static uint32_t last_config = 0; | ||
| 121 | static uint32_t last_sync[3]; | ||
| 50 | bool needs_sync = false; | 122 | bool needs_sync = false; |
| 51 | 123 | ||
| 52 | // Check if the state values are different | 124 | // Check if the state values are different |
| @@ -54,16 +126,53 @@ void user_state_sync(void) { | |||
| 54 | needs_sync = true; | 126 | needs_sync = true; |
| 55 | memcpy(&last_user_state, &user_state, sizeof(user_state)); | 127 | memcpy(&last_user_state, &user_state, sizeof(user_state)); |
| 56 | } | 128 | } |
| 57 | |||
| 58 | // Send to slave every 500ms regardless of state change | 129 | // Send to slave every 500ms regardless of state change |
| 59 | if (timer_elapsed32(last_sync) > 250) { | 130 | if (timer_elapsed32(last_sync[0]) > 250) { |
| 60 | needs_sync = true; | 131 | needs_sync = true; |
| 61 | } | 132 | } |
| 62 | 133 | ||
| 63 | // Perform the sync if requested | 134 | // Perform the sync if requested |
| 64 | if (needs_sync) { | 135 | if (needs_sync) { |
| 65 | if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) { | 136 | if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) { |
| 66 | last_sync = timer_read32(); | 137 | last_sync[0] = timer_read32(); |
| 138 | } | ||
| 139 | needs_sync = false; | ||
| 140 | } | ||
| 141 | |||
| 142 | // Check if the state values are different | ||
| 143 | if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) { | ||
| 144 | needs_sync = true; | ||
| 145 | memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config)); | ||
| 146 | } | ||
| 147 | |||
| 148 | // Send to slave every 500ms regardless of state change | ||
| 149 | if (timer_elapsed32(last_sync[1]) > 250) { | ||
| 150 | needs_sync = true; | ||
| 151 | } | ||
| 152 | |||
| 153 | // Perform the sync if requested | ||
| 154 | if (needs_sync) { | ||
| 155 | if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) { | ||
| 156 | last_sync[1] = timer_read32(); | ||
| 157 | } | ||
| 158 | needs_sync = false; | ||
| 159 | } | ||
| 160 | |||
| 161 | // Check if the state values are different | ||
| 162 | if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) { | ||
| 163 | needs_sync = true; | ||
| 164 | memcpy(&last_config, &user_state, sizeof(transport_userspace_config)); | ||
| 165 | } | ||
| 166 | |||
| 167 | // Send to slave every 500ms regardless of state change | ||
| 168 | if (timer_elapsed32(last_sync[2]) > 250) { | ||
| 169 | needs_sync = true; | ||
| 170 | } | ||
| 171 | |||
| 172 | // Perform the sync if requested | ||
| 173 | if (needs_sync) { | ||
| 174 | if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) { | ||
| 175 | last_sync[2] = timer_read32(); | ||
| 67 | } | 176 | } |
| 68 | } | 177 | } |
| 69 | } | 178 | } |
| @@ -71,9 +180,9 @@ void user_state_sync(void) { | |||
| 71 | 180 | ||
| 72 | void housekeeping_task_user(void) { | 181 | void housekeeping_task_user(void) { |
| 73 | // Update kb_state so we can send to slave | 182 | // Update kb_state so we can send to slave |
| 74 | user_state_update(); | 183 | user_transport_update(); |
| 75 | 184 | ||
| 76 | // Data sync from master to slave | 185 | // Data sync from master to slave |
| 77 | user_state_sync(); | 186 | user_transport_sync(); |
| 78 | } | 187 | } |
| 79 | #endif | 188 | #endif |
