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