trackball_mini.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright 2021 Colin Lam (Ploopy Corporation)
  2. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. * Copyright 2019 Sunjun Kim
  4. * Copyright 2019 Hiroyuki Okada
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "trackball_mini.h"
  20. #ifndef OPT_DEBOUNCE
  21. # define OPT_DEBOUNCE 5 // (ms) Time between scroll events
  22. #endif
  23. #ifndef SCROLL_BUTT_DEBOUNCE
  24. # define SCROLL_BUTT_DEBOUNCE 100 // (ms) Time between scroll events
  25. #endif
  26. #ifndef OPT_THRES
  27. # define OPT_THRES 150 // (0-1024) Threshold for actication
  28. #endif
  29. #ifndef OPT_SCALE
  30. # define OPT_SCALE 1 // Multiplier for wheel
  31. #endif
  32. #ifndef PLOOPY_DPI_OPTIONS
  33. # define PLOOPY_DPI_OPTIONS { CPI375, CPI750, CPI1375 }
  34. # ifndef PLOOPY_DPI_DEFAULT
  35. # define PLOOPY_DPI_DEFAULT 2
  36. # endif
  37. #endif
  38. #ifndef PLOOPY_DPI_DEFAULT
  39. # define PLOOPY_DPI_DEFAULT 1
  40. #endif
  41. // Transformation constants for delta-X and delta-Y
  42. const static float ADNS_X_TRANSFORM = -1.0;
  43. const static float ADNS_Y_TRANSFORM = 1.0;
  44. keyboard_config_t keyboard_config;
  45. uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
  46. #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
  47. // TODO: Implement libinput profiles
  48. // https://wayland.freedesktop.org/libinput/doc/latest/pointer-acceleration.html
  49. // Compile time accel selection
  50. // Valid options are ACC_NONE, ACC_LINEAR, ACC_CUSTOM, ACC_QUADRATIC
  51. // Trackball State
  52. bool is_scroll_clicked = false;
  53. bool BurstState = false; // init burst state for Trackball module
  54. uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  55. uint16_t lastScroll = 0; // Previous confirmed wheel event
  56. uint16_t lastMidClick = 0; // Stops scrollwheel from being read if it was pressed
  57. uint8_t OptLowPin = OPT_ENC1;
  58. bool debug_encoder = false;
  59. __attribute__((weak)) void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v) {
  60. mouse_report->h = h;
  61. mouse_report->v = v;
  62. }
  63. __attribute__((weak)) void process_wheel(report_mouse_t* mouse_report) {
  64. // If the mouse wheel was just released, do not scroll.
  65. if (timer_elapsed(lastMidClick) < SCROLL_BUTT_DEBOUNCE)
  66. return;
  67. // Limit the number of scrolls per unit time.
  68. if (timer_elapsed(lastScroll) < OPT_DEBOUNCE)
  69. return;
  70. // Don't scroll if the middle button is depressed.
  71. if (is_scroll_clicked) {
  72. #ifndef IGNORE_SCROLL_CLICK
  73. return;
  74. #endif
  75. }
  76. lastScroll = timer_read();
  77. uint16_t p1 = adc_read(OPT_ENC1_MUX);
  78. uint16_t p2 = adc_read(OPT_ENC2_MUX);
  79. if (debug_encoder)
  80. dprintf("OPT1: %d, OPT2: %d\n", p1, p2);
  81. uint8_t dir = opt_encoder_handler(p1, p2);
  82. if (dir == 0)
  83. return;
  84. process_wheel_user(mouse_report, mouse_report->h, (int)(mouse_report->v + (dir * OPT_SCALE)));
  85. }
  86. __attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
  87. mouse_report->x = x;
  88. mouse_report->y = y;
  89. }
  90. __attribute__((weak)) void process_mouse(report_mouse_t* mouse_report) {
  91. report_adns_t data = adns_read_burst();
  92. if (data.dx != 0 || data.dy != 0) {
  93. if (debug_mouse)
  94. dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  95. // Apply delta-X and delta-Y transformations.
  96. // x and y are swapped
  97. // the sensor is rotated
  98. // by 90 degrees
  99. float xt = (float) data.dy * ADNS_X_TRANSFORM;
  100. float yt = (float) data.dx * ADNS_Y_TRANSFORM;
  101. int16_t xti = (int16_t)xt;
  102. int16_t yti = (int16_t)yt;
  103. process_mouse_user(mouse_report, xti, yti);
  104. }
  105. }
  106. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  107. xprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  108. // Update Timer to prevent accidental scrolls
  109. if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
  110. lastMidClick = timer_read();
  111. is_scroll_clicked = record->event.pressed;
  112. }
  113. if (!process_record_user(keycode, record))
  114. return false;
  115. if (keycode == DPI_CONFIG && record->event.pressed) {
  116. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  117. eeconfig_update_kb(keyboard_config.raw);
  118. adns_set_cpi(dpi_array[keyboard_config.dpi_config]);
  119. }
  120. /* If Mousekeys is disabled, then use handle the mouse button
  121. * keycodes. This makes things simpler, and allows usage of
  122. * the keycodes in a consistent manner. But only do this if
  123. * Mousekeys is not enable, so it's not handled twice.
  124. */
  125. #ifndef MOUSEKEY_ENABLE
  126. if (IS_MOUSEKEY_BUTTON(keycode)) {
  127. report_mouse_t currentReport = pointing_device_get_report();
  128. if (record->event.pressed) {
  129. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  130. } else {
  131. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  132. }
  133. pointing_device_set_report(currentReport);
  134. pointing_device_send();
  135. }
  136. #endif
  137. return true;
  138. }
  139. // Hardware Setup
  140. void keyboard_pre_init_kb(void) {
  141. // debug_enable = true;
  142. // debug_matrix = true;
  143. debug_mouse = true;
  144. // debug_encoder = true;
  145. setPinInput(OPT_ENC1);
  146. setPinInput(OPT_ENC2);
  147. /* Ground all output pins connected to ground. This provides additional
  148. * pathways to ground. If you're messing with this, know this: driving ANY
  149. * of these pins high will cause a short. On the MCU. Ka-blooey.
  150. */
  151. #ifdef UNUSED_PINS
  152. const pin_t unused_pins[] = UNUSED_PINS;
  153. for (uint8_t i = 0; i < (sizeof(unused_pins) / sizeof(pin_t)); i++) {
  154. setPinOutput(unused_pins[i]);
  155. writePinLow(unused_pins[i]);
  156. }
  157. #endif
  158. keyboard_pre_init_user();
  159. }
  160. void pointing_device_init(void) {
  161. adns_init();
  162. opt_encoder_init();
  163. }
  164. void pointing_device_task(void) {
  165. report_mouse_t mouse_report = pointing_device_get_report();
  166. process_wheel(&mouse_report);
  167. process_mouse(&mouse_report);
  168. pointing_device_set_report(mouse_report);
  169. pointing_device_send();
  170. }
  171. void eeconfig_init_kb(void) {
  172. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  173. eeconfig_update_kb(keyboard_config.raw);
  174. eeconfig_init_user();
  175. }
  176. void matrix_init_kb(void) {
  177. // is safe to just read DPI setting since matrix init
  178. // comes before pointing device init.
  179. keyboard_config.raw = eeconfig_read_kb();
  180. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  181. eeconfig_init_kb();
  182. }
  183. matrix_init_user();
  184. }
  185. void keyboard_post_init_kb(void) {
  186. adns_set_cpi(dpi_array[keyboard_config.dpi_config]);
  187. keyboard_post_init_user();
  188. }