This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tallyarbiter-m5stickc.ino
439 lines (380 loc) · 11.7 KB
/
tallyarbiter-m5stickc.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
#define C_PLUS 1 //CHANGE TO 1 IF YOU USE THE M5STICK-C PLUS
#if C_PLUS == 1
#include <M5StickCPlus.h>
#else
#include <M5StickC.h>
#endif
#include <WiFi.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>
#include <Arduino_JSON.h>
#include <PinButton.h>
#include <Preferences.h>
#define GRAY 0x0020 // 8 8 8
#define GREEN 0x0200 // 0 64 0
#define RED 0xF800 // 255 0 0
/* USER CONFIG VARIABLES
* Change the following variables before compiling and sending the code to your device.
*/
bool CUT_BUS = true; // true = Programm + Preview = Red Tally; false = Programm + Preview = Yellow Tally
bool LAST_MSG = false; // true = show log on tally screen
//Wifi SSID and password
const char * networkSSID = "NetworkSSID";
const char * networkPass = "NetworkPass";
//For static IP Configuration, change USE_STATIC to true and define your IP address settings below
bool USE_STATIC = false; // true = use static, false = use DHCP
IPAddress clientIp(192, 168, 2, 5); // Static IP
IPAddress subnet(255, 255, 255, 0); // Subnet Mask
IPAddress gateway(192, 168, 2, 1); // Gateway
//Tally Arbiter Server
const char * tallyarbiter_host = "192.168.0.137"; //IP address of the Tally Arbiter Server
const int tallyarbiter_port = 4455;
/* END OF USER CONFIG */
//M5StickC variables
PinButton btnM5(37); //the "M5" button on the device
PinButton btnAction(39); //the "Action" button on the device
Preferences preferences;
uint8_t wasPressed();
const byte led_program = 10;
const int led_preview = 26; //OPTIONAL Led for preview on pin G26
//Tally Arbiter variables
SocketIOclient socket;
JSONVar BusOptions;
JSONVar Devices;
JSONVar DeviceStates;
String DeviceId = "unassigned";
String DeviceName = "Unassigned";
String ListenerType = "m5-stickc";
bool mode_preview = false;
bool mode_program = false;
String LastMessage = "";
//General Variables
int currentScreen = 0; //0 = Tally Screen, 1 = Settings Screen
int currentBrightness = 11; //12 is Max level
void setup() {
pinMode (led_preview, OUTPUT);
Serial.begin(115200);
while (!Serial);
// Initialize the M5StickC object
logger("Initializing M5StickC+.", "info-quiet");
M5.begin();
setCpuFrequencyMhz(80); //Save battery by turning down the CPU clock
btStop(); //Save battery by turning off BlueTooth
M5.Lcd.setRotation(3);
M5.Lcd.setCursor(0, 0);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextSize(1);
logger("Tally Arbiter M5StickC+ Listener Client booting.", "info");
// Enable interal led for program trigger
pinMode(led_program, OUTPUT);
digitalWrite(led_program, HIGH);
preferences.begin("tally-arbiter", false);
if(preferences.getString("deviceid").length() > 0){
DeviceId = preferences.getString("deviceid");
}
if(preferences.getString("devicename").length() > 0){
DeviceName = preferences.getString("devicename");
}
preferences.end();
delay(100); //wait 100ms before moving on
connectToNetwork(); //starts Wifi connection
}
void loop() {
socket.loop();
btnM5.update();
btnAction.update();
if (btnM5.isClick()) {
switch (currentScreen) {
case 0:
showSettings();
currentScreen = 1;
break;
case 1:
showDeviceInfo();
currentScreen = 0;
break;
}
}
if (btnAction.isClick()) {
updateBrightness();
}
}
void showSettings() {
//displays the current network connection and Tally Arbiter server data
M5.Lcd.setCursor(0, 0);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.println("SSID: " + String(networkSSID));
M5.Lcd.println(WiFi.localIP());
M5.Lcd.println();
M5.Lcd.println("Tally Arbiter Server:");
M5.Lcd.println(String(tallyarbiter_host) + ":" + String(tallyarbiter_port));
M5.Lcd.println();
M5.Lcd.println("Battery:");
int batteryLevel = floor(100.0 * (((M5.Axp.GetVbatData() * 1.1 / 1000) - 3.0) / (4.07 - 3.0)));
batteryLevel = batteryLevel > 100 ? 100 : batteryLevel;
if(batteryLevel >= 100){
M5.Lcd.println("Battery charging..."); // show when M5 is plugged in
}
else {
M5.Lcd.println("Battery:" + String(batteryLevel) + "%");
}
}
void showDeviceInfo() {
//displays the currently assigned device and tally data
evaluateMode();
}
void updateBrightness() {
if(currentBrightness >= 12) {
currentBrightness = 7;
}
else {
currentBrightness++;
}
M5.Axp.ScreenBreath(currentBrightness);
}
void logger(String strLog, String strType) {
if (strType == "info") {
Serial.println(strLog);
M5.Lcd.println(strLog);
}
else {
Serial.println(strLog);
}
}
void connectToNetwork() {
logger("Connecting to SSID: " + String(networkSSID), "info");
WiFi.disconnect(true);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA); //station
WiFi.setSleep(false);
if(USE_STATIC == true) {
WiFi.config(clientIp, gateway, subnet);
}
WiFi.begin(networkSSID, networkPass);
delay(1000); //Delay is needed to actually figure out, if connection is established
}
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
logger("Network connected!", "info");
logger(WiFi.localIP().toString(), "info");
connectToServer(); //if connection to wifi is established, actually start to connect the socket
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
logger("Network connection lost!", "info");
logger("WiFi: Trying to reconnect", "info");
WiFi.reconnect(); //if the connection to WiFi is lost (for whatever reason, try to reconnect
break;
}
}
void ws_emit(String event, const char *payload = NULL) {
if (payload) {
String msg = "[\"" + event + "\"," + payload + "]";
socket.sendEVENT(msg);
} else {
String msg = "[\"" + event + "\"]";
socket.sendEVENT(msg);
}
}
void connectToServer() {
logger("Connecting to Tally Arbiter host: " + String(tallyarbiter_host), "info");
socket.onEvent(socket_event);
socket.begin(tallyarbiter_host, tallyarbiter_port);
}
void socket_event(socketIOmessageType_t type, uint8_t * payload, size_t length) {
switch (type) {
case sIOtype_CONNECT:
socket_Connected((char*)payload, length);
break;
case sIOtype_DISCONNECT:
case sIOtype_ACK:
case sIOtype_ERROR:
case sIOtype_BINARY_EVENT:
case sIOtype_BINARY_ACK:
// Not handled
break;
case sIOtype_EVENT:
String msg = (char*)payload;
String type = msg.substring(2, msg.indexOf("\"",2));
String content = msg.substring(type.length() + 4);
content.remove(content.length() - 1);
logger("Got event '" + type + "', data: " + content, "info-quiet");
if (type == "bus_options") BusOptions = JSON.parse(content);
if (type == "reassign") socket_Reassign(content);
if (type == "flash") socket_Flash();
if (type == "messaging") socket_Messaging(content);
if (type == "deviceId") {
DeviceId = content.substring(1, content.length()-1);
SetDeviceName();
showDeviceInfo();
currentScreen = 0;
}
if (type == "devices") {
Devices = JSON.parse(content);
SetDeviceName();
}
if (type == "device_states") {
DeviceStates = JSON.parse(content);
processTallyData();
}
break;
}
}
void socket_Connected(const char * payload, size_t length) {
logger("Connected to Tally Arbiter server.", "info");
String deviceObj = "{\"deviceId\": \"" + DeviceId + "\", \"listenerType\": \"" + ListenerType + "\"}";
char charDeviceObj[1024];
strcpy(charDeviceObj, deviceObj.c_str());
ws_emit("bus_options");
ws_emit("device_listen_m5", charDeviceObj);
}
void socket_Flash() {
//flash the screen white 3 times
M5.Lcd.fillScreen(WHITE);
delay(500);
M5.Lcd.fillScreen(TFT_BLACK);
delay(500);
M5.Lcd.fillScreen(WHITE);
delay(500);
M5.Lcd.fillScreen(TFT_BLACK);
delay(500);
M5.Lcd.fillScreen(WHITE);
delay(500);
M5.Lcd.fillScreen(TFT_BLACK);
//then resume normal operation
switch (currentScreen) {
case 0:
showDeviceInfo();
break;
case 1:
showSettings();
break;
}
}
String strip_quot(String str) {
if (str[0] == '"') {
str.remove(0, 1);
}
if (str.endsWith("\"")) {
str.remove(str.length()-1, 1);
}
return str;
}
void socket_Reassign(String payload) {
String oldDeviceId = payload.substring(0, payload.indexOf(','));
String newDeviceId = payload.substring(oldDeviceId.length()+1);
oldDeviceId = strip_quot(oldDeviceId);
newDeviceId = strip_quot(newDeviceId);
String reassignObj = "{\"oldDeviceId\": \"" + oldDeviceId + "\", \"newDeviceId\": \"" + newDeviceId + "\"}";
char charReassignObj[1024];
strcpy(charReassignObj, reassignObj.c_str());
ws_emit("listener_reassign_object", charReassignObj);
ws_emit("devices");
M5.Lcd.fillScreen(WHITE);
delay(200);
M5.Lcd.fillScreen(TFT_BLACK);
delay(200);
M5.Lcd.fillScreen(WHITE);
delay(200);
M5.Lcd.fillScreen(TFT_BLACK);
DeviceId = newDeviceId;
preferences.begin("tally-arbiter", false);
preferences.putString("deviceid", newDeviceId);
preferences.end();
SetDeviceName();
}
void socket_Messaging(String payload) {
String strPayload = String(payload);
int typeQuoteIndex = strPayload.indexOf(',');
String messageType = strPayload.substring(0, typeQuoteIndex);
messageType.replace("\"", "");
int messageQuoteIndex = strPayload.lastIndexOf(',');
String message = strPayload.substring(messageQuoteIndex + 1);
message.replace("\"", "");
LastMessage = messageType + ": " + message;
evaluateMode();
}
void processTallyData() {
for (int i = 0; i < DeviceStates.length(); i++) {
if (getBusTypeById(JSON.stringify(DeviceStates[i]["busId"])) == "\"preview\"") {
if (DeviceStates[i]["sources"].length() > 0) {
mode_preview = true;
}
else {
mode_preview = false;
}
}
if (getBusTypeById(JSON.stringify(DeviceStates[i]["busId"])) == "\"program\"") {
if (DeviceStates[i]["sources"].length() > 0) {
mode_program = true;
}
else {
mode_program = false;
}
}
}
evaluateMode();
}
String getBusTypeById(String busId) {
for (int i = 0; i < BusOptions.length(); i++) {
if (JSON.stringify(BusOptions[i]["id"]) == busId) {
return JSON.stringify(BusOptions[i]["type"]);
}
}
return "invalid";
}
void SetDeviceName() {
for (int i = 0; i < Devices.length(); i++) {
if (JSON.stringify(Devices[i]["id"]) == "\"" + DeviceId + "\"") {
String strDevice = JSON.stringify(Devices[i]["name"]);
DeviceName = strDevice.substring(1, strDevice.length() - 1);
break;
}
}
preferences.begin("tally-arbiter", false);
preferences.putString("devicename", DeviceName);
preferences.end();
evaluateMode();
}
void evaluateMode() {
M5.Lcd.setCursor(0, 30);
M5.Lcd.setTextSize(2);
if (mode_preview && !mode_program) {
logger("The device is in preview.", "info-quiet");
M5.Lcd.setTextColor(BLACK);
M5.Lcd.fillScreen(GREEN);
digitalWrite(led_program, HIGH);
digitalWrite (led_preview, HIGH);
}
else if (!mode_preview && mode_program) {
logger("The device is in program.", "info-quiet");
M5.Lcd.setTextColor(BLACK);
M5.Lcd.fillScreen(RED);
digitalWrite(led_program, LOW);
digitalWrite(led_preview, LOW);
}
else if (mode_preview && mode_program) {
logger("The device is in preview+program.", "info-quiet");
M5.Lcd.setTextColor(BLACK);
if (CUT_BUS == true) {
M5.Lcd.fillScreen(RED);
}
else {
M5.Lcd.fillScreen(YELLOW);
}
digitalWrite(led_program, LOW);
digitalWrite (led_preview, HIGH);
}
else {
digitalWrite(led_program, HIGH);
digitalWrite(led_preview, LOW);
M5.Lcd.setTextColor(GRAY);
M5.Lcd.fillScreen(TFT_BLACK);
}
M5.Lcd.println(DeviceName);
if (LAST_MSG == true){
M5.Lcd.println(LastMessage);
}
}