djinn_split_sync.c (1712B)
1 // Copyright 2018-2023 Nick Brassel (@tzarc) 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 #include "transactions.h" 4 #include "split_util.h" 5 #include "djinn.h" 6 7 kb_runtime_config kb_state; 8 uint32_t last_slave_sync_time = 0; 9 10 void kb_state_update(void) { 11 if (is_keyboard_master()) { 12 // Modify allowed current limits 13 usbpd_update(); 14 } 15 } 16 17 void kb_state_sync(void) { 18 if (!is_transport_connected()) return; 19 20 if (is_keyboard_master()) { 21 // Keep track of the last state, so that we can tell if we need to propagate to slave 22 static kb_runtime_config last_kb_state; 23 static uint32_t last_sync; 24 bool needs_sync = false; 25 26 // Check if the state values are different 27 if (memcmp(&kb_state, &last_kb_state, sizeof(kb_runtime_config))) { 28 needs_sync = true; 29 memcpy(&last_kb_state, &kb_state, sizeof(kb_runtime_config)); 30 } 31 32 // Send to slave every 500ms regardless of state change 33 if (timer_elapsed32(last_sync) > 500) { 34 needs_sync = true; 35 } 36 37 // Perform the sync if requested 38 if (needs_sync) { 39 if (transaction_rpc_send(RPC_ID_SYNC_STATE_KB, sizeof(kb_runtime_config), &kb_state)) { 40 last_sync = timer_read32(); 41 } else { 42 dprint("Failed to perform data transaction\n"); 43 } 44 } 45 } 46 } 47 48 void kb_state_sync_slave(uint8_t m2s_size, const void* m2s_buffer, uint8_t s2m_size, void* s2m_buffer) { 49 if (m2s_size == sizeof(kb_runtime_config)) { 50 memcpy(&kb_state, m2s_buffer, sizeof(kb_runtime_config)); 51 last_slave_sync_time = timer_read32(); 52 } 53 }