forked from pixelmatix/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmx.h
169 lines (128 loc) · 3.21 KB
/
dmx.h
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
/********************************************
dmx.h - DMX interface to allow DMX control of
key functions
last update: 15Jul2021
coded to use SERIAL1 on digital pins 0 & 1
uses 3 channels:
0 - display on/off: 0 to 127 = off, > 128 = on
1 - select pattern every 10 counts: 0 to numPatterns
2 - options, so far experimental
*** Needs cleanup / optimization ***
***********************************************/
#ifdef USE_DMX
#pragma once
#include <TeensyDMX.h>
namespace teensydmx = ::qindesign::teensydmx;
// number of DMX addresses to listen for
#define DMX_SIZE 3
// prototypes
void dmxInit();
void checkDMX();
void updateDMX();
void printDmxBuffer();
// delay between updates to minimize impact
#define DMX_DELAY 2000
// dmx address and number of channels to monitor
extern uint8_t dmxAddress;
const uint8_t numDmxChans = DMX_SIZE;
// globals
uint8_t buff[DMX_SIZE] = { 0 };
uint8_t dmxValues[DMX_SIZE] = { 0 };
uint32_t lastUpdate = 0;
// Create the DMX receiver on Serial1 (pins 1 & 2)
// requires an RS-485 convertor to interface to DMX bus
teensydmx::Receiver dmxRx{Serial1};
// initialize dms
void dmxInit()
{
if (dmxAddress == 0)
dmxAddress = DMX_ADDRESS;
// configure the RS485 Transmit/Receive mode to receive
pinMode(DMX_RXTX_PIN, OUTPUT);
digitalWrite(DMX_RXTX_PIN, LOW);
// Start the receiver
dmxRx.begin();
Serial.println("dmx started");
// read and discard first dmx packet
delay(1000);
dmxRx.readPacket(buff, 0, DMX_SIZE);
lastUpdate = millis();
}
// check for dmx commands targeted for this device
void checkDMX()
{
if (lastUpdate - millis() > DMX_DELAY)
{
// just read data for this device
int bytes = dmxRx.readPacket(buff, dmxAddress, numDmxChans);
// did we get data?
if (bytes == numDmxChans)
{
// did dmx packet change?
if (memcmp(buff, dmxValues, numDmxChans) != 0)
{
if (memcpy(dmxValues, buff, numDmxChans) != 0)
{
// ok, now update display
updateDMX();
}
else
Serial.println("error copying buffer");
}
}
else
{
if (dmxDebug)
{
if (bytes < 1)
Serial.println("No DMX Data");
else
{
Serial.print("bytes read = ");
Serial.println(bytes);
}
}
}
}
}
void updateDMX()
{
printValue("dmx base addr", dmxAddress);
printValue("addr 0", dmxValues[0]);
printValue("addr 1", dmxValues[1]);
printValue("addr 2", dmxValues[2]);
// on/off control channel 0
uint8_t onOff = dmxValues[0];
if (onOff < 128)
{
// turn off
pattern = 0;
printValue("dmx pattern off", pattern);
}
else
// select pattern
{
// increment pattern for every 10 dmx counts
uint8_t p = dmxValues[1] / 10;
if (p < numPatterns)
{
pattern = p;
printValue("pattern set", pattern);
}
}
// experimental control stuff
simAudio = false;
if (dmxValues[2] < 32)
rectColor = WHITE;
else if (dmxValues[2] < 64)
rectColor = RED;
else if (dmxValues[2] < 96)
rectColor = GREEN;
else if (dmxValues[2] < 128)
rectColor = BLUE;
else if (dmxValues[2] < 160)
rectColor = PURPLE;
else
simAudio = true;
}
#endif