-
Notifications
You must be signed in to change notification settings - Fork 10
/
TeensyDmx.cpp
executable file
·2540 lines (2241 loc) · 78.1 KB
/
TeensyDmx.cpp
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
#include "TeensyDmx.h"
#include "rdm.h"
#include <limits>
namespace {
constexpr uint32_t BREAKSPEED = 100000;
constexpr uint32_t RDM_BREAKSPEED = 45500;
constexpr uint32_t BREAKFORMAT = SERIAL_8E1;
constexpr uint32_t DMXSPEED = 250000;
constexpr uint32_t DMXFORMAT = SERIAL_8N2;
constexpr uint16_t NACK_WAS_ACK = 0xffff; // Send an ACK, not a NACK
// RDM discovery debugging
// Enable: sed -i -e 's/Serial\./\/\/ Serial./g' TeensyDmx.{cpp,h}
// Disable: sed -i -e 's/\/\/ Serial\./Serial./g' TeensyDmx.{cpp,h}
// The DeviceInfoGetResponse structure (length = 19) has to be responded for
// E120_DEVICE_INFO. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=96
struct DeviceInfoGetResponse
{
byte protocolMajor;
byte protocolMinor;
uint16_t deviceModel;
uint16_t productCategory;
uint32_t softwareVersion;
uint16_t footprint;
byte currentPersonality;
byte personalityCount;
uint16_t startAddress;
uint16_t subDeviceCount;
byte sensorCount;
} __attribute__((__packed__)); // struct DeviceInfoGetResponse
static_assert((sizeof(DeviceInfoGetResponse) == 19),
"Invalid size for DeviceInfoGetResponse struct, is it packed?");
// The DmxPersonalityGetResponse structure (length = 2) has to be responded for
// E120_DMX_PERSONALITY. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=224
struct DmxPersonalityGetResponse
{
byte currentPersonality;
byte personalityCount;
} __attribute__((__packed__)); // struct DmxPersonalityGetResponse
static_assert((sizeof(DmxPersonalityGetResponse) == 2),
"Invalid size for DmxPersonalityGetResponse struct, is it packed?");
// The DmxPersonalityDescriptionGetResponse structure (length = 3 to 35) has to be responded for
// E120_DMX_PERSONALITY_DESCRIPTION. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=225
struct DmxPersonalityDescriptionGetResponse
{
byte personality;
uint16_t slotsRequired;
char name[RDM_MAX_STRING_LENGTH];
} __attribute__((__packed__)); // struct DmxPersonalityDescriptionGetResponse
static_assert((sizeof(DmxPersonalityDescriptionGetResponse) == 35),
"Invalid size for DmxPersonalityDescriptionGetResponse struct, is it packed?");
// The SelfTestDescriptionGetResponse structure (length = 1 to 33) has to be responded for
// E120_SELF_TEST_DESCRIPTION. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=4129
struct SelfTestDescriptionGetResponse
{
byte testNumber;
char description[RDM_MAX_STRING_LENGTH];
} __attribute__((__packed__)); // struct SelfTestDescriptionGetResponse
static_assert((sizeof(SelfTestDescriptionGetResponse) == 33),
"Invalid size for SelfTestDescriptionGetResponse struct, is it packed?");
// The SensorDefinitionGetResponse structure (length = 13 to 45) has to be responded for
// E120_SENSOR_DEFINITION. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=512
struct SensorDefinitionGetResponse
{
byte sensorNumber;
byte type;
byte unit;
byte prefix;
int16_t rangeMin;
int16_t rangeMax;
int16_t normalMin;
int16_t normalMax;
byte supportsRecording;
char name[RDM_MAX_STRING_LENGTH];
} __attribute__((__packed__)); // struct SensorDefinitionGetResponse
static_assert((sizeof(SensorDefinitionGetResponse) == 45),
"Invalid size for SensorDefinitionGetResponse struct, is it packed?");
// The SensorValueGetResponse structure (length = 9) has to be responded for
// E120_SENSOR_VALUE. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=513
struct SensorValueGetResponse
{
byte sensorNumber;
int16_t presentValue;
int16_t lowest;
int16_t highest;
int16_t recorded;
} __attribute__((__packed__)); // struct SensorValueGetResponse
static_assert((sizeof(SensorValueGetResponse) == 9),
"Invalid size for SensorValueGetResponse struct, is it packed?");
// SensorValueSetResponse is identical to SensorValueGetResponse
using SensorValueSetResponse = SensorValueGetResponse;
static_assert((sizeof(SensorValueSetResponse) == 9),
"Invalid size for SensorValueSetResponse struct, is it packed?");
// The CommsStatusGetResponse structure (length = 6) has to be responded for
// E120_COMMS_STATUS. See http://rdm.openlighting.org/pid/display?manufacturer=0&pid=21
struct CommsStatusGetResponse
{
uint16_t shortMessage;
uint16_t lengthMismatch;
uint16_t checksumFail;
} __attribute__((__packed__)); // struct CommsStatusGetResponse
static_assert((sizeof(CommsStatusGetResponse) == 6),
"Invalid size for CommsStatusGetResponse struct, is it packed?");
struct DiscUniqueBranchRequest
{
byte lowerBoundUID[RDM_UID_LENGTH];
byte upperBoundUID[RDM_UID_LENGTH];
} __attribute__((__packed__)); // struct DiscUniqueBranchRequest
static_assert((sizeof(DiscUniqueBranchRequest) == 12),
"Invalid size for DiscUniqueBranchRequest struct, is it packed?");
// the special discovery response message
struct DiscUniqueBranchResponse
{
byte headerFE[7];
byte headerAA;
byte maskedDevID[12];
byte checksum[4];
} __attribute__((__packed__)); // struct DiscUniqueBranchResponse
static_assert((sizeof(DiscUniqueBranchResponse) == 24),
"Invalid size for DiscUniqueBranchResponse struct, is it packed?");
enum { RDM_DUB_PREAMBLE_SIZE = (
sizeof(((DiscUniqueBranchResponse *)0)->headerFE) +
sizeof(((DiscUniqueBranchResponse *)0)->headerAA)) };
enum { RDM_PACKET_SIZE_NO_PD = (sizeof(RdmData) - RDM_MAX_PARAMETER_DATA_LENGTH) };
static_assert((RDM_PACKET_SIZE_NO_PD == 24),
"Invalid size for RDM packet without parameter data");
#if defined(HAS_KINETISK_UART5)
// Instance for UART0, UART1, UART2, UART3, UART4, UART5
TeensyDmx *uartInstances[6] = {0};
#elif defined(HAS_KINETISK_UART4)
// Instance for UART0, UART1, UART2, UART3, UART4
TeensyDmx *uartInstances[5] = {0};
#elif defined(HAS_KINETISK_UART3)
// Instance for UART0, UART1, UART2, UART3
TeensyDmx *uartInstances[4] = {0};
#else
// Instance for UART0, UART1, UART2
TeensyDmx *uartInstances[3] = {0};
#endif
inline uint16_t getUInt16(const byte* const buffer)
{
return (buffer[0] << 8) | buffer[1];
}
inline uint16_t swapUInt16(uint16_t i)
{
return (i << 8) | (i >> 8);
}
inline void putUInt16(void* const buffer, const uint16_t value)
{
reinterpret_cast<byte*>(buffer)[0] = value >> 8;
reinterpret_cast<byte*>(buffer)[1] = value & 0xff;
}
inline void putUInt32(void* const buffer, const uint32_t value)
{
reinterpret_cast<byte*>(buffer)[0] = (value & 0xff000000) >> 24;
reinterpret_cast<byte*>(buffer)[1] = (value & 0x00ff0000) >> 16;
reinterpret_cast<byte*>(buffer)[2] = (value & 0x0000ff00) >> 8;
reinterpret_cast<byte*>(buffer)[3] = (value & 0x000000ff);
}
inline void putUInt48(void* const buffer, const uint64_t value)
{
reinterpret_cast<byte*>(buffer)[0] = (value & 0xff0000000000) >> 40;
reinterpret_cast<byte*>(buffer)[1] = (value & 0x00ff00000000) >> 32;
reinterpret_cast<byte*>(buffer)[2] = (value & 0x0000ff000000) >> 24;
reinterpret_cast<byte*>(buffer)[3] = (value & 0x000000ff0000) >> 16;
reinterpret_cast<byte*>(buffer)[4] = (value & 0x00000000ff00) >> 8;
reinterpret_cast<byte*>(buffer)[5] = (value & 0x0000000000ff);
}
} // anon namespace
TeensyDmx::TeensyDmx(HardwareSerial& uart, RdmInit* rdm, uint8_t redePin) :
TeensyDmx(uart, rdm)
{
pinMode(redePin, OUTPUT);
m_redePin = portOutputRegister(redePin);
*m_redePin = 0;
}
TeensyDmx::TeensyDmx(HardwareSerial& uart, RdmInit* rdm) :
m_uart(uart),
m_dmxBuffer1{0},
m_dmxBuffer2{0},
m_activeBuffer(m_dmxBuffer1),
m_inactiveBuffer(m_dmxBuffer2),
m_dmxBufferIndex(0),
m_frameCount(0),
m_shortMessage(0),
m_checksumFail(0),
m_lengthMismatch(0),
m_newFrame(false),
m_rdmChange(false),
m_rdmResponseDue(0),
m_mode(DMX_OFF),
m_state(State::IDLE),
m_discoveryState(DiscoveryState::DISCOVERY_IDLE),
m_nextDiscoveryAction(0),
m_dubQueue{0},
m_dubPointer(0),
m_dubLowerBoundUid(RDM_MIN_LOWER_BOUND_UID),
m_dubUpperBoundUid(RDM_MAX_UPPER_BOUND_UID),
m_uidList{0},
m_uidCount(0),
m_controllerState(ControllerState::CONTROLLER_IDLE),
m_redePin(nullptr),
m_rdmMute(false),
m_identifyMode(false),
m_rdm(rdm),
m_rdmNeedsProcessing(false),
m_rdmBuffer(),
m_rdmChecksum(0),
m_deviceLabel{0}
{
// Serial.begin(9600);
// Serial.println("Started TeensyDmx");
if (&m_uart == &Serial1) {
uartInstances[0] = this;
} else if (&m_uart == &Serial2) {
uartInstances[1] = this;
} else if (&m_uart == &Serial3) {
uartInstances[2] = this;
}
#ifdef HAS_KINETISK_UART3
else if (&m_uart == &Serial4) {
uartInstances[3] = this;
}
#endif
#ifdef HAS_KINETISK_UART4
else if (&m_uart == &Serial5) {
uartInstances[4] = this;
}
#endif
#ifdef HAS_KINETISK_UART5
else if (&m_uart == &Serial6) {
uartInstances[5] = this;
}
#endif
}
const volatile uint8_t* TeensyDmx::getBuffer() const
{
if (m_mode == DMX_IN) {
// DMX Rx is double buffered due to the interrupt handler
return m_inactiveBuffer;
} else {
return m_activeBuffer;
}
}
uint8_t TeensyDmx::getChannel(const uint16_t address)
{
if (address < DMX_BUFFER_SIZE) {
return getBuffer()[address];
} else {
return 0;
}
}
bool TeensyDmx::isIdentify() const
{
return m_identifyMode;
}
const char* TeensyDmx::getLabel() const
{
return m_deviceLabel;
}
const volatile uint16_t TeensyDmx::getShortMessage() const
{
return m_shortMessage;
}
const volatile uint16_t TeensyDmx::getChecksumFail() const
{
return m_checksumFail;
}
const volatile uint16_t TeensyDmx::getLengthMismatch() const
{
return m_lengthMismatch;
}
void TeensyDmx::setMode(TeensyDmx::Mode mode)
{
// Stop what we were doing
m_state = IDLE;
switch (m_mode)
{
case DMX_IN:
stopReceive();
break;
case DMX_OUT:
stopTransmit();
break;
default:
// No action
break;
}
m_mode = mode;
switch (m_mode)
{
case DMX_IN:
startReceive();
break;
case DMX_OUT:
startTransmit();
break;
default:
setDirection(false); // Off puts in receive state so as to be passive
break;
}
}
void TeensyDmx::setChannel(const uint16_t address, const uint8_t value)
{
if (address < DMX_BUFFER_SIZE) {
m_activeBuffer[address] = value;
}
}
void TeensyDmx::setChannels(
const uint16_t startAddress,
const uint8_t* values,
const uint16_t length)
{
uint16_t currentAddress = 0;
while (currentAddress < startAddress && currentAddress < DMX_BUFFER_SIZE) {
m_activeBuffer[currentAddress] = 0;
++currentAddress;
}
for (uint16_t i = 0; i < length && currentAddress < DMX_BUFFER_SIZE; ++i) {
m_activeBuffer[currentAddress] = values[i];
++currentAddress;
}
while (currentAddress < DMX_BUFFER_SIZE) {
m_activeBuffer[currentAddress] = 0;
++currentAddress;
}
}
void TeensyDmx::nextTx()
{
if (m_state == State::BREAK) {
m_state = DMX_TX;
// Send the NSC
m_uart.begin(DMXSPEED, DMXFORMAT);
m_uart.write(0);
m_dmxBufferIndex = 0;
} else if (m_state == State::DMX_TX) {
// Check if we're at the end of the packet
if (m_dmxBufferIndex == DMX_BUFFER_SIZE) {
m_state = State::BREAK;
// Send BREAK
m_uart.begin(BREAKSPEED, BREAKFORMAT);
m_uart.write(0);
} else {
m_uart.write(m_activeBuffer[m_dmxBufferIndex]);
++m_dmxBufferIndex;
}
}
}
void uart0_status_isr(); // Back reference to serial1.c
void UART0TxStatus()
{
if ((UART0_S1 & UART_S1_TC)) {
// TX complete
uartInstances[0]->nextTx();
}
// Call standard ISR too
uart0_status_isr();
}
void uart1_status_isr(); // Back reference to serial2.c
void UART1TxStatus()
{
if ((UART1_S1 & UART_S1_TC)) {
// TX complete
uartInstances[1]->nextTx();
}
// Call standard ISR too
uart1_status_isr();
}
void uart2_status_isr(); // Back reference to serial3.c
void UART2TxStatus()
{
if ((UART2_S1 & UART_S1_TC)) {
// TX complete
uartInstances[2]->nextTx();
}
// Call standard ISR too
uart2_status_isr();
}
#ifdef HAS_KINETISK_UART3
void uart3_status_isr(); // Back reference to serial4.c
void UART3TxStatus()
{
if ((UART3_S1 & UART_S1_TC)) {
// TX complete
uartInstances[3]->nextTx();
}
// Call standard ISR too
uart3_status_isr();
}
#endif
#ifdef HAS_KINETISK_UART4
void uart4_status_isr(); // Back reference to serial5.c
void UART4TxStatus()
{
if ((UART4_S1 & UART_S1_TC)) {
// TX complete
uartInstances[4]->nextTx();
}
// Call standard ISR too
uart4_status_isr();
}
#endif
#ifdef HAS_KINETISK_UART5
void uart5_status_isr(); // Back reference to serial6.c
void UART5TxStatus()
{
if ((UART5_S1 & UART_S1_TC)) {
// TX complete
uartInstances[5]->nextTx();
}
// Call standard ISR too
uart5_status_isr();
}
#endif
void TeensyDmx::startTransmit()
{
setDirection(true);
m_dmxBufferIndex = 0;
if (&m_uart == &Serial1) {
// Change interrupt vector to mine to monitor TX complete
attachInterruptVector(IRQ_UART0_STATUS, UART0TxStatus);
} else if (&m_uart == &Serial2) {
// Change interrupt vector to mine to monitor TX complete
attachInterruptVector(IRQ_UART1_STATUS, UART1TxStatus);
} else if (&m_uart == &Serial3) {
// Change interrupt vector to mine to monitor TX complete
attachInterruptVector(IRQ_UART2_STATUS, UART2TxStatus);
}
#ifdef HAS_KINETISK_UART3
else if (&m_uart == &Serial4) {
// Change interrupt vector to mine to monitor TX complete
attachInterruptVector(IRQ_UART3_STATUS, UART3TxStatus);
}
#endif
#ifdef HAS_KINETISK_UART4
else if (&m_uart == &Serial5) {
// Change interrupt vector to mine to monitor TX complete
attachInterruptVector(IRQ_UART4_STATUS, UART4TxStatus);
}
#endif
#ifdef HAS_KINETISK_UART5
else if (&m_uart == &Serial6) {
// Change interrupt vector to mine to monitor TX complete
attachInterruptVector(IRQ_UART5_STATUS, UART5TxStatus);
}
#endif
// Send BREAK
m_state = State::BREAK;
m_uart.begin(BREAKSPEED, BREAKFORMAT);
m_uart.write(0);
}
void TeensyDmx::stopTransmit()
{
m_uart.end();
if (&m_uart == &Serial1) {
attachInterruptVector(IRQ_UART0_STATUS, uart0_status_isr);
} else if (&m_uart == &Serial2) {
attachInterruptVector(IRQ_UART1_STATUS, uart1_status_isr);
} else if (&m_uart == &Serial3) {
attachInterruptVector(IRQ_UART2_STATUS, uart2_status_isr);
}
#ifdef HAS_KINETISK_UART3
else if (&m_uart == &Serial4) {
attachInterruptVector(IRQ_UART3_STATUS, uart3_status_isr);
}
#endif
#ifdef HAS_KINETISK_UART4
else if (&m_uart == &Serial5) {
attachInterruptVector(IRQ_UART4_STATUS, uart4_status_isr);
}
#endif
#ifdef HAS_KINETISK_UART5
else if (&m_uart == &Serial6) {
attachInterruptVector(IRQ_UART5_STATUS, uart5_status_isr);
}
#endif
}
bool TeensyDmx::newFrame(void)
{
bool newFrame = m_newFrame;
m_newFrame = false;
return newFrame;
}
bool TeensyDmx::rdmChanged(void)
{
bool rdmChange = m_rdmChange;
m_rdmChange = false;
return rdmChange;
}
void TeensyDmx::completeFrame()
{
switch (m_state)
{
case State::DMX_RECV:
case State::DMX_COMPLETE:
// Update frame count and swap buffers
++m_frameCount;
if (m_activeBuffer == m_dmxBuffer1) {
m_activeBuffer = m_dmxBuffer2;
m_inactiveBuffer = m_dmxBuffer1;
} else {
m_activeBuffer = m_dmxBuffer1;
m_inactiveBuffer = m_dmxBuffer2;
}
m_newFrame = true;
break;
case State::RDM_RECV:
case State::RDM_RECV_CHECKSUM_HI:
// Double check the previous partial message was an RDM one
if ((m_mode == DMX_IN) &&
(m_rdmBuffer.subStartCode == E120_SC_SUB_MESSAGE)) {
if (m_dmxBufferIndex < 9) {
// Destination UID needs 8, but we post increment, hence 9
maybeIncrementShortMessage();
} else if (m_dmxBufferIndex < (m_rdmBuffer.length + 3)) {
// Expected length plus checksum, but we post increment, hence 3
maybeIncrementLengthMismatch();
}
}
// Fall through
default:
// Unknown, ASC? frame or was RDM packet
break;
}
m_state = State::BREAK;
}
void TeensyDmx::rdmDiscUniqueBranch()
{
if (m_rdm == nullptr || m_rdmMute) {
return;
}
if (m_rdmBuffer.length != (RDM_PACKET_SIZE_NO_PD + sizeof(DiscUniqueBranchRequest))) {
return;
}
if (m_rdmBuffer.dataLength != sizeof(DiscUniqueBranchRequest)) {
return;
}
DiscUniqueBranchRequest *dub_request =
reinterpret_cast<DiscUniqueBranchRequest*>(m_rdmBuffer.data);
if (memcmp(dub_request->lowerBoundUID, m_rdm->uid, RDM_UID_LENGTH) <= 0 &&
memcmp(m_rdm->uid, dub_request->upperBoundUID, RDM_UID_LENGTH) <= 0) {
// I'm in range - say hello to the lovely controller
// respond with the special discovery message !
DiscUniqueBranchResponse *dub_response =
reinterpret_cast<DiscUniqueBranchResponse*>(&m_rdmBuffer);
// fill in the discovery response structure
for (byte i = 0; i < 7; ++i) {
dub_response->headerFE[i] = 0xFE;
}
dub_response->headerAA = 0xAA;
for (byte i = 0; i < 6; ++i) {
dub_response->maskedDevID[i+i] = m_rdm->uid[i] | 0xAA;
dub_response->maskedDevID[i+i+1] = m_rdm->uid[i] | 0x55;
}
uint16_t checksum =
rdmCalculateChecksum(dub_response->maskedDevID,
sizeof(dub_response->maskedDevID));
dub_response->checksum[0] = (checksum >> 8) | 0xAA;
dub_response->checksum[1] = (checksum >> 8) | 0x55;
dub_response->checksum[2] = (checksum & 0xFF) | 0xAA;
dub_response->checksum[3] = (checksum & 0xFF) | 0x55;
// Send reply
stopReceive();
setDirection(true);
// No break for DUB
m_uart.begin(DMXSPEED, DMXFORMAT);
m_uart.write(reinterpret_cast<uint8_t*>(&m_rdmBuffer),
sizeof(DiscUniqueBranchResponse));
m_uart.flush();
startReceive();
}
}
uint16_t TeensyDmx::rdmDiscUnMute()
{
if (m_rdmBuffer.dataLength != 0) {
return E120_NR_FORMAT_ERROR;
}
m_rdmMute = false;
// Control field
m_rdmBuffer.data[0] = 0;
m_rdmBuffer.data[1] = 0;
m_rdmBuffer.dataLength = 2;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmDiscMute()
{
if (m_rdmBuffer.dataLength != 0) {
return E120_NR_FORMAT_ERROR;
}
m_rdmMute = true;
// Control field
m_rdmBuffer.data[0] = 0;
m_rdmBuffer.data[1] = 0;
m_rdmBuffer.dataLength = 2;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmSetIdentifyDevice()
{
if (m_rdmBuffer.dataLength != 1) {
// Oversized data
return E120_NR_FORMAT_ERROR;
}
if ((m_rdmBuffer.data[0] != 0) && (m_rdmBuffer.data[0] != 1)) {
// Out of range data
return E120_NR_DATA_OUT_OF_RANGE;
}
m_identifyMode = m_rdmBuffer.data[0] != 0;
m_rdmChange = true;
m_rdmBuffer.dataLength = 0;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmSetCommsStatus()
{
if (m_rdmBuffer.dataLength != 0) {
// Oversized data
return E120_NR_FORMAT_ERROR;
}
m_shortMessage = 0;
m_lengthMismatch = 0;
m_checksumFail = 0;
m_rdmChange = true;
m_rdmBuffer.dataLength = 0;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmSetDeviceLabel()
{
if (m_rdmBuffer.dataLength > RDM_MAX_STRING_LENGTH) {
// Oversized data
return E120_NR_FORMAT_ERROR;
}
memcpy(m_deviceLabel, m_rdmBuffer.data, m_rdmBuffer.dataLength);
m_deviceLabel[m_rdmBuffer.dataLength] = '\0';
m_rdmBuffer.dataLength = 0;
m_rdmChange = true;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmSetDMXStartAddress()
{
if (m_rdmBuffer.dataLength != 2) {
// Oversized data
return E120_NR_FORMAT_ERROR;
}
uint16_t newStartAddress = getUInt16(m_rdmBuffer.data);
if ((newStartAddress <= 0) || (newStartAddress > DMX_BUFFER_SIZE)) {
// Out of range start address
return E120_NR_DATA_OUT_OF_RANGE;
}
if (m_rdm == nullptr) {
return E120_NR_HARDWARE_FAULT;
}
m_rdm->startAddress = newStartAddress;
m_rdmBuffer.dataLength = 0;
m_rdmChange = true;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmGetCommsStatus()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
}
if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
}
// return all comms status data
// The data to be responded has to be in the Data buffer.
CommsStatusGetResponse *commsStatus =
reinterpret_cast<CommsStatusGetResponse*>(m_rdmBuffer.data);
putUInt16(&commsStatus->shortMessage, m_shortMessage);
putUInt16(&commsStatus->lengthMismatch, m_lengthMismatch);
putUInt16(&commsStatus->checksumFail, m_checksumFail);
m_rdmBuffer.dataLength = sizeof(CommsStatusGetResponse);
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmGetIdentifyDevice()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
}
if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
}
m_rdmBuffer.data[0] = m_identifyMode;
m_rdmBuffer.dataLength = 1;
return NACK_WAS_ACK;
}
uint16_t TeensyDmx::rdmGetDeviceInfo()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else {
// return all device info data
// The data to be responded has to be in the Data buffer.
DeviceInfoGetResponse *devInfo =
reinterpret_cast<DeviceInfoGetResponse*>(m_rdmBuffer.data);
devInfo->protocolMajor = 1;
devInfo->protocolMinor = 0;
devInfo->currentPersonality = 1;
devInfo->personalityCount = 1;
devInfo->subDeviceCount = 0;
devInfo->sensorCount = 0;
if (m_rdm == nullptr) {
devInfo->deviceModel = 0;
putUInt16(&devInfo->productCategory, E120_PRODUCT_CATEGORY_NOT_DECLARED);
devInfo->softwareVersion = 0;
devInfo->startAddress = 0;
devInfo->footprint = 0;
} else {
putUInt16(&devInfo->deviceModel, m_rdm->deviceModelId);
putUInt16(&devInfo->productCategory, m_rdm->productCategory);
putUInt32(&devInfo->softwareVersion, m_rdm->softwareVersionId);
putUInt16(&devInfo->startAddress, m_rdm->startAddress);
putUInt16(&devInfo->footprint, m_rdm->footprint);
}
m_rdmBuffer.dataLength = sizeof(DeviceInfoGetResponse);
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmGetManufacturerLabel()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else if (m_rdm == nullptr) {
return E120_NR_HARDWARE_FAULT;
} else {
// return the manufacturer label
m_rdmBuffer.dataLength = strnlen(m_rdm->manufacturerLabel, RDM_MAX_STRING_LENGTH);
memcpy(m_rdmBuffer.data, m_rdm->manufacturerLabel, m_rdmBuffer.dataLength);
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmGetDeviceModelDescription()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else if (m_rdm == nullptr) {
return E120_NR_HARDWARE_FAULT;
} else {
// return the DEVICE MODEL DESCRIPTION
m_rdmBuffer.dataLength = strnlen(m_rdm->deviceModel, RDM_MAX_STRING_LENGTH);
memcpy(m_rdmBuffer.data, m_rdm->deviceModel, m_rdmBuffer.dataLength);
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmGetDeviceLabel()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else {
m_rdmBuffer.dataLength = strnlen(m_deviceLabel, RDM_MAX_STRING_LENGTH);
memcpy(m_rdmBuffer.data, m_deviceLabel, m_rdmBuffer.dataLength);
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmGetSoftwareVersionLabel()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else if (m_rdm == nullptr) {
return E120_NR_HARDWARE_FAULT;
} else {
// return the SOFTWARE_VERSION_LABEL
m_rdmBuffer.dataLength = strnlen(m_rdm->softwareLabel, RDM_MAX_STRING_LENGTH);
memcpy(m_rdmBuffer.data, m_rdm->softwareLabel, m_rdmBuffer.dataLength);
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmGetDMXStartAddress()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else {
if (m_rdm == nullptr) {
putUInt16(m_rdmBuffer.data, 0);
} else {
putUInt16(m_rdmBuffer.data, m_rdm->startAddress);
}
m_rdmBuffer.dataLength = sizeof(m_rdm->startAddress);
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmGetSupportedParameters()
{
if (m_rdmBuffer.dataLength > 0) {
// Unexpected data
return E120_NR_FORMAT_ERROR;
} else if (m_rdmBuffer.subDev != RDM_ROOT_DEVICE) {
// No sub-devices supported
return E120_NR_SUB_DEVICE_OUT_OF_RANGE;
} else {
m_rdmBuffer.dataLength = 8;
putUInt16(&m_rdmBuffer.data[0], E120_MANUFACTURER_LABEL);
putUInt16(&m_rdmBuffer.data[2], E120_DEVICE_MODEL_DESCRIPTION);
putUInt16(&m_rdmBuffer.data[4], E120_DEVICE_LABEL);
putUInt16(&m_rdmBuffer.data[6], E120_COMMS_STATUS);
if (m_rdm != nullptr) {
for (int n = 0; n < m_rdm->additionalCommandsLength; ++n) {
putUInt16(&m_rdmBuffer.data[m_rdmBuffer.dataLength],
m_rdm->additionalCommands[n]);
m_rdmBuffer.dataLength += 2;
}
}
return NACK_WAS_ACK;
}
}
uint16_t TeensyDmx::rdmCalculateChecksum(uint8_t* data, uint8_t length)
{
uint16_t checksum = 0;
// calculate checksum
for (unsigned int i = 0; i < length; ++i) {
checksum += *data;
++data;
}
return checksum;
}
bool TeensyDmx::isForMe(const byte* id)
{
return (memcmp(id, m_rdm->uid, RDM_UID_LENGTH) == 0);
}
bool TeensyDmx::isForVendor(const byte* id)
{
if (id[0] != m_rdm->uid[0] || id[1] != m_rdm->uid[1])
{
return false;
}
return isForMany(id);
}
bool TeensyDmx::isForAll(const byte* id)
{
for (int i = 0; i < RDM_UID_LENGTH; ++i) {
if (*id != 0xff) {
return false;
}
++id;
}
return true;
}
bool TeensyDmx::isForMany(const byte* id)
{
// Broadcast or vendorcast
for (int i = 2; i < RDM_UID_LENGTH; ++i)
{
if (id[i] != 0xff) {
return false;
}
}
return true;
}
void TeensyDmx::maybeIncrementShortMessage()
{
// RDM message and not complete destination UID
// We only call this when we get a message without a destination UID
// Ensure we don't overflow
if (m_shortMessage < std::numeric_limits<uint16_t>::max()) {
++m_shortMessage;
}
}
void TeensyDmx::maybeIncrementLengthMismatch()
{
// RDM message for me, vendorcast or broadcast where length didn't match message length plus checksum, either too long or too short
// We only call this when we get a message with an invalid length
if (isForMe(m_rdmBuffer.destId) || isForAll(m_rdmBuffer.destId) || isForVendor(m_rdmBuffer.destId)) {
// Ensure we don't overflow
if (m_lengthMismatch < std::numeric_limits<uint16_t>::max()) {
++m_lengthMismatch;
}
}
}
void TeensyDmx::maybeIncrementChecksumFail()
{
// RDM message for me, vendorcast or broadcast where checksum was incorrect
// We only call this when we get an invalid checksum
if (isForMe(m_rdmBuffer.destId) || isForAll(m_rdmBuffer.destId) || isForVendor(m_rdmBuffer.destId)) {
// Ensure we don't overflow
if (m_checksumFail < std::numeric_limits<uint16_t>::max()) {
++m_checksumFail;
}
}
}