pointing_device_drivers.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "pointing_device_internal.h"
  20. #include "debug.h"
  21. #include "wait.h"
  22. #include "timer.h"
  23. #include <stddef.h>
  24. #define CONSTRAIN_HID(amt) ((amt) < INT8_MIN ? INT8_MIN : ((amt) > INT8_MAX ? INT8_MAX : (amt)))
  25. #define CONSTRAIN_HID_XY(amt) ((amt) < XY_REPORT_MIN ? XY_REPORT_MIN : ((amt) > XY_REPORT_MAX ? XY_REPORT_MAX : (amt)))
  26. // get_report functions should probably be moved to their respective drivers.
  27. #if defined(POINTING_DEVICE_DRIVER_adns5050)
  28. report_mouse_t adns5050_get_report(report_mouse_t mouse_report) {
  29. report_adns5050_t data = adns5050_read_burst();
  30. if (data.dx != 0 || data.dy != 0) {
  31. pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  32. mouse_report.x = (mouse_xy_report_t)data.dx;
  33. mouse_report.y = (mouse_xy_report_t)data.dy;
  34. }
  35. return mouse_report;
  36. }
  37. // clang-format off
  38. const pointing_device_driver_t pointing_device_driver = {
  39. .init = adns5050_init,
  40. .get_report = adns5050_get_report,
  41. .set_cpi = adns5050_set_cpi,
  42. .get_cpi = adns5050_get_cpi,
  43. };
  44. // clang-format on
  45. #elif defined(POINTING_DEVICE_DRIVER_adns9800)
  46. report_mouse_t adns9800_get_report_driver(report_mouse_t mouse_report) {
  47. report_adns9800_t sensor_report = adns9800_get_report();
  48. mouse_report.x = CONSTRAIN_HID_XY(sensor_report.x);
  49. mouse_report.y = CONSTRAIN_HID_XY(sensor_report.y);
  50. return mouse_report;
  51. }
  52. // clang-format off
  53. const pointing_device_driver_t pointing_device_driver = {
  54. .init = adns9800_init,
  55. .get_report = adns9800_get_report_driver,
  56. .set_cpi = adns9800_set_cpi,
  57. .get_cpi = adns9800_get_cpi
  58. };
  59. // clang-format on
  60. #elif defined(POINTING_DEVICE_DRIVER_analog_joystick)
  61. report_mouse_t analog_joystick_get_report(report_mouse_t mouse_report) {
  62. report_analog_joystick_t data = analog_joystick_read();
  63. pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);
  64. mouse_report.x = data.x;
  65. mouse_report.y = data.y;
  66. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, data.button, POINTING_DEVICE_BUTTON1);
  67. return mouse_report;
  68. }
  69. // clang-format off
  70. const pointing_device_driver_t pointing_device_driver = {
  71. .init = analog_joystick_init,
  72. .get_report = analog_joystick_get_report,
  73. .set_cpi = NULL,
  74. .get_cpi = NULL
  75. };
  76. // clang-format on
  77. #elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
  78. # ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
  79. static bool cursor_glide_enable = true;
  80. static cursor_glide_context_t glide = {.config = {
  81. .coef = 102, /* Good default friction coef */
  82. .interval = 10, /* 100sps */
  83. .trigger_px = 10, /* Default threshold in case of hover, set to 0 if you'd like */
  84. }};
  85. void cirque_pinnacle_enable_cursor_glide(bool enable) {
  86. cursor_glide_enable = enable;
  87. }
  88. void cirque_pinnacle_configure_cursor_glide(float trigger_px) {
  89. glide.config.trigger_px = trigger_px;
  90. }
  91. # endif
  92. # if CIRQUE_PINNACLE_POSITION_MODE
  93. # ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE
  94. static bool is_touch_down;
  95. bool auto_mouse_activation(report_mouse_t mouse_report) {
  96. return is_touch_down || mouse_report.x != 0 || mouse_report.y != 0 || mouse_report.h != 0 || mouse_report.v != 0 || mouse_report.buttons;
  97. }
  98. # endif
  99. report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) {
  100. uint16_t scale = cirque_pinnacle_get_scale();
  101. pinnacle_data_t touchData = cirque_pinnacle_read_data();
  102. mouse_xy_report_t report_x = 0, report_y = 0;
  103. static uint16_t x = 0, y = 0, last_scale = 0;
  104. # if defined(CIRQUE_PINNACLE_TAP_ENABLE)
  105. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  106. # endif
  107. # ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
  108. cursor_glide_t glide_report = {0};
  109. if (cursor_glide_enable) {
  110. glide_report = cursor_glide_check(&glide);
  111. }
  112. # endif
  113. if (!touchData.valid) {
  114. # ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
  115. if (cursor_glide_enable && glide_report.valid) {
  116. report_x = glide_report.dx;
  117. report_y = glide_report.dy;
  118. goto mouse_report_update;
  119. }
  120. # endif
  121. return mouse_report;
  122. }
  123. if (touchData.touchDown) {
  124. pd_dprintf("cirque_pinnacle touchData x=%4d y=%4d z=%2d\n", touchData.xValue, touchData.yValue, touchData.zValue);
  125. }
  126. # ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE
  127. is_touch_down = touchData.touchDown;
  128. # endif
  129. // Scale coordinates to arbitrary X, Y resolution
  130. cirque_pinnacle_scale_data(&touchData, scale, scale);
  131. if (!cirque_pinnacle_gestures(&mouse_report, touchData)) {
  132. if (last_scale && scale == last_scale && x && y && touchData.xValue && touchData.yValue) {
  133. report_x = CONSTRAIN_HID_XY((int16_t)(touchData.xValue - x));
  134. report_y = CONSTRAIN_HID_XY((int16_t)(touchData.yValue - y));
  135. }
  136. x = touchData.xValue;
  137. y = touchData.yValue;
  138. last_scale = scale;
  139. # ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
  140. if (cursor_glide_enable) {
  141. if (touchData.touchDown) {
  142. cursor_glide_update(&glide, report_x, report_y, touchData.zValue);
  143. } else if (!glide_report.valid) {
  144. glide_report = cursor_glide_start(&glide);
  145. if (glide_report.valid) {
  146. report_x = glide_report.dx;
  147. report_y = glide_report.dy;
  148. }
  149. }
  150. }
  151. # endif
  152. }
  153. # ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
  154. mouse_report_update:
  155. # endif
  156. mouse_report.x = report_x;
  157. mouse_report.y = report_y;
  158. return mouse_report;
  159. }
  160. uint16_t cirque_pinnacle_get_cpi(void) {
  161. return CIRQUE_PINNACLE_PX_TO_INCH(cirque_pinnacle_get_scale());
  162. }
  163. void cirque_pinnacle_set_cpi(uint16_t cpi) {
  164. cirque_pinnacle_set_scale(CIRQUE_PINNACLE_INCH_TO_PX(cpi));
  165. }
  166. // clang-format off
  167. const pointing_device_driver_t pointing_device_driver = {
  168. .init = cirque_pinnacle_init,
  169. .get_report = cirque_pinnacle_get_report,
  170. .set_cpi = cirque_pinnacle_set_cpi,
  171. .get_cpi = cirque_pinnacle_get_cpi
  172. };
  173. // clang-format on
  174. # else
  175. report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) {
  176. pinnacle_data_t touchData = cirque_pinnacle_read_data();
  177. // Scale coordinates to arbitrary X, Y resolution
  178. cirque_pinnacle_scale_data(&touchData, cirque_pinnacle_get_scale(), cirque_pinnacle_get_scale());
  179. if (touchData.valid) {
  180. mouse_report.buttons = touchData.buttons;
  181. mouse_report.x = CONSTRAIN_HID_XY(touchData.xDelta);
  182. mouse_report.y = CONSTRAIN_HID_XY(touchData.yDelta);
  183. mouse_report.v = touchData.wheelCount;
  184. }
  185. return mouse_report;
  186. }
  187. // clang-format off
  188. const pointing_device_driver_t pointing_device_driver = {
  189. .init = cirque_pinnacle_init,
  190. .get_report = cirque_pinnacle_get_report,
  191. .set_cpi = cirque_pinnacle_set_scale,
  192. .get_cpi = cirque_pinnacle_get_scale
  193. };
  194. // clang-format on
  195. # endif
  196. #elif defined(POINTING_DEVICE_DRIVER_paw3204)
  197. report_mouse_t paw3204_get_report(report_mouse_t mouse_report) {
  198. report_paw3204_t data = paw3204_read();
  199. if (data.isMotion) {
  200. pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);
  201. mouse_report.x = data.x;
  202. mouse_report.y = data.y;
  203. }
  204. return mouse_report;
  205. }
  206. const pointing_device_driver_t pointing_device_driver = {
  207. .init = paw3204_init,
  208. .get_report = paw3204_get_report,
  209. .set_cpi = paw3204_set_cpi,
  210. .get_cpi = paw3204_get_cpi,
  211. };
  212. #elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
  213. mouse_xy_report_t pimoroni_trackball_adapt_values(clamp_range_t* offset) {
  214. if (*offset > XY_REPORT_MAX) {
  215. *offset -= XY_REPORT_MAX;
  216. return (mouse_xy_report_t)XY_REPORT_MAX;
  217. } else if (*offset < XY_REPORT_MIN) {
  218. *offset += XY_REPORT_MAX;
  219. return (mouse_xy_report_t)XY_REPORT_MIN;
  220. } else {
  221. mouse_xy_report_t temp_return = *offset;
  222. *offset = 0;
  223. return temp_return;
  224. }
  225. }
  226. report_mouse_t pimoroni_trackball_get_report(report_mouse_t mouse_report) {
  227. static uint16_t debounce = 0;
  228. static uint8_t error_count = 0;
  229. pimoroni_data_t pimoroni_data = {0};
  230. static clamp_range_t x_offset = 0, y_offset = 0;
  231. if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT) {
  232. i2c_status_t status = read_pimoroni_trackball(&pimoroni_data);
  233. if (status == I2C_STATUS_SUCCESS) {
  234. error_count = 0;
  235. if (!(pimoroni_data.click & 128)) {
  236. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  237. if (!debounce) {
  238. x_offset += pimoroni_trackball_get_offsets(pimoroni_data.right, pimoroni_data.left, PIMORONI_TRACKBALL_SCALE);
  239. y_offset += pimoroni_trackball_get_offsets(pimoroni_data.down, pimoroni_data.up, PIMORONI_TRACKBALL_SCALE);
  240. mouse_report.x = pimoroni_trackball_adapt_values(&x_offset);
  241. mouse_report.y = pimoroni_trackball_adapt_values(&y_offset);
  242. } else {
  243. debounce--;
  244. }
  245. } else {
  246. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  247. debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES;
  248. }
  249. } else {
  250. error_count++;
  251. }
  252. }
  253. return mouse_report;
  254. }
  255. // clang-format off
  256. const pointing_device_driver_t pointing_device_driver = {
  257. .init = pimoroni_trackball_device_init,
  258. .get_report = pimoroni_trackball_get_report,
  259. .set_cpi = pimoroni_trackball_set_cpi,
  260. .get_cpi = pimoroni_trackball_get_cpi
  261. };
  262. // clang-format on
  263. #elif defined(POINTING_DEVICE_DRIVER_pmw3360) || defined(POINTING_DEVICE_DRIVER_pmw3389)
  264. static void pmw33xx_init_wrapper(void) {
  265. pmw33xx_init(0);
  266. }
  267. static void pmw33xx_set_cpi_wrapper(uint16_t cpi) {
  268. pmw33xx_set_cpi(0, cpi);
  269. }
  270. static uint16_t pmw33xx_get_cpi_wrapper(void) {
  271. return pmw33xx_get_cpi(0);
  272. }
  273. report_mouse_t pmw33xx_get_report(report_mouse_t mouse_report) {
  274. pmw33xx_report_t report = pmw33xx_read_burst(0);
  275. static bool in_motion = false;
  276. if (report.motion.b.is_lifted) {
  277. return mouse_report;
  278. }
  279. if (!report.motion.b.is_motion) {
  280. in_motion = false;
  281. return mouse_report;
  282. }
  283. if (!in_motion) {
  284. in_motion = true;
  285. pd_dprintf("PWM3360 (0): starting motion\n");
  286. }
  287. mouse_report.x = CONSTRAIN_HID_XY(report.delta_x);
  288. mouse_report.y = CONSTRAIN_HID_XY(report.delta_y);
  289. return mouse_report;
  290. }
  291. // clang-format off
  292. const pointing_device_driver_t pointing_device_driver = {
  293. .init = pmw33xx_init_wrapper,
  294. .get_report = pmw33xx_get_report,
  295. .set_cpi = pmw33xx_set_cpi_wrapper,
  296. .get_cpi = pmw33xx_get_cpi_wrapper
  297. };
  298. // clang-format on
  299. #else
  300. __attribute__((weak)) void pointing_device_driver_init(void) {}
  301. __attribute__((weak)) report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) {
  302. return mouse_report;
  303. }
  304. __attribute__((weak)) uint16_t pointing_device_driver_get_cpi(void) {
  305. return 0;
  306. }
  307. __attribute__((weak)) void pointing_device_driver_set_cpi(uint16_t cpi) {}
  308. // clang-format off
  309. const pointing_device_driver_t pointing_device_driver = {
  310. .init = pointing_device_driver_init,
  311. .get_report = pointing_device_driver_get_report,
  312. .get_cpi = pointing_device_driver_get_cpi,
  313. .set_cpi = pointing_device_driver_set_cpi
  314. };
  315. // clang-format on
  316. #endif