trackball.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2019 Sunjun Kim
  3. * Copyright 2020 Ploopy Corporation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "trackball.h"
  19. #ifndef OPT_DEBOUNCE
  20. # define OPT_DEBOUNCE 5 // (ms) Time between scroll events
  21. #endif
  22. #ifndef SCROLL_BUTT_DEBOUNCE
  23. # define SCROLL_BUTT_DEBOUNCE 100 // (ms) Time between scroll events
  24. #endif
  25. #ifndef OPT_THRES
  26. # define OPT_THRES 150 // (0-1024) Threshold for actication
  27. #endif
  28. #ifndef OPT_SCALE
  29. # define OPT_SCALE 1 // Multiplier for wheel
  30. #endif
  31. #ifndef PLOOPY_DPI_OPTIONS
  32. # define PLOOPY_DPI_OPTIONS { 1200, 1600, 2400 }
  33. # ifndef PLOOPY_DPI_DEFAULT
  34. # define PLOOPY_DPI_DEFAULT 1
  35. # endif
  36. #endif
  37. #ifndef PLOOPY_DPI_DEFAULT
  38. # define PLOOPY_DPI_DEFAULT 0
  39. #endif
  40. #ifndef PLOOPY_DRAGSCROLL_DPI
  41. # define PLOOPY_DRAGSCROLL_DPI 100 // Fixed-DPI Drag Scroll
  42. #endif
  43. #ifndef PLOOPY_DRAGSCROLL_MULTIPLIER
  44. # define PLOOPY_DRAGSCROLL_MULTIPLIER 0.75 // Variable-DPI Drag Scroll
  45. #endif
  46. keyboard_config_t keyboard_config;
  47. uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
  48. #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
  49. // TODO: Implement libinput profiles
  50. // https://wayland.freedesktop.org/libinput/doc/latest/pointer-acceleration.html
  51. // Compile time accel selection
  52. // Valid options are ACC_NONE, ACC_LINEAR, ACC_CUSTOM, ACC_QUADRATIC
  53. // Trackball State
  54. bool is_scroll_clicked = false;
  55. bool BurstState = false; // init burst state for Trackball module
  56. uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  57. uint16_t lastScroll = 0; // Previous confirmed wheel event
  58. uint16_t lastMidClick = 0; // Stops scrollwheel from being read if it was pressed
  59. uint8_t OptLowPin = OPT_ENC1;
  60. bool debug_encoder = false;
  61. bool is_drag_scroll = false;
  62. __attribute__((weak)) void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v) {
  63. mouse_report->h = h;
  64. mouse_report->v = v;
  65. }
  66. __attribute__((weak)) void process_wheel(report_mouse_t* mouse_report) {
  67. // TODO: Replace this with interrupt driven code, polling is S L O W
  68. // Lovingly ripped from the Ploopy Source
  69. // If the mouse wheel was just released, do not scroll.
  70. if (timer_elapsed(lastMidClick) < SCROLL_BUTT_DEBOUNCE) {
  71. return;
  72. }
  73. // Limit the number of scrolls per unit time.
  74. if (timer_elapsed(lastScroll) < OPT_DEBOUNCE) {
  75. return;
  76. }
  77. // Don't scroll if the middle button is depressed.
  78. if (is_scroll_clicked) {
  79. #ifndef IGNORE_SCROLL_CLICK
  80. return;
  81. #endif
  82. }
  83. lastScroll = timer_read();
  84. uint16_t p1 = adc_read(OPT_ENC1_MUX);
  85. uint16_t p2 = adc_read(OPT_ENC2_MUX);
  86. if (debug_encoder) dprintf("OPT1: %d, OPT2: %d\n", p1, p2);
  87. uint8_t dir = opt_encoder_handler(p1, p2);
  88. if (dir == 0) return;
  89. process_wheel_user(mouse_report, mouse_report->h, (int)(mouse_report->v + (dir * OPT_SCALE)));
  90. }
  91. __attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
  92. mouse_report->x = x;
  93. mouse_report->y = y;
  94. }
  95. __attribute__((weak)) void process_mouse(report_mouse_t* mouse_report) {
  96. report_pmw_t data = pmw_read_burst();
  97. if (data.isOnSurface && data.isMotion) {
  98. // Reset timer if stopped moving
  99. if (!data.isMotion) {
  100. if (MotionStart != 0) MotionStart = 0;
  101. return;
  102. }
  103. // Set timer if new motion
  104. if ((MotionStart == 0) && data.isMotion) {
  105. if (debug_mouse) dprintf("Starting motion.\n");
  106. MotionStart = timer_read();
  107. }
  108. if (debug_mouse) {
  109. dprintf("Delt] d: %d t: %u\n", abs(data.dx) + abs(data.dy), MotionStart);
  110. }
  111. if (debug_mouse) {
  112. dprintf("Pre ] X: %d, Y: %d\n", data.dx, data.dy);
  113. }
  114. #if defined(PROFILE_LINEAR)
  115. float scale = float(timer_elaspsed(MotionStart)) / 1000.0;
  116. data.dx *= scale;
  117. data.dy *= scale;
  118. #elif defined(PROFILE_INVERSE)
  119. // TODO
  120. #else
  121. // no post processing
  122. #endif
  123. // apply multiplier
  124. // data.dx *= mouse_multiplier;
  125. // data.dy *= mouse_multiplier;
  126. // Wrap to HID size
  127. data.dx = constrain(data.dx, -127, 127);
  128. data.dy = constrain(data.dy, -127, 127);
  129. if (debug_mouse) dprintf("Cons] X: %d, Y: %d\n", data.dx, data.dy);
  130. // dprintf("Elapsed:%u, X: %f Y: %\n", i, pgm_read_byte(firmware_data+i));
  131. process_mouse_user(mouse_report, data.dx, -data.dy);
  132. }
  133. }
  134. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  135. if (true) {
  136. xprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  137. }
  138. // Update Timer to prevent accidental scrolls
  139. if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
  140. lastMidClick = timer_read();
  141. is_scroll_clicked = record->event.pressed;
  142. }
  143. if (!process_record_user(keycode, record)) {
  144. return false;
  145. }
  146. if (keycode == DPI_CONFIG && record->event.pressed) {
  147. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  148. eeconfig_update_kb(keyboard_config.raw);
  149. pmw_set_cpi(dpi_array[keyboard_config.dpi_config]);
  150. }
  151. if (keycode == DRAG_SCROLL) {
  152. #ifndef PLOOPY_DRAGSCROLL_MOMENTARY
  153. if (record->event.pressed)
  154. #endif
  155. {
  156. is_drag_scroll ^= 1;
  157. }
  158. #ifdef PLOOPY_DRAGSCROLL_FIXED
  159. pmw_set_cpi(is_drag_scroll ? PLOOPY_DRAGSCROLL_DPI : dpi_array[keyboard_config.dpi_config]);
  160. #else
  161. pmw_set_cpi(is_drag_scroll ? (dpi_array[keyboard_config.dpi_config] * PLOOPY_DRAGSCROLL_MULTIPLIER) : dpi_array[keyboard_config.dpi_config]);
  162. #endif
  163. }
  164. /* If Mousekeys is disabled, then use handle the mouse button
  165. * keycodes. This makes things simpler, and allows usage of
  166. * the keycodes in a consistent manner. But only do this if
  167. * Mousekeys is not enable, so it's not handled twice.
  168. */
  169. #ifndef MOUSEKEY_ENABLE
  170. if (IS_MOUSEKEY_BUTTON(keycode)) {
  171. report_mouse_t currentReport = pointing_device_get_report();
  172. if (record->event.pressed) {
  173. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  174. } else {
  175. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  176. }
  177. pointing_device_set_report(currentReport);
  178. pointing_device_send();
  179. }
  180. #endif
  181. return true;
  182. }
  183. // Hardware Setup
  184. void keyboard_pre_init_kb(void) {
  185. // debug_enable = true;
  186. // debug_matrix = true;
  187. // debug_mouse = true;
  188. // debug_encoder = true;
  189. setPinInput(OPT_ENC1);
  190. setPinInput(OPT_ENC2);
  191. /* Ground all output pins connected to ground. This provides additional
  192. * pathways to ground. If you're messing with this, know this: driving ANY
  193. * of these pins high will cause a short. On the MCU. Ka-blooey.
  194. */
  195. #ifdef UNUSED_PINS
  196. const pin_t unused_pins[] = UNUSED_PINS;
  197. for (uint8_t i = 0; i < (sizeof(unused_pins) / sizeof(pin_t)); i++) {
  198. setPinOutput(unused_pins[i]);
  199. writePinLow(unused_pins[i]);
  200. }
  201. #endif
  202. // This is the debug LED.
  203. #if defined(DEBUG_LED_PIN)
  204. setPinOutput(DEBUG_LED_PIN);
  205. writePin(DEBUG_LED_PIN, debug_enable);
  206. #endif
  207. keyboard_pre_init_user();
  208. }
  209. void pointing_device_init(void) {
  210. // initialize ball sensor
  211. pmw_spi_init();
  212. // initialize the scroll wheel's optical encoder
  213. opt_encoder_init();
  214. }
  215. bool has_report_changed(report_mouse_t new, report_mouse_t old) { return (new.buttons != old.buttons) || (new.x && new.x != old.x) || (new.y && new.y != old.y) || (new.h && new.h != old.h) || (new.v && new.v != old.v); }
  216. void pointing_device_task(void) {
  217. report_mouse_t mouse_report = pointing_device_get_report();
  218. process_wheel(&mouse_report);
  219. process_mouse(&mouse_report);
  220. if (is_drag_scroll) {
  221. mouse_report.h = mouse_report.x;
  222. mouse_report.v = mouse_report.y;
  223. mouse_report.x = 0;
  224. mouse_report.y = 0;
  225. }
  226. pointing_device_set_report(mouse_report);
  227. pointing_device_send();
  228. }
  229. void pointing_device_send(void) {
  230. static report_mouse_t old_report = {};
  231. report_mouse_t mouseReport = pointing_device_get_report();
  232. // If you need to do other things, like debugging, this is the place to do it.
  233. if (has_report_changed(mouseReport, old_report)) {
  234. host_mouse_send(&mouseReport);
  235. }
  236. // send it and 0 it out except for buttons, so those stay until they are explicity over-ridden using update_pointing_device
  237. mouseReport.x = 0;
  238. mouseReport.y = 0;
  239. mouseReport.v = 0;
  240. mouseReport.h = 0;
  241. pointing_device_set_report(mouseReport);
  242. old_report = mouseReport;
  243. }
  244. void eeconfig_init_kb(void) {
  245. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  246. eeconfig_update_kb(keyboard_config.raw);
  247. }
  248. void matrix_init_kb(void) {
  249. // is safe to just read DPI setting since matrix init
  250. // comes before pointing device init.
  251. keyboard_config.raw = eeconfig_read_kb();
  252. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  253. eeconfig_init_kb();
  254. }
  255. matrix_init_user();
  256. }
  257. void keyboard_post_init_kb(void) {
  258. pmw_set_cpi(dpi_array[keyboard_config.dpi_config]);
  259. keyboard_post_init_user();
  260. }