Skip to content

Commit

Permalink
plugins/dmxusb: eurolite pro makes use of async tx
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallegari committed Aug 9, 2018
1 parent 03ef9b2 commit 4cbacdd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 21 deletions.
83 changes: 66 additions & 17 deletions plugins/dmxusb/src/euroliteusbdmxpro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@

EuroliteUSBDMXPro::EuroliteUSBDMXPro(DMXInterface *interface, quint32 outputLine)
: DMXUSBWidget(interface, outputLine)
, m_running(false)
{
}

EuroliteUSBDMXPro::~EuroliteUSBDMXPro()
{
stop();

#ifdef QTSERIAL
if (isOpen())
DMXUSBWidget::close();
Expand Down Expand Up @@ -124,6 +127,10 @@ bool EuroliteUSBDMXPro::open(quint32 line, bool input)
return false;
}
#endif

// start the output thread
start();

return true;
}

Expand All @@ -132,6 +139,8 @@ bool EuroliteUSBDMXPro::close(quint32 line, bool input)
Q_UNUSED(line)
Q_UNUSED(input)

stop();

#ifdef QTSERIAL
if (isOpen())
return DMXUSBWidget::close();
Expand Down Expand Up @@ -190,30 +199,70 @@ bool EuroliteUSBDMXPro::writeUniverse(quint32 universe, quint32 output, const QB
return false;
#endif

QByteArray request(data);
request.prepend(char(EUROLITE_USB_DMX_PRO_DMX_ZERO)); // DMX start code (Which constitutes the + 1 below)
request.prepend(((data.size() + 1) >> 8) & 0xff); // Data length MSB
request.prepend((data.size() + 1) & 0xff); // Data length LSB
request.prepend(EUROLITE_USB_DMX_PRO_SEND_DMX_RQ); // Send request
request.prepend(EUROLITE_USB_DMX_PRO_START_OF_MSG); // Start byte
request.append(EUROLITE_USB_DMX_PRO_END_OF_MSG); // Stop byte
if (m_outputLines[0].m_universeData.size() == 0)
m_outputLines[0].m_universeData.append(data);
else
m_outputLines[0].m_universeData.replace(0, data.size(), data);

return true;
}

void EuroliteUSBDMXPro::stop()
{
if (isRunning() == true)
{
m_running = false;
wait();
}
}

void EuroliteUSBDMXPro::run()
{
qDebug() << "OUTPUT thread started";
QElapsedTimer timer;
QByteArray request;

// One "official" DMX frame can take (1s/44Hz) = 23ms
int frameTimeUs = (int) (floor(((double)1000 / m_frequency) + (double)0.5)) * 1000;

m_running = true;
while (m_running == true)
{
timer.restart();

int dataLen = m_outputLines[0].m_universeData.length();
if (dataLen == 0)
goto framesleep;

request.clear();
request.append(EUROLITE_USB_DMX_PRO_START_OF_MSG); // Start byte
request.append(EUROLITE_USB_DMX_PRO_SEND_DMX_RQ); // Send request
request.append((dataLen + 1) & 0xff); // Data length LSB
request.append(((dataLen + 1) >> 8) & 0xff); // Data length MSB
request.append(char(EUROLITE_USB_DMX_PRO_DMX_ZERO)); // DMX start code (Which constitutes the + 1 below)
request.append(m_outputLines[0].m_universeData);
request.append(EUROLITE_USB_DMX_PRO_END_OF_MSG); // Stop byte

#ifdef QTSERIAL
if (interface()->write(request) == false)
if (interface()->write(request) == false)
#else
if (m_file.write(request) == false)
if (m_file.write(request) == false)
#endif
{
qWarning() << Q_FUNC_INFO << name() << "will not accept DMX data";
{
qWarning() << Q_FUNC_INFO << name() << "will not accept DMX data";
#ifdef QTSERIAL
interface()->purgeBuffers();
interface()->purgeBuffers();
#endif
return false;
}
else
{
return true;
}
framesleep:
int timetoSleep = frameTimeUs - (timer.nsecsElapsed() / 1000);
if (timetoSleep < 0)
qWarning() << "DMX output is running late !";
else
usleep(timetoSleep);
}

qDebug() << "OUTPUT thread terminated";
}


12 changes: 9 additions & 3 deletions plugins/dmxusb/src/euroliteusbdmxpro.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#define EUROLITE_USB_DMX_PRO_START_OF_MSG char(0x7E)
#define EUROLITE_USB_DMX_PRO_END_OF_MSG char(0xE7)

class EuroliteUSBDMXPro : public DMXUSBWidget
class EuroliteUSBDMXPro : public QThread, public DMXUSBWidget
{
/************************************************************************
* Initialization
Expand Down Expand Up @@ -60,14 +60,20 @@ class EuroliteUSBDMXPro : public DMXUSBWidget
/** @reimp */
bool writeUniverse(quint32 universe, quint32 output, const QByteArray& data);

protected:
/** Stop the writer thread */
void stop();

/** DMX writer thread worker method */
void run();

private:
QString getDeviceName();

private:
/** File handle for /dev/ttyACMx */
QFile m_file;

QByteArray m_universe;
bool m_running;
};

#endif
4 changes: 3 additions & 1 deletion plugins/dmxusb/src/nanodmx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ NanoDMX::NanoDMX(DMXInterface *interface, quint32 outputLine)

NanoDMX::~NanoDMX()
{
stop();

#ifdef QTSERIAL
if (isOpen())
DMXUSBWidget::close();
Expand Down Expand Up @@ -212,7 +214,7 @@ bool NanoDMX::close(quint32 line, bool input)
m_file.close();
#endif

return DMXUSBWidget::close(line);
return true;
}

QString NanoDMX::uniqueName(ushort line, bool input) const
Expand Down

0 comments on commit 4cbacdd

Please sign in to comment.