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.

295 lines
10 KiB

  1. /* Copyright 2017 Joshua Broekhuijsen <snipeye+qmk@gmail.com>
  2. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. * Copyright 2021 Dasky (@daskygit)
  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 "pointing_device.h"
  19. #include "debug.h"
  20. #include "wait.h"
  21. #include "timer.h"
  22. #include <stddef.h>
  23. // hid mouse reports cannot exceed -127 to 127, so constrain to that value
  24. #define constrain_hid(amt) ((amt) < -127 ? -127 : ((amt) > 127 ? 127 : (amt)))
  25. // get_report functions should probably be moved to their respective drivers.
  26. #if defined(POINTING_DEVICE_DRIVER_adns5050)
  27. report_mouse_t adns5050_get_report(report_mouse_t mouse_report) {
  28. report_adns5050_t data = adns5050_read_burst();
  29. if (data.dx != 0 || data.dy != 0) {
  30. # ifdef CONSOLE_ENABLE
  31. if (debug_mouse) dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  32. # endif
  33. mouse_report.x = data.dx;
  34. mouse_report.y = data.dy;
  35. }
  36. return mouse_report;
  37. }
  38. // clang-format off
  39. const pointing_device_driver_t pointing_device_driver = {
  40. .init = adns5050_init,
  41. .get_report = adns5050_get_report,
  42. .set_cpi = adns5050_set_cpi,
  43. .get_cpi = adns5050_get_cpi,
  44. };
  45. // clang-format on
  46. #elif defined(POINTING_DEVICE_DRIVER_adns9800)
  47. report_mouse_t adns9800_get_report_driver(report_mouse_t mouse_report) {
  48. report_adns9800_t sensor_report = adns9800_get_report();
  49. int8_t clamped_x = constrain_hid(sensor_report.x);
  50. int8_t clamped_y = constrain_hid(sensor_report.y);
  51. mouse_report.x = clamped_x;
  52. mouse_report.y = clamped_y;
  53. return mouse_report;
  54. }
  55. // clang-format off
  56. const pointing_device_driver_t pointing_device_driver = {
  57. .init = adns9800_init,
  58. .get_report = adns9800_get_report_driver,
  59. .set_cpi = adns9800_set_cpi,
  60. .get_cpi = adns9800_get_cpi
  61. };
  62. // clang-format on
  63. #elif defined(POINTING_DEVICE_DRIVER_analog_joystick)
  64. report_mouse_t analog_joystick_get_report(report_mouse_t mouse_report) {
  65. report_analog_joystick_t data = analog_joystick_read();
  66. # ifdef CONSOLE_ENABLE
  67. if (debug_mouse) dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);
  68. # endif
  69. mouse_report.x = data.x;
  70. mouse_report.y = data.y;
  71. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, data.button, POINTING_DEVICE_BUTTON1);
  72. return mouse_report;
  73. }
  74. // clang-format off
  75. const pointing_device_driver_t pointing_device_driver = {
  76. .init = analog_joystick_init,
  77. .get_report = analog_joystick_get_report,
  78. .set_cpi = NULL,
  79. .get_cpi = NULL
  80. };
  81. // clang-format on
  82. #elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
  83. # ifndef CIRQUE_PINNACLE_TAPPING_TERM
  84. # include "action.h"
  85. # include "action_tapping.h"
  86. # define CIRQUE_PINNACLE_TAPPING_TERM GET_TAPPING_TERM(KC_BTN1, &(keyrecord_t){})
  87. # endif
  88. # ifndef CIRQUE_PINNACLE_TOUCH_DEBOUNCE
  89. # define CIRQUE_PINNACLE_TOUCH_DEBOUNCE (CIRQUE_PINNACLE_TAPPING_TERM * 8)
  90. # endif
  91. report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) {
  92. pinnacle_data_t touchData = cirque_pinnacle_read_data();
  93. static uint16_t x = 0, y = 0, mouse_timer = 0;
  94. int8_t report_x = 0, report_y = 0;
  95. static bool is_z_down = false;
  96. cirque_pinnacle_scale_data(&touchData, cirque_pinnacle_get_scale(), cirque_pinnacle_get_scale()); // Scale coordinates to arbitrary X, Y resolution
  97. if (x && y && touchData.xValue && touchData.yValue) {
  98. report_x = (int8_t)(touchData.xValue - x);
  99. report_y = (int8_t)(touchData.yValue - y);
  100. }
  101. x = touchData.xValue;
  102. y = touchData.yValue;
  103. if ((bool)touchData.zValue != is_z_down) {
  104. is_z_down = (bool)touchData.zValue;
  105. if (!touchData.zValue) {
  106. if (timer_elapsed(mouse_timer) < CIRQUE_PINNACLE_TAPPING_TERM && mouse_timer != 0) {
  107. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  108. pointing_device_set_report(mouse_report);
  109. pointing_device_send();
  110. # if TAP_CODE_DELAY > 0
  111. wait_ms(TAP_CODE_DELAY);
  112. # endif
  113. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  114. pointing_device_set_report(mouse_report);
  115. pointing_device_send();
  116. }
  117. }
  118. mouse_timer = timer_read();
  119. }
  120. if (timer_elapsed(mouse_timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
  121. mouse_timer = 0;
  122. }
  123. mouse_report.x = report_x;
  124. mouse_report.y = report_y;
  125. return mouse_report;
  126. }
  127. // clang-format off
  128. const pointing_device_driver_t pointing_device_driver = {
  129. .init = cirque_pinnacle_init,
  130. .get_report = cirque_pinnacle_get_report,
  131. .set_cpi = cirque_pinnacle_set_scale,
  132. .get_cpi = cirque_pinnacle_get_scale
  133. };
  134. // clang-format on
  135. #elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
  136. report_mouse_t pimoroni_trackball_get_report(report_mouse_t mouse_report) {
  137. static uint16_t debounce = 0;
  138. static uint8_t error_count = 0;
  139. pimoroni_data_t pimoroni_data = {0};
  140. static int16_t x_offset = 0, y_offset = 0;
  141. if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT) {
  142. i2c_status_t status = read_pimoroni_trackball(&pimoroni_data);
  143. if (status == I2C_STATUS_SUCCESS) {
  144. error_count = 0;
  145. if (!(pimoroni_data.click & 128)) {
  146. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  147. if (!debounce) {
  148. x_offset += pimoroni_trackball_get_offsets(pimoroni_data.right, pimoroni_data.left, PIMORONI_TRACKBALL_SCALE);
  149. y_offset += pimoroni_trackball_get_offsets(pimoroni_data.down, pimoroni_data.up, PIMORONI_TRACKBALL_SCALE);
  150. pimoroni_trackball_adapt_values(&mouse_report.x, &x_offset);
  151. pimoroni_trackball_adapt_values(&mouse_report.y, &y_offset);
  152. } else {
  153. debounce--;
  154. }
  155. } else {
  156. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  157. debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES;
  158. }
  159. } else {
  160. error_count++;
  161. }
  162. }
  163. return mouse_report;
  164. }
  165. // clang-format off
  166. const pointing_device_driver_t pointing_device_driver = {
  167. .init = pimoroni_trackball_device_init,
  168. .get_report = pimoroni_trackball_get_report,
  169. .set_cpi = pimoroni_trackball_set_cpi,
  170. .get_cpi = pimoroni_trackball_get_cpi
  171. };
  172. // clang-format on
  173. #elif defined(POINTING_DEVICE_DRIVER_pmw3360)
  174. static void pmw3360_device_init(void) {
  175. pmw3360_init(0);
  176. }
  177. report_mouse_t pmw3360_get_report(report_mouse_t mouse_report) {
  178. report_pmw3360_t data = pmw3360_read_burst(0);
  179. static uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  180. if (data.isOnSurface && data.isMotion) {
  181. // Reset timer if stopped moving
  182. if (!data.isMotion) {
  183. if (MotionStart != 0) MotionStart = 0;
  184. return mouse_report;
  185. }
  186. // Set timer if new motion
  187. if ((MotionStart == 0) && data.isMotion) {
  188. # ifdef CONSOLE_ENABLE
  189. if (debug_mouse) dprintf("Starting motion.\n");
  190. # endif
  191. MotionStart = timer_read();
  192. }
  193. mouse_report.x = constrain_hid(data.dx);
  194. mouse_report.y = constrain_hid(data.dy);
  195. }
  196. return mouse_report;
  197. }
  198. // clang-format off
  199. const pointing_device_driver_t pointing_device_driver = {
  200. .init = pmw3360_device_init,
  201. .get_report = pmw3360_get_report,
  202. .set_cpi = pmw3360_set_cpi,
  203. .get_cpi = pmw3360_get_cpi
  204. };
  205. // clang-format on
  206. #elif defined(POINTING_DEVICE_DRIVER_pmw3389)
  207. static void pmw3389_device_init(void) {
  208. pmw3389_init();
  209. }
  210. report_mouse_t pmw3389_get_report(report_mouse_t mouse_report) {
  211. report_pmw3389_t data = pmw3389_read_burst();
  212. static uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  213. if (data.isOnSurface && data.isMotion) {
  214. // Reset timer if stopped moving
  215. if (!data.isMotion) {
  216. if (MotionStart != 0) MotionStart = 0;
  217. return mouse_report;
  218. }
  219. // Set timer if new motion
  220. if ((MotionStart == 0) && data.isMotion) {
  221. # ifdef CONSOLE_ENABLE
  222. if (debug_mouse) dprintf("Starting motion.\n");
  223. # endif
  224. MotionStart = timer_read();
  225. }
  226. mouse_report.x = constrain_hid(data.dx);
  227. mouse_report.y = constrain_hid(data.dy);
  228. }
  229. return mouse_report;
  230. }
  231. // clang-format off
  232. const pointing_device_driver_t pointing_device_driver = {
  233. .init = pmw3389_device_init,
  234. .get_report = pmw3389_get_report,
  235. .set_cpi = pmw3389_set_cpi,
  236. .get_cpi = pmw3389_get_cpi
  237. };
  238. // clang-format on
  239. #else
  240. __attribute__((weak)) void pointing_device_driver_init(void) {}
  241. __attribute__((weak)) report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) {
  242. return mouse_report;
  243. }
  244. __attribute__((weak)) uint16_t pointing_device_driver_get_cpi(void) {
  245. return 0;
  246. }
  247. __attribute__((weak)) void pointing_device_driver_set_cpi(uint16_t cpi) {}
  248. // clang-format off
  249. const pointing_device_driver_t pointing_device_driver = {
  250. .init = pointing_device_driver_init,
  251. .get_report = pointing_device_driver_get_report,
  252. .get_cpi = pointing_device_driver_get_cpi,
  253. .set_cpi = pointing_device_driver_set_cpi
  254. };
  255. // clang-format on
  256. #endif