-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tally.ino
229 lines (183 loc) · 6.14 KB
/
Tally.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
/*****************
Tally light ESP32 for Blackmagic ATEM switcher
Version 2.1 (Modified by Clarity)
A wireless (WiFi) tally light for Blackmagic Design
ATEM video switchers, based on the M5StickCPlus ESP32 development
board and the Arduino IDE.
Based on the work of Kasper Skårhøj:
https://github.com/kasperskaarhoj/SKAARHOJ-Open-Engineering
Download SkaarhojPgmspace,ATEMbase, ATEMstd from here
https://github.com/kasperskaarhoj/SKAARHOJ-Open-Engineering/tree/master/ArduinoLibs
******************/
#include <M5StickCPlus.h>
#include <WiFi.h>
#include <SkaarhojPgmspace.h>
#include <ATEMbase.h>
#include <ATEMstd.h>
IPAddress switcherIp(0, 0, 0, 0); // IP address of the ATEM switcher
ATEMstd AtemSwitcher;
const char* ssid = "network";
const char* password = "password";
// Set this to 1 if you want the orientation to update automatically
#define AUTOUPDATE_ORIENTATION 1
/////////////////////////////////////////////////////////////
// You probably don't need to change things below this line
#define previewLED 25
#define bothLED 0
#define programLED 26
int orientation = 0;
int orientationPrevious = 0;
int orientationMillisPrevious = millis();
int buttonBMillis = 0;
int cameraNumber = 3;
bool showBatteryLevel = false;
int previewTallyPrevious = 1;
int programTallyPrevious = 1;
int cameraNumberPrevious = cameraNumber;
bool showBatteryLevelPrevious = showBatteryLevel;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
M5.begin();
M5.Imu.Init();
M5.Lcd.setRotation(orientation);
pinMode(previewLED, OUTPUT);
pinMode(bothLED, OUTPUT);
pinMode(programLED, OUTPUT);
digitalWrite(previewLED, HIGH);
digitalWrite(bothLED, HIGH);
digitalWrite(programLED, HIGH);
AtemSwitcher.begin(switcherIp);
AtemSwitcher.serialOutput(0x80);
AtemSwitcher.connect();
}
void setOrientation() {
float accX = 0, accY = 0, accZ = 0;
M5.Imu.getAccelData(&accX, &accY, &accZ);
Serial.printf("%.2f %.2f %.2f \n", accX * 1000, accY * 1000, accZ * 1000);
if (accZ < .9) {
if (accX > .6) {
orientation = 1;
} else if (accX < .4 && accX > -.5) {
if (accY > 0) {
orientation = 0;
} else {
orientation = 2;
}
} else {
orientation = 3;
}
}
if (orientation != orientationPrevious) {
Serial.printf("Orientation changed to %d\n", orientation);
M5.Lcd.setRotation(orientation);
}
}
void loop() {
M5.update();
if (AUTOUPDATE_ORIENTATION) {
if (orientationMillisPrevious + 500 < millis()) {
setOrientation();
orientationMillisPrevious = millis();
}
}
// Buttons Press
if (M5.BtnA.wasPressed()) {
buttonBMillis = millis();
showBatteryLevel = !showBatteryLevel;
refreshScreen();
}
if (M5.BtnB.isPressed())
AtemSwitcher.changeProgramInput(cameraNumber);
if (M5.BtnA.isPressed() && buttonBMillis != 0 && buttonBMillis < millis() - 500) {
Serial.println("Changing camera number");
cameraNumber = (cameraNumber % 8) + 1;
Serial.printf("New camera number: %d\n", cameraNumber);
buttonBMillis = 0;
}
// Check for packets, respond to them etc. Keeping the connection alive!
AtemSwitcher.runLoop();
refreshScreen();
}
void refreshScreen()
{
int programTally = AtemSwitcher.getProgramTally(cameraNumber);
int previewTally = AtemSwitcher.getPreviewTally(cameraNumber);
if ((orientation != orientationPrevious)
|| (cameraNumber != cameraNumberPrevious)
|| (programTallyPrevious != programTally)
|| (previewTallyPrevious != previewTally)
|| (showBatteryLevel != showBatteryLevelPrevious))
{
if (programTally && !previewTally) { // only program
drawLabel(TFT_RED, TFT_BLACK, LOW, LOW, HIGH);
} else if (programTally && previewTally) { // program AND preview
drawLabel(TFT_BLUE, TFT_BLACK, LOW, HIGH, LOW);
} else if (previewTally && !programTally) { // only preview
drawLabel(TFT_DARKGREEN, TFT_BLACK, HIGH, LOW, LOW);
} else if (!previewTally || !programTally) { // neither
drawLabel(TFT_BLACK, TFT_DARKGREY, LOW, LOW, LOW);
}
}
programTallyPrevious = programTally;
previewTallyPrevious = previewTally;
cameraNumberPrevious = cameraNumber;
orientationPrevious = orientation;
showBatteryLevelPrevious = showBatteryLevel;
}
void drawLabel(unsigned long int screenColor, unsigned long int labelColor, bool previewValue, bool bothValue, bool programValue) {
digitalWrite(previewLED, previewValue);
digitalWrite(bothLED, bothValue);
digitalWrite(programLED, programValue);
M5.Lcd.fillScreen(screenColor);
M5.Lcd.setTextColor(labelColor);
drawStringInCenter(String(cameraNumber), 8);
showBatteryInfo();
}
void showBatteryInfo()
{
int batteryLevel = getBatteryLevel();
if(batteryLevel <= 15 || showBatteryLevel)
{
float voltage = M5.Axp.GetBatVoltage();
float current = M5.Axp.GetBatCurrent();
char batteryStatus[9];
char chargingIcon = current == 0 ? ' ' : (current > 0 ? '+' : '-');
sprintf(batteryStatus, "%c%.2fv", chargingIcon, voltage);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(M5.Lcd.width() - 30,10);
M5.Lcd.setTextDatum(TL_DATUM);
M5.Lcd.drawString(batteryStatus, 1, 5, 1);
if(batteryLevel <= 15)
M5.Lcd.setTextColor(TFT_RED);
else if(batteryLevel <= 40)
M5.Lcd.setTextColor(TFT_YELLOW);
else
M5.Lcd.setTextColor(TFT_GREEN);
M5.Lcd.setTextDatum(TR_DATUM);
M5.Lcd.drawString(String(batteryLevel)+ "%", M5.Lcd.width() - 1, 5, 1);
}
}
void drawStringInCenter(String input, int font) {
int datumPrevious = M5.Lcd.getTextDatum();
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextSize(1);
M5.Lcd.drawString(input, M5.Lcd.width() / 2, M5.Lcd.height() / 2, font);
M5.Lcd.setTextDatum(datumPrevious);
}
int getBatteryLevel(void) {
uint16_t vbatData = M5.Axp.GetVbatData();
double vbat = vbatData * 1.1 / 1000;
double percentage = 100.0 * ((vbat - 3.4) / (4.07 - 3.4));
if(percentage <= 0)
return 0;
else if(percentage >= 100)
return 100;
else
return percentage;
}