ploopyco.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "ploopyco.h"
  19. #include "analog.h"
  20. #include "opt_encoder.h"
  21. // for legacy support
  22. #if defined(OPT_DEBOUNCE) && !defined(PLOOPY_SCROLL_DEBOUNCE)
  23. # define PLOOPY_SCROLL_DEBOUNCE OPT_DEBOUNCE
  24. #endif
  25. #if defined(SCROLL_BUTT_DEBOUNCE) && !defined(PLOOPY_SCROLL_BUTTON_DEBOUNCE)
  26. # define PLOOPY_SCROLL_BUTTON_DEBOUNCE SCROLL_BUTT_DEBOUNCE
  27. #endif
  28. #ifndef PLOOPY_SCROLL_DEBOUNCE
  29. # define PLOOPY_SCROLL_DEBOUNCE 5
  30. #endif
  31. #ifndef PLOOPY_SCROLL_BUTTON_DEBOUNCE
  32. # define PLOOPY_SCROLL_BUTTON_DEBOUNCE 100
  33. #endif
  34. #ifndef PLOOPY_DPI_OPTIONS
  35. # define PLOOPY_DPI_OPTIONS \
  36. { 600, 900, 1200, 1600, 2400 }
  37. # ifndef PLOOPY_DPI_DEFAULT
  38. # define PLOOPY_DPI_DEFAULT 1
  39. # endif
  40. #endif
  41. #ifndef PLOOPY_DPI_DEFAULT
  42. # define PLOOPY_DPI_DEFAULT 0
  43. #endif
  44. #ifndef PLOOPY_DRAGSCROLL_DIVISOR_H
  45. # define PLOOPY_DRAGSCROLL_DIVISOR_H 8.0
  46. #endif
  47. #ifndef PLOOPY_DRAGSCROLL_DIVISOR_V
  48. # define PLOOPY_DRAGSCROLL_DIVISOR_V 8.0
  49. #endif
  50. #ifndef ENCODER_BUTTON_ROW
  51. # define ENCODER_BUTTON_ROW 0
  52. #endif
  53. #ifndef ENCODER_BUTTON_COL
  54. # define ENCODER_BUTTON_COL 0
  55. #endif
  56. keyboard_config_t keyboard_config;
  57. uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
  58. #define DPI_OPTION_SIZE ARRAY_SIZE(dpi_array)
  59. // Trackball State
  60. bool is_scroll_clicked = false;
  61. bool is_drag_scroll = false;
  62. float scroll_accumulated_h = 0;
  63. float scroll_accumulated_v = 0;
  64. #ifdef ENCODER_ENABLE
  65. uint16_t lastScroll = 0; // Previous confirmed wheel event
  66. uint16_t lastMidClick = 0; // Stops scrollwheel from being read if it was pressed
  67. pin_t encoder_pins_a[1] = ENCODER_A_PINS;
  68. pin_t encoder_pins_b[1] = ENCODER_B_PINS;
  69. bool debug_encoder = false;
  70. bool encoder_update_kb(uint8_t index, bool clockwise) {
  71. if (!encoder_update_user(index, clockwise)) {
  72. return false;
  73. }
  74. # ifdef MOUSEKEY_ENABLE
  75. tap_code(clockwise ? MS_WHLU : MS_WHLD);
  76. # else
  77. report_mouse_t mouse_report = pointing_device_get_report();
  78. mouse_report.v = clockwise ? 1 : -1;
  79. pointing_device_set_report(mouse_report);
  80. pointing_device_send();
  81. # endif
  82. return true;
  83. }
  84. void encoder_driver_init(void) {
  85. for (uint8_t i = 0; i < ARRAY_SIZE(encoder_pins_a); i++) {
  86. gpio_set_pin_input(encoder_pins_a[i]);
  87. gpio_set_pin_input(encoder_pins_b[i]);
  88. }
  89. opt_encoder_init();
  90. }
  91. void encoder_driver_task(void) {
  92. uint16_t p1 = analogReadPin(encoder_pins_a[0]);
  93. uint16_t p2 = analogReadPin(encoder_pins_b[0]);
  94. if (debug_encoder) dprintf("OPT1: %d, OPT2: %d\n", p1, p2);
  95. int8_t dir = opt_encoder_handler(p1, p2);
  96. // If the mouse wheel was just released, do not scroll.
  97. if (timer_elapsed(lastMidClick) < PLOOPY_SCROLL_BUTTON_DEBOUNCE) {
  98. return;
  99. }
  100. // Limit the number of scrolls per unit time.
  101. if (timer_elapsed(lastScroll) < PLOOPY_SCROLL_DEBOUNCE) {
  102. return;
  103. }
  104. // Don't scroll if the middle button is depressed.
  105. if (is_scroll_clicked) {
  106. # ifndef PLOOPY_IGNORE_SCROLL_CLICK
  107. return;
  108. # endif
  109. }
  110. if (dir == 0) return;
  111. encoder_queue_event(0, dir > 0);
  112. lastScroll = timer_read();
  113. }
  114. #endif
  115. void toggle_drag_scroll(void) {
  116. is_drag_scroll ^= 1;
  117. }
  118. void cycle_dpi(void) {
  119. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  120. eeconfig_update_kb(keyboard_config.raw);
  121. pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
  122. }
  123. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  124. mouse_report = pointing_device_task_user(mouse_report);
  125. if (is_drag_scroll) {
  126. scroll_accumulated_h += (float)mouse_report.x / PLOOPY_DRAGSCROLL_DIVISOR_H;
  127. scroll_accumulated_v += (float)mouse_report.y / PLOOPY_DRAGSCROLL_DIVISOR_V;
  128. // Assign integer parts of accumulated scroll values to the mouse report
  129. mouse_report.h = (int8_t)scroll_accumulated_h;
  130. #ifdef PLOOPY_DRAGSCROLL_INVERT
  131. mouse_report.v = -(int8_t)scroll_accumulated_v;
  132. #else
  133. mouse_report.v = (int8_t)scroll_accumulated_v;
  134. #endif
  135. // Update accumulated scroll values by subtracting the integer parts
  136. scroll_accumulated_h -= (int8_t)scroll_accumulated_h;
  137. scroll_accumulated_v -= (int8_t)scroll_accumulated_v;
  138. // Clear the X and Y values of the mouse report
  139. mouse_report.x = 0;
  140. mouse_report.y = 0;
  141. mouse_report.x = 0;
  142. mouse_report.y = 0;
  143. }
  144. return mouse_report;
  145. }
  146. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  147. if (debug_mouse) {
  148. dprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  149. }
  150. // Update Timer to prevent accidental scrolls
  151. #ifdef ENCODER_ENABLE
  152. if ((record->event.key.col == ENCODER_BUTTON_COL) && (record->event.key.row == ENCODER_BUTTON_ROW)) {
  153. lastMidClick = timer_read();
  154. is_scroll_clicked = record->event.pressed;
  155. }
  156. #endif
  157. if (!process_record_user(keycode, record)) {
  158. return false;
  159. }
  160. if (keycode == DPI_CONFIG && record->event.pressed) {
  161. cycle_dpi();
  162. }
  163. if (keycode == DRAG_SCROLL) {
  164. #ifdef PLOOPY_DRAGSCROLL_MOMENTARY
  165. is_drag_scroll = record->event.pressed;
  166. #else
  167. if (record->event.pressed) {
  168. toggle_drag_scroll();
  169. }
  170. #endif
  171. }
  172. return true;
  173. }
  174. // Hardware Setup
  175. void keyboard_pre_init_kb(void) {
  176. // debug_enable = true;
  177. // debug_matrix = true;
  178. // debug_mouse = true;
  179. // debug_encoder = true;
  180. /* Ground all output pins connected to ground. This provides additional
  181. * pathways to ground. If you're messing with this, know this: driving ANY
  182. * of these pins high will cause a short. On the MCU. Ka-blooey.
  183. */
  184. #ifdef UNUSABLE_PINS
  185. const pin_t unused_pins[] = UNUSABLE_PINS;
  186. for (uint8_t i = 0; i < ARRAY_SIZE(unused_pins); i++) {
  187. gpio_set_pin_output_push_pull(unused_pins[i]);
  188. gpio_write_pin_low(unused_pins[i]);
  189. }
  190. #endif
  191. // This is the debug LED.
  192. #if defined(DEBUG_LED_PIN)
  193. gpio_set_pin_output_push_pull(DEBUG_LED_PIN);
  194. gpio_write_pin(DEBUG_LED_PIN, debug_enable);
  195. #endif
  196. keyboard_pre_init_user();
  197. }
  198. void pointing_device_init_kb(void) {
  199. keyboard_config.raw = eeconfig_read_kb();
  200. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  201. eeconfig_init_kb();
  202. }
  203. pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
  204. }
  205. void eeconfig_init_kb(void) {
  206. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  207. eeconfig_update_kb(keyboard_config.raw);
  208. eeconfig_init_user();
  209. }