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.

225 lines
6.9 KiB

  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 QMK_KEYBOARD_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 \
  33. { 1200, 1600, 2400 }
  34. # ifndef PLOOPY_DPI_DEFAULT
  35. # define PLOOPY_DPI_DEFAULT 1
  36. # endif
  37. #endif
  38. #ifndef PLOOPY_DPI_DEFAULT
  39. # define PLOOPY_DPI_DEFAULT 0
  40. #endif
  41. #ifndef PLOOPY_DRAGSCROLL_DPI
  42. # define PLOOPY_DRAGSCROLL_DPI 100 // Fixed-DPI Drag Scroll
  43. #endif
  44. #ifndef PLOOPY_DRAGSCROLL_MULTIPLIER
  45. # define PLOOPY_DRAGSCROLL_MULTIPLIER 0.75 // Variable-DPI Drag Scroll
  46. #endif
  47. keyboard_config_t keyboard_config;
  48. uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
  49. #define DPI_OPTION_SIZE ARRAY_SIZE(dpi_array)
  50. // TODO: Implement libinput profiles
  51. // https://wayland.freedesktop.org/libinput/doc/latest/pointer-acceleration.html
  52. // Compile time accel selection
  53. // Valid options are ACC_NONE, ACC_LINEAR, ACC_CUSTOM, ACC_QUADRATIC
  54. // Trackball State
  55. bool is_scroll_clicked = false;
  56. bool BurstState = false; // init burst state for Trackball module
  57. uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  58. uint16_t lastScroll = 0; // Previous confirmed wheel event
  59. uint16_t lastMidClick = 0; // Stops scrollwheel from being read if it was pressed
  60. uint8_t OptLowPin = OPT_ENC1;
  61. bool debug_encoder = false;
  62. bool is_drag_scroll = false;
  63. __attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) { return true; }
  64. bool encoder_update_kb(uint8_t index, bool clockwise) {
  65. if (!encoder_update_user(index, clockwise)) {
  66. return false;
  67. }
  68. #ifdef MOUSEKEY_ENABLE
  69. tap_code(clockwise ? KC_WH_U : KC_WH_D);
  70. #else
  71. mouse_report_t mouse_report = pointing_device_get_report();
  72. mouse_report.v = clockwise ? 1 : -1;
  73. pointing_device_set_report(mouse_report);
  74. pointing_device_send();
  75. #endif
  76. return true;
  77. }
  78. void process_wheel(void) {
  79. // Lovingly ripped from the Ploopy Source
  80. // If the mouse wheel was just released, do not scroll.
  81. if (timer_elapsed(lastMidClick) < SCROLL_BUTT_DEBOUNCE) {
  82. return;
  83. }
  84. // Limit the number of scrolls per unit time.
  85. if (timer_elapsed(lastScroll) < OPT_DEBOUNCE) {
  86. return;
  87. }
  88. // Don't scroll if the middle button is depressed.
  89. if (is_scroll_clicked) {
  90. #ifndef IGNORE_SCROLL_CLICK
  91. return;
  92. #endif
  93. }
  94. lastScroll = timer_read();
  95. uint16_t p1 = adc_read(OPT_ENC1_MUX);
  96. uint16_t p2 = adc_read(OPT_ENC2_MUX);
  97. if (debug_encoder) dprintf("OPT1: %d, OPT2: %d\n", p1, p2);
  98. int dir = opt_encoder_handler(p1, p2);
  99. if (dir == 0) return;
  100. encoder_update_kb(0, dir > 0);
  101. }
  102. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  103. process_wheel();
  104. if (is_drag_scroll) {
  105. mouse_report.h = mouse_report.x;
  106. #ifdef PLOOPY_DRAGSCROLL_INVERT
  107. // Invert vertical scroll direction
  108. mouse_report.v = -mouse_report.y;
  109. #else
  110. mouse_report.v = mouse_report.y;
  111. #endif
  112. mouse_report.x = 0;
  113. mouse_report.y = 0;
  114. }
  115. return pointing_device_task_user(mouse_report);
  116. }
  117. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  118. if (debug_mouse) {
  119. dprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  120. }
  121. // Update Timer to prevent accidental scrolls
  122. if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
  123. lastMidClick = timer_read();
  124. is_scroll_clicked = record->event.pressed;
  125. }
  126. if (!process_record_user(keycode, record)) {
  127. return false;
  128. }
  129. if (keycode == DPI_CONFIG && record->event.pressed) {
  130. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  131. eeconfig_update_kb(keyboard_config.raw);
  132. pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
  133. }
  134. if (keycode == DRAG_SCROLL) {
  135. #ifndef PLOOPY_DRAGSCROLL_MOMENTARY
  136. if (record->event.pressed)
  137. #endif
  138. {
  139. is_drag_scroll ^= 1;
  140. }
  141. #ifdef PLOOPY_DRAGSCROLL_FIXED
  142. pointing_device_set_cpi(is_drag_scroll ? PLOOPY_DRAGSCROLL_DPI : dpi_array[keyboard_config.dpi_config]);
  143. #else
  144. pointing_device_set_cpi(is_drag_scroll ? (dpi_array[keyboard_config.dpi_config] * PLOOPY_DRAGSCROLL_MULTIPLIER) : dpi_array[keyboard_config.dpi_config]);
  145. #endif
  146. }
  147. return true;
  148. }
  149. // Hardware Setup
  150. void keyboard_pre_init_kb(void) {
  151. // debug_enable = true;
  152. // debug_matrix = true;
  153. // debug_mouse = true;
  154. // debug_encoder = true;
  155. setPinInput(OPT_ENC1);
  156. setPinInput(OPT_ENC2);
  157. /* Ground all output pins connected to ground. This provides additional
  158. * pathways to ground. If you're messing with this, know this: driving ANY
  159. * of these pins high will cause a short. On the MCU. Ka-blooey.
  160. */
  161. #ifdef UNUSABLE_PINS
  162. const pin_t unused_pins[] = UNUSABLE_PINS;
  163. for (uint8_t i = 0; i < ARRAY_SIZE(unused_pins); i++) {
  164. setPinOutput(unused_pins[i]);
  165. writePinLow(unused_pins[i]);
  166. }
  167. #endif
  168. // This is the debug LED.
  169. #if defined(DEBUG_LED_PIN)
  170. setPinOutput(DEBUG_LED_PIN);
  171. writePin(DEBUG_LED_PIN, debug_enable);
  172. #endif
  173. keyboard_pre_init_user();
  174. }
  175. void pointing_device_init_kb(void) {
  176. pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
  177. // initialize the scroll wheel's optical encoder
  178. opt_encoder_init();
  179. }
  180. void eeconfig_init_kb(void) {
  181. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  182. eeconfig_update_kb(keyboard_config.raw);
  183. eeconfig_init_user();
  184. }
  185. void matrix_init_kb(void) {
  186. // is safe to just read DPI setting since matrix init
  187. // comes before pointing device init.
  188. keyboard_config.raw = eeconfig_read_kb();
  189. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  190. eeconfig_init_kb();
  191. }
  192. matrix_init_user();
  193. }