-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDMXPro.cpp
More file actions
276 lines (217 loc) · 6.01 KB
/
DMXPro.cpp
File metadata and controls
276 lines (217 loc) · 6.01 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
/*
* (This version as been slightly modified by lars)
*
* DMXPro.cpp
*
* Created by Andrea Cuius
* The MIT License (MIT)
* Copyright (c) 2014 Nocte Studio Ltd.
*
* www.nocte.co.uk
*
*/
#include "cinder/app/App.h"
#include "cinder/Utilities.h"
#include "cinder/Log.h"
#include <iostream>
#include "dmx/DMXPro.hpp"
using namespace ci;
using namespace app;
using namespace std;
DMXPro::DMXPro(const string& deviceName, DeviceMode mode) : mSerialDeviceName(deviceName)
{
mDeviceMode = mode;
mSerial = nullptr;
mSenderThreadSleepFor = 1000 / DMXPRO_FRAME_RATE;
init();
setZeros();
}
DMXPro::~DMXPro()
{
shutdown(true);
}
void DMXPro::shutdown(bool send_zeros)
{
CI_LOG_I("DMXPro shutting down..");
if (mSerial)
{
if (send_zeros)
setZeros(); // send zeros to all channels
sleep(mSenderThreadSleepFor * 2);
mRunDataThread = false;
if (mDataThread.joinable())
{
CI_LOG_I("thread is joinable");
mDataThread.join();
}
else
CI_LOG_W("cannot join thread!");
mSerial->flush();
mSerial = nullptr;
}
else
{
mDataThread.detach();
}
CI_LOG_I("DMXPro > shutdown!");
}
void DMXPro::init(bool initWithZeros)
{
CI_LOG_I("DMXPro > Initializing device");
initDMX();
initSerial(initWithZeros);
mDataThread = std::thread(&DMXPro::processDMXData, this);
}
void DMXPro::initSerial(bool initWithZeros)
{
if (mSerial)
{
if (initWithZeros)
{
setZeros(); // send zeros to all channels
CI_LOG_V("DMXPro > Init serial with zeros() before disconnect");
sleep(100);
}
mSerial->flush();
mSerial = nullptr;
sleep(50);
}
try
{
Serial::Device dev = Serial::findDeviceByNameContains(mSerialDeviceName);
if (dev.getName() == "")
{
CI_LOG_W("DMXPro > cannot open device, device not found! > " << mSerialDeviceName);
return;
}
mSerial = Serial::create(dev, DMXPRO_BAUD_RATE);
CI_LOG_I("DMXPro > Connected to usb DMX interface: " << dev.getName());
}
catch (...)
{
CI_LOG_E("DMXPro > There was an error initializing the usb DMX device");
mSerial = nullptr;
}
}
void DMXPro::initDMX()
{
for (int i = 0; i < DMXPRO_PACKET_SIZE; i++) // initialize all channels with zeros, data starts from [5]
mDMXPacketOut[i] = 0;
mDMXPacketOut[0] = 0x7E; //DMXPRO_START_MSG; // DMX start delimiter 0x7E
mDMXPacketOut[1] = 0x06; // set message type
mDMXPacketOut[2] = (int)DMXPRO_DATA_SIZE & 0xFF; // Data Length LSB - 0x1
mDMXPacketOut[3] = (static_cast<int>(DMXPRO_DATA_SIZE) >> 8) & 0xFF; // Data Length MSBc - 0x2
mDMXPacketOut[4] = 0x0; // DMX start code
// init DMX data
mDMXPacketOut[517] = 0xE7; // DMX start delimiter 0xE7
// init incoming DMX data
for (size_t k = 0; k < 512; k++)
mDMXDataIn[k] = 0;
}
void DMXPro::processDMXData()
{
CI_LOG_V("DMXPro::processDMXData() start thread");
mRunDataThread = true;
if (mDeviceMode == SENDER)
{
while (mSerial && mRunDataThread)
{
std::unique_lock<std::mutex> dataLock(mDMXDataMutex); // get DMX packet UNIQUE lock
mSerial->writeBytes(mDMXPacketOut, DMXPRO_PACKET_SIZE); // send data
dataLock.unlock(); // unlock data
std::this_thread::sleep_for(std::chrono::milliseconds(mSenderThreadSleepFor));
}
}
else if (mDeviceMode == RECEIVER)
{
unsigned char value = '*'; // set to something different than packet label or start msg
uint32_t packetDataSize;
while (mSerial && mRunDataThread)
{
// wait for start message and label
/*
while ( value != DMXPRO_START_MSG && mSerial->getNumBytesAvailable() > 0 )
{
value = mSerial->readByte();
}
if ( mSerial->getNumBytesAvailable() == 0 || mSerial->readByte() != DMXPRO_RECEIVE_PACKET_LABEL )
{
value = '*';
continue;
}
*/
while (mRunDataThread && value != DMXPRO_RECEIVE_PACKET_LABEL)
{
while (mRunDataThread && value != DMXPRO_START_MSG)
{
if (mSerial->getNumBytesAvailable() > 0)
value = mSerial->readByte();
}
if (mSerial->getNumBytesAvailable() > 0)
value = mSerial->readByte();
}
// read header
if (mSerial->getNumBytesAvailable() < 2)
continue;
packetDataSize = mSerial->readByte(); // LSB
packetDataSize += static_cast<uint32_t>(mSerial->readByte()) << 8; // MSB
// Check Length is not greater than allowed
if (packetDataSize <= 514) // dmx data + 2 start zeros
{
// Read the actual Response Data
mSerial->readAvailableBytes(mDMXPacketIn, packetDataSize);
// finally check the end code
if (mSerial->getNumBytesAvailable() > 0 && mSerial->readByte() == DMXPRO_END_MSG)
{
// valid packet, parse DMX data
// the first 2 bytes are 0(by specs there should only be 1 zero, not sure where the other one comes from!)
std::unique_lock<std::mutex> dataLock(mDMXDataMutex);
for (size_t k = 2; k < packetDataSize; k++)
{
mDMXDataIn[k - 2] = mDMXPacketIn[k];
}
dataLock.unlock();
}
else // invalid packet, reset
value = '*';
}
else // invalid packet, reset
value = '*';
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
}
mRunDataThread = false;
CI_LOG_V("DMXPro > sendDMXData() thread exited!");
}
void DMXPro::setValue(int value, int channel)
{
if (channel <= 0 || channel > 512)
{
CI_LOG_W("DMXPro > invalid DMX channel: " << channel);
return;
}
// DMX channels start form byte [5] and end at byte [DMXPRO_PACKET_SIZE-2], last byte is EOT(0xE7)
value = math<int>::clamp(value, 0, 255);
std::unique_lock<std::mutex> dataLock(mDMXDataMutex); // get DMX packet UNIQUE lock
mDMXPacketOut[4 + channel] = value; // update value
dataLock.unlock(); // unlock mutex
}
size_t DMXPro::getValue(int channel)
{
if (channel <= 0 || channel > 512)
{
CI_LOG_W("DMXPro > invalid DMX channel: " << channel);
return 0;
}
size_t val;
std::unique_lock<std::mutex> dataLock(mDMXDataMutex);
val = mDMXDataIn[channel];
dataLock.unlock();
return val;
}
void DMXPro::setZeros()
{
for (int i = 5; i < DMXPRO_PACKET_SIZE - 2; i++)
// DMX channels start form byte [5] and end at byte [DMXPRO_PACKET_SIZE-2], last byte is EOT(0xE7)
mDMXPacketOut[i] = 0;
}