djinn_split_sync.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2018-2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string.h>
  4. #include "quantum.h"
  5. #include "transactions.h"
  6. #include "split_util.h"
  7. #include "djinn.h"
  8. kb_runtime_config kb_state;
  9. uint32_t last_slave_sync_time = 0;
  10. void kb_state_update(void) {
  11. if (is_keyboard_master()) {
  12. // Modify allowed current limits
  13. usbpd_update();
  14. // Turn off the LCD if there's been no matrix activity
  15. kb_state.lcd_power = (last_input_activity_elapsed() < LCD_ACTIVITY_TIMEOUT) ? 1 : 0;
  16. }
  17. }
  18. void kb_state_sync(void) {
  19. if (!is_transport_connected()) return;
  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. // Check if the state values are different
  26. if (memcmp(&kb_state, &last_kb_state, sizeof(kb_runtime_config))) {
  27. needs_sync = true;
  28. memcpy(&last_kb_state, &kb_state, sizeof(kb_runtime_config));
  29. }
  30. // Send to slave every 500ms regardless of state change
  31. if (timer_elapsed32(last_sync) > 500) {
  32. needs_sync = true;
  33. }
  34. // Perform the sync if requested
  35. if (needs_sync) {
  36. if (transaction_rpc_send(RPC_ID_SYNC_STATE_KB, sizeof(kb_runtime_config), &kb_state)) {
  37. last_sync = timer_read32();
  38. } else {
  39. dprint("Failed to perform data transaction\n");
  40. }
  41. }
  42. }
  43. }
  44. void kb_state_sync_slave(uint8_t m2s_size, const void* m2s_buffer, uint8_t s2m_size, void* s2m_buffer) {
  45. if (m2s_size == sizeof(kb_runtime_config)) {
  46. memcpy(&kb_state, m2s_buffer, sizeof(kb_runtime_config));
  47. last_slave_sync_time = timer_read32();
  48. }
  49. }