-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchBar.cpp
525 lines (477 loc) · 16.6 KB
/
TouchBar.cpp
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "TouchBar.h"
/* General */
TouchBar::TouchBar (TouchBarCommon *CommonPtr, TouchBarConfig *ConfigPtr)
{
Common = CommonPtr;
Config = ConfigPtr;
Current = Config->Default;
Previous = Config->Default;
Target = Config->Default;
}
void TouchBar::Reconfigure (TouchBarConfig *ConfigPtr)
{
Config = ConfigPtr;
Target = Current;
}
/* Settings */
void TouchBar::Reset ()
{
if (Config->GetRampFlag() == true)
Target = Config->Default;
else
Current = Config->Default;
}
void TouchBar::SetPosition (unsigned int NewPosition)
{
Current = NewPosition;
}
void TouchBar::SetTarget (unsigned int NewTarget)
{
Target = NewTarget;
}
/* Input / Output */
void TouchBar::Update (byte NewValue) // This compiles to 30 bytes less then the other Update method.
{
Shift ();
//TwitchSuppression (NewValue % 8); // This is more beginner friendly...
TwitchSuppression (NewValue & 0x07);
Main ();
}
void TouchBar::Update (boolean A, boolean B, boolean C)
{
Shift ();
byte X = 0;
// This does the same, but uses 2 bytes less flash... (I tried like 4 ways of doing this, this is the most compact when compiled, that's 2 instructions less to execute each cycle.)
X = C;
X = X << 1;
X = X + B;
X = X << 1;
X = X + A;
//X = (C << 2) + (B << 1) + A; // This looks better and does the same but for some reason not as compact then compiled.
TwitchSuppression (X);
Main ();
}
void TouchBar::Shift ()
{
if (ABCPads != ABCPrevious[0])
{
ABCPrevious[2] = ABCPrevious[1];
ABCPrevious[1] = ABCPrevious[0];
ABCPrevious[0] = ABCPads;
}
}
void TouchBar::TwitchSuppression (byte NewValue)
{
if (TSCounter < 255)
TSCounter += 1;
if (NewValue != Raw)
TSCounter = 0;
if (NewValue != ABCPads && NewValue != 0 && ABCPads != 0 || NewValue ^ ABCPads && TSCounter == Common->TwitchSuppressionDelay)
ABCPads = NewValue; // This does the same, compiles to the same size
Raw = NewValue;
}
boolean TouchBar::Event ()
{
if (Current != Previous)
return true;
else
return false;
}
char TouchBar::PadEvent ()
{
if (TapCounter < Common->TapTimeout && ABCPads == 0 && ABCPrevious[0] != 0)
switch (ABCPrevious[0])
{
case 1: return 'A';
break;;
case 2: return 'B';
break;;
case 4: return 'C';
break;;
default: return 'Z';
break;;
}
else
return 'Z';
}
unsigned int TouchBar::GetPositionInt ()
{
return Current;
}
float TouchBar::GetPositionFloat ()
{
return float(Current) / 100;
}
unsigned int TouchBar::GetTargetInt ()
{
return Target;
}
float TouchBar::GetTargetFloat ()
{
return float(Target) / 100;
}
/* Execution */
void TouchBar::Main ()
{
// Swap bits 0 and 2 if necessary
if (Config->GetFlipFlag() == true)
{
boolean X = bitRead(ABCPads, 0);
bitWrite(ABCPads, 0, bitRead(ABCPads, 2));
bitWrite(ABCPads, 2, X);
}
// Tap detection
if (ABCPads == 1 || ABCPads == 2 || ABCPads == 4 || ABCPads == 0 && ABCPrevious[0] != 0)
TapCounter += 1;
else if (ABCPads == 0 && ABCPrevious[0] == 0)
TapCounter = 0;
else
TapCounter = Common->TapTimeout;
Previous = Current; // This must be before the snap, otherwise snapping works, but does not report the event.
// Snap
if (Config->GetSnapFlag() == true && PadEvent() != 'Z')
switch (PadEvent())
{
case 'A': if (Config->GetRampFlag() == true)
Target = 0;
else
Current = 0;
break;;
case 'B': Reset();
break;;
case 'C': if (Config->GetRampFlag() == true)
Target = Config->Limit;
else
Current = Config->Limit;
break;;
}
GetDirection (); // Caluclate direction
AdjustOutput (); // React...
}
void TouchBar::GetDirection ()
{
if (ABCPads == 0)
{
Direction = Static;
ABCPrevious[1] = 0;
ABCPrevious[2] = 0;
if (Config->GetSpringBackFlag() == true)
Reset ();
}
else
{
/*
Signals in commenTB (for humans):
- T - Touched (Rising edge)
- H - Held (High)
- R - Released (Falling edge)
- LU - Left Untouched (Low)
*/
Direction = Static;
// The following does the same as the commented section above, except it compiles to 76-94 bytes less (depending on which Update method is used.).
// Light touch scenario (only touching 1-2 pads at a time.)
// Update: The condition for skipping a state change was added as an afterthought, which sped up the input significantly, but it can't be optimized the way it was before.
// Increment case 1/6
if (ABCPads == 3)
{
if (ABCPrevious[0] == 1) // Normal (A=H, B=T, C=LU)
Direction = Increment;
if (ABCPrevious[0] == 5) // Skiping (A=H, B=T, C=R)
Direction = Increment2;
}
// Increment case 2/6
if (ABCPads == 2)
{
if (ABCPrevious[0] == 3) // Normal (A=R, B=H, C=LU)
Direction = Increment;
if (ABCPrevious[0] == 1) // Skiping (A=R, B=T, C=LU)
Direction = Increment2;
}
// Increment case 3/6
if (ABCPads == 6)
{
if (ABCPrevious[0] == 2) // Normal (A=LU, B=H, C=T)
Direction = Increment;
if (ABCPrevious[0] == 3) // Skiping (A=R, B=H, C=T)
Direction = Increment2;
}
// Increment case 4/6
if (ABCPads == 4)
{
if (ABCPrevious[0] == 6) // Normal (A=LU, B=R, C=H)
Direction = Increment;
if (ABCPrevious[0] == 2) // Skiping (A=LU, B=R, C=T)
Direction = Increment2;
}
// Increment case 5/6
if (ABCPads == 5)
{
if (ABCPrevious[0] == 4) // Normal (A=T, B=LU, C=H)
Direction = Increment;
if (ABCPrevious[0] == 6) // Skiping (A=T, B=R, C=H)
Direction = Increment2;
}
// Increment case 6/6
if (ABCPads == 1)
{
if (ABCPrevious[0] == 5) // Normal (A=H, B=LU, C=R)
Direction = Increment;
if (ABCPrevious[0] == 4) // Skiping (A=T, B=LU, C=R)
Direction = Increment2;
}
// Decrement case 1/6
if (ABCPads == 5)
{
if (ABCPrevious[0] == 1) // Normal (A=H, B=LU, C=T)
Direction = Decrement;
if (ABCPrevious[0] == 3) // Skiping (A=H, B=R, C=T)
Direction = Decrement2;
}
// Decrement case 2/6
if (ABCPads == 4)
{
if (ABCPrevious[0] == 5) // Normal (A=R, B=LU, C=H)
Direction = Decrement;
if (ABCPrevious[0] == 1) // Skiping (A=R, B=LU, C=T)
Direction = Decrement2;
}
// Decrement case 3/6
if (ABCPads == 6)
{
if (ABCPrevious[0] == 4) // Normal (A=LU, B=T, C=H)
Direction = Decrement;
if (ABCPrevious[0] == 5) // Skiping (A=R, B=T, C=H)
Direction = Decrement2;
}
// Decrement case 4/6
if (ABCPads == 2)
{
if (ABCPrevious[0] == 6) // Normal (A=LU, B=H, C=R)
Direction = Decrement;
if (ABCPrevious[0] == 4) // Skiping (A=LU, B=T, C=R)
Direction = Decrement2;
}
// Decrement case 5/6
if (ABCPads == 3)
{
if (ABCPrevious[0] == 2) // Normal (A=T, B=H, C=LU)
Direction = Decrement;
if (ABCPrevious[0] == 6) // Skiping (A=T, B=H, C=R)
Direction = Decrement2;
}
// Decrement case 6/6
if (ABCPads == 1)
{
if (ABCPrevious[0] == 3) // Normal (A=H, B=R, C=LU)
Direction = Decrement;
if (ABCPrevious[0] == 2) // Skiping (A=T, B=R, C=LU)
Direction = Decrement2;
}
/*
// Update: The condition for skipping can only be implemented for light touch, as the twitch suppression would filter out fast change on the same pin anyway, so it would not make sense.
// (It is hard to drag your finger so fast that if would skipp state change when you're pushing it hard on the surface, so it has very limited usefulness anyway.)
// This commented section works as the next optimized section, however it's kept cause it may be more understandable for beginners. ...and me. :P I don't wanna stare at it and wonder how the hack did I do it in some time... ;)
// Hard touch scenario (touching 2-3 pads at a time.) Rrequires checking the 2nd and 3rd previous status(which together shows second previous event) to determine the direction.
// Previous event won't do cause the cases for forward and backward are all the same, only determined by the the previously active pad, and each pat is first released and then touched again while the other 2 are held, so we need to go back 1 event further with the checks.
// Case 1/6 (A=H, B=H, C=T)
if (ABCPads == 7 && ABCPrevious[0] == 3)
{
if (ABCPrevious[1] == 7 && ABCPrevious[2] == 5 || ABCPrevious[1] == 1 && ABCPrevious[2] == 0) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (ABCPrevious[1] == 7 && ABCPrevious[2] == 6 || ABCPrevious[1] == 2 && ABCPrevious[2] == 0) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 2/6 (A=R, B=H, C=H)
if (ABCPads == 6 && ABCPrevious[0] == 7)
{
if (ABCPrevious[1] == 3 && ABCPrevious[2] == 7 || ABCPrevious[1] == 3 && ABCPrevious[2] == 1) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (ABCPrevious[1] == 5 && ABCPrevious[2] == 7 || ABCPrevious[1] == 5 && ABCPrevious[2] == 1) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 3/6 (A=T, B=H, C=H)
if (ABCPads == 7 && ABCPrevious[0] == 6)
{
if (ABCPrevious[1] == 7 && ABCPrevious[2] == 3 || ABCPrevious[1] == 2 && ABCPrevious[2] == 0) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (ABCPrevious[1] == 7 && ABCPrevious[2] == 5 || ABCPrevious[1] == 4 && ABCPrevious[2] == 0) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 4/6 (A=H, B=R, C=H)
if (ABCPads == 5 && ABCPrevious[0] == 7)
{
if (ABCPrevious[1] == 6 && ABCPrevious[2] == 7 || ABCPrevious[1] == 6 && ABCPrevious[2] == 2) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (ABCPrevious[1] == 3 && ABCPrevious[2] == 7 || ABCPrevious[1] == 3 && ABCPrevious[2] == 2) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 5/6 (A=H, B=T, C=H)
if (ABCPads == 7 && ABCPrevious[0] == 5)
{
if (ABCPrevious[1] == 7 && ABCPrevious[2] == 6 || ABCPrevious[1] == 4 && ABCPrevious[2] == 0) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (ABCPrevious[1] == 7 && ABCPrevious[2] == 3 || ABCPrevious[1] == 1 && ABCPrevious[2] == 0) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 6/6 (A=H, B=H, C=R)
if (ABCPads == 3 && ABCPrevious[0] == 7)
{
if (ABCPrevious[1] == 5 && ABCPrevious[2] == 7 || ABCPrevious[1] == 5 && ABCPrevious[2] == 4) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (ABCPrevious[1] == 6 && ABCPrevious[2] == 7 || ABCPrevious[1] == 6 && ABCPrevious[2] == 4) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
*/
// Hard touch scenario (touching 2-3 pads at a time.) Rrequires checking the 2nd and 3rd previous status(which together shows second previous event) to determine the direction.
// Previous event won't do cause the cases for forward and backward are all the same, only determined by the the previously active pad, and each pat is first released and then touched again while the other 2 are held, so we need to go back 1 event further with the checks.
// Case 1/6 (A=H, B=H, C=T)
if (!(ABCPads ^ 7 | ABCPrevious[0] ^ 3))
{
if (!(ABCPrevious[1] ^ 7 | ABCPrevious[2] ^ 5) || !(ABCPrevious[1] ^ 1 | ABCPrevious[2] ^ 0)) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (!(ABCPrevious[1] ^ 7 | ABCPrevious[2] ^ 6) || !(ABCPrevious[1] ^ 2 | ABCPrevious[2] ^ 0)) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 2/6 (A=R, B=H, C=H)
if (!(ABCPads ^ 6 | ABCPrevious[0] ^ 7))
{
if (!(ABCPrevious[1] ^ 3 | ABCPrevious[2] ^ 7) || !(ABCPrevious[1] ^ 3 | ABCPrevious[2] ^ 1)) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (!(ABCPrevious[1] ^ 5 | ABCPrevious[2] ^ 7) || !(ABCPrevious[1] ^ 5 | ABCPrevious[2] ^ 1)) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 3/6 (A=T, B=H, C=H)
if (!(ABCPads ^ 7 | ABCPrevious[0] ^ 6))
{
if (!(ABCPrevious[1] ^ 7 | ABCPrevious[2] ^ 3) || !(ABCPrevious[1] ^ 2 | ABCPrevious[2] ^ 0)) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (!(ABCPrevious[1] ^ 7 | ABCPrevious[2] ^ 5) || !(ABCPrevious[1] ^ 4 | ABCPrevious[2] ^ 0)) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 4/6 (A=H, B=R, C=H)
if (!(ABCPads ^ 5 | ABCPrevious[0] ^ 7))
{
if (!(ABCPrevious[1] ^ 6 | ABCPrevious[2] ^ 7) || !(ABCPrevious[1] ^ 6 | ABCPrevious[2] ^ 2)) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (!(ABCPrevious[1] ^ 3 | ABCPrevious[2] ^ 7) || !(ABCPrevious[1] ^ 3 | ABCPrevious[2] ^ 2)) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 5/6 (A=H, B=T, C=H)
if (!(ABCPads ^ 7 | ABCPrevious[0] ^ 5))
{
if (!(ABCPrevious[1] ^ 7 | ABCPrevious[2] ^ 6) || !(ABCPrevious[1] ^ 4 | ABCPrevious[2] ^ 0)) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (!(ABCPrevious[1] ^ 7 | ABCPrevious[2] ^ 3) || !(ABCPrevious[1] ^ 1 | ABCPrevious[2] ^ 0)) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
// Case 6/6 (A=H, B=H, C=R)
if (!(ABCPads ^ 3 | ABCPrevious[0] ^ 7))
{
if (!(ABCPrevious[1] ^ 5 | ABCPrevious[2] ^ 7) || !(ABCPrevious[1] ^ 5 | ABCPrevious[2] ^ 4)) // Increment (2nd previous event B=T || B=LU)
Direction = Increment;
if (!(ABCPrevious[1] ^ 6 | ABCPrevious[2] ^ 7) || !(ABCPrevious[1] ^ 6 | ABCPrevious[2] ^ 4)) // Decrement (2nd previous event A=T || A=LU)
Direction = Decrement;
}
}
}
void TouchBar::AdjustOutput ()
{
if (Config->GetRampFlag() == true)
{
if (Direction > Static)
{
if (Target < Config->Limit - Config->Resolution && Direction == Increment)
Target += Config->Resolution;
else
{
if (Target < Config->Limit - Config->Resolution && Direction == Increment2)
Target += Config->Resolution * 2;
else
Target = Config->Limit;
}
}
if (Direction < Static)
{
if (Target >= Config->Resolution && Direction == Decrement)
Target -= Config->Resolution;
else
{
if (Target >= Config->Resolution && Direction == Decrement2)
Target -= Config->Resolution * 2;
else
Target = 0;
}
}
if (RampCounter == Config->RampDelay - 1)
{
if (Current < Target)
if (Current < Target - Config->RampResolution)
Current += Config->RampResolution;
else
Current = Target;
if (Current > Target)
if (Current > Target + Config->RampResolution)
Current -= Config->RampResolution;
else
Current = Target;
}
RampCounter += 1;
RampCounter %= Config->RampDelay;
}
else
{
if (Direction > Static)
{
if (Config->GetRollOverFlag() == true)
{
if (Direction == Increment)
Current += Config->Resolution;
if (Direction == Increment2)
Current += Config->Resolution * 2;
Current %= Config->Limit;
}
else
{
if (Current < Config->Limit - Config->Resolution && Direction == Increment)
Current += Config->Resolution;
else
{
if (Current < Config->Limit - Config->Resolution * 2 && Direction == Increment2)
Current += Config->Resolution * 2;
else
Current = Config->Limit;
}
}
}
if (Direction < Static)
{
if (Config->GetRollOverFlag() == true)
{
byte X = 0;
if (Direction == Decrement)
X = Config->Resolution;
if (Direction == Decrement2)
X = Config->Resolution * 2;
if (Current - X >= Config->Limit)
{
X -= Current;
Current = Config->Limit - X;
}
else
Current -= X;
}
else
{
if (Current >= Config->Resolution && Direction == Decrement)
Current -= Config->Resolution;
else
{
if (Current >= Config->Resolution * 2 && Direction == Decrement2)
Current -= Config->Resolution * 2;
else
Current = 0;
}
}
}
}
}