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.

199 lines
6.5 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)) kb_pointer_data_t process_mouse(void) {
  40. kb_pointer_data_t temp_data = {.mouse_x = 0, .mouse_y = 0};
  41. report_pmw_t data = pmw_read_burst();
  42. if (data.isOnSurface && data.isMotion) {
  43. // Reset timer if stopped moving
  44. if (!data.isMotion) {
  45. if (MotionStart != 0) MotionStart = 0;
  46. return temp_data;
  47. }
  48. // Set timer if new motion
  49. if ((MotionStart == 0) && data.isMotion) {
  50. if (debug_mouse) dprintf("Starting motion.\n");
  51. MotionStart = timer_read();
  52. }
  53. if (debug_mouse) { dprintf("Delt] d: %d t: %u\n", abs(data.dx) + abs(data.dy), MotionStart); }
  54. if (debug_mouse) { dprintf("Pre ] X: %d, Y: %d\n", data.dx, data.dy); }
  55. #if defined(PROFILE_LINEAR)
  56. float scale = float(timer_elaspsed(MotionStart)) / 1000.0;
  57. data.dx *= scale;
  58. data.dy *= scale;
  59. #elif defined(PROFILE_INVERSE)
  60. // TODO
  61. #else
  62. // no post processing
  63. #endif
  64. // Wrap to HID size
  65. data.dx = constrain(data.dx, -127, 127);
  66. data.dy = constrain(data.dy, -127, 127);
  67. if (debug_mouse) dprintf("Cons] X: %d, Y: %d\n", data.dx, data.dy);
  68. // dprintf("Elapsed:%u, X: %f Y: %\n", i, pgm_read_byte(firmware_data+i));
  69. temp_data.mouse_x = -data.dx;
  70. temp_data.mouse_y = data.dy;
  71. }
  72. return temp_data;
  73. }
  74. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  75. if (!process_record_user(keycode, record)) { return false; }
  76. #ifdef POINTING_DEVICE_ENABLE
  77. if (keycode == DPI_CONFIG && record->event.pressed) {
  78. if ((get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT) {
  79. keyboard_config.dpi_config = (keyboard_config.dpi_config - 1) % DPI_OPTION_SIZE;
  80. } else {
  81. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  82. }
  83. eeconfig_update_kb(keyboard_config.raw);
  84. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  85. }
  86. #endif
  87. /* If Mousekeys is disabled, then use handle the mouse button
  88. * keycodes. This makes things simpler, and allows usage of
  89. * the keycodes in a consistent manner. But only do this if
  90. * Mousekeys is not enable, so it's not handled twice.
  91. */
  92. #ifndef MOUSEKEY_ENABLE
  93. if (IS_MOUSEKEY_BUTTON(keycode)) {
  94. report_mouse_t currentReport = pointing_device_get_report();
  95. if (record->event.pressed) {
  96. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  97. } else {
  98. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  99. }
  100. pointing_device_set_report(currentReport);
  101. pointing_device_send();
  102. }
  103. #endif
  104. return true;
  105. }
  106. __attribute__((weak)) void keyboard_pre_init_sync(void) {}
  107. // Hardware Setup
  108. void keyboard_pre_init_kb(void) {
  109. // debug_enable = true;
  110. // debug_matrix = true;
  111. // debug_mouse = true;
  112. // debug_encoder = true;
  113. /* Ground all output pins connected to ground. This provides additional
  114. * pathways to ground. If you're messing with this, know this: driving ANY
  115. * of these pins high will cause a short. On the MCU. Ka-blooey.
  116. */
  117. // This is the debug LED.
  118. #if defined(DEBUG_LED_PIN)
  119. setPinOutput(DEBUG_LED_PIN);
  120. writePin(DEBUG_LED_PIN, debug_enable);
  121. #endif
  122. keyboard_pre_init_sync();
  123. keyboard_pre_init_user();
  124. }
  125. #ifdef POINTING_DEVICE_ENABLE
  126. void pointing_device_init(void) {
  127. if (!is_keyboard_left()) {
  128. // initialize ball sensor
  129. pmw_spi_init();
  130. }
  131. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  132. }
  133. void pointing_device_task(void) {
  134. report_mouse_t mouse_report = pointing_device_get_report();
  135. kb_pointer_data_t data = {.mouse_x = mouse_report.x, .mouse_y = mouse_report.y};
  136. if (is_keyboard_left()) {
  137. if (is_keyboard_master()) {
  138. data = kb_pointer_sync_get();
  139. process_mouse_user(&mouse_report, data.mouse_x, data.mouse_y);
  140. }
  141. } else {
  142. data = process_mouse();
  143. if (!is_keyboard_master()) {
  144. kb_pointer_sync_send(data.mouse_x, data.mouse_y);
  145. } else {
  146. process_mouse_user(&mouse_report, data.mouse_x, data.mouse_y);
  147. }
  148. }
  149. pointing_device_set_report(mouse_report);
  150. pointing_device_send();
  151. }
  152. #endif
  153. void eeconfig_init_kb(void) {
  154. keyboard_config.dpi_config = TRACKBALL_DPI_DEFAULT;
  155. #ifdef POINTING_DEVICE_ENABLE
  156. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  157. #endif
  158. eeconfig_update_kb(keyboard_config.raw);
  159. eeconfig_init_user();
  160. }
  161. __attribute__((weak)) void matrix_init_sub_kb(void) {}
  162. void matrix_init_kb(void) {
  163. // is safe to just read DPI setting since matrix init
  164. // comes before pointing device init.
  165. keyboard_config.raw = eeconfig_read_kb();
  166. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) { eeconfig_init_kb(); }
  167. matrix_init_sub_kb();
  168. matrix_init_user();
  169. }
  170. __attribute__((weak)) void matrix_scan_sub_kb(void) {}
  171. void matrix_scan_kb(void) {
  172. matrix_scan_sub_kb();
  173. matrix_scan_user();
  174. }