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
7.0 KiB

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