-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpimoroni_trackball.c
320 lines (268 loc) · 8.95 KB
/
pimoroni_trackball.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "pimoroni_trackball.h"
#include "i2c_master.h"
#include "pointing_device.h"
#ifndef TRACKBALL_NO_MATH
#include "math.h"
# ifndef TRACKBALL_ANGLE_OFFSET
# define TRACKBALL_ANGLE_OFFSET 0
# endif
#endif
#undef TRACKBALL_REVERSE_VSCROLL
#undef TRACKBALL_REVERSE_HSCROLL
#undef TRACKBALL_ORIENTATION
#define TRACKBALL_ORIENTATION 1
#ifndef TRACKBALL_ORIENTATION
# define TRACKBALL_ORIENTATION 0
#endif
#ifndef TRACKBALL_REVERSE_VSCROLL
# define TRACKBALL_REVERSE_VSCROLL false
#endif
#ifndef TRACKBALL_REVERSE_HSCROLL
# define TRACKBALL_REVERSE_HSCROLL false
#endif
#ifndef TRACKBALL_ACCELERATION_WINDOW
# define TRACKBALL_ACCELERATION_WINDOW 50000 // ms window to increase acceleration factor
#endif
#define TRACKBALL_INTERRUPT_PIN D3
#define TRACKBALL_TIMEOUT 5
bool scrolling = false;
bool trackball_idle = true;
uint8_t tb_brightness = 42;
void trackball_init(void) {
i2c_init();
#ifdef TRACKBALL_INTERRUPT_PIN
setPinInput(TRACKBALL_INTERRUPT_PIN);
writePinLow(TRACKBALL_INTERRUPT_PIN);
uint8_t data[] = {REG_INTERRUPT_PIN, MASK_INTERRUPT_PIN_ENABLE};
i2c_transmit(TRACKBALL_WRITE, data, 2, TB_I2C_TIMEOUT);
#endif
}
bool trackball_get_interrupt(void) {
#ifndef TRACKBALL_INTERRUPT_PIN
uint8_t data[1] = {};
i2c_readReg(TRACKBALL_WRITE, REG_INTERRUPT_PIN, data, 1, TB_I2C_TIMEOUT);
return data[0] & MASK_INTERRUPT_TRIGGERED;
#else
return !readPin(TRACKBALL_INTERRUPT_PIN);
#endif
}
void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
uint8_t data[] = {0x00, red, green, blue, white};
i2c_transmit(TRACKBALL_WRITE, data, 5, TB_I2C_TIMEOUT);
}
void trackball_read_state(uint8_t* data, uint16_t size_of_data) {
i2c_readReg(TRACKBALL_WRITE, REG_LEFT, data, size_of_data, TB_I2C_TIMEOUT);
}
void trackball_set_scrolling(bool scroll) {
scrolling = scroll;
}
trackball_state_t trackball_get_state(void) {
// up down left right button
uint8_t s[5] = {};
trackball_read_state(s, 5);
trackball_state_t state = {
#if TRACKBALL_ORIENTATION == 0
// Pimoroni text is up
.y = s[0] - s[1],
.x = s[3] - s[2],
#elif TRACKBALL_ORIENTATION == 1
// Pimoroni text is right
.y = s[3] - s[2],
.x = s[1] - s[0],
#elif TRACKBALL_ORIENTATION == 2
// Pimoroni text is down
.y = s[1] - s[0],
.x = s[2] - s[3],
#else
// Pimoroni text is left
.y = s[2] - s[3],
.x = s[0] - s[1],
#endif
.button_down = s[4] & 0x80,
.button_triggered = s[4] & 0x01,
};
#ifndef TRACKBALL_NO_MATH
state.angle_rad = atan2(state.y, state.x) + TRACKBALL_ANGLE_OFFSET;
state.vector_length = sqrt(pow(state.x, 2) + pow(state.y, 2));
state.raw_x = state.x;
state.raw_y = state.y;
state.x = (int16_t)(state.vector_length * cos(state.angle_rad));
state.y = (int16_t)(state.vector_length * sin(state.angle_rad));
#endif
return state;
}
void trackball_sleep(void) {
/* not sure how this is supposed to work */
uint8_t data[] = {REG_CTRL, MSK_CTRL_FWRITE | MSK_CTRL_SLEEP};
i2c_transmit(TRACKBALL_WRITE, data, 2, TB_I2C_TIMEOUT);
}
void trackball_set_brightness(uint8_t brightness) {
uint8_t data[4] = {};
i2c_readReg(TRACKBALL_WRITE, REG_RED, data, 4, TB_I2C_TIMEOUT);
for (int i=0; i<4; i++) {
if (data[i]) {
data[i] = brightness;
}
}
i2c_writeReg(TRACKBALL_WRITE, REG_RED, data, 4, TB_I2C_TIMEOUT);
}
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
void trackball_set_hsv(uint8_t hue, uint8_t sat, uint8_t brightness) {
RGB rgb = hsv_to_rgb((HSV){hue, sat, brightness});
uint8_t white = MIN(rgb.r, MIN(rgb.g, rgb.b));
rgb.r -= white;
rgb.g -= white;
rgb.b -= white;
trackball_set_rgbw(rgb.r, rgb.g, rgb.b, white);
}
__attribute__((weak)) void pointing_device_init(void) {
trackball_init();
trackball_set_rgbw(0,0,tb_brightness,0);
}
__attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y, int16_t h, int16_t v) {
mouse_report->x = x;
mouse_report->y = y;
mouse_report->h = h;
mouse_report->v = v;
}
__attribute__((weak)) void update_member(int8_t* member, int16_t* offset) {
if (*offset > 127) {
*member = 127;
*offset -= 127;
} else if (*offset < -127) {
*member = -127;
*offset += 127;
} else {
*member = *offset;
*offset = 0;
}
}
__attribute__((weak)) bool has_report_changed(report_mouse_t new, report_mouse_t old) {
return (new.buttons != old.buttons) ||
(new.x && new.x != old.x) ||
(new.y && new.y != old.y) ||
(new.h && new.h != old.h) ||
(new.v && new.v != old.v);
}
static int16_t x_offset = 0;
static int16_t y_offset = 0;
static int16_t v_offset = 0;
static int16_t h_offset = 0;
static int16_t tb_timer = 0;
uint16_t acceleration_timer = 0;
__attribute__((weak)) void process_mouse(report_mouse_t* mouse, bool fast_scroll) {
static int8_t new_x_offset = 0;
static int8_t new_y_offset = 0;
static int8_t new_v_offset = 0;
static int8_t new_h_offset = 0;
if (trackball_get_interrupt() && (!tb_timer || timer_elapsed(tb_timer) > TRACKBALL_TIMEOUT)) {
tb_timer = timer_read() | 1;
trackball_state_t state = trackball_get_state();
if (state.button_triggered) {
if(state.button_down) {
mouse->buttons |= MOUSE_BTN1;
} else {
mouse->buttons &= ~MOUSE_BTN1;
}
} else {
//--------------------------------------------------------------
//DONT WANT TO MOVE THESE BUT HERE THEY ARE
//--------------------------------------------------------------
float power = 2.5;
float var_accel = 1;
if (fast_scroll) {
var_accel = 2;
power = 3;
}
double newlen = pow(state.vector_length*var_accel, power);
//float var_accel = 2; //acceleration factor
//double newlen = pow(state.vector_length, power);
/*
if (state.vector_length > 3 && (timer_elapsed(acceleration_timer) == 0 || timer_elapsed(acceleration_timer) < TRACKBALL_ACCELERATION_WINDOW)) {
acceleration_timer = timer_read();
newlen += pow(state.vector_length*var_accel, power);
} else {
acceleration_timer = timer_read();
newlen += pow(state.vector_length, power);
}
*/
//newlen = pow(state.vector_length, power);
x_offset += (newlen * cos(state.angle_rad));
y_offset += (newlen * sin(state.angle_rad));
/*
float y_correction = 0.8;
if ( y_offset > 0 && ((newlen * sin(state.angle_rad) * y_correction == 0)) {
y_offset = 0.2;
}
else {
y_offset *= y_correction;
}
*/
#if TRACKBALL_REVERSE_VSCROLL == true
v_offset += (newlen * sin(state.angle_rad));
#else
v_offset -= (newlen * sin(state.angle_rad));
#endif
#if TRACKBALL_REVERSE_HSCROLL == true
h_offset -= (newlen * cos(state.angle_rad));
#else
h_offset += (newlen * cos(state.angle_rad));
#endif
}
}
while (x_offset || y_offset || h_offset || v_offset) {
update_member(&new_x_offset, &x_offset);
update_member(&new_y_offset, &y_offset);
update_member(&new_v_offset, &v_offset);
update_member(&new_h_offset, &h_offset);
mouse->x = new_x_offset;
mouse->y = new_y_offset;
mouse->v = new_v_offset;
mouse->h = new_h_offset;
}
}
__attribute__((weak)) void pointing_device_task(void) {
report_mouse_t mouse_report = pointing_device_get_report();
//trackball_set_rgbw(0,0,70,0);
if (is_keyboard_master()) {
process_mouse(&mouse_report, false);
}
pointing_device_set_report(mouse_report);
pointing_device_send();
}
__attribute__((weak)) void pointing_device_send(void) {
static report_mouse_t old_report = {};
report_mouse_t mouseReport = pointing_device_get_report();
if (is_keyboard_master()) {
int8_t x = mouseReport.x, y = mouseReport.y, h = mouseReport.h, v = mouseReport.v;
mouseReport.x = 0;
mouseReport.y = 0;
mouseReport.h = 0;
mouseReport.v = 0;
if (!scrolling) {
process_mouse_user(&mouseReport, x, y, 0, 0);
} else {
process_mouse_user(&mouseReport, 0, 0, h, v);
}
if (has_report_changed(mouseReport, old_report)) {
trackball_idle = false;
host_mouse_send(&mouseReport);
} else {
trackball_idle = true;
}
} else {
if (has_report_changed(mouseReport, old_report)) {
trackball_idle = false;
} else {
trackball_idle = true;
}
}
mouseReport.x = 0;
mouseReport.y = 0;
mouseReport.v = 0;
mouseReport.h = 0;
old_report = mouseReport;
pointing_device_set_report(mouseReport);
}