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.

213 lines
6.6 KiB

  1. /* Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  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. #ifndef TRACKBALL_DPI_OPTIONS
  21. # define TRACKBALL_DPI_OPTIONS \
  22. { 1200, 1600, 2400 }
  23. # ifndef TRACKBALL_DPI_DEFAULT
  24. # define TRACKBALL_DPI_DEFAULT 1
  25. # endif
  26. #endif
  27. #ifndef TRACKBALL_DPI_DEFAULT
  28. # define TRACKBALL_DPI_DEFAULT 0
  29. #endif
  30. keyboard_config_t keyboard_config;
  31. uint16_t dpi_array[] = TRACKBALL_DPI_OPTIONS;
  32. #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
  33. bool BurstState = false; // init burst state for Trackball module
  34. uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  35. __attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int8_t x, int8_t y) {
  36. mouse_report->x = x;
  37. mouse_report->y = y;
  38. }
  39. __attribute__((weak)) void process_mouse(void) {
  40. report_pmw_t data = pmw_read_burst();
  41. // Reset timer if stopped moving
  42. if (!data.isMotion) {
  43. if (MotionStart != 0) MotionStart = 0;
  44. return;
  45. }
  46. if (data.isOnSurface) {
  47. // Set timer if new motion
  48. if (MotionStart == 0) {
  49. if (debug_mouse) dprintf("Starting motion.\n");
  50. MotionStart = timer_read();
  51. }
  52. if (debug_mouse) {
  53. dprintf("Delt] d: %d t: %u\n", abs(data.dx) + abs(data.dy), MotionStart);
  54. }
  55. if (debug_mouse) {
  56. dprintf("Pre ] X: %d, Y: %d\n", data.dx, data.dy);
  57. }
  58. #if defined(PROFILE_LINEAR)
  59. float scale = float(timer_elaspsed(MotionStart)) / 1000.0;
  60. data.dx *= scale;
  61. data.dy *= scale;
  62. #elif defined(PROFILE_INVERSE)
  63. // TODO
  64. #else
  65. // no post processing
  66. #endif
  67. // Wrap to HID size
  68. data.dx = constrain(data.dx, -127, 127);
  69. data.dy = constrain(data.dy, -127, 127);
  70. if (debug_mouse) dprintf("Cons] X: %d, Y: %d\n", data.dx, data.dy);
  71. // dprintf("Elapsed:%u, X: %f Y: %\n", i, pgm_read_byte(firmware_data+i));
  72. sync_mouse_report.x = -data.dx;
  73. sync_mouse_report.y = data.dy;
  74. }
  75. }
  76. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  77. if (!process_record_user(keycode, record)) {
  78. return false;
  79. }
  80. #ifdef POINTING_DEVICE_ENABLE
  81. if (keycode == DPI_CONFIG && record->event.pressed) {
  82. if ((get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT) {
  83. keyboard_config.dpi_config = (keyboard_config.dpi_config - 1) % DPI_OPTION_SIZE;
  84. } else {
  85. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  86. }
  87. eeconfig_update_kb(keyboard_config.raw);
  88. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  89. }
  90. #endif
  91. /* If Mousekeys is disabled, then use handle the mouse button
  92. * keycodes. This makes things simpler, and allows usage of
  93. * the keycodes in a consistent manner. But only do this if
  94. * Mousekeys is not enable, so it's not handled twice.
  95. */
  96. #ifndef MOUSEKEY_ENABLE
  97. if (IS_MOUSEKEY_BUTTON(keycode)) {
  98. report_mouse_t currentReport = pointing_device_get_report();
  99. if (record->event.pressed) {
  100. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  101. } else {
  102. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  103. }
  104. pointing_device_set_report(currentReport);
  105. pointing_device_send();
  106. }
  107. #endif
  108. return true;
  109. }
  110. __attribute__((weak)) void keyboard_pre_init_sync(void) {}
  111. void keyboard_pre_init_kb(void) {
  112. // debug_enable = true;
  113. // debug_matrix = true;
  114. // debug_mouse = true;
  115. // debug_encoder = true;
  116. // This is the debug LED.
  117. #if defined(DEBUG_LED_PIN)
  118. setPinOutput(DEBUG_LED_PIN);
  119. writePin(DEBUG_LED_PIN, !debug_enable);
  120. #endif
  121. keyboard_pre_init_sync();
  122. keyboard_pre_init_user();
  123. }
  124. __attribute__((weak)) void keyboard_post_init_sync(void) {}
  125. void keyboard_post_init_kb(void) {
  126. keyboard_post_init_sync();
  127. keyboard_post_init_user();
  128. }
  129. #ifdef POINTING_DEVICE_ENABLE
  130. void pointing_device_init(void) {
  131. if (!is_keyboard_left()) {
  132. // initialize ball sensor
  133. pmw_spi_init();
  134. }
  135. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  136. }
  137. void pointing_device_task(void) {
  138. report_mouse_t mouse_report = pointing_device_get_report();
  139. if (is_keyboard_left()) {
  140. if (is_keyboard_master()) {
  141. transaction_rpc_recv(RPC_ID_POINTER_STATE_SYNC, sizeof(sync_mouse_report), &sync_mouse_report);
  142. process_mouse_user(&mouse_report, sync_mouse_report.x, sync_mouse_report.y);
  143. }
  144. } else {
  145. process_mouse();
  146. if (is_keyboard_master()) {
  147. process_mouse_user(&mouse_report, sync_mouse_report.x, sync_mouse_report.y);
  148. sync_mouse_report.x = 0;
  149. sync_mouse_report.y = 0;
  150. }
  151. }
  152. pointing_device_set_report(mouse_report);
  153. pointing_device_send();
  154. }
  155. #endif
  156. void eeconfig_init_kb(void) {
  157. keyboard_config.dpi_config = TRACKBALL_DPI_DEFAULT;
  158. #ifdef POINTING_DEVICE_ENABLE
  159. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  160. #endif
  161. eeconfig_update_kb(keyboard_config.raw);
  162. eeconfig_init_user();
  163. }
  164. __attribute__((weak)) void matrix_init_sub_kb(void) {}
  165. void matrix_init_kb(void) {
  166. // is safe to just read DPI setting since matrix init
  167. // comes before pointing device init.
  168. keyboard_config.raw = eeconfig_read_kb();
  169. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  170. eeconfig_init_kb();
  171. }
  172. matrix_init_sub_kb();
  173. matrix_init_user();
  174. }
  175. __attribute__((weak)) void matrix_scan_sub_kb(void) {}
  176. void matrix_scan_kb(void) {
  177. matrix_scan_sub_kb();
  178. matrix_scan_user();
  179. }
  180. __attribute__((weak)) void housekeeping_task_sync(void) {}
  181. void housekeeping_task_kb(void) {
  182. housekeeping_task_sync();
  183. // no need for user function, is called already
  184. }
  185. #ifdef POINTING_DEVICE_ENABLE
  186. void matrix_power_up(void) { pointing_device_task(); }
  187. #endif