pointing_device_drivers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #define CONSTRAIN_HID(amt) ((amt) < INT8_MIN ? INT8_MIN : ((amt) > INT8_MAX ? INT8_MAX : (amt)))
  24. #define CONSTRAIN_HID_XY(amt) ((amt) < XY_REPORT_MIN ? XY_REPORT_MIN : ((amt) > XY_REPORT_MAX ? XY_REPORT_MAX : (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 = (mouse_xy_report_t)data.dx;
  34. mouse_report.y = (mouse_xy_report_t)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. mouse_report.x = CONSTRAIN_HID_XY(sensor_report.x);
  50. mouse_report.y = CONSTRAIN_HID_XY(sensor_report.y);
  51. return mouse_report;
  52. }
  53. // clang-format off
  54. const pointing_device_driver_t pointing_device_driver = {
  55. .init = adns9800_init,
  56. .get_report = adns9800_get_report_driver,
  57. .set_cpi = adns9800_set_cpi,
  58. .get_cpi = adns9800_get_cpi
  59. };
  60. // clang-format on
  61. #elif defined(POINTING_DEVICE_DRIVER_analog_joystick)
  62. report_mouse_t analog_joystick_get_report(report_mouse_t mouse_report) {
  63. report_analog_joystick_t data = analog_joystick_read();
  64. # ifdef CONSOLE_ENABLE
  65. if (debug_mouse) dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);
  66. # endif
  67. mouse_report.x = data.x;
  68. mouse_report.y = data.y;
  69. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, data.button, POINTING_DEVICE_BUTTON1);
  70. return mouse_report;
  71. }
  72. // clang-format off
  73. const pointing_device_driver_t pointing_device_driver = {
  74. .init = analog_joystick_init,
  75. .get_report = analog_joystick_get_report,
  76. .set_cpi = NULL,
  77. .get_cpi = NULL
  78. };
  79. // clang-format on
  80. #elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
  81. # ifndef CIRQUE_PINNACLE_TAPPING_TERM
  82. # include "action.h"
  83. # include "action_tapping.h"
  84. # define CIRQUE_PINNACLE_TAPPING_TERM GET_TAPPING_TERM(KC_BTN1, &(keyrecord_t){})
  85. # endif
  86. # ifndef CIRQUE_PINNACLE_TOUCH_DEBOUNCE
  87. # define CIRQUE_PINNACLE_TOUCH_DEBOUNCE (CIRQUE_PINNACLE_TAPPING_TERM * 8)
  88. # endif
  89. report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) {
  90. pinnacle_data_t touchData = cirque_pinnacle_read_data();
  91. mouse_xy_report_t report_x = 0, report_y = 0;
  92. static mouse_xy_report_t x = 0, y = 0;
  93. static uint16_t mouse_timer = 0;
  94. static bool is_z_down = false;
  95. # if !CIRQUE_PINNACLE_POSITION_MODE
  96. # error Cirque Pinnacle with relative mode not implemented yet.
  97. # endif
  98. if (!touchData.valid) {
  99. return mouse_report;
  100. }
  101. # if CONSOLE_ENABLE
  102. if (debug_mouse && touchData.touchDown) {
  103. dprintf("cirque_pinnacle touchData x=%4d y=%4d z=%2d\n", touchData.xValue, touchData.yValue, touchData.zValue);
  104. }
  105. # endif
  106. // Scale coordinates to arbitrary X, Y resolution
  107. cirque_pinnacle_scale_data(&touchData, cirque_pinnacle_get_scale(), cirque_pinnacle_get_scale());
  108. if (x && y && touchData.xValue && touchData.yValue) {
  109. report_x = (mouse_xy_report_t)(touchData.xValue - x);
  110. report_y = (mouse_xy_report_t)(touchData.yValue - y);
  111. }
  112. x = touchData.xValue;
  113. y = touchData.yValue;
  114. mouse_report.x = report_x;
  115. mouse_report.y = report_y;
  116. if (touchData.touchDown != is_z_down) {
  117. is_z_down = touchData.touchDown;
  118. if (!touchData.zValue) {
  119. if (timer_elapsed(mouse_timer) < CIRQUE_PINNACLE_TAPPING_TERM && mouse_timer != 0) {
  120. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  121. pointing_device_set_report(mouse_report);
  122. pointing_device_send();
  123. # if TAP_CODE_DELAY > 0
  124. wait_ms(TAP_CODE_DELAY);
  125. # endif
  126. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  127. pointing_device_set_report(mouse_report);
  128. pointing_device_send();
  129. }
  130. }
  131. mouse_timer = timer_read();
  132. }
  133. if (timer_elapsed(mouse_timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
  134. mouse_timer = 0;
  135. }
  136. return mouse_report;
  137. }
  138. // clang-format off
  139. const pointing_device_driver_t pointing_device_driver = {
  140. .init = cirque_pinnacle_init,
  141. .get_report = cirque_pinnacle_get_report,
  142. .set_cpi = cirque_pinnacle_set_scale,
  143. .get_cpi = cirque_pinnacle_get_scale
  144. };
  145. // clang-format on
  146. #elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
  147. mouse_xy_report_t pimoroni_trackball_adapt_values(clamp_range_t* offset) {
  148. if (*offset > XY_REPORT_MAX) {
  149. *offset -= XY_REPORT_MAX;
  150. return (mouse_xy_report_t)XY_REPORT_MAX;
  151. } else if (*offset < XY_REPORT_MIN) {
  152. *offset += XY_REPORT_MAX;
  153. return (mouse_xy_report_t)XY_REPORT_MIN;
  154. } else {
  155. mouse_xy_report_t temp_return = *offset;
  156. *offset = 0;
  157. return temp_return;
  158. }
  159. }
  160. report_mouse_t pimoroni_trackball_get_report(report_mouse_t mouse_report) {
  161. static uint16_t debounce = 0;
  162. static uint8_t error_count = 0;
  163. pimoroni_data_t pimoroni_data = {0};
  164. static clamp_range_t x_offset = 0, y_offset = 0;
  165. if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT) {
  166. i2c_status_t status = read_pimoroni_trackball(&pimoroni_data);
  167. if (status == I2C_STATUS_SUCCESS) {
  168. error_count = 0;
  169. if (!(pimoroni_data.click & 128)) {
  170. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  171. if (!debounce) {
  172. x_offset += pimoroni_trackball_get_offsets(pimoroni_data.right, pimoroni_data.left, PIMORONI_TRACKBALL_SCALE);
  173. y_offset += pimoroni_trackball_get_offsets(pimoroni_data.down, pimoroni_data.up, PIMORONI_TRACKBALL_SCALE);
  174. mouse_report.x = pimoroni_trackball_adapt_values(&x_offset);
  175. mouse_report.y = pimoroni_trackball_adapt_values(&y_offset);
  176. } else {
  177. debounce--;
  178. }
  179. } else {
  180. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  181. debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES;
  182. }
  183. } else {
  184. error_count++;
  185. }
  186. }
  187. return mouse_report;
  188. }
  189. // clang-format off
  190. const pointing_device_driver_t pointing_device_driver = {
  191. .init = pimoroni_trackball_device_init,
  192. .get_report = pimoroni_trackball_get_report,
  193. .set_cpi = pimoroni_trackball_set_cpi,
  194. .get_cpi = pimoroni_trackball_get_cpi
  195. };
  196. // clang-format on
  197. #elif defined(POINTING_DEVICE_DRIVER_pmw3360)
  198. static void pmw3360_device_init(void) {
  199. pmw3360_init(0);
  200. }
  201. report_mouse_t pmw3360_get_report(report_mouse_t mouse_report) {
  202. report_pmw3360_t data = pmw3360_read_burst(0);
  203. static uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  204. if (data.isOnSurface && data.isMotion) {
  205. // Reset timer if stopped moving
  206. if (!data.isMotion) {
  207. if (MotionStart != 0) MotionStart = 0;
  208. return mouse_report;
  209. }
  210. // Set timer if new motion
  211. if ((MotionStart == 0) && data.isMotion) {
  212. # ifdef CONSOLE_ENABLE
  213. if (debug_mouse) dprintf("Starting motion.\n");
  214. # endif
  215. MotionStart = timer_read();
  216. }
  217. mouse_report.x = CONSTRAIN_HID_XY(data.dx);
  218. mouse_report.y = CONSTRAIN_HID_XY(data.dy);
  219. }
  220. return mouse_report;
  221. }
  222. // clang-format off
  223. const pointing_device_driver_t pointing_device_driver = {
  224. .init = pmw3360_device_init,
  225. .get_report = pmw3360_get_report,
  226. .set_cpi = pmw3360_set_cpi,
  227. .get_cpi = pmw3360_get_cpi
  228. };
  229. // clang-format on
  230. #elif defined(POINTING_DEVICE_DRIVER_pmw3389)
  231. static void pmw3389_device_init(void) {
  232. pmw3389_init();
  233. }
  234. report_mouse_t pmw3389_get_report(report_mouse_t mouse_report) {
  235. report_pmw3389_t data = pmw3389_read_burst();
  236. static uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
  237. if (data.isOnSurface && data.isMotion) {
  238. // Reset timer if stopped moving
  239. if (!data.isMotion) {
  240. if (MotionStart != 0) MotionStart = 0;
  241. return mouse_report;
  242. }
  243. // Set timer if new motion
  244. if ((MotionStart == 0) && data.isMotion) {
  245. # ifdef CONSOLE_ENABLE
  246. if (debug_mouse) dprintf("Starting motion.\n");
  247. # endif
  248. MotionStart = timer_read();
  249. }
  250. mouse_report.x = CONSTRAIN_HID_XY(data.dx);
  251. mouse_report.y = CONSTRAIN_HID_XY(data.dy);
  252. }
  253. return mouse_report;
  254. }
  255. // clang-format off
  256. const pointing_device_driver_t pointing_device_driver = {
  257. .init = pmw3389_device_init,
  258. .get_report = pmw3389_get_report,
  259. .set_cpi = pmw3389_set_cpi,
  260. .get_cpi = pmw3389_get_cpi
  261. };
  262. // clang-format on
  263. #else
  264. __attribute__((weak)) void pointing_device_driver_init(void) {}
  265. __attribute__((weak)) report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) {
  266. return mouse_report;
  267. }
  268. __attribute__((weak)) uint16_t pointing_device_driver_get_cpi(void) {
  269. return 0;
  270. }
  271. __attribute__((weak)) void pointing_device_driver_set_cpi(uint16_t cpi) {}
  272. // clang-format off
  273. const pointing_device_driver_t pointing_device_driver = {
  274. .init = pointing_device_driver_init,
  275. .get_report = pointing_device_driver_get_report,
  276. .get_cpi = pointing_device_driver_get_cpi,
  277. .set_cpi = pointing_device_driver_set_cpi
  278. };
  279. // clang-format on
  280. #endif