trackball_nano.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_nano.h"
  20. #include "wait.h"
  21. #ifndef OPT_DEBOUNCE
  22. # define OPT_DEBOUNCE 5 // (ms) Time between scroll events
  23. #endif
  24. #ifndef SCROLL_BUTT_DEBOUNCE
  25. # define SCROLL_BUTT_DEBOUNCE 100 // (ms) Time between scroll events
  26. #endif
  27. #ifndef OPT_THRES
  28. # define OPT_THRES 150 // (0-1024) Threshold for actication
  29. #endif
  30. #ifndef OPT_SCALE
  31. # define OPT_SCALE 1 // Multiplier for wheel
  32. #endif
  33. #ifndef PLOOPY_DPI_OPTIONS
  34. # define PLOOPY_DPI_OPTIONS { CPI375, CPI750, CPI1375 }
  35. # ifndef PLOOPY_DPI_DEFAULT
  36. # define PLOOPY_DPI_DEFAULT 2
  37. # endif
  38. #endif
  39. #ifndef PLOOPY_DPI_DEFAULT
  40. # define PLOOPY_DPI_DEFAULT 2
  41. #endif
  42. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { };
  43. // Transformation constants for delta-X and delta-Y
  44. const static float ADNS_X_TRANSFORM = -1.0;
  45. const static float ADNS_Y_TRANSFORM = 1.0;
  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. __attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
  62. mouse_report->x = x;
  63. mouse_report->y = y;
  64. }
  65. __attribute__((weak)) void process_mouse(report_mouse_t* mouse_report) {
  66. report_adns_t data = adns_read_burst();
  67. if (data.dx != 0 || data.dy != 0) {
  68. if (debug_mouse)
  69. dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  70. // Apply delta-X and delta-Y transformations.
  71. // x and y are swapped
  72. // the sensor is rotated
  73. // by 90 degrees
  74. float xt = (float) data.dy * ADNS_X_TRANSFORM;
  75. float yt = (float) data.dx * ADNS_Y_TRANSFORM;
  76. int16_t xti = (int16_t)xt;
  77. int16_t yti = (int16_t)yt;
  78. process_mouse_user(mouse_report, xti, yti);
  79. }
  80. }
  81. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  82. xprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  83. // Update Timer to prevent accidental scrolls
  84. if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
  85. lastMidClick = timer_read();
  86. is_scroll_clicked = record->event.pressed;
  87. }
  88. if (!process_record_user(keycode, record))
  89. return false;
  90. if (keycode == DPI_CONFIG && record->event.pressed) {
  91. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  92. eeconfig_update_kb(keyboard_config.raw);
  93. adns_set_cpi(dpi_array[keyboard_config.dpi_config]);
  94. }
  95. /* If Mousekeys is disabled, then use handle the mouse button
  96. * keycodes. This makes things simpler, and allows usage of
  97. * the keycodes in a consistent manner. But only do this if
  98. * Mousekeys is not enable, so it's not handled twice.
  99. */
  100. #ifndef MOUSEKEY_ENABLE
  101. if (IS_MOUSEKEY_BUTTON(keycode)) {
  102. report_mouse_t currentReport = pointing_device_get_report();
  103. if (record->event.pressed) {
  104. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  105. } else {
  106. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  107. }
  108. pointing_device_set_report(currentReport);
  109. pointing_device_send();
  110. }
  111. #endif
  112. return true;
  113. }
  114. // Hardware Setup
  115. void keyboard_pre_init_kb(void) {
  116. // debug_enable = true;
  117. // debug_matrix = true;
  118. // debug_mouse = true;
  119. // debug_encoder = true;
  120. setPinInput(OPT_ENC1);
  121. setPinInput(OPT_ENC2);
  122. /* Ground all output pins connected to ground. This provides additional
  123. * pathways to ground. If you're messing with this, know this: driving ANY
  124. * of these pins high will cause a short. On the MCU. Ka-blooey.
  125. */
  126. #ifdef UNUSED_PINS
  127. const pin_t unused_pins[] = UNUSED_PINS;
  128. for (uint8_t i = 0; i < (sizeof(unused_pins) / sizeof(pin_t)); i++) {
  129. setPinOutput(unused_pins[i]);
  130. writePinLow(unused_pins[i]);
  131. }
  132. #endif
  133. keyboard_pre_init_user();
  134. }
  135. void pointing_device_init(void) {
  136. adns_init();
  137. opt_encoder_init();
  138. // reboot the adns.
  139. // if the adns hasn't initialized yet, this is harmless.
  140. adns_write_reg(REG_CHIP_RESET, 0x5a);
  141. // wait maximum time before adns is ready.
  142. // this ensures that the adns is actuall ready after reset.
  143. wait_ms(55);
  144. // read a burst from the adns and then discard it.
  145. // gets the adns ready for write commands
  146. // (for example, setting the dpi).
  147. adns_read_burst();
  148. // set the DPI.
  149. adns_set_cpi(dpi_array[keyboard_config.dpi_config]);
  150. }
  151. void pointing_device_task(void) {
  152. report_mouse_t mouse_report = pointing_device_get_report();
  153. process_mouse(&mouse_report);
  154. pointing_device_set_report(mouse_report);
  155. pointing_device_send();
  156. }
  157. void eeconfig_init_kb(void) {
  158. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  159. eeconfig_update_kb(keyboard_config.raw);
  160. eeconfig_init_user();
  161. }
  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) {
  167. eeconfig_init_kb();
  168. }
  169. matrix_init_user();
  170. }