/
stageprofi.cpp
251 lines (198 loc) · 6.24 KB
/
stageprofi.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
/*
Q Light Controller Plus
stageprofi.cpp
Copyright (C) Massimo Callegari
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "stageprofi.h"
#include <QString>
#include <QDebug>
Stageprofi::Stageprofi(DMXInterface *interface, quint32 outputLine)
: DMXUSBWidget(interface, outputLine, DEFAULT_OUTPUT_FREQUENCY)
, m_running(false)
{
}
Stageprofi::~Stageprofi()
{
stop();
}
DMXUSBWidget::Type Stageprofi::type() const
{
return DMXUSBWidget::DMX4ALL;
}
bool Stageprofi::checkReply()
{
bool ok = false;
uchar res;
res = iface()->readByte(&ok);
if (ok == false || res != 0x47)
return false;
return true;
/*
for (int i = 0; i < 16; i++)
{
QByteArray reply = ftdi()->read(1);
//qDebug() << Q_FUNC_INFO << "Reply: " << QString::number(reply[0], 16);
if (reply.count() > 0 && reply[0] == 'G')
{
qDebug() << Q_FUNC_INFO << name() << "Good connection.";
return true;
}
}
qWarning() << Q_FUNC_INFO << name() << "got no reply";
return false;
*/
}
bool Stageprofi::sendChannelValue(int channel, uchar value)
{
QByteArray chanMsg;
QString msg;
chanMsg.append(msg.asprintf("C%03dL%03d", channel, value).toUtf8());
return iface()->write(chanMsg);
}
/****************************************************************************
* Open & Close
****************************************************************************/
bool Stageprofi::open(quint32 line, bool input)
{
Q_UNUSED(line)
Q_UNUSED(input)
if (DMXUSBWidget::open() == false)
return false;
QByteArray initSequence;
/* Check connection */
initSequence.append("C?");
if (iface()->write(initSequence) == true)
{
if (checkReply() == false)
qWarning() << Q_FUNC_INFO << name() << "Initialization failed";
}
else
qWarning() << Q_FUNC_INFO << name() << "Initialization failed";
/* set the DMX OUT channels number */
initSequence.clear();
initSequence.append("N511");
if (iface()->write(initSequence) == true)
{
if (checkReply() == false)
qWarning() << Q_FUNC_INFO << name() << "Channels initialization failed";
}
// start the output thread
start();
return true;
}
bool Stageprofi::close(quint32 line, bool input)
{
Q_UNUSED(input)
stop();
return DMXUSBWidget::close(line);
}
QString Stageprofi::uniqueName(ushort line, bool input) const
{
Q_UNUSED(line)
Q_UNUSED(input)
return QString("%1").arg(name());
}
/****************************************************************************
* Name & Serial
****************************************************************************/
QString Stageprofi::additionalInfo() const
{
QString info;
info += QString("<P>");
info += QString("<B>%1:</B> %2 (%3)").arg(QObject::tr("Protocol"))
.arg("DMX4ALL DMX-USB")
.arg(QObject::tr("Output"));
info += QString("<BR>");
info += QString("<B>%1:</B> %2").arg(QObject::tr("Manufacturer"))
.arg(vendor());
info += QString("<BR>");
info += QString("<B>%1:</B> %2").arg(QObject::tr("Serial number"))
.arg(serial());
info += QString("</P>");
return info;
}
/****************************************************************************
* Write universe data
****************************************************************************/
bool Stageprofi::writeUniverse(quint32 universe, quint32 output, const QByteArray& data, bool dataChanged)
{
Q_UNUSED(universe)
Q_UNUSED(output)
if (isOpen() == false)
return false;
if (m_outputLines[0].m_universeData.size() == 0)
{
m_outputLines[0].m_universeData.append(data);
m_outputLines[0].m_universeData.append(DMX_CHANNELS - data.size(), 0);
}
if (dataChanged)
m_outputLines[0].m_universeData.replace(0, data.size(), data);
return true;
}
void Stageprofi::stop()
{
if (isRunning() == true)
{
m_running = false;
wait();
}
}
void Stageprofi::run()
{
qDebug() << "OUTPUT thread started";
QElapsedTimer timer;
m_running = true;
if (m_outputLines[0].m_compareData.size() == 0)
m_outputLines[0].m_compareData.fill(0, 512);
// Wait for device to settle in case the device was opened just recently
usleep(1000);
while (m_running == true)
{
timer.restart();
for (int i = 0; i < m_outputLines[0].m_universeData.length(); i++)
{
char val = m_outputLines[0].m_universeData[i];
if (val == m_outputLines[0].m_compareData[i])
continue;
QByteArray fastTrans;
if (i < 256)
{
fastTrans.append((char)0xE2);
fastTrans.append((char)i);
}
else
{
fastTrans.append((char)0xE3);
fastTrans.append((char)(i - 256));
}
fastTrans.append(val);
if (iface()->write(fastTrans) == false)
{
qWarning() << Q_FUNC_INFO << name() << "will not accept DMX data";
iface()->purgeBuffers();
continue;
}
else
{
m_outputLines[0].m_compareData[i] = val;
if (checkReply() == false)
iface()->purgeBuffers();
}
}
int timetoSleep = m_frameTimeUs - (timer.nsecsElapsed() / 1000);
if (timetoSleep < 0)
qWarning() << "DMX output is running late !";
else
usleep(timetoSleep);
}
qDebug() << "OUTPUT thread terminated";
}