aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ploopyco/mouse/mouse.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/ploopyco/mouse/mouse.c')
-rw-r--r--keyboards/ploopyco/mouse/mouse.c237
1 files changed, 237 insertions, 0 deletions
diff --git a/keyboards/ploopyco/mouse/mouse.c b/keyboards/ploopyco/mouse/mouse.c
new file mode 100644
index 000000000..6a9bffbff
--- /dev/null
+++ b/keyboards/ploopyco/mouse/mouse.c
@@ -0,0 +1,237 @@
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
19#include QMK_KEYBOARD_H
20
21#ifndef OPT_DEBOUNCE
22# define OPT_DEBOUNCE 5 // (ms) Time between scroll events
23#endif
24#ifndef SCROLL_BUTT_DEBOUNCE
25# define SCROLL_BUTT_DEBOUNCE 100 // (ms) Time between scroll events
26#endif
27#ifndef OPT_THRES
28# define OPT_THRES 150 // (0-1024) Threshold for actication
29#endif
30#ifndef OPT_SCALE
31# define OPT_SCALE 1 // Multiplier for wheel
32#endif
33
34// TODO: Implement libinput profiles
35// https://wayland.freedesktop.org/libinput/doc/latest/pointer-acceleration.html
36// Compile time accel selection
37// Valid options are ACC_NONE, ACC_LINEAR, ACC_CUSTOM, ACC_QUADRATIC
38
39// Trackball State
40bool is_scroll_clicked = false;
41bool BurstState = false; // init burst state for Trackball module
42uint16_t MotionStart = 0; // Timer for accel, 0 is resting state
43uint16_t lastScroll = 0; // Previous confirmed wheel event
44uint16_t lastMidClick = 0; // Stops scrollwheel from being read if it was pressed
45uint8_t OptLowPin = OPT_ENC1;
46bool debug_encoder = false;
47
48__attribute__((weak)) void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v) {
49 mouse_report->h = h;
50 mouse_report->v = v;
51}
52
53__attribute__((weak)) void process_wheel(report_mouse_t* mouse_report) {
54 // TODO: Replace this with interrupt driven code, polling is S L O W
55 // Lovingly ripped from the Ploopy Source
56
57 // If the mouse wheel was just released, do not scroll.
58 if (timer_elapsed(lastMidClick) < SCROLL_BUTT_DEBOUNCE) {
59 return;
60 }
61
62 // Limit the number of scrolls per unit time.
63 if (timer_elapsed(lastScroll) < OPT_DEBOUNCE) {
64 return;
65 }
66
67 // Don't scroll if the middle button is depressed.
68 if (is_scroll_clicked) {
69#ifndef IGNORE_SCROLL_CLICK
70 return;
71#endif
72 }
73
74 lastScroll = timer_read();
75 uint16_t p1 = adc_read(OPT_ENC1_MUX);
76 uint16_t p2 = adc_read(OPT_ENC2_MUX);
77 if (debug_encoder) dprintf("OPT1: %d, OPT2: %d\n", p1, p2);
78
79 uint8_t dir = opt_encoder_handler(p1, p2);
80
81 if (dir == 0) return;
82 process_wheel_user(mouse_report, mouse_report->h, (int)(mouse_report->v + (dir * OPT_SCALE)));
83}
84
85__attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
86 mouse_report->x = x;
87 mouse_report->y = y;
88}
89
90__attribute__((weak)) void process_mouse(report_mouse_t* mouse_report) {
91 report_pmw_t data = pmw_read_burst();
92 if (data.isOnSurface && data.isMotion) {
93 // Reset timer if stopped moving
94 if (!data.isMotion) {
95 if (MotionStart != 0) MotionStart = 0;
96 return;
97 }
98
99 // Set timer if new motion
100 if ((MotionStart == 0) && data.isMotion) {
101 if (debug_mouse) dprintf("Starting motion.\n");
102 MotionStart = timer_read();
103 }
104
105 if (debug_mouse) {
106 dprintf("Delt] d: %d t: %u\n", abs(data.dx) + abs(data.dy), MotionStart);
107 }
108 if (debug_mouse) {
109 dprintf("Pre ] X: %d, Y: %d\n", data.dx, data.dy);
110 }
111#if defined(PROFILE_LINEAR)
112 float scale = float(timer_elaspsed(MotionStart)) / 1000.0;
113 data.dx *= scale;
114 data.dy *= scale;
115#elif defined(PROFILE_INVERSE)
116 // TODO
117#else
118 // no post processing
119#endif
120 // apply multiplier
121 // data.dx *= mouse_multiplier;
122 // data.dy *= mouse_multiplier;
123
124 // Wrap to HID size
125 data.dx = constrain(data.dx, -127, 127);
126 data.dy = constrain(data.dy, -127, 127);
127 if (debug_mouse) dprintf("Cons] X: %d, Y: %d\n", data.dx, data.dy);
128 // dprintf("Elapsed:%u, X: %f Y: %\n", i, pgm_read_byte(firmware_data+i));
129
130 process_mouse_user(mouse_report, data.dx, data.dy);
131 }
132}
133
134bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
135 if (debug_mouse) {
136 dprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
137 }
138
139 // Update Timer to prevent accidental scrolls
140 if ((record->event.key.col == 2) && (record->event.key.row == 0)) {
141 lastMidClick = timer_read();
142 is_scroll_clicked = record->event.pressed;
143 }
144
145/* If Mousekeys is disabled, then use handle the mouse button
146 * keycodes. This makes things simpler, and allows usage of
147 * the keycodes in a consistent manner. But only do this if
148 * Mousekeys is not enable, so it's not handled twice.
149 */
150#ifndef MOUSEKEY_ENABLE
151 if (IS_MOUSEKEY_BUTTON(keycode)) {
152 report_mouse_t currentReport = pointing_device_get_report();
153 if (record->event.pressed) {
154 if (keycode == KC_MS_BTN1)
155 currentReport.buttons |= MOUSE_BTN1;
156 else if (keycode == KC_MS_BTN2)
157 currentReport.buttons |= MOUSE_BTN2;
158 else if (keycode == KC_MS_BTN3)
159 currentReport.buttons |= MOUSE_BTN3;
160 else if (keycode == KC_MS_BTN4)
161 currentReport.buttons |= MOUSE_BTN4;
162 else if (keycode == KC_MS_BTN5)
163 currentReport.buttons |= MOUSE_BTN5;
164 } else {
165 if (keycode == KC_MS_BTN1)
166 currentReport.buttons &= ~MOUSE_BTN1;
167 else if (keycode == KC_MS_BTN2)
168 currentReport.buttons &= ~MOUSE_BTN2;
169 else if (keycode == KC_MS_BTN3)
170 currentReport.buttons &= ~MOUSE_BTN3;
171 else if (keycode == KC_MS_BTN4)
172 currentReport.buttons &= ~MOUSE_BTN4;
173 else if (keycode == KC_MS_BTN5)
174 currentReport.buttons &= ~MOUSE_BTN5;
175 }
176 pointing_device_set_report(currentReport);
177 }
178#endif
179
180 return process_record_user(keycode, record);
181}
182
183// Hardware Setup
184void keyboard_pre_init_kb(void) {
185 // debug_enable = true;
186 // debug_matrix = true;
187 // debug_mouse = true;
188 // debug_encoder = true;
189
190 setPinInput(OPT_ENC1);
191 setPinInput(OPT_ENC2);
192
193 // This is the debug LED.
194 setPinOutput(F7);
195 writePin(F7, debug_enable);
196
197 /* Ground all output pins connected to ground. This provides additional
198 * pathways to ground. If you're messing with this, know this: driving ANY
199 * of these pins high will cause a short. On the MCU. Ka-blooey.
200 */
201#ifdef UNUSED_PINS
202 const pin_t unused_pins[] = UNUSED_PINS;
203
204 for (uint8_t i = 0; i < (sizeof(unused_pins) / sizeof(pin_t)); i++) {
205 setPinOutput(unused_pins[i]);
206 writePinLow(unused_pins[i]);
207 }
208#endif
209 keyboard_pre_init_user();
210}
211
212void pointing_device_init(void) {
213 // initialize ball sensor
214 pmw_spi_init();
215 // initialize the scroll wheel's optical encoder
216 opt_encoder_init();
217}
218
219bool has_report_changed (report_mouse_t first, report_mouse_t second) {
220 return !(
221 (!first.buttons && first.buttons == second.buttons) &&
222 (!first.x && first.x == second.x) &&
223 (!first.y && first.y == second.y) &&
224 (!first.h && first.h == second.h) &&
225 (!first.v && first.v == second.v) );
226}
227
228void pointing_device_task(void) {
229 report_mouse_t mouse_report = pointing_device_get_report();
230 process_wheel(&mouse_report);
231 process_mouse(&mouse_report);
232
233 pointing_device_set_report(mouse_report);
234 if (has_report_changed(mouse_report, pointing_device_get_report())) {
235 pointing_device_send();
236 }
237}