split_data_sync.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2026 QMK
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "debug.h"
  4. #include "timer.h"
  5. #include "transactions.h"
  6. typedef struct _master_to_slave_t {
  7. int m2s_data;
  8. } master_to_slave_t;
  9. typedef struct _slave_to_master_t {
  10. int s2m_data;
  11. } slave_to_master_t;
  12. static void module_sync_slave_handler(uint8_t in_buflen, const void *in_data, uint8_t out_buflen, void *out_data) {
  13. const master_to_slave_t *m2s = (const master_to_slave_t *)in_data;
  14. slave_to_master_t *s2m = (slave_to_master_t *)out_data;
  15. s2m->s2m_data = m2s->m2s_data + 5; // whatever comes in, add 5 so it can be sent back
  16. }
  17. void keyboard_post_init_split_data_sync(void) {
  18. transaction_register_rpc(EXAMPLE_MODULE_SYNC_A, module_sync_slave_handler);
  19. }
  20. void housekeeping_task_split_data_sync(void) {
  21. if (is_keyboard_master()) {
  22. // Interact with slave every 500ms
  23. static uint32_t last_sync = 0;
  24. if (timer_elapsed32(last_sync) > 500) {
  25. master_to_slave_t m2s = {6};
  26. slave_to_master_t s2m = {0};
  27. if (transaction_rpc_exec(EXAMPLE_MODULE_SYNC_A, sizeof(m2s), &m2s, sizeof(s2m), &s2m)) {
  28. last_sync = timer_read32();
  29. dprintf("Slave value: %d\n", s2m.s2m_data); // this will now be 11, as the slave adds 5
  30. } else {
  31. dprint("Slave sync failed!\n");
  32. }
  33. }
  34. }
  35. }