aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ploopyco/trackball/trackball.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/ploopyco/trackball/trackball.c')
-rw-r--r--keyboards/ploopyco/trackball/trackball.c105
1 files changed, 21 insertions, 84 deletions
diff --git a/keyboards/ploopyco/trackball/trackball.c b/keyboards/ploopyco/trackball/trackball.c
index 719020997..e2a6e4ed5 100644
--- a/keyboards/ploopyco/trackball/trackball.c
+++ b/keyboards/ploopyco/trackball/trackball.c
@@ -65,12 +65,7 @@ uint8_t OptLowPin = OPT_ENC1;
65bool debug_encoder = false; 65bool debug_encoder = false;
66bool is_drag_scroll = false; 66bool is_drag_scroll = false;
67 67
68__attribute__((weak)) void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v) { 68void process_wheel(report_mouse_t* mouse_report) {
69 mouse_report->h = h;
70 mouse_report->v = v;
71}
72
73__attribute__((weak)) void process_wheel(report_mouse_t* mouse_report) {
74 // TODO: Replace this with interrupt driven code, polling is S L O W 69 // TODO: Replace this with interrupt driven code, polling is S L O W
75 // Lovingly ripped from the Ploopy Source 70 // Lovingly ripped from the Ploopy Source
76 71
@@ -99,56 +94,25 @@ __attribute__((weak)) void process_wheel(report_mouse_t* mouse_report) {
99 int dir = opt_encoder_handler(p1, p2); 94 int dir = opt_encoder_handler(p1, p2);
100 95
101 if (dir == 0) return; 96 if (dir == 0) return;
102 process_wheel_user(mouse_report, mouse_report->h, (int)(mouse_report->v + (dir * OPT_SCALE))); 97 mouse_report->v = (int8_t)(dir * OPT_SCALE);
103} 98}
104 99
105__attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) { 100report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
106 mouse_report->x = x; 101 process_wheel(&mouse_report);
107 mouse_report->y = y;
108}
109
110__attribute__((weak)) void process_mouse(report_mouse_t* mouse_report) {
111 report_pmw_t data = pmw_read_burst();
112 if (data.isOnSurface && data.isMotion) {
113 // Reset timer if stopped moving
114 if (!data.isMotion) {
115 if (MotionStart != 0) MotionStart = 0;
116 return;
117 }
118
119 // Set timer if new motion
120 if ((MotionStart == 0) && data.isMotion) {
121 if (debug_mouse) dprintf("Starting motion.\n");
122 MotionStart = timer_read();
123 }
124 102
125 if (debug_mouse) { 103 if (is_drag_scroll) {
126 dprintf("Delt] d: %d t: %u\n", abs(data.dx) + abs(data.dy), MotionStart); 104 mouse_report.h = mouse_report.x;
127 } 105#ifdef PLOOPY_DRAGSCROLL_INVERT
128 if (debug_mouse) { 106 // Invert vertical scroll direction
129 dprintf("Pre ] X: %d, Y: %d\n", data.dx, data.dy); 107 mouse_report.v = -mouse_report.y;
130 }
131#if defined(PROFILE_LINEAR)
132 float scale = float(timer_elaspsed(MotionStart)) / 1000.0;
133 data.dx *= scale;
134 data.dy *= scale;
135#elif defined(PROFILE_INVERSE)
136 // TODO
137#else 108#else
138 // no post processing 109 mouse_report.v = mouse_report.y;
139#endif 110#endif
140 // apply multiplier 111 mouse_report.x = 0;
141 // data.dx *= mouse_multiplier; 112 mouse_report.y = 0;
142 // data.dy *= mouse_multiplier;
143
144 // Wrap to HID size
145 data.dx = constrain(data.dx, -127, 127);
146 data.dy = constrain(data.dy, -127, 127);
147 if (debug_mouse) dprintf("Cons] X: %d, Y: %d\n", data.dx, data.dy);
148 // dprintf("Elapsed:%u, X: %f Y: %\n", i, pgm_read_byte(firmware_data+i));
149
150 process_mouse_user(mouse_report, data.dx, -data.dy);
151 } 113 }
114
115 return pointing_device_task_user(mouse_report);
152} 116}
153 117
154bool process_record_kb(uint16_t keycode, keyrecord_t* record) { 118bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
@@ -169,7 +133,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
169 if (keycode == DPI_CONFIG && record->event.pressed) { 133 if (keycode == DPI_CONFIG && record->event.pressed) {
170 keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE; 134 keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
171 eeconfig_update_kb(keyboard_config.raw); 135 eeconfig_update_kb(keyboard_config.raw);
172 pmw_set_cpi(dpi_array[keyboard_config.dpi_config]); 136 pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
173 } 137 }
174 138
175 if (keycode == DRAG_SCROLL) { 139 if (keycode == DRAG_SCROLL) {
@@ -180,9 +144,9 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
180 is_drag_scroll ^= 1; 144 is_drag_scroll ^= 1;
181 } 145 }
182#ifdef PLOOPY_DRAGSCROLL_FIXED 146#ifdef PLOOPY_DRAGSCROLL_FIXED
183 pmw_set_cpi(is_drag_scroll ? PLOOPY_DRAGSCROLL_DPI : dpi_array[keyboard_config.dpi_config]); 147 pointing_device_set_cpi(is_drag_scroll ? PLOOPY_DRAGSCROLL_DPI : dpi_array[keyboard_config.dpi_config]);
184#else 148#else
185 pmw_set_cpi(is_drag_scroll ? (dpi_array[keyboard_config.dpi_config] * PLOOPY_DRAGSCROLL_MULTIPLIER) : dpi_array[keyboard_config.dpi_config]); 149 pointing_device_set_cpi(is_drag_scroll ? (dpi_array[keyboard_config.dpi_config] * PLOOPY_DRAGSCROLL_MULTIPLIER) : dpi_array[keyboard_config.dpi_config]);
186#endif 150#endif
187 } 151 }
188 152
@@ -194,11 +158,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
194#ifndef MOUSEKEY_ENABLE 158#ifndef MOUSEKEY_ENABLE
195 if (IS_MOUSEKEY_BUTTON(keycode)) { 159 if (IS_MOUSEKEY_BUTTON(keycode)) {
196 report_mouse_t currentReport = pointing_device_get_report(); 160 report_mouse_t currentReport = pointing_device_get_report();
197 if (record->event.pressed) { 161 currentReport.buttons = pointing_device_handle_buttons(record->event.pressed, keycode - KC_MS_BTN1);
198 currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
199 } else {
200 currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
201 }
202 pointing_device_set_report(currentReport); 162 pointing_device_set_report(currentReport);
203 pointing_device_send(); 163 pointing_device_send();
204 } 164 }
@@ -239,35 +199,12 @@ void keyboard_pre_init_kb(void) {
239 keyboard_pre_init_user(); 199 keyboard_pre_init_user();
240} 200}
241 201
242void pointing_device_init(void) { 202void pointing_device_init_kb(void) {
243 // initialize ball sensor 203 pmw3360_set_cpi(dpi_array[keyboard_config.dpi_config]);
244 pmw_spi_init();
245 // initialize the scroll wheel's optical encoder 204 // initialize the scroll wheel's optical encoder
246 opt_encoder_init(); 205 opt_encoder_init();
247} 206}
248 207
249
250void pointing_device_task(void) {
251 report_mouse_t mouse_report = pointing_device_get_report();
252 process_wheel(&mouse_report);
253 process_mouse(&mouse_report);
254
255 if (is_drag_scroll) {
256 mouse_report.h = mouse_report.x;
257#ifdef PLOOPY_DRAGSCROLL_INVERT
258 // Invert vertical scroll direction
259 mouse_report.v = -mouse_report.y;
260#else
261 mouse_report.v = mouse_report.y;
262#endif
263 mouse_report.x = 0;
264 mouse_report.y = 0;
265 }
266
267 pointing_device_set_report(mouse_report);
268 pointing_device_send();
269}
270
271void eeconfig_init_kb(void) { 208void eeconfig_init_kb(void) {
272 keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT; 209 keyboard_config.dpi_config = PLOOPY_DPI_DEFAULT;
273 eeconfig_update_kb(keyboard_config.raw); 210 eeconfig_update_kb(keyboard_config.raw);
@@ -285,7 +222,7 @@ void matrix_init_kb(void) {
285} 222}
286 223
287void keyboard_post_init_kb(void) { 224void keyboard_post_init_kb(void) {
288 pmw_set_cpi(dpi_array[keyboard_config.dpi_config]); 225 pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
289 226
290 keyboard_post_init_user(); 227 keyboard_post_init_user();
291} 228}