You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.2 KiB

  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "tractyl_manuform.h"
  17. #include "transactions.h"
  18. #include <string.h>
  19. #include "drivers/sensors/pmw3360.h"
  20. kb_config_data_t kb_config;
  21. kb_mouse_report_t sync_mouse_report;
  22. void kb_pointer_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  23. if (target2initiator_buffer_size == sizeof(sync_mouse_report)) {
  24. memcpy(target2initiator_buffer, &sync_mouse_report, sizeof(sync_mouse_report));
  25. }
  26. sync_mouse_report.x = 0;
  27. sync_mouse_report.y = 0;
  28. }
  29. void kb_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  30. if (initiator2target_buffer_size == sizeof(kb_config)) {
  31. memcpy(&kb_config, initiator2target_buffer, sizeof(kb_config));
  32. }
  33. static uint16_t cpi = 0;
  34. // Check if the state values are different
  35. if (cpi != kb_config.device_cpi) {
  36. cpi = kb_config.device_cpi;
  37. }
  38. }
  39. void keyboard_pre_init_sync(void) {
  40. memset(&kb_config, 0, sizeof(kb_config));
  41. memset(&sync_mouse_report, 0, sizeof(sync_mouse_report));
  42. }
  43. void keyboard_post_init_sync(void) {
  44. // Register keyboard state sync split transaction
  45. transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, kb_config_sync_handler);
  46. transaction_register_rpc(RPC_ID_POINTER_STATE_SYNC, kb_pointer_sync_handler);
  47. }
  48. void housekeeping_task_sync(void) {
  49. if (is_keyboard_master()) {
  50. // Keep track of the last state, so that we can tell if we need to propagate to slave
  51. static kb_config_data_t last_kb_config;
  52. static uint32_t last_sync = 0;
  53. bool needs_sync = false;
  54. // Check if the state values are different
  55. if (memcmp(&kb_config, &last_kb_config, sizeof(kb_config))) {
  56. needs_sync = true;
  57. memcpy(&last_kb_config, &kb_config, sizeof(kb_config));
  58. }
  59. // Send to slave every 500ms regardless of state change
  60. if (timer_elapsed32(last_sync) > 500) {
  61. needs_sync = true;
  62. }
  63. // Perform the sync if requested
  64. if (needs_sync) {
  65. if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(kb_config), &kb_config)) {
  66. last_sync = timer_read32();
  67. }
  68. }
  69. }
  70. }
  71. void trackball_set_cpi(uint16_t cpi) {
  72. kb_config.device_cpi = cpi;
  73. if (!is_keyboard_left()) {
  74. pmw_set_cpi(cpi);
  75. }
  76. }