-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplusVariant.ino
More file actions
414 lines (377 loc) · 13 KB
/
Copy pathplusVariant.ino
File metadata and controls
414 lines (377 loc) · 13 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#ifndef USER_USB_RAM
#error "Go to Tools > USB Settings and select 'USER CODE w/ 266B USB ram'"
#endif
#include "src/CdcHidKeyboardMouse/USBCDC.h"
#include "src/CdcHidKeyboardMouse/USBHIDKeyboard.h"
#include "src/CdcHidKeyboardMouse/USBHIDMouse.h"
#define PIN_SENS_1 15
#define PIN_SENS_2 11
#define PIN_JOY_X 32
#define PIN_JOY_Y 14
#define VARIANT_NAME "PLUS"
#define DEADZONE 6
#define DEFAULT_SPEED 12
#define MAX_SPEED_LIMIT 30
#define UPDATE_DELAY 8
#define JOY_MAGIC 0x55
#define THRESH_PUFF 30
#define THRESH_SIP -30
#define COMBO_HOLD_MS 3000
#define EE_S1_PUFF 0
#define EE_S1_SIP 3
#define EE_S2_PUFF 6
#define EE_S2_SIP 9
#define EE_SPEED 12
#define EE_JOY_MAGIC 13
#define EE_JOY_CAL 14
#define EE_JOY_FLAGS 20
#define EE_ACTION_MAGIC 21
#define ACTION_MAGIC 0xA5
#define EE_SCROLL_SPEED 22
#define DEFAULT_SCROLL_SPEED 3
#define MAX_SCROLL_SPEED 10
typedef struct {
uint8_t dest;
uint8_t mod;
uint8_t key;
} ActionPayload;
ActionPayload s1Puff, s1Sip;
ActionPayload s2Puff, s2Sip;
uint8_t mouseSpeed = DEFAULT_SPEED;
uint8_t scrollSpeed = DEFAULT_SCROLL_SPEED;
uint8_t joyFlags = 0;
uint8_t sens1Center, sens2Center;
uint8_t xCenter, yCenter, xMin, xMax, yMin, yMax;
uint8_t s1State = 0, s2State = 0;
uint8_t s1ScrollTick = 0, s2ScrollTick = 0;
unsigned long comboStartMs = 0;
bool comboArmed = false;
void eeWriteAction(uint8_t base, ActionPayload p) {
eeprom_write_byte(base, p.dest);
eeprom_write_byte(base + 1, p.mod);
eeprom_write_byte(base + 2, p.key);
}
ActionPayload eeReadAction(uint8_t base) {
ActionPayload p;
p.dest = eeprom_read_byte(base);
p.mod = eeprom_read_byte(base + 1);
p.key = eeprom_read_byte(base + 2);
return p;
}
static bool validateAction(ActionPayload *p, uint8_t fallbackKey) {
if (p->dest < 1 || p->dest > 3 || p->key == 0 || p->key == 0xFF) {
p->dest = 2; p->mod = 0; p->key = fallbackKey;
return true;
}
return false;
}
void loadAllPayloads() {
if (eeprom_read_byte(EE_ACTION_MAGIC) != ACTION_MAGIC) {
s1Puff.dest = 2; s1Puff.mod = 0; s1Puff.key = MOUSE_LEFT;
s1Sip.dest = 2; s1Sip.mod = 0; s1Sip.key = MOUSE_RIGHT;
s2Puff.dest = 2; s2Puff.mod = 0; s2Puff.key = MOUSE_LEFT;
s2Sip.dest = 2; s2Sip.mod = 0; s2Sip.key = MOUSE_RIGHT;
eeWriteAction(EE_S1_PUFF, s1Puff);
eeWriteAction(EE_S1_SIP, s1Sip);
eeWriteAction(EE_S2_PUFF, s2Puff);
eeWriteAction(EE_S2_SIP, s2Sip);
eeprom_write_byte(EE_SPEED, DEFAULT_SPEED);
eeprom_write_byte(EE_JOY_FLAGS, 0);
eeprom_write_byte(EE_SCROLL_SPEED, DEFAULT_SCROLL_SPEED);
eeprom_write_byte(EE_ACTION_MAGIC, ACTION_MAGIC);
mouseSpeed = DEFAULT_SPEED;
scrollSpeed = DEFAULT_SCROLL_SPEED;
joyFlags = 0;
return;
}
s1Puff = eeReadAction(EE_S1_PUFF);
s1Sip = eeReadAction(EE_S1_SIP);
s2Puff = eeReadAction(EE_S2_PUFF);
s2Sip = eeReadAction(EE_S2_SIP);
if (validateAction(&s1Puff, MOUSE_LEFT)) eeWriteAction(EE_S1_PUFF, s1Puff);
if (validateAction(&s1Sip, MOUSE_RIGHT)) eeWriteAction(EE_S1_SIP, s1Sip);
if (validateAction(&s2Puff, MOUSE_LEFT)) eeWriteAction(EE_S2_PUFF, s2Puff);
if (validateAction(&s2Sip, MOUSE_RIGHT)) eeWriteAction(EE_S2_SIP, s2Sip);
mouseSpeed = eeprom_read_byte(EE_SPEED);
if (mouseSpeed == 0xFF || mouseSpeed == 0 || mouseSpeed > MAX_SPEED_LIMIT) mouseSpeed = DEFAULT_SPEED;
joyFlags = eeprom_read_byte(EE_JOY_FLAGS);
if (joyFlags == 0xFF) joyFlags = 0;
scrollSpeed = eeprom_read_byte(EE_SCROLL_SPEED);
if (scrollSpeed == 0xFF || scrollSpeed == 0 || scrollSpeed > MAX_SCROLL_SPEED) scrollSpeed = DEFAULT_SCROLL_SPEED;
}
void saveJoyCal() {
eeprom_write_byte(EE_JOY_MAGIC, JOY_MAGIC);
eeprom_write_byte(EE_JOY_CAL + 0, (uint8_t)xCenter);
eeprom_write_byte(EE_JOY_CAL + 1, (uint8_t)yCenter);
eeprom_write_byte(EE_JOY_CAL + 2, (uint8_t)xMin);
eeprom_write_byte(EE_JOY_CAL + 3, (uint8_t)xMax);
eeprom_write_byte(EE_JOY_CAL + 4, (uint8_t)yMin);
eeprom_write_byte(EE_JOY_CAL + 5, (uint8_t)yMax);
}
void loadJoyCal() {
xCenter = eeprom_read_byte(EE_JOY_CAL + 0);
yCenter = eeprom_read_byte(EE_JOY_CAL + 1);
xMin = eeprom_read_byte(EE_JOY_CAL + 2);
xMax = eeprom_read_byte(EE_JOY_CAL + 3);
yMin = eeprom_read_byte(EE_JOY_CAL + 4);
yMax = eeprom_read_byte(EE_JOY_CAL + 5);
}
int clamp(int v, int lo, int hi) { return v < lo ? lo : (v > hi ? hi : v); }
int8_t adcToMouse(int v, int c, int mn, int mx) {
int diff = v - c;
if (diff > -DEADZONE && diff < DEADZONE) return 0;
int range = (diff > 0) ? (mx - c) : (c - mn);
if (range <= 0) return 0;
if (diff > range) diff = range;
else if (diff < -range) diff = -range;
int move = (diff * (int)mouseSpeed) / range;
return (int8_t)clamp(move, -127, 127);
}
static void reJoyCal() {
// Phase 1: sample resting centre (~160 ms, joystick held still)
uint32_t sx = 0, sy = 0;
for (uint8_t i = 0; i < 32; i++) {
sx += analogRead(PIN_JOY_X);
sy += analogRead(PIN_JOY_Y);
delay(5);
}
xCenter = sx / 32;
yCenter = sy / 32;
// Phase 2: sweep — move stick to all extremes for 3 s
xMin = xCenter; xMax = xCenter;
yMin = yCenter; yMax = yCenter;
unsigned long t = millis();
while (millis() - t < 3000) {
uint8_t rx = (uint8_t)analogRead(PIN_JOY_X);
uint8_t ry = (uint8_t)analogRead(PIN_JOY_Y);
if (rx < xMin) xMin = rx;
if (rx > xMax) xMax = rx;
if (ry < yMin) yMin = ry;
if (ry > yMax) yMax = ry;
delay(10);
}
saveJoyCal();
}
void pressModifiers(uint8_t mod) {
for (uint8_t i = 0; i < 8; i++) if (mod & (1 << i)) Keyboard_press(0x80 + i);
}
void releaseModifiers(uint8_t mod) {
for (int8_t i = 7; i >= 0; i--) if (mod & (1 << i)) Keyboard_release(0x80 + i);
}
void executeAction(ActionPayload p) {
if (p.dest == 1 && p.key > 0) { pressModifiers(p.mod); Keyboard_press(p.key); }
else if (p.dest == 2 && p.key > 0) { Mouse_press(p.key); }
}
void releaseAction(ActionPayload p) {
if (p.dest == 1 && p.key > 0) { Keyboard_release(p.key); releaseModifiers(p.mod); }
else if (p.dest == 2 && p.key > 0) { Mouse_release(p.key); }
}
void continuousScrollAction(ActionPayload p, uint8_t *tick) {
if (p.dest != 3) return;
if (++(*tick) < (70 / scrollSpeed)) return;
*tick = 0;
int8_t amount = (p.key == 1) ? 1 : -1;
Mouse_scroll(amount);
}
void emitAction(ActionPayload p) {
USBSerial_print(p.dest); USBSerial_print(",");
USBSerial_print(p.mod); USBSerial_print(",");
USBSerial_print(p.key);
}
bool waitForBytes(uint8_t n) {
for (int i = 0; i < 100 && USBSerial_available() < n; i++) delay(1);
return USBSerial_available() >= n;
}
void handleSerial() {
if (USBSerial_available() <= 0) return;
char cmd = USBSerial_read();
switch (cmd) {
case 'Q':
USBSerial_print("STATE:" VARIANT_NAME ":");
emitAction(s1Puff); USBSerial_print(":");
emitAction(s1Sip); USBSerial_print(":");
emitAction(s2Puff); USBSerial_print(":");
emitAction(s2Sip); USBSerial_print(":");
USBSerial_print(mouseSpeed); USBSerial_print(":");
USBSerial_print(joyFlags); USBSerial_print(":");
USBSerial_print(scrollSpeed);
USBSerial_println();
USBSerial_flush();
break;
case 'J':
if (waitForBytes(1)) {
joyFlags = USBSerial_read();
eeprom_write_byte(EE_JOY_FLAGS, joyFlags);
USBSerial_print("SAVED\n"); USBSerial_flush();
}
break;
case 'W':
if (waitForBytes(5)) {
uint8_t sId = USBSerial_read();
uint8_t trig = USBSerial_read();
uint8_t d = USBSerial_read();
uint8_t m = USBSerial_read();
uint8_t k = USBSerial_read();
ActionPayload p = {d, m, k};
bool valid = true;
uint8_t base = 0;
if (sId == 1 && trig == 0) { s1Puff = p; base = EE_S1_PUFF; }
else if (sId == 1 && trig == 1) { s1Sip = p; base = EE_S1_SIP; }
else if (sId == 2 && trig == 0) { s2Puff = p; base = EE_S2_PUFF; }
else if (sId == 2 && trig == 1) { s2Sip = p; base = EE_S2_SIP; }
else valid = false;
if (valid) eeWriteAction(base, p);
USBSerial_print(valid ? "SAVED\n" : "ERR\n"); USBSerial_flush();
}
break;
case 'S':
if (waitForBytes(1)) {
uint8_t sp = USBSerial_read();
if (sp == 0) sp = 1;
if (sp > MAX_SPEED_LIMIT) sp = MAX_SPEED_LIMIT;
mouseSpeed = sp;
eeprom_write_byte(EE_SPEED, sp);
USBSerial_print("SAVED\n"); USBSerial_flush();
}
break;
case 'R':
USBSerial_print("RECAL\n"); USBSerial_flush();
reJoyCal();
break;
case 'F':
s1Puff.dest = 2; s1Puff.mod = 0; s1Puff.key = MOUSE_LEFT;
s1Sip.dest = 2; s1Sip.mod = 0; s1Sip.key = MOUSE_RIGHT;
s2Puff.dest = 2; s2Puff.mod = 0; s2Puff.key = MOUSE_LEFT;
s2Sip.dest = 2; s2Sip.mod = 0; s2Sip.key = MOUSE_RIGHT;
eeWriteAction(EE_S1_PUFF, s1Puff);
eeWriteAction(EE_S1_SIP, s1Sip);
eeWriteAction(EE_S2_PUFF, s2Puff);
eeWriteAction(EE_S2_SIP, s2Sip);
eeprom_write_byte(EE_ACTION_MAGIC, ACTION_MAGIC);
USBSerial_print("RESET\n"); USBSerial_flush();
break;
case 'V': {
int raw1 = analogRead(PIN_SENS_1);
int raw2 = analogRead(PIN_SENS_2);
USBSerial_print("DIAG:");
USBSerial_print(raw1); USBSerial_print(",");
USBSerial_print(sens1Center); USBSerial_print(",");
USBSerial_print(raw1 - (int)sens1Center); USBSerial_print(",");
USBSerial_print(raw2); USBSerial_print(",");
USBSerial_print(sens2Center); USBSerial_print(",");
USBSerial_print(raw2 - (int)sens2Center);
USBSerial_println(); USBSerial_flush();
break;
}
case 'L':
if (waitForBytes(1)) {
uint8_t ss = USBSerial_read();
if (ss == 0) ss = 1;
if (ss > MAX_SCROLL_SPEED) ss = MAX_SCROLL_SPEED;
scrollSpeed = ss;
eeprom_write_byte(EE_SCROLL_SPEED, ss);
USBSerial_print("SAVED\n"); USBSerial_flush();
}
break;
case 'X':
USBSerial_print("BOOTLOADER\n"); USBSerial_flush();
delay(50);
USB_CTRL = 0; EA = 0; TMOD = 0;
delayMicroseconds(50000);
__asm__("lcall #0x3800");
while(1);
break;
}
}
void setup() {
USBInit();
delay(600);
loadAllPayloads();
uint32_t s1 = 0, s2 = 0, jx = 0, jy = 0;
for (uint8_t i = 0; i < 32; i++) {
s1 += analogRead(PIN_SENS_1);
s2 += analogRead(PIN_SENS_2);
analogRead(PIN_JOY_X); // discard: AIN0→AIN3 needs one settling cycle
jx += analogRead(PIN_JOY_X);
jy += analogRead(PIN_JOY_Y);
delay(5);
}
sens1Center = s1 / 32;
sens2Center = s2 / 32;
xCenter = jx / 32;
yCenter = jy / 32;
if (eeprom_read_byte(EE_JOY_MAGIC) != JOY_MAGIC) {
xMin = xCenter; xMax = xCenter;
yMin = yCenter; yMax = yCenter;
unsigned long t = millis();
while (millis() - t < 3000) {
uint8_t rx = (uint8_t)analogRead(PIN_JOY_X);
uint8_t ry = (uint8_t)analogRead(PIN_JOY_Y);
if (rx < xMin) xMin = rx;
if (rx > xMax) xMax = rx;
if (ry < yMin) yMin = ry;
if (ry > yMax) yMax = ry;
delay(10);
}
saveJoyCal();
} else {
loadJoyCal();
xCenter = jx / 32;
yCenter = jy / 32;
}
}
void loop() {
handleSerial();
// Sensor 1 — inline, exactly like 1sensor
int raw1 = analogRead(PIN_SENS_1);
int diff1 = raw1 - (int)sens1Center;
uint8_t newS1 = 0;
if (diff1 > THRESH_PUFF) newS1 = 1;
else if (diff1 < THRESH_SIP) newS1 = 2;
if (newS1 != s1State) {
if (s1State == 1) releaseAction(s1Puff);
else if (s1State == 2) releaseAction(s1Sip);
if (newS1 == 1) executeAction(s1Puff);
else if (newS1 == 2) executeAction(s1Sip);
s1ScrollTick = 0;
s1State = newS1;
}
// Sensor 2 — same inline pattern
int raw2 = analogRead(PIN_SENS_2);
int diff2 = raw2 - (int)sens2Center;
uint8_t newS2 = 0;
if (diff2 > THRESH_PUFF) newS2 = 1;
else if (diff2 < THRESH_SIP) newS2 = 2;
if (newS2 != s2State) {
if (s2State == 1) releaseAction(s2Puff);
else if (s2State == 2) releaseAction(s2Sip);
if (newS2 == 1) executeAction(s2Puff);
else if (newS2 == 2) executeAction(s2Sip);
s2ScrollTick = 0;
s2State = newS2;
}
if (s1State == 1) continuousScrollAction(s1Puff, &s1ScrollTick);
else if (s1State == 2) continuousScrollAction(s1Sip, &s1ScrollTick);
if (s2State == 1) continuousScrollAction(s2Puff, &s2ScrollTick);
else if (s2State == 2) continuousScrollAction(s2Sip, &s2ScrollTick);
analogRead(PIN_JOY_X); // discard: settle ADC after AIN0 (SENS2) → AIN3 switch
int8_t mx = adcToMouse(analogRead(PIN_JOY_X), xCenter, xMin, xMax);
int8_t my = adcToMouse(analogRead(PIN_JOY_Y), yCenter, yMin, yMax);
if (joyFlags & 0x04) { int8_t t = mx; mx = my; my = t; }
if (joyFlags & 0x01) mx = -mx;
if (joyFlags & 0x02) my = -my;
if (mx || my) Mouse_move(mx, -my);
// Combo gesture: hold both sensors 3s = recalibrate joystick
bool both = (s1State != 0) && (s2State != 0);
if (both) {
if (!comboArmed) { comboArmed = true; comboStartMs = millis(); }
else if (millis() - comboStartMs >= COMBO_HOLD_MS) {
releaseAction(s1Puff); releaseAction(s1Sip);
releaseAction(s2Puff); releaseAction(s2Sip);
s1State = 0; s2State = 0;
comboArmed = false;
reJoyCal();
}
} else { comboArmed = false; }
delay(UPDATE_DELAY);
}