177 lines
4.8 KiB
C++
177 lines
4.8 KiB
C++
//LightController.cpp
|
|
#include "LightController.h"
|
|
#include <QtSerialPort/QSerialPort>
|
|
#include <QtSerialPort/QSerialPortInfo>
|
|
|
|
namespace {
|
|
QChar expectedBrightnessAck(LightController::Channel ch)
|
|
{
|
|
switch (ch) {
|
|
case LightController::CH1: return 'A';
|
|
case LightController::CH2: return 'B';
|
|
case LightController::ALL: return 'W';
|
|
}
|
|
return QChar();
|
|
}
|
|
}
|
|
|
|
LightController::LightController(QObject* parent)
|
|
: QObject(parent)
|
|
, m_serial(new QSerialPort(this))
|
|
{
|
|
}
|
|
|
|
LightController::~LightController()
|
|
{
|
|
close();
|
|
}
|
|
|
|
bool LightController::open(const QString& portName)
|
|
{
|
|
if (m_serial->isOpen())
|
|
m_serial->close();
|
|
|
|
m_serial->setPortName(portName);
|
|
m_serial->setBaudRate(QSerialPort::Baud19200);
|
|
m_serial->setDataBits(QSerialPort::Data8);
|
|
m_serial->setStopBits(QSerialPort::OneStop);
|
|
m_serial->setParity(QSerialPort::NoParity);
|
|
m_serial->setFlowControl(QSerialPort::NoFlowControl);
|
|
m_serial->clearError();
|
|
|
|
if (!m_serial->open(QIODevice::ReadWrite)) {
|
|
emit errorOccurred(QString("Cannot open %1: %2")
|
|
.arg(portName).arg(m_serial->errorString()));
|
|
return false;
|
|
}
|
|
|
|
// The light controller uses a simple 3-wire command protocol. Keep hardware
|
|
// handshake lines inactive; asserting RTS can prevent some USB-RS485
|
|
// adapters from receiving the ACK byte.
|
|
m_serial->setDataTerminalReady(false);
|
|
m_serial->setRequestToSend(false);
|
|
|
|
m_serial->readAll();
|
|
emit connected();
|
|
return true;
|
|
}
|
|
|
|
void LightController::close()
|
|
{
|
|
if (m_serial->isOpen()) {
|
|
m_serial->close();
|
|
emit disconnected();
|
|
}
|
|
}
|
|
|
|
bool LightController::isOpen() const
|
|
{
|
|
return m_serial->isOpen();
|
|
}
|
|
|
|
QString LightController::buildBrightnessCmd(Channel ch, int value)
|
|
{
|
|
value = qBound(0, value, 255);
|
|
QChar yCode;
|
|
switch (ch) {
|
|
case CH1: yCode = 'A'; break;
|
|
case CH2: yCode = 'B'; break;
|
|
case ALL: yCode = 'W'; break;
|
|
}
|
|
return QString("S%1%2#").arg(yCode).arg(value, 4, 10, QChar('0'));
|
|
}
|
|
|
|
bool LightController::setBrightness(Channel ch, int value)
|
|
{
|
|
QString cmd = buildBrightnessCmd(ch, value);
|
|
QString resp;
|
|
if (!sendCommand(cmd, resp))
|
|
return false;
|
|
const QString trimmed = resp.trimmed();
|
|
const QChar expected = expectedBrightnessAck(ch);
|
|
if (trimmed == "?") {
|
|
emit errorOccurred(QString("Brightness command rejected: %1 RX:%2").arg(cmd, trimmed));
|
|
return false;
|
|
}
|
|
if (!trimmed.isEmpty() && trimmed != "(no response)" && expected.isLetter()
|
|
&& !trimmed.startsWith(expected, Qt::CaseInsensitive)) {
|
|
emit errorOccurred(QString("Unexpected brightness response: %1 RX:%2 expected:%3")
|
|
.arg(cmd, trimmed, QString(expected)));
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool LightController::setBrightnessValue(int channel, int value)
|
|
{
|
|
Channel ch = ALL;
|
|
if (channel == 0)
|
|
ch = CH1;
|
|
else if (channel == 1)
|
|
ch = CH2;
|
|
return setBrightness(ch, value);
|
|
}
|
|
|
|
bool LightController::setAlwaysOn()
|
|
{
|
|
QString resp;
|
|
return sendCommand("SL1111#", resp);
|
|
}
|
|
|
|
bool LightController::setAlwaysOff()
|
|
{
|
|
QString resp;
|
|
return sendCommand("SM0000#", resp);
|
|
}
|
|
|
|
bool LightController::softTrigger(Channel ch)
|
|
{
|
|
QString xxxx;
|
|
switch (ch) {
|
|
case CH1: xxxx = "1000"; break;
|
|
case CH2: xxxx = "0100"; break;
|
|
case ALL: xxxx = "1100"; break;
|
|
}
|
|
QString resp;
|
|
return sendCommand(QString("SK%1#").arg(xxxx), resp);
|
|
}
|
|
|
|
bool LightController::sendCommand(const QString& cmd, QString& response)
|
|
{
|
|
if (!m_serial->isOpen()) {
|
|
emit errorOccurred("Serial port not open");
|
|
return false;
|
|
}
|
|
|
|
m_serial->readAll();
|
|
m_serial->clear(QSerialPort::Input);
|
|
QByteArray data = cmd.toLatin1();
|
|
if (m_serial->write(data) != data.size()) {
|
|
emit errorOccurred(QString("Write failed: %1 (%2)").arg(cmd, m_serial->errorString()));
|
|
return false;
|
|
}
|
|
m_serial->flush();
|
|
if (!m_serial->waitForBytesWritten(100)) {
|
|
response = "(write timeout)";
|
|
emit commandSent(cmd, response);
|
|
emit errorOccurred(QString("Write timeout: %1 (%2)").arg(cmd, m_serial->errorString()));
|
|
return false;
|
|
}
|
|
|
|
if (m_serial->waitForReadyRead(180)) {
|
|
QByteArray resp = m_serial->readAll();
|
|
while (m_serial->waitForReadyRead(30))
|
|
resp += m_serial->readAll();
|
|
response = QString::fromLatin1(resp).trimmed();
|
|
emit commandSent(cmd, response);
|
|
return true;
|
|
}
|
|
|
|
response = "(no response)";
|
|
emit commandSent(cmd, response);
|
|
// This controller applies commands without returning an ACK on some
|
|
// USB/serial adapters. A completed write is therefore the success
|
|
// criterion; a missing optional response is kept in the log only.
|
|
return true;
|
|
}
|