ploopyco.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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] = ENCODERS_PAD_A;
  68. pin_t encoder_pins_b[1] = ENCODERS_PAD_B;
  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 ? KC_WH_U : KC_WH_D);
  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. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  116. if (is_drag_scroll) {
  117. scroll_accumulated_h += (float)mouse_report.x / PLOOPY_DRAGSCROLL_DIVISOR_H;
  118. scroll_accumulated_v += (float)mouse_report.y / PLOOPY_DRAGSCROLL_DIVISOR_V;
  119. // Assign integer parts of accumulated scroll values to the mouse report
  120. mouse_report.h = (int8_t)scroll_accumulated_h;
  121. #ifdef PLOOPY_DRAGSCROLL_INVERT
  122. mouse_report.v = -(int8_t)scroll_accumulated_v;
  123. #else
  124. mouse_report.v = (int8_t)scroll_accumulated_v;
  125. #endif
  126. // Update accumulated scroll values by subtracting the integer parts
  127. scroll_accumulated_h -= (int8_t)scroll_accumulated_h;
  128. scroll_accumulated_v -= (int8_t)scroll_accumulated_v;
  129. // Clear the X and Y values of the mouse report
  130. mouse_report.x = 0;
  131. mouse_report.y = 0;
  132. mouse_report.x = 0;
  133. mouse_report.y = 0;
  134. }
  135. return pointing_device_task_user(mouse_report);
  136. }
  137. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  138. if (debug_mouse) {
  139. dprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  140. }
  141. // Update Timer to prevent accidental scrolls
  142. #ifdef ENCODER_ENABLE
  143. if ((record->event.key.col == ENCODER_BUTTON_COL) && (record->event.key.row == ENCODER_BUTTON_ROW)) {
  144. lastMidClick = timer_read();
  145. is_scroll_clicked = record->event.pressed;
  146. }
  147. #endif
  148. if (!process_record_user(keycode, record)) {
  149. return false;
  150. }
  151. if (keycode == DPI_CONFIG && record->event.pressed) {
  152. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  153. eeconfig_update_kb(keyboard_config.raw);
  154. pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
  155. }
  156. if (keycode == DRAG_SCROLL) {
  157. #ifdef PLOOPY_DRAGSCROLL_MOMENTARY
  158. is_drag_scroll = record->event.pressed;
  159. #else
  160. if (record->event.pressed) {
  161. is_drag_scroll ^= 1;
  162. }
  163. #endif
  164. }
  165. return true;
  166. }
  167. // Hardware Setup
  168. void keyboard_pre_init_kb(void) {
  169. // debug_enable = true;
  170. // debug_matrix = true;
  171. // debug_mouse = true;
  172. // debug_encoder = true;
  173. /* Ground all output pins connected to ground. This provides additional
  174. * pathways to ground. If you're messing with this, know this: driving ANY
  175. * of these pins high will cause a short. On the MCU. Ka-blooey.
  176. */
  177. #ifdef UNUSABLE_PINS
  178. const pin_t unused_pins[] = UNUSABLE_PINS;
  179. for (uint8_t i = 0; i < ARRAY_SIZE(unused_pins); i++) {
  180. gpio_set_pin_output_push_pull(unused_pins[i]);
  181. gpio_write_pin_low(unused_pins[i]);
  182. }
  183. #endif
  184. // This is the debug LED.
  185. #if defined(DEBUG_LED_PIN)
  186. gpio_set_pin_output_push_pull(DEBUG_LED_PIN);
  187. gpio_write_pin(DEBUG_LED_PIN, debug_enable);
  188. #endif
  189. keyboard_pre_init_user();
  190. }
  191. void pointing_device_init_kb(void) {
  192. keyboard_config.raw = eeconfig_read_kb();
  193. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  194. eeconfig_init_kb();
  195. }
  196. pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
  197. }
  198. void eeconfig_init_kb(void) {
  199. keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
  200. eeconfig_update_kb(keyboard_config.raw);
  201. eeconfig_init_user();
  202. }