Repository navigation
Expand file tree
/
Copy pathSparkFunDMX.cpp
More file actions
166 lines (133 loc) · 4.88 KB
/
Copy pathSparkFunDMX.cpp
File metadata and controls
166 lines (133 loc) · 4.88 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
/******************************************************************************
SparkFunDMX.h
Arduino Library for the SparkFun ESP32 LED to DMX Shield
Dryw Wade @ SparkFun Electronics
10/3/2023
Development environment specifics:
Arduino IDE 2.2.1
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact [email protected].
Distributed as-is; no warranty is given.
******************************************************************************/
/* ----- LIBRARIES ----- */
#include "SparkFunDMX.h"
void SparkFunDMX::begin(HardwareSerial& port, uint8_t enPin, uint16_t numChannels)
{
// Store serial stream port
_dmxSerial = &port;
// Store enable pin
_enPin = enPin;
// Store number of requested channels, plus 1 for channel 0
_numChannels = numChannels + 1;
// Ensure number of channels is not above the limit
if(_numChannels > DMX_MAX_CHANNELS)
_numChannels = DMX_MAX_CHANNELS;
memset(_dmxBuffer, 0, _numChannels);
// Configure enable pin, default to reading
pinMode(_enPin, OUTPUT);
_comDir = DMX_WRITE_DIR;
setComDir(DMX_READ_DIR);
_dataAvailable = false;
}
void SparkFunDMX::setComDir(bool comDir)
{
// No need to do anything if this direction is already set
if(_comDir == comDir)
return;
// Store communication direction
_comDir = comDir;
// Flush serial buffer contents
_dmxSerial->flush(false);
if(comDir == DMX_WRITE_DIR)
{
// Enable output
digitalWrite(_enPin, HIGH);
}
else // DMX_READ_DIR
{
// Disable output
digitalWrite(_enPin, LOW);
}
}
void SparkFunDMX::writeBytes(uint8_t* data, uint16_t numBytes, uint16_t startChannel)
{
// Copy data into buffer
uint8_t* startPtr = _dmxBuffer + startChannel;
memcpy(startPtr, data, numBytes);
}
void SparkFunDMX::writeByte(uint8_t data, uint16_t channel)
{
// Store data
_dmxBuffer[channel] = data;
}
void SparkFunDMX::readBytes(uint8_t* data, uint16_t numBytes, uint16_t startChannel)
{
// Clear flag, this is now old data
_dataAvailable = false;
// Copy data into buffer
uint8_t* startPtr = _dmxBuffer + startChannel;
memcpy(data, startPtr, numBytes);
}
uint8_t SparkFunDMX::readByte(uint16_t channel)
{
// Clear flag, this is now old data
_dataAvailable = false;
// Return requested data
return _dmxBuffer[channel];
}
bool SparkFunDMX::dataAvailable()
{
return _dataAvailable;
}
bool SparkFunDMX::update()
{
if(_comDir == DMX_WRITE_DIR)
{
// We need to send a break signal to indicate the start of the message.
// Arduino doesn't really have a way to actually send a break signal, so
// this is a hacky solution: the baud rate is reduced such that sending
// a zero byte creates a sufficiently long pulse to simulate a break
// Reduce baud rate
uint32_t breakBaud = 1000000 * 8 / DMX_BREAK_DURATION_MICROS;
_dmxSerial->updateBaudRate(breakBaud);
// Send a zero at this new baud rate
_dmxSerial->write(0);
_dmxSerial->flush();
// Return baud rate to original value
_dmxSerial->updateBaudRate(DMX_BAUD);
// Send message
_dmxSerial->write(_dmxBuffer, _numChannels);
// Success
return true;
}
// else _comDir == DMX_READ_DIR
// Check if we've received the amount of data expected, +1 for break signal
else if(_dmxSerial->available() >= (_numChannels + 1))
{
// We need to detect a break signal indicating the start of the message.
// Arduino doesn't really have a way to actually read a break signal, it
// will instead appear as an extra zero byte at the start. This has the
// risk of not synchronizing properly, but it should be resolved by
// calling update() freuently enough (ie. faster than messages are sent)
// and flushing the RX buffer if not synced. We can do one other check:
// after the break signal, channel 0 is sent, which should always have a
// value of zero
// Read out break signal (hopefully!) and peek at channel 0 (hopefully!)
if((_dmxSerial->read() != 0) || (_dmxSerial->peek() != 0))
{
// If we get here, then we're not synced properly. We can try to
// flush out all the data and hope the next time is synced
_dmxSerial->flush(false);
return false;
}
// We're probably synced! Read data into buffer
_dmxSerial->read(_dmxBuffer, _numChannels);
// Set flag indicating we have new data
_dataAvailable = true;
// Success
return true;
}
// Something went wrong (most likely haven't received all the bytes yet)
return false;
}