-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266-XR-Console-Remote.ino
392 lines (350 loc) · 8.56 KB
/
ESP8266-XR-Console-Remote.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
// 4-button WiFi remote for Behringer and Midas XR/MR series mixing consoles
// Designed for the Wemos D1 devboard, 4 push-buttons, 4 status LEDs
// OSC lib by Yotam Mann and Adrian Freed : https://github.com/CNMAT/OSC
// LEDs are common cathode (common negative)
// A and B switches have pull-down resistors
// C and D switches have pull-up resistors
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
#define DEBOUNCE_DELAY_MS 40 // Button press debounce delay
#define REPEAT_MESSAGE 1 // Number of times an OSC message is sent
#define WAIT_AFTER_MESSAGE_MS 10 // Pause in ms after an OSC message is sent
#define XREMOTE_REFRESH_MS 5000 // Delay between two /xremote messages
// Buttons and LEDs pins definition
#define BUTTON_A_PIN D1
#define BUTTON_B_PIN D2
#define BUTTON_C_PIN D3
#define BUTTON_D_PIN D4
#define BUTTON_A_LED D5
#define BUTTON_B_LED D6
#define BUTTON_C_LED D7
#define BUTTON_D_LED D8
const char* ssid = ""; // Wifi SSID
const char* password = ""; // WiFi password
WiFiUDP Udp;
const IPAddress consoleIp(192, 168, 0, 6); // XR/MR mixing console IP address
const unsigned int consolePort = 10024; // Console OSC port (for outgoing OSC messages : 10023 X32/M32, 10024 for XR/MR series)
const unsigned int localUdpPort = 8888; // Local OSC port (for incoming OSC messages)
OSCErrorCode error;
bool bMonoOn = false;
bool bCompOn = false;
bool bHpfOn = false;
bool bMuteOn = false;
unsigned long previousMillis;
void setup()
{
pinMode(BUTTON_A_PIN, INPUT);
pinMode(BUTTON_B_PIN, INPUT);
pinMode(BUTTON_C_PIN, INPUT);
pinMode(BUTTON_D_PIN, INPUT);
pinMode(BUTTON_A_LED, OUTPUT);
pinMode(BUTTON_B_LED, OUTPUT);
pinMode(BUTTON_C_LED, OUTPUT);
pinMode(BUTTON_D_LED, OUTPUT);
digitalWrite(BUTTON_A_LED, LOW);
digitalWrite(BUTTON_B_LED, LOW);
digitalWrite(BUTTON_C_LED, LOW);
digitalWrite(BUTTON_D_LED, LOW);
Serial.begin(115200);
delay(100);
Serial.println();
Serial.printf("\nConnecting to %s ", ssid);
Serial.println();
WiFi.begin(ssid, password);
waitForWifi();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Opening UDP port");
Udp.begin(localUdpPort);
flashALittle();
loadSnapshot(1); // Initialize with snapshot 1
Serial.println("Running");
}
void loop()
{
if (digitalRead(BUTTON_A_PIN) == HIGH)
{
if (bHpfOn == false)
{
hpFilter(1);
}
else
{
hpFilter(0);
}
while (digitalRead(BUTTON_A_PIN) == HIGH) // Waits for key release
{
delay(1);
}
delay(DEBOUNCE_DELAY_MS);
}
if (digitalRead(BUTTON_B_PIN) == HIGH)
{
if (bCompOn == false)
{
loadSnapshot(2);
}
else
{
loadSnapshot(1);
}
while (digitalRead(BUTTON_B_PIN) == HIGH) // Waits for key release
{
delay(1);
}
delay(DEBOUNCE_DELAY_MS);
}
if (digitalRead(BUTTON_C_PIN) == LOW)
{
if (bMonoOn == false)
{
monoMonitoring(1);
}
else
{
monoMonitoring(0);
}
while (digitalRead(BUTTON_C_PIN) == LOW) // Waits for key release
{
delay(1);
}
delay(DEBOUNCE_DELAY_MS);
}
if (digitalRead(BUTTON_D_PIN) == LOW)
{
if (bMuteOn == false)
{
muteMonitoring(1);
}
else
{
muteMonitoring(0);
}
while (digitalRead(BUTTON_D_PIN) == LOW) // Waits for key release
{
delay(1);
}
delay(DEBOUNCE_DELAY_MS);
}
waitForWifi();
keepSubscription();
processIncomingMessages();
}
void loadSnapshot(int snapshotIdx)
{
sendOscMessage("/-snap/load", snapshotIdx);
}
void monoMonitoring(int onOff)
{
sendOscMessage("/config/solo/mono", onOff);
}
void hpFilter(int onOff)
{
if (onOff == 1)
{
// Sets a low shelf EQ too
sendOscMessage("/ch/01/eq/1/type", 1); // Low-shelf
processIncomingMessages();
sendOscMessage("/ch/01/eq/1/f", 0.335f); // 200 Hz
processIncomingMessages();
sendOscMessage("/ch/01/eq/1/g", 0.4f); // -3 dB
processIncomingMessages();
sendOscMessage("/ch/01/eq/on", 1); // EQ on
processIncomingMessages();
sendOscMessage("/ch/01/preamp/hpf", 0.57f); // 110 Hz
processIncomingMessages();
}
else
{
// Disables the low shelf EQ
sendOscMessage("/ch/01/eq/1/g", 0.5f); // 0 dB
processIncomingMessages();
}
sendOscMessage("/ch/01/preamp/hpon", onOff);
}
void muteMonitoring(int onOff)
{
sendOscMessage("/config/solo/mute", onOff);
}
void processIncomingMessages()
{
// Processes incoming OSC messages
OSCMessage msg;
int size = Udp.parsePacket();
if (size > 0) {
while (size--) {
msg.fill(Udp.read());
}
if (!msg.hasError()) {
msg.dispatch("/config/solo/mono", monoHandle);
msg.dispatch("/ch/01/preamp/hpon", hpfHandle);
msg.dispatch("/-snap/index", snapHandle);
msg.dispatch("/config/solo/mute", muteHandle);
} else {
error = msg.getError();
Serial.print("error: ");
Serial.println(error);
}
}
}
void hpfHandle(OSCMessage &msg)
{
int value = msg.getInt(0);
if (value == 0)
{
digitalWrite(BUTTON_A_LED, LOW);
bHpfOn = false;
}
else
{
digitalWrite(BUTTON_A_LED, HIGH);
bHpfOn = true;
}
}
void snapHandle(OSCMessage &msg)
{
int value = msg.getInt(0);
if (value == 2)
{
digitalWrite(BUTTON_B_LED, HIGH);
bCompOn = true;
}
else
{
digitalWrite(BUTTON_B_LED, LOW);
bCompOn = false;
}
}
void monoHandle(OSCMessage &msg)
{
int value = msg.getInt(0);
if (value == 0)
{
digitalWrite(BUTTON_C_LED, LOW);
bMonoOn = false;
}
else
{
digitalWrite(BUTTON_C_LED, HIGH);
bMonoOn = true;
}
}
void muteHandle(OSCMessage &msg)
{
int value = msg.getInt(0);
if (value == 0)
{
digitalWrite(BUTTON_D_LED, LOW);
bMuteOn = false;
}
else
{
digitalWrite(BUTTON_D_LED, HIGH);
bMuteOn = true;
}
}
void keepSubscription()
{
long currentMillis = millis();
if (currentMillis - previousMillis > XREMOTE_REFRESH_MS)
{
// Subscribe to everything
sendOscMessage("/xremote");
// Polls possibly missed changes
sendOscMessage("/config/solo/mono");
sendOscMessage("/ch/01/preamp/hpon");
sendOscMessage("/-snap/index");
sendOscMessage("/config/solo/mute");
previousMillis = currentMillis;
}
}
void sendOscMessage(char command[])
{
OSCMessage msg(command);
for (int i = 0; i < REPEAT_MESSAGE; i++)
{
Udp.beginPacket(consoleIp, consolePort);
msg.send(Udp);
Udp.endPacket();
}
msg.empty();
delay(WAIT_AFTER_MESSAGE_MS);
}
void sendOscMessage(char command[], int value)
{
OSCMessage msg(command);
msg.add(value);
for (int i = 0; i < REPEAT_MESSAGE; i++)
{
Udp.beginPacket(consoleIp, consolePort);
msg.send(Udp);
Udp.endPacket();
}
msg.empty();
delay(WAIT_AFTER_MESSAGE_MS);
}
void sendOscMessage(char command[], float value)
{
OSCMessage msg(command);
msg.add(value);
for (int i = 0; i < REPEAT_MESSAGE; i++)
{
Udp.beginPacket(consoleIp, consolePort);
msg.send(Udp);
Udp.endPacket();
}
msg.empty();
delay(WAIT_AFTER_MESSAGE_MS);
}
void waitForWifi()
{
bool ledAState = digitalRead(BUTTON_A_LED);
bool ledBState = digitalRead(BUTTON_B_LED);
bool ledCState = digitalRead(BUTTON_C_LED);
bool ledDState = digitalRead(BUTTON_D_LED);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(BUTTON_A_LED, LOW);
digitalWrite(BUTTON_B_LED, LOW);
digitalWrite(BUTTON_C_LED, LOW);
digitalWrite(BUTTON_D_LED, LOW);
delay(150);
Serial.print(".");
digitalWrite(BUTTON_A_LED, HIGH);
digitalWrite(BUTTON_B_LED, HIGH);
digitalWrite(BUTTON_C_LED, HIGH);
digitalWrite(BUTTON_D_LED, HIGH);
delay(150);
}
digitalWrite(BUTTON_A_LED, ledAState);
digitalWrite(BUTTON_B_LED, ledBState);
digitalWrite(BUTTON_C_LED, ledCState);
digitalWrite(BUTTON_D_LED, ledDState);
}
void flashALittle()
{
bool ledAState = digitalRead(BUTTON_A_LED);
bool ledBState = digitalRead(BUTTON_B_LED);
bool ledCState = digitalRead(BUTTON_C_LED);
bool ledDState = digitalRead(BUTTON_D_LED);
for (int i = 0; i < 5; i++)
{
digitalWrite(BUTTON_A_LED, LOW);
digitalWrite(BUTTON_B_LED, LOW);
digitalWrite(BUTTON_C_LED, LOW);
digitalWrite(BUTTON_D_LED, LOW);
delay(270);
digitalWrite(BUTTON_A_LED, HIGH);
digitalWrite(BUTTON_B_LED, HIGH);
digitalWrite(BUTTON_C_LED, HIGH);
digitalWrite(BUTTON_D_LED, HIGH);
delay(30);
}
digitalWrite(BUTTON_A_LED, ledAState);
digitalWrite(BUTTON_B_LED, ledBState);
digitalWrite(BUTTON_C_LED, ledCState);
digitalWrite(BUTTON_D_LED, ledDState);
}