forked from mathijsk/Arduino-Library-for-ATEM-Switchers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATEM.cpp
More file actions
executable file
·1331 lines (1157 loc) · 49.8 KB
/
ATEM.cpp
File metadata and controls
executable file
·1331 lines (1157 loc) · 49.8 KB
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2012 Kasper Skårhøj, SKAARHOJ, kasperskaarhoj@gmail.com
This file is part of the ATEM library for Arduino
The ATEM library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The ATEM library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the ATEM library. If not, see http://www.gnu.org/licenses/.
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "ATEM.h"
//#include <MemoryFree.h>
/**
* Constructor (using arguments is deprecated! Use begin() instead)
*/
ATEM::ATEM(){}
ATEM::ATEM(const IPAddress ip, const uint16_t localPort){
_ATEM_FtbS_state = false;
begin(ip, localPort);
}
/**
* Setting up IP address for the switcher (and local port to send packets from)
*/
void ATEM::begin(const IPAddress ip, const uint16_t localPort){
// Set up Udp communication object:
EthernetUDP Udp;
_Udp = Udp;
_switcherIP = ip; // Set switcher IP address
_localPort = localPort; // Set local port (just a random number I picked)
_serialOutput = false;
_isConnectingTime = 0;
_ATEM_AMLv_channel=0;
}
/**
* Get ATEM session ID
*/
uint16_t ATEM::getSessionID() {
return _sessionID;
}
/**
* Initiating connection handshake to the ATEM switcher
*/
void ATEM::connect() {
_isConnectingTime = millis();
_localPacketIdCounter = 1; // Init localPacketIDCounter to 1;
_hasInitialized = false;
_lastContact = 0;
_Udp.begin(_localPort);
// Setting this, because even though we haven't had contact, it constitutes an attempt that should be responded to at least:
_lastContact = millis();
// Send connectString to ATEM:
// TODO: Describe packet contents according to rev.eng. API
if (_serialOutput) {
Serial.println(F("Sending connect packet to ATEM switcher."));
}
byte connectHello[] = {
0x10, 0x14, 0x53, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
_Udp.beginPacket(_switcherIP, 9910);
_Udp.write(connectHello,20);
_Udp.endPacket();
}
/**
* Keeps connection to the switcher alive - basically, this means answering back to ping packages.
* Therefore: Call this in the Arduino loop() function and make sure it gets call at least 2 times a second
* Other recommendations might come up in the future.
*/
void ATEM::runLoop() {
// WARNING:
// It can cause severe timing problems using "slow" functions such as Serial.print*()
// in the runloop, in particular during "boot" where the ATEM delivers some 10-20 kbytes of system status info which
// must exit the RX-buffer quite fast. Therefore, using Serial.print for debugging in this
// critical phase will in it self affect program execution!
// Limit of the RX buffer of the Ethernet interface is another general issue.
// When ATEM sends the initial system status packets (10-20 kbytes), they are sent with a few microseconds in between
// The RX buffer of the Ethernet interface on Arduino simply does not have the kapacity to take more than 2k at a time.
// This means, that we only receive the first packet, the others seems to be discarded. Luckily most information we like to
// know about is in the first packet (and some in the second, the rest is probably thumbnails for the media player).
// It may be possible to bump up this buffer to 4 or 8 k by simply re-configuring the amount of allowed sockets on the interface.
// For some more information from a guy seemingly having a similar issue, look here:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282170842
uint16_t packetSize = 0;
if (_isConnectingTime > 0) {
// Waiting for the ATEM to answer back with a packet 20 bytes long.
// According to packet analysis with WireShark, this feedback from ATEM
// comes within a few microseconds!
packetSize = _Udp.parsePacket();
if (_Udp.available() && packetSize==20) {
// Read the response packet. We will only subtract the session ID
// According to packet analysis with WireShark, this feedback from ATEM
// comes a few microseconds after our connect invitation above. Two packets immediately follow each other.
// After approx. 200 milliseconds a third packet is sent from ATEM - a sort of re-sent because it gets impatient.
// And it seems that THIS third packet is the one we actually read and respond to. In other words, I believe that
// the ethernet interface on Arduino actually misses the first two for some reason!
_Udp.read(_packetBuffer,20);
//_sessionID = _packetBuffer[15];
// Send connectAnswerString to ATEM:
_Udp.beginPacket(_switcherIP, 9910);
// TODO: Describe packet contents according to rev.eng. API
byte connectHelloAnswerString[] = {
0x80, 0x0c, 0x53, 0xab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00 };
_Udp.write(connectHelloAnswerString,12);
_Udp.endPacket();
_isConnectingTime = 0; // End connecting
} else {
if (_isConnectingTime+2000 < (unsigned long)millis()) {
if (_serialOutput) {
Serial.println(F("Timeout waiting for ATEM switcher response"));
}
_isConnectingTime = 0;
}
}
} else {
// If there's data available, read a packet, empty up:
// Serial.println("ATEM runLoop():");
while(true) { // Iterate until buffer is empty:
packetSize = _Udp.parsePacket();
if (_Udp.available() && packetSize !=0) {
// Serial.print("New Packet");
// Serial.print(("PACKET: "));
// Serial.println(packetSize, DEC);
// Read packet header of 12 bytes:
_Udp.read(_packetBuffer, 12);
_sessionID = word(_packetBuffer[2], _packetBuffer[3]);
// Read out packet length (first word), remote packet ID number and "command":
uint16_t packetLength = word(_packetBuffer[0] & B00000111, _packetBuffer[1]);
_lastRemotePacketID = word(_packetBuffer[10],_packetBuffer[11]);
uint8_t command = _packetBuffer[0] & B11111000;
boolean command_ACK = command & B00001000 ? true : false; // If true, ATEM expects an acknowledgement answer back!
boolean command_INIT = command & B00010000 ? true : false; // If true, ATEM expects an acknowledgement answer back!
// The five bits in "command" (from LSB to MSB):
// 1 = ACK, "Please respond to this packet" (using the _lastRemotePacketID). Exception: The initial 10-20 kbytes of Switcher status
// 2 = ?. Set during initialization? (first hand-shake packets contains that)
// 3 = "This is a retransmission". You will see this bit set if the ATEM switcher did not get a timely response to a packet.
// 4 = ? ("hello packet" according to "ratte", forum at atemuser.com)
// 5 = "This is a response on your request". So set this when answering...
if (packetSize==packetLength) { // Just to make sure these are equal, they should be!
_lastContact = millis();
// If a packet is 12 bytes long it indicates that all the initial information
// has been delivered from the ATEM and we can begin to answer back on every request
// Currently we don't know any other way to decide if an answer should be sent back...
if(!_hasInitialized && packetSize == 12) {
_hasInitialized = true;
if (_serialOutput) Serial.println(F("_hasInitialized=TRUE"));
Serial.print("Session ID: ");
Serial.println(_sessionID, DEC);
}
if (packetLength > 12 && !command_INIT) { // !command_INIT is because there seems to be no commands in these packets and that will generate an error.
_parsePacket(packetLength);
}
// If we are initialized, lets answer back no matter what:
// TODO: "_hasInitialized && " should be inserted back before "command_ACK" but
// with Arduino 1.0 UDP library it has proven MORE likely that the initial
// connection is made if we ALWAYS answer the switcher back.
// Apparently the initial "chaos" of keeping up with the incoming data confuses
// the UDP library so that we might never get initialized - and thus never get connected
// So... for now this is how we do it:
// CHANGED with arduino 1.0.1..... put back in.
if (_hasInitialized && command_ACK) {
if (_serialOutput) {
Serial.print(F("ACK, rpID: "));
Serial.println(_lastRemotePacketID, DEC);
}
_sendAnswerPacket(_lastRemotePacketID);
}
} else {
if (_serialOutput) {
/* Serial.print(("ERROR: Packet size mismatch: "));
Serial.print(packetSize, DEC);
Serial.print(" != ");
Serial.println(packetLength, DEC);
*/ }
// Flushing the buffer:
// TODO: Other way? _Udp.flush() ??
while(_Udp.available()) {
_Udp.read(_packetBuffer, 96);
}
}
} else {
break; // Exit while(true) loop because there is no more packets in buffer.
}
}
}
}
bool ATEM::isConnectionTimedOut() {
unsigned long currentTime = millis();
if (_lastContact>0 && _lastContact+10000 < currentTime) { // Timeout of 10 sec.
_lastContact = 0;
return true;
}
return false;
}
void ATEM::delay(const unsigned int delayTimeMillis) { // Responsible delay function which keeps the ATEM run loop up! DO NOT USE INSIDE THIS CLASS! Recursion could happen...
unsigned long timeout = millis();
timeout+=delayTimeMillis;
while(timeout > millis()) {
runLoop();
}
}
/**
* Reads from UDP channel to buffer. Will fill the buffer to the max or to the size of the current segment being parsed
* Returns false if there are no more bytes, otherwise true
*/
bool ATEM::_readToPacketBuffer() {
return _readToPacketBuffer(96);
}
bool ATEM::_readToPacketBuffer(uint8_t maxBytes) {
maxBytes = maxBytes<=96 ? maxBytes : 96;
int remainingBytes = _cmdLength-8-_cmdPointer;
if (remainingBytes>0) {
if (remainingBytes <= maxBytes) {
_Udp.read(_packetBuffer, remainingBytes);
_cmdPointer+= remainingBytes;
return false; // Returns false if finished.
} else {
_Udp.read(_packetBuffer, maxBytes);
_cmdPointer+= maxBytes;
return true; // Returns true if there are still bytes to be read.
}
} else {
return false;
}
}
/**
* If a package longer than a normal acknowledgement is received from the ATEM Switcher we must read through the contents.
* Usually such a package contains updated state information about the mixer
* Selected information is extracted in this function and transferred to internal variables in this library.
*/
void ATEM::_parsePacket(uint16_t packetLength) {
uint8_t idx; // General reusable index usable for keyers, mediaplayer etc below.
// If packet is more than an ACK packet (= if its longer than 12 bytes header), lets parse it:
uint16_t indexPointer = 12; // 12 bytes has already been read from the packet...
while (indexPointer < packetLength) {
// Read the length of segment (first word):
_Udp.read(_packetBuffer, 8);
_cmdLength = word(_packetBuffer[0], _packetBuffer[1]);
_cmdPointer = 0;
// Get the "command string", basically this is the 4 char variable name in the ATEM memory holding the various state values of the system:
char cmdStr[] = {
_packetBuffer[4], _packetBuffer[5], _packetBuffer[6], _packetBuffer[7], '\0'};
// If length of segment larger than 8 (should always be...!)
if (_cmdLength>8) {
if(strcmp(cmdStr, "AMLv")) {
_readToPacketBuffer(); // Fill packet buffer unless it's AMLv (AudioMonitorLevels)
}
// Extract the specific state information we like to know about:
if(strcmp(cmdStr, "PrgI") == 0) { // Program Bus status
if (!ver42()) {
_ATEM_PrgI = _packetBuffer[1];
} else {
_ATEM_PrgI = (uint16_t)(_packetBuffer[2]<<8) | _packetBuffer[3];
}
if (_serialOutput) Serial.print(F("Program Bus: "));
if (_serialOutput) Serial.println(_ATEM_PrgI, DEC);
} else
if(strcmp(cmdStr, "PrvI") == 0) { // Preview Bus status
if (!ver42()) {
_ATEM_PrvI = _packetBuffer[1];
} else {
_ATEM_PrvI = (uint16_t)(_packetBuffer[2]<<8) | _packetBuffer[3];
}
if (_serialOutput) Serial.print(F("Preview Bus: "));
if (_serialOutput) Serial.println(_ATEM_PrvI, DEC);
} else
if(strcmp(cmdStr, "TlIn") == 0) { // Tally status for inputs 1-8
uint8_t count = _packetBuffer[1]; // Number of inputs
// 16 inputs supported so make sure to read max 16.
if(count > 16) {
count = 16;
}
// Inputs 1-16, bit 0 = Prg tally, bit 1 = Prv tally. Both can be set simultaneously.
if (_serialOutput) Serial.println(F("Tally updated: "));
for(uint8_t i = 0; i < count; ++i) {
_ATEM_TlIn[i] = _packetBuffer[2+i];
}
} else
if(strcmp(cmdStr, "Time") == 0) { // Time. What is this anyway?
/* Serial.print(_packetBuffer[0]);
Serial.print(':');
Serial.print(_packetBuffer[1]);
Serial.print(':');
Serial.print(_packetBuffer[2]);
Serial.print(':');
Serial.print(_packetBuffer[3]);
Serial.println();
*/} else
if(strcmp(cmdStr, "TrPr") == 0) { // Transition Preview
_ATEM_TrPr = _packetBuffer[1] > 0 ? true : false;
if (_serialOutput) Serial.print(F("Transition Preview: "));
if (_serialOutput) Serial.println(_ATEM_TrPr, BIN);
} else
if(strcmp(cmdStr, "TrPs") == 0) { // Transition Position
_ATEM_TrPs_frameCount = _packetBuffer[2]; // Frames count down
_ATEM_TrPs_position = _packetBuffer[4]*256 + _packetBuffer[5]; // Position 0-1000 - maybe more in later firmwares?
} else
if(strcmp(cmdStr, "TrSS") == 0) { // Transition Style and Keyer on next transition
_ATEM_TrSS_KeyersOnNextTransition = _packetBuffer[2] & B11111; // Bit 0: Background; Bit 1-4: Key 1-4
if (_serialOutput) Serial.print(F("Keyers on Next Transition: "));
if (_serialOutput) Serial.println(_ATEM_TrSS_KeyersOnNextTransition, BIN);
_ATEM_TrSS_TransitionStyle = _packetBuffer[1];
if (_serialOutput) Serial.print(F("Transition Style: ")); // 0=MIX, 1=DIP, 2=WIPE, 3=DVE, 4=STING
if (_serialOutput) Serial.println(_ATEM_TrSS_TransitionStyle, DEC);
} else
if(strcmp(cmdStr, "FtbS") == 0) { // Fade To Black State
_ATEM_FtbS_state = _packetBuffer[2]; // State of Fade To Black, 0 = off and 1 = activated
_ATEM_FtbS_frameCount = _packetBuffer[3]; // Frames count down
if (_serialOutput) Serial.print(F("FTB:"));
if (_serialOutput) Serial.print(_ATEM_FtbS_state);
if (_serialOutput) Serial.print(F("/"));
if (_serialOutput) Serial.println(_ATEM_FtbS_frameCount);
} else
if(strcmp(cmdStr, "FtbP") == 0) { // Fade To Black - Positions(?) (Transition Time in frames for FTB): 0x01-0xFA
_ATEM_FtbP_time = _packetBuffer[1];
} else
if(strcmp(cmdStr, "TMxP") == 0) { // Mix Transition Position(?) (Transition Time in frames for Mix transitions.): 0x01-0xFA
_ATEM_TMxP_time = _packetBuffer[1];
} else
if(strcmp(cmdStr, "DskS") == 0) { // Downstream Keyer state. Also contains information about the frame count in case of "Auto"
idx = _packetBuffer[0];
if (idx >=0 && idx <=1) {
_ATEM_DskOn[idx] = _packetBuffer[1] > 0 ? true : false;
if (_serialOutput) Serial.print(F("Dsk Keyer "));
if (_serialOutput) Serial.print(idx+1);
if (_serialOutput) Serial.print(F(": "));
if (_serialOutput) Serial.println(_ATEM_DskOn[idx], BIN);
}
} else
if(strcmp(cmdStr, "DskP") == 0) { // Downstream Keyer Tie
idx = _packetBuffer[0];
if (idx >=0 && idx <=1) {
_ATEM_DskTie[idx] = _packetBuffer[1] > 0 ? true : false;
if (_serialOutput) Serial.print(F("Dsk Keyer"));
if (_serialOutput) Serial.print(idx+1);
if (_serialOutput) Serial.print(F(" Tie: "));
if (_serialOutput) Serial.println(_ATEM_DskTie[idx], BIN);
}
} else
if(strcmp(cmdStr, "KeOn") == 0) { // Upstream Keyer on
idx = _packetBuffer[1];
if (idx >=0 && idx <=3) {
_ATEM_KeOn[idx] = _packetBuffer[2] > 0 ? true : false;
if (_serialOutput) Serial.print(F("Upstream Keyer "));
if (_serialOutput) Serial.print(idx+1);
if (_serialOutput) Serial.print(F(": "));
if (_serialOutput) Serial.println(_ATEM_KeOn[idx], BIN);
}
} else
if(strcmp(cmdStr, "ColV") == 0) { // Color Generator Change
// Todo: Relatively easy: 8 bytes, first is the color generator, the last 6 is hsl words
} else
if(strcmp(cmdStr, "MPCE") == 0) { // Media Player Clip Enable
idx = _packetBuffer[0];
if (idx >=0 && idx <=1) {
_ATEM_MPType[idx] = _packetBuffer[1];
_ATEM_MPStill[idx] = _packetBuffer[2];
_ATEM_MPClip[idx] = _packetBuffer[3];
}
} else
if(strcmp(cmdStr, "AuxS") == 0) { // Aux Output Source
uint8_t auxInput = _packetBuffer[0];
if (auxInput >=0 && auxInput <=2) {
if (!ver42()) {
_ATEM_AuxS[auxInput] = _packetBuffer[1];
} else {
_ATEM_AuxS[auxInput] = (uint16_t)(_packetBuffer[2]<<8) | _packetBuffer[3];
}
if (_serialOutput) Serial.print(F("Aux "));
if (_serialOutput) Serial.print(auxInput+1);
if (_serialOutput) Serial.print(F(" Output: "));
if (_serialOutput) Serial.println(_ATEM_AuxS[auxInput], DEC);
}
} else
if(strcmp(cmdStr, "_ver") == 0) { // Firmware version
_ATEM_ver_m = _packetBuffer[1]; // Firmware version, "left of decimal point" (what is that called anyway?)
_ATEM_ver_l = _packetBuffer[3]; // Firmware version, decimals ("right of decimal point")
} else
if(strcmp(cmdStr, "_pin") == 0) { // Name
for(uint8_t i=0;i<16;i++) {
_ATEM_pin[i] = _packetBuffer[i];
}
_ATEM_pin[16] = 0; // Termination
} else
if(strcmp(cmdStr, "AMTl") == 0) { // Audio Monitor Tally (on/off settings)
// Same system as for video: "TlIn"... just implement when time.
} else
// Note for future reveng: For master control, volume at least comes back in "AMMO" (CAMM is the command code.)
if(strcmp(cmdStr, "AMIP") == 0) { // Audio Monitor Input P... (state) (On, Off, AFV)
if (_packetBuffer[1]<13) {
_ATEM_AudioChannelMode[_packetBuffer[1]] = _packetBuffer[8];
// 0+1 = Channel (high+low byte)
// 6 = On/Off/AFV
// 10+11 = Balance (0xD8F0 - 0x0000 - 0x2710)
// 8+9 = Volume (0x0020 - 0xFF65)
}
/* for(uint8_t a=0;a<_cmdLength-8;a++) {
Serial.print((uint8_t)_packetBuffer[a], HEX);
Serial.print(" ");
}
Serial.println("");
*/
/* 1M/E:
0: MASTER
1: (Monitor?)
2-9: HDMI1 - SDI8
10: MP1
11: MP2
12: EXT
TVS:
0: MASTER
1: (Monitor?)
2-7: INPUT1-6 (HDMI - HDMI - HDMI/SDI - HDMI/SDI - SDI - SDI)
8: EXT
*/
/* Serial.print("Audio Channel: ");
Serial.println(_packetBuffer[0]); // _packetBuffer[2] seems to be input number (one higher...)
Serial.print(" - State: ");
Serial.println(_packetBuffer[3] == 0x01 ? "ON" : (_packetBuffer[3] == 0x02 ? "AFV" : (_packetBuffer[3] > 0 ? "???" : "OFF")));
Serial.print(" - Volume: ");
Serial.print((uint16_t)_packetBuffer[4]*256+_packetBuffer[5]);
Serial.print("/");
Serial.println((uint16_t)_packetBuffer[6]*256+_packetBuffer[7]);
*/ } else
if(strcmp(cmdStr, "AMLv") == 0) { // Audio Monitor Levels
// Get number of channels:
_readToPacketBuffer(4); // AMLv (AudioMonitorLevels)
uint8_t numberOfChannels = _packetBuffer[1];
uint8_t readingOffset=0;
_readToPacketBuffer(32); // AMLv (AudioMonitorLevels)
if (_ATEM_AMLv_channel<=1) { // Master or Monitor vol
readingOffset= _ATEM_AMLv_channel<<4;
_ATEM_AMLv[0] = ((uint16_t)(_packetBuffer[readingOffset+1]<<8) | _packetBuffer[readingOffset+2]); //drops the 8 least sign. bits! -> 15 bit resolution for VU purposes. fine enough.
readingOffset+=4;
_ATEM_AMLv[1] = ((uint16_t)(_packetBuffer[readingOffset+1]<<8) | _packetBuffer[readingOffset+2]); //drops the 8 least sign. bits! -> 15 bit resolution for VU purposes. fine enough.
} else {
// Match indexes to input src numbers:
_readToPacketBuffer(numberOfChannels & 1 ? (numberOfChannels+1)<<1 : numberOfChannels<<1); // The block of input source numbers is always divisible by 4 bytes, so we must read a multiplum of 4 at all times
for(uint8_t j=0; j<numberOfChannels; j++) {
// uint16_t inputNum = ((uint16_t)(_packetBuffer[j<<1]<<8) | _packetBuffer[(j<<1)+1]);
// Serial.println(inputNum);
/*
0x07D1 = 2001 = MP1
0x07D2 = 2002 = MP2
0x03E9 = 1001 = EXT
0x04b1 = 1201 = RCA
*/
}
// Get level data for each input:
for(uint8_t j=0; j<numberOfChannels; j++) {
_readToPacketBuffer(16);
if (_ATEM_AMLv_channel == j+3) {
readingOffset = 0;
_ATEM_AMLv[0] = ((uint16_t)(_packetBuffer[readingOffset+1]<<8) | _packetBuffer[readingOffset+2]); //drops the 8 least sign. bits! -> 15 bit resolution for VU purposes. fine enough.
readingOffset+=4;
_ATEM_AMLv[1] = ((uint16_t)(_packetBuffer[readingOffset+1]<<8) | _packetBuffer[readingOffset+2]); //drops the 8 least sign. bits! -> 15 bit resolution for VU purposes. fine enough.
}
}
}
} else
if(strcmp(cmdStr, "VidM") == 0) { // Video format (SD, HD, framerate etc.)
_ATEM_VidM = _packetBuffer[0];
} else {
// SHOULD ONLY THE UNCOMMENTED for development and with a high Baud rate on serial - 115200 for instance. Otherwise it will not connect due to serial writing speeds.
/*
if (_serialOutput) {
Serial.print(("???? Unknown token: "));
Serial.print(cmdStr);
Serial.print(" : ");
}
for(uint8_t a=(-2+8);a<_cmdLength-2;a++) {
if (_serialOutput && (uint8_t)_packetBuffer[a]<16) Serial.print(0);
if (_serialOutput) Serial.print((uint8_t)_packetBuffer[a], HEX);
if (_serialOutput) Serial.print(" ");
}
if (_serialOutput) Serial.println("");
*/
}
// Empty, if long packet and not read yet:
while (_readToPacketBuffer()) {}
indexPointer+=_cmdLength;
} else {
indexPointer = 2000;
// Flushing the buffer:
// TODO: Other way? _Udp.flush() ??
while(_Udp.available()) {
_Udp.read(_packetBuffer, 96);
}
}
}
}
/**
* Sending a regular answer packet back (tell the switcher that "we heard you, thanks.")
*/
void ATEM::_sendAnswerPacket(uint16_t remotePacketID) {
//Answer packet:
memset(_packetBuffer, 0, 12); // Using 12 bytes of answer buffer, setting to zeros.
_packetBuffer[2] = _sessionID >> 8; // Session ID
_packetBuffer[3] = _sessionID & 0xFF; // Session ID
_packetBuffer[4] = remotePacketID/256; // Remote Packet ID, MSB
_packetBuffer[5] = remotePacketID%256; // Remote Packet ID, LSB
_packetBuffer[9] = 0x41; // ??? API
// The rest is zeros.
// Create header:
uint16_t returnPacketLength = 10+2;
_packetBuffer[0] = returnPacketLength/256;
_packetBuffer[1] = returnPacketLength%256;
_packetBuffer[0] |= B10000000;
// Send connectAnswerString to ATEM:
_Udp.beginPacket(_switcherIP, 9910);
_Udp.write(_packetBuffer,returnPacketLength);
_Udp.endPacket();
}
/**
* Sending a command packet back (ask the switcher to do something)
*/
void ATEM::_sendCommandPacket(const char cmd[4], uint8_t commandBytes[64], uint8_t cmdBytes) { // TEMP: 16->64
if (cmdBytes <= 64) { // Currently, only a lenght up to 16 - can be extended, but then the _packetBuffer buffer must be prolonged as well (to more than 36) <- TEMP 16->64
//Answer packet preparations:
memset(_packetBuffer, 0, 84); // <- TEMP 36->84
_packetBuffer[2] = _sessionID >> 8; // Session ID
_packetBuffer[3] = _sessionID & 0xFF; // Session ID
_packetBuffer[10] = _localPacketIdCounter/256; // Remote Packet ID, MSB
_packetBuffer[11] = _localPacketIdCounter%256; // Remote Packet ID, LSB
// The rest is zeros.
// Command identifier (4 bytes, after header (12 bytes) and local segment length (4 bytes)):
int i;
for (i=0; i<4; i++) {
_packetBuffer[12+4+i] = cmd[i];
}
// Command value (after command):
for (i=0; i<cmdBytes; i++) {
_packetBuffer[12+4+4+i] = commandBytes[i];
}
// Command length:
_packetBuffer[12] = (4+4+cmdBytes)/256;
_packetBuffer[12+1] = (4+4+cmdBytes)%256;
// Create header:
uint16_t returnPacketLength = 10+2+(4+4+cmdBytes);
_packetBuffer[0] = returnPacketLength/256;
_packetBuffer[1] = returnPacketLength%256;
_packetBuffer[0] |= B00001000;
// Send connectAnswerString to ATEM:
_Udp.beginPacket(_switcherIP, 9910);
_Udp.write(_packetBuffer,returnPacketLength);
_Udp.endPacket();
_localPacketIdCounter++;
}
}
/**
* Sets all zeros in packet buffer:
*/
void ATEM::_wipeCleanPacketBuffer() {
memset(_packetBuffer, 0, 96);
}
/**
* Sets all zeros in packet buffer:
*/
void ATEM::_sendPacketBufferCmdData(const char cmd[4], uint8_t cmdBytes) {
if (cmdBytes <= 96-20) {
//Answer packet preparations:
uint8_t _headerBuffer[20];
memset(_headerBuffer, 0, 20);
_headerBuffer[2] = _sessionID >> 8; // Session ID
_headerBuffer[3] = _sessionID & 0xFF; // Session ID
_headerBuffer[10] = _localPacketIdCounter/256; // Remote Packet ID, MSB
_headerBuffer[11] = _localPacketIdCounter%256; // Remote Packet ID, LSB
// The rest is zeros.
// Command identifier (4 bytes, after header (12 bytes) and local segment length (4 bytes)):
int i;
for (i=0; i<4; i++) {
_headerBuffer[12+4+i] = cmd[i];
}
// Command length:
_headerBuffer[12] = (4+4+cmdBytes)/256;
_headerBuffer[12+1] = (4+4+cmdBytes)%256;
// Create header:
uint16_t returnPacketLength = 20+cmdBytes;
_headerBuffer[0] = returnPacketLength/256;
_headerBuffer[1] = returnPacketLength%256;
_headerBuffer[0] |= B00001000;
// Send connectAnswerString to ATEM:
_Udp.beginPacket(_switcherIP, 9910);
_Udp.write(_headerBuffer,20);
_Udp.write(_packetBuffer,cmdBytes);
_Udp.endPacket();
_localPacketIdCounter++;
}
}
/********************************
*
* General Getter/Setter methods
*
********************************/
/**
* Setter method: If _serialOutput is set, the library may use Serial.print() to give away information about its operation - mostly for debugging.
*/
void ATEM::serialOutput(boolean serialOutput) {
_serialOutput = serialOutput;
}
/**
* Getter method: If true, the initial handshake and "stressful" information exchange has occured and now the switcher connection should be ready for operation.
*/
bool ATEM::hasInitialized() {
return _hasInitialized;
}
/**
* Returns last Remote Packet ID
*/
uint16_t ATEM::getATEM_lastRemotePacketId() {
return _lastRemotePacketID;
}
uint8_t ATEM::getATEMmodel() {
/* Serial.println(_ATEM_pin);
Serial.println(strcmp(_ATEM_pin, "ATEM Television ") == 0);
Serial.println(strcmp(_ATEM_pin, "ATEM 1 M/E Produ") == 0);
Serial.println(strcmp(_ATEM_pin, "ATEM 2 M/E Produ") == 0); // Didn't test this yet!
*/
if (_ATEM_pin[5]=='T') {
if (_serialOutput) Serial.println(F("ATEM TeleVision Studio Detected"));
return 0;
}
if (_ATEM_pin[5]=='1') {
if (_serialOutput) Serial.println(F("ATEM 1 M/E Detected"));
return 1;
}
if (_ATEM_pin[5]=='2') {
if (_serialOutput) Serial.println(F("ATEM 2 M/E Detected"));
return 2;
}
if (_ATEM_pin[5]=='P') {
if (_serialOutput) Serial.println(F("ATEM Production Studio 4K"));
return 3;
}
return 255;
}
/********************************
*
* ATEM Switcher state methods
* Returns the most recent information we've
* got about the switchers state
*
********************************/
uint16_t ATEM::getProgramInput() {
return _ATEM_PrgI;
}
uint16_t ATEM::getPreviewInput() {
return _ATEM_PrvI;
}
boolean ATEM::getProgramTally(uint8_t inputNumber) {
// TODO: Validate that input number exists on current model! <8 at the moment.
return (_ATEM_TlIn[inputNumber-1] & 1) >0 ? true : false;
}
boolean ATEM::getPreviewTally(uint8_t inputNumber) {
// TODO: Validate that input number exists on current model! 1-8 at the moment.
return (_ATEM_TlIn[inputNumber-1] & 2) >0 ? true : false;
}
boolean ATEM::getUpstreamKeyerStatus(uint8_t inputNumber) {
if (inputNumber>=1 && inputNumber<=4) {
return _ATEM_KeOn[inputNumber-1];
}
return false;
}
boolean ATEM::getUpstreamKeyerOnNextTransitionStatus(uint8_t inputNumber) { // input 0 = background
if (inputNumber>=0 && inputNumber<=4) {
// Notice: the first bit is set for the "background", not valid.
return (_ATEM_TrSS_KeyersOnNextTransition & (0x01 << inputNumber)) ? true : false;
}
return false;
}
boolean ATEM::getDownstreamKeyerStatus(uint8_t inputNumber) {
if (inputNumber>=1 && inputNumber<=2) {
return _ATEM_DskOn[inputNumber-1];
}
return false;
}
uint16_t ATEM::getTransitionPosition() {
return _ATEM_TrPs_position;
}
bool ATEM::getTransitionPreview() {
return _ATEM_TrPr;
}
uint8_t ATEM::getTransitionType() {
return _ATEM_TrSS_TransitionStyle;
}
uint8_t ATEM::getTransitionMixTime() {
return _ATEM_TMxP_time; // Transition time for Mix Transitions
}
boolean ATEM::getFadeToBlackState() {
return _ATEM_FtbS_state; // Active state of Fade-to-black
}
uint8_t ATEM::getFadeToBlackFrameCount() {
return _ATEM_FtbS_frameCount; // Returns current frame in the FTB
}
uint8_t ATEM::getFadeToBlackTime() {
return _ATEM_FtbP_time; // Transition time for Fade-to-black
}
bool ATEM::getDownstreamKeyTie(uint8_t keyer) {
if (keyer>=1 && keyer<=2) { // Todo: Should match available keyers depending on model?
return _ATEM_DskTie[keyer-1];
}
return false;
}
uint16_t ATEM::getAuxState(uint8_t auxOutput) {
// TODO: Validate that input number exists on current model!
// On ATEM 1M/E: Black (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (7), 8 (8), Bars (9), Color1 (10), Color 2 (11), Media 1 (12), Media 1 Key (13), Media 2 (14), Media 2 Key (15), Program (16), Preview (17), Clean1 (18), Clean 2 (19)
if (auxOutput>=1 && auxOutput<=3) { // Todo: Should match available aux outputs
return _ATEM_AuxS[auxOutput-1];
}
return 0;
}
uint8_t ATEM::getMediaPlayerType(uint8_t mediaPlayer) {
if (mediaPlayer>=1 && mediaPlayer<=2) { // TODO: Adjust to particular ATEM model... (here 1M/E)
return _ATEM_MPType[mediaPlayer-1]; // Media Player 1/2: Type (1=Clip, 2=Still)
}
return 0;
}
uint8_t ATEM::getMediaPlayerStill(uint8_t mediaPlayer) {
if (mediaPlayer>=1 && mediaPlayer<=2) { // TODO: Adjust to particular ATEM model... (here 1M/E)
return _ATEM_MPStill[mediaPlayer-1]+1; // Still number (if MPType==2)
}
return 0;
}
uint8_t ATEM::getMediaPlayerClip(uint8_t mediaPlayer) {
if (mediaPlayer>=1 && mediaPlayer<=2) { // TODO: Adjust to particular ATEM model... (here 1M/E)
return _ATEM_MPClip[mediaPlayer-1]+1; // Clip number (if MPType==1)
}
return 0;
}
uint16_t ATEM::getAudioLevels(uint8_t channel) {
// channel can be 0 (L) or 1 (R)
return _ATEM_AMLv[channel];
}
uint8_t ATEM::getAudioChannelMode(uint8_t channelNumber) {
if (channelNumber<13) {
/* 0: MASTER
1: (Monitor?)
2-9: HDMI1 - SDI8
10: MP1
11: MP2
12: EXT*/
return _ATEM_AudioChannelMode[channelNumber];
}
}
/********************************
*
* ATEM Switcher Change methods
* Asks the switcher to changes something
*
********************************/
void ATEM::changeProgramInput(uint16_t inputNumber) {
// TODO: Validate that input number exists on current model!
// On ATEM 1M/E: Black (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (7), 8 (8), Bars (9), Color1 (10), Color 2 (11), Media 1 (12), Media 2 (14)
_wipeCleanPacketBuffer();
if (!ver42()) {
_packetBuffer[1] = inputNumber;
} else {
_packetBuffer[2] = (inputNumber >> 8);
_packetBuffer[3] = (inputNumber & 0xFF);
}
_sendPacketBufferCmdData("CPgI", 4);
}
void ATEM::changePreviewInput(uint16_t inputNumber) {
// TODO: Validate that input number exists on current model!
_wipeCleanPacketBuffer();
if (!ver42()) {
_packetBuffer[1] = inputNumber;
} else {
_packetBuffer[2] = (inputNumber >> 8);
_packetBuffer[3] = (inputNumber & 0xFF);
}
_sendPacketBufferCmdData("CPvI", 4);
}
void ATEM::doCut() {
_wipeCleanPacketBuffer();
_packetBuffer[1] = 0xef;
_packetBuffer[2] = 0xbf;
_packetBuffer[3] = 0x5f;
_sendPacketBufferCmdData("DCut", 4);
}
void ATEM::doAuto() {
doAuto(0);
}
void ATEM::doAuto(uint8_t me) {
_wipeCleanPacketBuffer();
_packetBuffer[0] = me;
_sendPacketBufferCmdData("DAut", 4);
}
void ATEM::fadeToBlackActivate() {
_wipeCleanPacketBuffer();
_packetBuffer[1] = 0x02;
_packetBuffer[2] = 0x58;
_packetBuffer[3] = 0x99;
_sendPacketBufferCmdData("FtbA", 4); // Reflected back from ATEM in "FtbS"
}
void ATEM::changeTransitionPosition(word value) {
if (value>0 && value<=1000) {
uint8_t commandBytes[4] = {0, 0xe4, (value*10)/256, (value*10)%256};
_sendCommandPacket("CTPs", commandBytes, 4); // Change Transition Position (CTPs)
}
}
void ATEM::changeTransitionPositionDone() { // When the last value of the transition is sent (1000), send this one too (we are done, change tally lights and preview bus!)
uint8_t commandBytes[4] = {0, 0xf6, 0, 0}; // Done
_sendCommandPacket("CTPs", commandBytes, 4); // Change Transition Position (CTPs)
}
void ATEM::changeTransitionPreview(bool state) {
uint8_t commandBytes[4] = {0x00, state ? 0x01 : 0x00, 0x00, 0x00};
_sendCommandPacket("CTPr", commandBytes, 4); // Reflected back from ATEM in "TrPr"
}
void ATEM::changeTransitionType(uint8_t type) {
if (type>=0 && type<=4) { // 0=MIX, 1=DIP, 2=WIPE, 3=DVE, 4=STING
uint8_t commandBytes[4] = {0x01, 0x00, type, 0x02};
_sendCommandPacket("CTTp", commandBytes, 4); // Reflected back from ATEM in "TrSS"
}
}
void ATEM::changeTransitionMixTime(uint8_t frames) {
if (frames>=1 && frames<=0xFA) {
uint8_t commandBytes[4] = {0x00, frames, 0x00, 0x00};
_sendCommandPacket("CTMx", commandBytes, 4); // Reflected back from ATEM in "TMxP"
}
}
void ATEM::changeFadeToBlackTime(uint8_t frames) {
if (frames>=1 && frames<=0xFA) {
uint8_t commandBytes[4] = {0x01, 0x00, frames, 0x02};
_sendCommandPacket("FtbC", commandBytes, 4); // Reflected back from ATEM in "FtbP"
}
}
void ATEM::changeUpstreamKeyOn(uint8_t keyer, bool state) {
if (keyer>=1 && keyer<=4) { // Todo: Should match available keyers depending on model?
_wipeCleanPacketBuffer();
_packetBuffer[1] = keyer-1;
_packetBuffer[2] = state ? 0x01 : 0x00;
_packetBuffer[3] = 0x90;
_sendPacketBufferCmdData("CKOn", 4); // Reflected back from ATEM in "KeOn"
}
}
void ATEM::changeUpstreamKeyNextTransition(uint8_t keyer, bool state) { // Supporting "Background" by "0"
if (keyer>=0 && keyer<=4) { // Todo: Should match available keyers depending on model?
uint8_t stateValue = _ATEM_TrSS_KeyersOnNextTransition;
if (state) {
stateValue = stateValue | (B1 << keyer);
} else {
stateValue = stateValue & (~(B1 << keyer));
}
// TODO: Requires internal storage of state here so we can preserve all other states when changing the one we want to change.
// Below: Byte 2 is which ME (1 or 2):
uint8_t commandBytes[4] = {0x02, 0x00, 0x6a, stateValue & B11111};
_sendCommandPacket("CTTp", commandBytes, 4); // Reflected back from ATEM in "TrSS"
}
}
void ATEM::changeDownstreamKeyOn(uint8_t keyer, bool state) {
if (keyer>=1 && keyer<=2) { // Todo: Should match available keyers depending on model?
uint8_t commandBytes[4] = {keyer-1, state ? 0x01 : 0x00, 0xff, 0xff};
_sendCommandPacket("CDsL", commandBytes, 4); // Reflected back from ATEM in "DskP" and "DskS"
}
}
void ATEM::changeDownstreamKeyTie(uint8_t keyer, bool state) {
if (keyer>=1 && keyer<=2) { // Todo: Should match available keyers depending on model?
uint8_t commandBytes[4] = {keyer-1, state ? 0x01 : 0x00, 0xff, 0xff};
_sendCommandPacket("CDsT", commandBytes, 4);
}
}
void ATEM::doAutoDownstreamKeyer(uint8_t keyer) {
if (keyer>=1 && keyer<=2) { // Todo: Should match available keyers depending on model?
uint8_t commandBytes[4] = {keyer-1, 0x32, 0x16, 0x02}; // I don't know what that actually means...
_sendCommandPacket("DDsA", commandBytes, 4);
}
}
void ATEM::changeAuxState(uint8_t auxOutput, uint16_t inputNumber) {
// TODO: Validate that input number exists on current model!
// On ATEM 1M/E: Black (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (7), 8 (8), Bars (9), Color1 (10), Color 2 (11), Media 1 (12), Media 1 Key (13), Media 2 (14), Media 2 Key (15), Program (16), Preview (17), Clean1 (18), Clean 2 (19)
if (auxOutput>=1 && auxOutput<=3) { // Todo: Should match available aux outputs
if (!ver42()) {
uint8_t commandBytes[4] = {auxOutput-1, inputNumber, 0, 0};