trackball_nano.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #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. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { };
  42. // Transformation constants for delta-X and delta-Y
  43. const static float ADNS_X_TRANSFORM = -1.0;
  44. const static float ADNS_Y_TRANSFORM = 1.0;
  45. keyboard_config_t keyboard_config;
  46. uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
  47. #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
  48. // TODO: Implement libinput profiles
  49. // https://wayland.freedesktop.org/libinput/doc/latest/pointer-acceleration.html
  50. // Compile time accel selection
  51. // Valid options are ACC_NONE, ACC_LINEAR, ACC_CUSTOM, ACC_QUADRATIC
  52. // Trackball State
  53. bool is_scroll_clicked = false;
  54. bool BurstState = false; // init burst state for Trackball module
  55. uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  56. uint16_t lastScroll = 0; // Previous confirmed wheel event
  57. uint16_t lastMidClick = 0; // Stops scrollwheel from being read if it was pressed
  58. uint8_t OptLowPin = OPT_ENC1;
  59. bool debug_encoder = false;
  60. __attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
  61. mouse_report->x = x;
  62. mouse_report->y = y;
  63. }
  64. __attribute__((weak)) void process_mouse(report_mouse_t* mouse_report) {
  65. report_adns_t data = adns_read_burst();
  66. if (data.dx != 0 || data.dy != 0) {
  67. if (debug_mouse)
  68. dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  69. // Apply delta-X and delta-Y transformations.
  70. // x and y are swapped
  71. // the sensor is rotated
  72. // by 90 degrees
  73. float xt = (float) data.dy * ADNS_X_TRANSFORM;
  74. float yt = (float) data.dx * ADNS_Y_TRANSFORM;
  75. int16_t xti = (int16_t)xt;
  76. int16_t yti = (int16_t)yt;
  77. process_mouse_user(mouse_report, xti, yti);
  78. }
  79. }
  80. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  81. xprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  82. // Update Timer to prevent accidental scrolls
  83. if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
  84. lastMidClick = timer_read();
  85. is_scroll_clicked = record->event.pressed;
  86. }
  87. if (!process_record_user(keycode, record))
  88. return false;
  89. if (keycode == DPI_CONFIG && record->event.pressed) {
  90. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  91. eeconfig_update_kb(keyboard_config.raw);
  92. adns_set_cpi(dpi_array[keyboard_config.dpi_config]);
  93. }
  94. /* If Mousekeys is disabled, then use handle the mouse button
  95. * keycodes. This makes things simpler, and allows usage of
  96. * the keycodes in a consistent manner. But only do this if
  97. * Mousekeys is not enable, so it's not handled twice.
  98. */
  99. #ifndef MOUSEKEY_ENABLE
  100. if (IS_MOUSEKEY_BUTTON(keycode)) {
  101. report_mouse_t currentReport = pointing_device_get_report();
  102. if (record->event.pressed) {
  103. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  104. } else {
  105. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  106. }
  107. pointing_device_set_report(currentReport);
  108. pointing_device_send();
  109. }
  110. #endif
  111. return true;
  112. }
  113. // Hardware Setup
  114. void keyboard_pre_init_kb(void) {
  115. // debug_enable = true;
  116. // debug_matrix = true;
  117. debug_mouse = true;
  118. // debug_encoder = true;
  119. setPinInput(OPT_ENC1);
  120. setPinInput(OPT_ENC2);
  121. /* Ground all output pins connected to ground. This provides additional
  122. * pathways to ground. If you're messing with this, know this: driving ANY
  123. * of these pins high will cause a short. On the MCU. Ka-blooey.
  124. */
  125. #ifdef UNUSED_PINS
  126. const pin_t unused_pins[] = UNUSED_PINS;
  127. for (uint8_t i = 0; i < (sizeof(unused_pins) / sizeof(pin_t)); i++) {
  128. setPinOutput(unused_pins[i]);
  129. writePinLow(unused_pins[i]);
  130. }
  131. #endif
  132. keyboard_pre_init_user();
  133. }
  134. void pointing_device_init(void) {
  135. adns_init();
  136. opt_encoder_init();
  137. }
  138. void pointing_device_task(void) {
  139. report_mouse_t mouse_report = pointing_device_get_report();
  140. process_mouse(&mouse_report);
  141. pointing_device_set_report(mouse_report);
  142. pointing_device_send();
  143. }
  144. void eeconfig_init_kb(void) {
  145. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  146. eeconfig_update_kb(keyboard_config.raw);
  147. eeconfig_init_user();
  148. }
  149. void matrix_init_kb(void) {
  150. // is safe to just read DPI setting since matrix init
  151. // comes before pointing device init.
  152. keyboard_config.raw = eeconfig_read_kb();
  153. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  154. eeconfig_init_kb();
  155. }
  156. matrix_init_user();
  157. }
  158. void keyboard_post_init_kb(void) {
  159. adns_set_cpi(dpi_array[keyboard_config.dpi_config]);
  160. keyboard_post_init_user();
  161. }