forked from rogerlinndesign/linnstrument-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls_numericDataChange.ino
292 lines (239 loc) · 9.99 KB
/
ls_numericDataChange.ino
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
/****************************** ls_settings: LinnStrument Settings ********************************
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
***************************************************************************************************
These functions allow for changing numeric values by sliding over the LinnStrument surface.
**************************************************************************************************/
short numericActiveColDown = 0; // Number of columns currently held down, during numeric data changes
signed char numericDataChangeCol = -1; // If -1, button has been pressed, but a starting column hasn't been set
signed char numericDataChangeColLast = -1; // The last column that was pressed
unsigned long numericDataChangeColTime = 0; // time of last touch for value change
unsigned long numericDataReleaseColTime = 0; // time of last touch release
short numericActiveRowDown = 0; // Number of rows currently held down, during numeric data changes
signed char numericDataChangeRow = -1; // If -1, button has been pressed, but a starting row hasn't been set
signed char numericDataChangeRowLast = -1; // The last row that was pressed
unsigned long numericDataChangeRowTime = 0; // time of last touch for value change
unsigned long numericDataReleaseRowTime = 0; // time of last touch release
void resetNumericDataChange() {
resetNumericDataChangeCol();
resetNumericDataChangeRow();
}
void resetNumericDataChangeCol() {
numericDataChangeCol = -1;
numericDataChangeColLast = -1;
numericActiveColDown = 0;
numericDataChangeColTime = 0;
numericDataReleaseColTime = 0;
}
void resetNumericDataChangeRow() {
numericDataChangeRow = -1;
numericDataChangeRowLast = -1;
numericActiveRowDown = 0;
numericDataChangeRowTime = 0;
numericDataReleaseRowTime = 0;
}
boolean handleNumericDataNewTouchCol(boolean ¤tData) {
unsigned short newData = handleNumericDataNewTouchColRaw(currentData, false, true, true);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchCol(unsigned short ¤tData, unsigned short minimum, unsigned short maximum, boolean useFineChanges) {
unsigned short newData = handleNumericDataNewTouchColRaw(currentData, minimum, maximum, useFineChanges);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchCol(byte ¤tData, byte minimum, byte maximum, boolean useFineChanges) {
byte newData = handleNumericDataNewTouchColRaw(currentData, minimum, maximum, useFineChanges);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchCol(char ¤tData, char minimum, char maximum, boolean useFineChanges) {
char newData = handleNumericDataNewTouchColRaw(currentData, minimum, maximum, useFineChanges);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchCol(signed char ¤tData, signed char minimum, signed char maximum, boolean useFineChanges) {
signed char newData = handleNumericDataNewTouchColRaw(currentData, minimum, maximum, useFineChanges);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchCol(short ¤tData, short minimum, short maximum, boolean useFineChanges) {
short newData = handleNumericDataNewTouchColRaw(currentData, minimum, maximum, useFineChanges);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
int handleNumericDataNewTouchColRaw(int currentData, int minimum, int maximum, boolean useFineChanges) {
unsigned long now = micros();
// If there are no columns down, reset so that things are
// relative to the next new touch
if (numericActiveColDown <= 0 && calcTimeDelta(now, numericDataReleaseColTime) > 200000) {
resetNumericDataChangeCol();
}
// keep track of how many columns are currently down
numericActiveColDown++;
int newData = currentData;
if (sensorCol != numericDataChangeColLast && // only react when the column is actually different
calcTimeDelta(now, numericDataChangeRowTime) > 300000) { // and prevent these too quickly after a vertical slide to make the interface feel more stable
if (numericDataChangeCol < 0 ||
(numericDataChangeCol < sensorCol && numericDataChangeColLast > sensorCol) ||
(numericDataChangeCol > sensorCol && numericDataChangeColLast < sensorCol)) {
// First cell hit after starting a data change or change of direction,
// don't change data yet.
numericDataChangeCol = sensorCol;
}
else {
byte increment = 1;
// If the swipe is fast, increment by a larger amount.
if (calcTimeDelta(now, numericDataChangeColTime) < 40000) {
increment = 20;
}
else if (calcTimeDelta(now, numericDataChangeColTime) < 70000) {
increment = 4;
}
else if (calcTimeDelta(now, numericDataChangeColTime) < 120000) {
increment = 2;
}
if (useFineChanges && abs(numericDataChangeCol - sensorCol) <= 3) {
increment = 1;
}
if (sensorCol > numericDataChangeCol) {
newData = constrain(currentData + increment, minimum, maximum);
}
else if (sensorCol < numericDataChangeCol) {
newData = constrain(currentData - increment, minimum, maximum);
}
numericDataChangeColTime = now;
// reset the vertical slide info when there's a horizontal change
numericDataChangeRow = sensorRow;
numericDataChangeRowLast = sensorRow;
}
}
numericDataChangeColLast = sensorCol;
return newData;
}
void handleNumericDataReleaseCol(boolean handleSplitSelection) {
// The top row doesn't change the data, it only lets you change which side you're controlling
if (handleSplitSelection && handleShowSplit()) { // see if one of the "Show Split" cells have been hit
return;
}
numericActiveColDown--;
numericDataReleaseColTime = micros();
}
boolean handleNumericDataNewTouchRow(unsigned short ¤tData, unsigned short minimum, unsigned short maximum) {
unsigned short newData = handleNumericDataNewTouchRowRaw(currentData, minimum, maximum);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchRow(byte ¤tData, byte minimum, byte maximum) {
byte newData = handleNumericDataNewTouchRowRaw(currentData, minimum, maximum);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchRow(char ¤tData, char minimum, char maximum) {
char newData = handleNumericDataNewTouchRowRaw(currentData, minimum, maximum);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
boolean handleNumericDataNewTouchRow(short ¤tData, short minimum, short maximum) {
short newData = handleNumericDataNewTouchRowRaw(currentData, minimum, maximum);
if (newData != currentData) {
currentData = newData;
updateDisplay();
return true;
}
return false;
}
int handleNumericDataNewTouchRowRaw(int currentData, int minimum, int maximum) {
unsigned long now = micros();
// If there are no rows down, reset so that things are
// relative to the next new touch
if (numericActiveRowDown <= 0 && calcTimeDelta(now, numericDataReleaseRowTime) > 200000) {
resetNumericDataChangeRow();
}
// keep track of how many rows are currently down
numericActiveRowDown++;
int newData = currentData;
if (sensorRow != numericDataChangeRowLast && // only react when the row is actually different
calcTimeDelta(now, numericDataChangeColTime) > 300000) { // and prevent these too quickly after a horizontal slide to make the interface feel more stable
if (numericDataChangeRow < 0 ||
(numericDataChangeRow < sensorRow && numericDataChangeRowLast > sensorRow) ||
(numericDataChangeRow > sensorRow && numericDataChangeRowLast < sensorRow)) {
// First cell hit after starting a data change or change of direction,
// don't change data yet.
numericDataChangeRow = sensorRow;
}
else {
byte increment = 1;
// If the swipe is fast, increment by a larger amount.
if (calcTimeDelta(now, numericDataChangeRowTime) < 70000) {
increment = 4;
}
else if (calcTimeDelta(now, numericDataChangeRowTime) < 120000) {
increment = 2;
}
if (abs(numericDataChangeRow - sensorRow) <= 1) {
increment = 0;
}
else if (abs(numericDataChangeRow - sensorRow) <= 3) {
increment = 1;
}
if (sensorRow > numericDataChangeRow) {
newData = constrain(currentData + increment, minimum, maximum);
}
else if (sensorRow < numericDataChangeRow) {
newData = constrain(currentData - increment, minimum, maximum);
}
numericDataChangeRowTime = now;
// reset the horizontal slide info when there's a vertical change
numericDataChangeCol = sensorCol;
numericDataChangeColLast = sensorCol;
}
}
numericDataChangeRowLast = sensorRow;
return newData;
}
void handleNumericDataReleaseRow(boolean handleSplitSelection) {
// The top row doesn't change the data, it only lets you change which side you're controlling
if (handleSplitSelection && handleShowSplit()) { // see if one of the "Show Split" cells have been hit
return;
}
numericActiveRowDown--;
numericDataReleaseRowTime = micros();
}