-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4BY4.ino
381 lines (261 loc) · 7.83 KB
/
4BY4.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
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
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include "MUX74HC4067.h"
/* -------------- READ -------------- */
// http://adam-meyer.com/arduino/CD74HC4067
/* -------------- DEBUG -------------- */
#define DO_HACK // <-- ATTN: REMOVE THIS (knob I wired wrong)
#define DO_LCD
#define DO_MUX
#define DO_ROT
/* -------------- MUX HC4067 -------------- */
// for HC4067
#define MUX_SIG_PIN A0
#define MUX_EN_PIN 7 // D7
#define MUX_S3_PIN 6
#define MUX_S2_PIN 5
#define MUX_S1_PIN 4
#define MUX_S0_PIN 3
#ifdef DO_MUX
MUX74HC4067 mux(MUX_EN_PIN, MUX_S0_PIN, MUX_S1_PIN, MUX_S2_PIN, MUX_S3_PIN);
#endif
/* -------------- TFT SPI 1.8" -------------- */
// for SPI 1.8" TFT
// # LED
#define TFT_SCK_PIN 13 // [SCK] + Clock = Teensy2.0:1, Uno:D13
#define TFT_MOSI_PIN 11 // [SDA] + MOSI + Data Output = Teensy2.0:2, Uno:D11
#define TFT_DC_PIN 9 // [A0] = DC = EDITABLE ~ must be PWM Uno:D9
#define TFT_RST_PIN 99 // [RESET] = EDITABLE:12 OR ARDUINO RESET PIN
#define TFT_CS_PIN 10 // [CS/SS/NSS] = Select = Teensy2.0:0, Uno:D10
// # GND
// # VCC
/* -------------- ROTARY ENCODER -------------- */
#define ROT_CLK 12
#define ROT_DT 8
#define ROT_SW -1
// for reference
/* -------------- CONFIG / APPLICATION -------------- */
#define NUM_INPUTS 16
#define NUM_CHANNELS 4
#define NUM_TOTAL 64
#define HEIGHT 130
#define WIDTH 150
#define SPACE 30
int SQRT = sqrt(NUM_INPUTS); // square root of number of inputs (ie. 4)
int SIZE = min(WIDTH,HEIGHT); // minimum size
int _lastClk;
int _currentClk;
int _currentDT;
int _inited = 0;
bool _changed = false;
/* -------------- GLOBAL REFERENCES -------------- */
int _lookup[NUM_TOTAL];
int _angles[NUM_INPUTS];
int _values[NUM_INPUTS];
int _centersX[NUM_INPUTS];
int _centersY[NUM_INPUTS];
int _statuses[NUM_INPUTS];
int _directions[NUM_INPUTS];
int _channel = 0;
word _colours[4];
/* -------------- INCLUDES -------------- */
#ifdef DO_LCD
#include "drawing.h"
#endif
bool isClose( int value, int ref ) {
return (value >= ref - 3 && value <= ref + 3 );
}
/* -------------- SETUP -------------- */
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_INPUTS * NUM_CHANNELS; i++) {
_lookup[i] = 512;
}
for (int i = 0; i < NUM_INPUTS; i++) {
_values[i] = 512;
_angles[i] = -999;
}
// set mux lookup
#ifdef DO_LCD
_colours[0] = colour( 0,255,255 );
_colours[1] = colour( 255,255,0 );
_colours[2] = colour( 0,255,0 );
_colours[3] = colour( 0,0,255 );
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST7735_BLACK);
tft.setRotation(3);
for (int i = 0; i < NUM_CHANNELS; i++) drawTab(i);
drawDot(0, COL_WHITE);
#endif
#ifdef DO_MUX
mux.signalPin(MUX_SIG_PIN, INPUT, ANALOG);
#endif
#ifdef DO_ROT
pinMode(ROT_CLK,INPUT);
pinMode(ROT_DT,INPUT);
// attachInterrupt(0,onEncoder,CHANGE);
// attachInterrupt(1,onEncoder,CHANGE);
_lastClk = digitalRead( ROT_CLK );
// tft.drawRect(22, 2, 5, SIZE - 6, COL_WHITE);
#endif
}
unsigned long timestamp;
int proposed_channel = 0;
int last_proposed_channel = 0;
void drawFromCenter( int i, int v, word col ) {
return;
int xx = 0;
int yy = 0;
int sz = SIZE / SQRT / 2;
int angle = (360 - map(v, 0, 1024, SPACE, 360-SPACE)) + 90;
pointFromAngle( _centersX[i], _centersY[i], angle, sz - 6, xx, yy);
tft.fillCircle(xx,yy,1,col);
}
int getIdx( int idx ) {
#ifdef DO_HACK
// I wired these the wrong way around, lol
if (idx == 7) return 8;
if (idx == 8) return 7;
#endif
return idx;
}
void loop() {
#ifdef DO_ROT
_currentClk = digitalRead(ROT_CLK);
_currentDT = digitalRead(ROT_DT);
bool changed = false;
if (_currentClk != _lastClk && _currentClk == 1) {
timestamp = millis();
if (_currentDT != _currentClk) {
proposed_channel--;
} else {
proposed_channel++;
}
if (proposed_channel < 0) proposed_channel = NUM_CHANNELS - 1;
if (proposed_channel >= NUM_CHANNELS) proposed_channel = 0;
if (proposed_channel != last_proposed_channel) {
for (int i = 0; i < NUM_CHANNELS; i++) drawDot(i, COL_BLACK);
drawDot(proposed_channel, COL_WHITE);
last_proposed_channel = proposed_channel;
}
Serial.print("info:propose:");
Serial.println(proposed_channel);
changed = true;
delay(1);
}
_lastClk = _currentClk;
if (changed) return;
if ((millis() - timestamp > 600 && _channel != proposed_channel) || _inited == 1) {
for (int i = 0; i < NUM_INPUTS; i++) {
int idx = i;
int IDX = (_channel * NUM_INPUTS) + idx;
drawLine(idx, _values[idx], COL_BLACK);
drawLine(idx, _lookup[IDX], COL_BLACK);
drawFromCenter(idx, _values[idx], COL_BLACK);
drawFromCenter(idx, _lookup[IDX], COL_BLACK);
}
_channel = proposed_channel;
for (int i = 0; i < NUM_INPUTS; i++) {
int idx = i;
int IDX = (_channel * NUM_INPUTS) + idx;
drawKnob(idx);
_directions[idx] = _values[idx] < _lookup[IDX];
_statuses[idx] = false;
}
Serial.print("info:page:");
Serial.println(_channel);
if (_inited != 2) _inited = 2;
_changed = true;
return;
}
#endif
#ifdef DO_MUX
int value;
int lookupVal;
int pastVal;
bool status;
for (byte i = 0; i < NUM_INPUTS; i++) {
int idx = i;
int IDX = (_channel * NUM_INPUTS) + idx;
int angle = getAngle(value);
value = mux.read(getIdx(i));
String _i = String(idx);
String _IDX = String(IDX);
lookupVal = _lookup[IDX];
pastVal = _values[idx];
status = _statuses[idx];
if (_inited == 0) {
_values[idx] = value;
_angles[idx] = -999;
_lookup[IDX] = 512;
Serial.print("info:inited:");
Serial.println(_i);
} else {
bool bCloseLookup = isClose(value, lookupVal );
bool bCloseValues = isClose(value, pastVal );
bool bFlipped = _directions[idx] ? value >= lookupVal : value < lookupVal;
// Serial.println(_i + ":" + _value + ":" + _lookup );
if ( !status ) {
// Serial.println(bCloseLookup);
// Serial.println(bFlipped);
// Serial.println(value);
// Serial.println(_lookup[IDX]);
// Serial.println("---");
if (bCloseLookup || bFlipped) {
/* RESET */
_statuses[idx] = true;
// Serial.print(_i + " A = ");
// Serial.print(lookupVal);
// Serial.println(pastVal);
drawLine( idx, pastVal, COL_BLACK );
drawFromCenter( idx, pastVal, COL_BLACK );
drawLine(idx, lookupVal, COL_BLACK);
drawFromCenter(idx, lookupVal, COL_BLACK);
drawLine( idx, value, COL_WHITE );
drawFromCenter( idx, value, COL_WHITE );
drawKnob( idx );
_values[idx] = value;
// tft.fillCircle( _centersX[idx], _centersY[idx], 2, COL_WHITE );c
} else {
/* DISABLED */
// Serial.print(_i + " B = ");
// Serial.print(lookupVal);
// Serial.println(pastVal);
// drawLine(i, pastVal, COL_BLACK);
// drawLine(i, lookupVal, COL_RED);
// drawLine(i, value, COL_WHITE);
if (!bCloseValues || _changed) {
drawLine( idx, pastVal, COL_BLACK );
drawFromCenter( idx, pastVal, COL_BLACK );
drawLine(idx, lookupVal, COL_RED);
drawFromCenter(idx, lookupVal, COL_RED);
drawLine( idx, value, COL_WHITE );
drawFromCenter( idx, value, COL_WHITE );
drawKnob( idx, COL_RED );
_values[idx] = value;
}
}
} else {
// drawLine(i, pastVal, COL_BLACK);
// drawLine(i, value, COL_WHITE);
if (!bCloseValues || _changed) {
drawLine( idx, pastVal, COL_BLACK );
drawFromCenter( idx, pastVal, COL_BLACK );
drawLine( idx, value, COL_WHITE );
drawFromCenter( idx, value, COL_WHITE );
_values[idx] = value;
_lookup[IDX] = value;
Serial.print("change:");
Serial.print(_IDX);
Serial.print(":");
Serial.println(_lookup[IDX]);
}
}
}
}
// Serial.println(" ");
#endif
if (!_inited) _inited = 1;
_changed = false;
}