logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

ploopyco.c (7387B)


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