-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSB_OSC_5_encoders_5_Pages_wheels_With_fader.ino
704 lines (534 loc) · 19.8 KB
/
USB_OSC_5_encoders_5_Pages_wheels_With_fader.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright (c) 2017 Electronic Theatre Controls, Inc., http://www.etcconnect.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/*******************************************************************************
Electronic Theatre Controls
lighthack - Box 1
(c) 2017 by ETC
This code implements a Pan/Tilt module using two encoders and three
buttons. The two encoders function as pan and tilt controllers with one
button being reserved for controlling fine/coarse mode. The other two
buttons are assigned to Next and Last which make it easy to switch between
channels.
*******************************************************************************
NOTE: UPDATE VERSION_STRING IN DEFINITIONS BELOW WHEN VERSION NUMBER CHANGES
Revision History
yyyy-mm-dd Vxx By_Who Comment
2017-07-21 1.0.0.1 Ethan Oswald Massey Original creation
2017-10-19 1.0.0.2 Sam Kearney Fix build errors on some
Arduino platforms. Change
OSC subscribe parameters
2017-10-24 1.0.0.3 Sam Kearney Add ability to scale encoder
output
2017-11-22 1.0.0.4 Hans Hinrichsen Add splash msg before Eos
connects
2017-12-07 1.0.0.5 Hans Hinrichsen Added timeout to disconnect
and show splash screen again
******************************************************************************/
/*******************************************************************************
Includes
******************************************************************************/
#include <SPI.h>
#include <OSCBoards.h>
#include <OSCBundle.h>
#include <OSCData.h>
#include <OSCMatch.h>
#include <OSCMessage.h>
#include <OSCTiming.h>
#ifdef BOARD_HAS_USB_SERIAL
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial(thisBoardsSerialUSB);
#else
#include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial);
#endif
#include <string.h>
/*******************************************************************************
Macros and Constants
******************************************************************************/
#define ONE_BTN 11
#define TWO_BTN 12
#define THREE_BTN 13
#define SHIFT_BTN 2
#define SUBSCRIBE ((int32_t)1)
#define UNSUBSCRIBE ((int32_t)0)
#define EDGE_DOWN ((int32_t)1)
#define EDGE_UP ((int32_t)0)
#define FORWARD 0
#define REVERSE 1
// Change these values to switch which direction increase/decrease pan/tilt
#define ENC1_DIR FORWARD
#define ENC2_DIR FORWARD
#define ENC3_DIR FORWARD
#define ENC4_DIR FORWARD
#define ENC5_DIR FORWARD
// Use these values to make the encoder more coarse or fine. This controls
// the number of wheel "ticks" the device sends to Eos for each tick of the
// encoder. 1 is the default and the most fine setting. Must be an integer.
//changed to an int so they can be modified at run time.
int ENC1_SCALE = 1;
int ENC2_SCALE = 1;
int ENC3_SCALE = 1;
int ENC4_SCALE = 1;
int ENC5_SCALE = 1;
int sensorPin = A0;
float sensorValue = 0.000;
float outputValue = 0.000;
float sensorVal = 0.000;
float sensorValstring = 0.000;
//added variables to set global "coarse" and "normal" encoder modes
int encNorm = 1;
int encCoarse = 8;
#define SIG_DIGITS 2 // Number of significant digits displayed
#define OSC_BUF_MAX_SIZE 512
const String HANDSHAKE_QUERY = "ETCOSC?";
const String HANDSHAKE_REPLY = "OK";
const String EOS_KEY = "/eos/key/";
//See displayScreen() below - limited to 10 chars (after 6 prefix chars)
const String VERSION_STRING = "1.0.0.5C";
// Change these values to alter how long we wait before sending an OSC ping
// to see if Eos is still there, and then finally how long before we
// disconnect and show the splash screen
// Values are in milliseconds
#define PING_AFTER_IDLE_INTERVAL 5000
#define TIMEOUT_AFTER_IDLE_INTERVAL 15000
char currentChannel[255];
int mode = 1;
static uint32_t debounceTime1 = 0;
static uint32_t debounceTime2 = 0;
static uint32_t debounceTime3 = 0;
/*******************************************************************************
Local Types
******************************************************************************/
enum WHEEL_TYPE { ENC1, ENC2, ENC3, ENC4, ENC5 };
enum WHEEL_MODE { COARSE, FINE };
struct Encoder
{
uint8_t pinA;
uint8_t pinB;
int pinAPrevious;
int pinBPrevious;
float pos;
uint8_t direction;
};
struct Encoder enc1Wheel;
struct Encoder enc2Wheel;
struct Encoder enc3Wheel;
struct Encoder enc4Wheel;
struct Encoder enc5Wheel;
/*******************************************************************************
Global Variables
******************************************************************************/
bool connectedToEos = false;
unsigned long lastMessageRxTime = 0;
bool timeoutPingSent = false;
/*******************************************************************************
Local Functions
******************************************************************************/
/*******************************************************************************
Issues all our subscribes to Eos. When subscribed, Eos will keep us updated
with the latest values for a given parameter.
Parameters: none
Return Value: void
******************************************************************************/
void issueSubscribes()
{
// Add a filter so we don't get spammed with unwanted OSC messages from Eos
OSCMessage filter("/eos/filter/add");
filter.add("/eos/out/param/*");
filter.add("/eos/out/ping");
SLIPSerial.beginPacket();
filter.send(SLIPSerial);
SLIPSerial.endPacket();
// subscribe to Eos pan & tilt updates
OSCMessage subPan("/eos/subscribe/param/pan/tilt/zoom/edge");
subPan.add(SUBSCRIBE);
SLIPSerial.beginPacket();
subPan.send(SLIPSerial);
SLIPSerial.endPacket();
OSCMessage faderSetup("eos/fader/1/config/1/10");
SLIPSerial.beginPacket();
faderSetup.send(SLIPSerial);
SLIPSerial.endPacket();
}
/*******************************************************************************
Given a valid OSCMessage (relevant to Pan/Tilt), we update our Encoder struct
with the new position information.
Parameters:
msg - The OSC message we will use to update our internal data
addressOffset - Unused (allows for multiple nested roots)
Return Value: void
******************************************************************************/
void parseNull(OSCMessage& msg, int addressOffset)
{
}
/*******************************************************************************
Given an unknown OSC message we check to see if it's a handshake message.
If it's a handshake we issue a subscribe, otherwise we begin route the OSC
message to the appropriate function.
Parameters:
msg - The OSC message of unknown importance
Return Value: void
******************************************************************************/
void parseOSCMessage(String& msg)
{
// check to see if this is the handshake string
if (msg.indexOf(HANDSHAKE_QUERY) != -1)
{
// handshake string found!
SLIPSerial.beginPacket();
SLIPSerial.write((const uint8_t*)HANDSHAKE_REPLY.c_str(), (size_t)HANDSHAKE_REPLY.length());
SLIPSerial.endPacket();
// Let Eos know we want updates on some things
issueSubscribes();
// Make our splash screen go away
connectedToEos = true;
}
else
{
// prepare the message for routing by filling an OSCMessage object with our message string
OSCMessage oscmsg;
oscmsg.fill((uint8_t*)msg.c_str(), (int)msg.length());
}
}
/*******************************************************************************
Initializes a given encoder struct to the requested parameters.
Parameters:
encoder - Pointer to the encoder we will be initializing
pinA - Where the A pin is connected to the Arduino
pinB - Where the B pin is connected to the Arduino
direction - Determines if clockwise or counterclockwise is "forward"
Return Value: void
******************************************************************************/
void initEncoder(struct Encoder* encoder, uint8_t pinA, uint8_t pinB, uint8_t direction)
{
encoder->pinA = pinA;
encoder->pinB = pinB;
encoder->pos = 0;
encoder->direction = direction;
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
encoder->pinAPrevious = digitalRead(pinA);
encoder->pinBPrevious = digitalRead(pinB);
}
/*******************************************************************************
Checks if the encoder has moved by comparing the previous state of the pins
with the current state. If they are different, we know there is movement.
In the event of movement we update the current state of our pins.
Parameters:
encoder - Pointer to the encoder we will be checking for motion
Return Value:
encoderMotion - Returns the 0 if the encoder has not moved
1 for forward motion
-1 for reverse motion
******************************************************************************/
int8_t updateEncoder(struct Encoder* encoder)
{
int8_t encoderMotion = 0;
int pinACurrent = digitalRead(encoder->pinA);
int pinBCurrent = digitalRead(encoder->pinB);
// has the encoder moved at all?
if (encoder->pinAPrevious != pinACurrent)
{
// Since it has moved, we must determine if the encoder has moved forwards or backwards
encoderMotion = (encoder->pinAPrevious == encoder->pinBPrevious) ? -1 : 1;
// If we are in reverse mode, flip the direction of the encoder motion
if (encoder->direction == REVERSE)
encoderMotion = -encoderMotion;
}
encoder->pinAPrevious = pinACurrent;
encoder->pinBPrevious = pinBCurrent;
return encoderMotion;
}
/*******************************************************************************
Sends a message to Eos informing them of a wheel movement.
Parameters:
type - the type of wheel that's moving (i.e. pan or tilt)
ticks - the direction and intensity of the movement
Return Value: void
******************************************************************************/
void sendWheelMove(WHEEL_TYPE type, float ticks)
{
String wheelMsg("/eos/active/wheel");
if (digitalRead(SHIFT_BTN) == LOW)
wheelMsg.concat("/fine");
else
wheelMsg.concat("/coarse");
if (type == ENC1) {
wheelMsg.concat("/1");
}
else if (type == ENC2) {
wheelMsg.concat("/2");
}
else if (type == ENC3) {
wheelMsg.concat("/3");
}
else if (type == ENC4) {
wheelMsg.concat("/4");
}
else if (type == ENC5) {
wheelMsg.concat("/5");
}
else
// something has gone very wrong
return;
OSCMessage wheelUpdate(wheelMsg.c_str());
wheelUpdate.add(ticks);
SLIPSerial.beginPacket();
wheelUpdate.send(SLIPSerial);
SLIPSerial.endPacket();
}
/*******************************************************************************
Sends a message to Eos informing them of a key press.
Parameters:
down - whether a key has been pushed down (true) or released (false)
key - the key that has moved
Return Value: void
******************************************************************************/
void sendKeyPress(bool down, String key)
{
key = "/eos/key/" + key;
OSCMessage keyMsg(key.c_str());
// if (down)
// keyMsg.add(EDGE_DOWN);
// else
// keyMsg.add(EDGE_UP);
SLIPSerial.beginPacket();
keyMsg.send(SLIPSerial);
SLIPSerial.endPacket();
}
/*******************************************************************************
Checks the status of all the buttons relevant to Eos (i.e. Next & Last)
NOTE: This does not check the shift key. The shift key is used in tandem with
the encoder to determine coarse/fine mode and thus does not report to Eos
directly.
Parameters: none
Return Value: void
******************************************************************************/
void checkButtons()
{
static int oneKeyState = HIGH;
static int twoKeyState = HIGH;
static int threeKeyState = HIGH;
// Has the button state changed
if (digitalRead(ONE_BTN) != oneKeyState)
{
oneKeyState = digitalRead(ONE_BTN);
// Notify Eos of this key press
if (oneKeyState == LOW)
{
debounceTime1 = millis();
}
else
{
debounceTime1 = 0;
}
}
if (debounceTime1 > 0 && (millis() - debounceTime1 > 10)) {
// ... set the time stamp to 0 to say we have finished debouncing
debounceTime1 = 0;
OSCMessage LoadMsg("/eos/fader/1/1/load");
SLIPSerial.beginPacket();
LoadMsg.send(SLIPSerial);
SLIPSerial.endPacket();
}
if (digitalRead(TWO_BTN) != twoKeyState)
{
twoKeyState = digitalRead(TWO_BTN);
if (twoKeyState == LOW)
{
debounceTime2 = millis();
}
else
{
debounceTime2 = 0;
}
}
if (debounceTime2 > 0 && (millis() - debounceTime2 > 10)) {
// ... set the time stamp to 0 to say we have finished debouncing
debounceTime2 = 0;
OSCMessage StopMsg("/eos/fader/1/1/stop");
SLIPSerial.beginPacket();
StopMsg.send(SLIPSerial);
SLIPSerial.endPacket();
}
if (digitalRead(THREE_BTN) != threeKeyState)
{
threeKeyState = digitalRead(THREE_BTN);
if (threeKeyState == LOW)
{
debounceTime3 = millis();
}
else
{
debounceTime3 = 0;
}
}
if (debounceTime3 > 0 && (millis() - debounceTime3 > 10)) {
// ... set the time stamp to 0 to say we have finished debouncing
debounceTime3 = 0;
OSCMessage FireMsg("/eos/fader/1/1/fire");
SLIPSerial.beginPacket();
FireMsg.send(SLIPSerial);
SLIPSerial.endPacket();
}
}
void checkFaders()
{
sensorValue = analogRead(sensorPin);
outputValue = sensorValue;
outputValue = constrain(outputValue, 0, 1000);
// String sensorValstring = String(outputValue/10);
sensorValstring = outputValue / 1000;
// sensorValstring = sensorValstring + '.' + (outputValue % 1000);
if (sensorVal == sensorValue)
// if( abs(sensorValstring2 - outputValue) < THRESHOLD)
//value has really changed - do something with the new value
{
}
else if (sensorVal > sensorValue + 3 or sensorVal < sensorValue - 3)
{
OSCMessage msg("/eos/fader/1/1");
SLIPSerial.beginPacket();
msg.add(sensorValstring);
msg.send(SLIPSerial);
SLIPSerial.endPacket();
msg.empty();
}
delay(10);
sensorVal = sensorValue;
}
/*******************************************************************************
Here we setup our encoder, lcd, and various input devices. We also prepare
to communicate OSC with Eos by setting up SLIPSerial. Once we are done with
setup() we pass control over to loop() and never call setup() again.
NOTE: This function is the entry function. This is where control over the
Arduino is passed to us (the end user).
Parameters: none
Return Value: void
******************************************************************************/
void setup()
{
SLIPSerial.begin(115200);
// This is a hack around an Arduino bug. It was taken from the OSC library
//examples
//#ifdef BOARD_HAS_USB_SERIAL
// while (!SerialUSB);
//#else
// while (!Serial);
//#endif
// This is necessary for reconnecting a device because it needs some time
// for the serial port to open, but meanwhile the handshake message was
// sent from Eos
SLIPSerial.beginPacket();
SLIPSerial.write((const uint8_t*)HANDSHAKE_REPLY.c_str(), (size_t)HANDSHAKE_REPLY.length());
SLIPSerial.endPacket();
// Let Eos know we want updates on some things
issueSubscribes();
initEncoder(&enc1Wheel, A1, A2, ENC1_DIR);
initEncoder(&enc2Wheel, A3, A4, ENC2_DIR);
initEncoder(&enc3Wheel, 5, 6, ENC3_DIR);
initEncoder(&enc4Wheel, 7, 8, ENC4_DIR);
initEncoder(&enc5Wheel, 9, 10, ENC5_DIR);
pinMode(ONE_BTN, INPUT_PULLUP);
pinMode(TWO_BTN, INPUT_PULLUP);
pinMode(THREE_BTN, INPUT_PULLUP);
pinMode(SHIFT_BTN, INPUT_PULLUP);
}
/*******************************************************************************
Here we service, monitor, and otherwise control all our peripheral devices.
First, we retrieve the status of our encoders and buttons and update Eos.
Next, we check if there are any OSC messages for us.
Finally, we update our display (if an update is necessary)
NOTE: This function is our main loop and thus this function will be called
repeatedly forever
Parameters: none
Return Value: void
******************************************************************************/
void loop()
{
static String curMsg;
int size;
// get the updated state of each encoder
int32_t enc1Motion = updateEncoder(&enc1Wheel);
int32_t enc2Motion = updateEncoder(&enc2Wheel);
int32_t enc3Motion = updateEncoder(&enc3Wheel);
int32_t enc4Motion = updateEncoder(&enc4Wheel);
int32_t enc5Motion = updateEncoder(&enc5Wheel);
// Scale the result by a scaling factor
enc1Motion *= ENC1_SCALE;
enc2Motion *= ENC2_SCALE;
enc3Motion *= ENC3_SCALE;
enc4Motion *= ENC4_SCALE;
enc4Motion *= ENC5_SCALE;
// check for next/last updates
checkButtons();
checkFaders();
// now update our wheels
if (enc1Motion != 0)
sendWheelMove(ENC1, enc1Motion);
if (enc2Motion != 0)
sendWheelMove(ENC2, enc2Motion);
if (enc3Motion != 0)
sendWheelMove(ENC3, enc3Motion);
if (enc4Motion != 0)
sendWheelMove(ENC4, enc4Motion);
if (enc5Motion != 0)
sendWheelMove(ENC5, enc5Motion);
// Then we check to see if any OSC commands have come from Eos
// and update the display accordingly.
size = SLIPSerial.available();
if (size > 0)
{
// Fill the msg with all of the available bytes
while (size--)
curMsg += (char)(SLIPSerial.read());
}
if (SLIPSerial.endofPacket())
{
parseOSCMessage(curMsg);
lastMessageRxTime = millis();
// We only care about the ping if we haven't heard recently
// Clear flag when we get any traffic
timeoutPingSent = false;
curMsg = String();
}
if (lastMessageRxTime > 0)
{
unsigned long diff = millis() - lastMessageRxTime;
//We first check if it's been too long and we need to time out
if (diff > TIMEOUT_AFTER_IDLE_INTERVAL)
{
connectedToEos = false;
lastMessageRxTime = 0;
timeoutPingSent = false;
}
//It could be the console is sitting idle. Send a ping once to
// double check that it's still there, but only once after 2.5s have passed
if (!timeoutPingSent && diff > PING_AFTER_IDLE_INTERVAL)
{
OSCMessage ping("/eos/ping");
ping.add("5Encoder"); // This way we know who is sending the ping
SLIPSerial.beginPacket();
ping.send(SLIPSerial);
SLIPSerial.endPacket();
timeoutPingSent = true;
}
}
}