Update ws2812fx plugin

master
Michael Zanetti 2019-09-19 23:42:30 +02:00
parent 7b4a71a13a
commit 0d0d389166
2 changed files with 232 additions and 239 deletions

View File

@ -44,16 +44,18 @@ DevicePluginWs2812fx ::DevicePluginWs2812fx ()
{
}
Device::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *device)
void DevicePluginWs2812fx::setupDevice(DeviceSetupInfo *info)
{
if (device->deviceClassId() == ws2812fxDeviceClassId) {
Device *device = info->device();
QString interface = device->paramValue(ws2812fxDeviceSerialPortParamTypeId).toString();
if (!m_usedInterfaces.contains(interface)) {
if (m_usedInterfaces.contains(interface)) {
info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("This serial port is already used."));
return;
}
QSerialPort *serialPort = new QSerialPort(interface, this);
if(!serialPort)
return Device::DeviceSetupStatusFailure;
serialPort->setBaudRate(115200);
serialPort->setDataBits(QSerialPort::DataBits::Data8);
@ -64,7 +66,7 @@ Device::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *device)
if (!serialPort->open(QIODevice::ReadWrite)) {
qCWarning(dcWs2812fx()) << "Could not open serial port" << interface << serialPort->errorString();
serialPort->deleteLater();
return Device::DeviceSetupStatusFailure;
return info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error opening serial port."));
}
connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError)));
@ -82,42 +84,37 @@ Device::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *device)
connect(m_reconnectTimer, &QTimer::timeout, this, &DevicePluginWs2812fx::onReconnectTimer);
}
} else {
return Device::DeviceSetupStatusFailure;
}
return Device::DeviceSetupStatusSuccess;
}
return Device::DeviceSetupStatusFailure;
info->finish(Device::DeviceErrorNoError);
}
Device::DeviceError DevicePluginWs2812fx::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
void DevicePluginWs2812fx::discoverDevices(DeviceDiscoveryInfo *info)
{
Q_UNUSED(params)
// Create the list of available serial interfaces
QList<DeviceDescriptor> deviceDescriptors;
Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
qCDebug(dcWs2812fx()) << "Found serial port:" << port.portName();
QString description = port.manufacturer() + " " + port.description();
DeviceDescriptor descriptor(deviceClassId, port.portName(), description);
DeviceDescriptor descriptor(info->deviceClassId(), port.portName(), description);
foreach (Device *existingDevice, myDevices().filterByParam(ws2812fxDeviceSerialPortParamTypeId, port.portName())) {
descriptor.setDeviceId(existingDevice->id());
}
ParamList parameters;
parameters.append(Param(ws2812fxDeviceSerialPortParamTypeId, port.portName()));
descriptor.setParams(parameters);
deviceDescriptors.append(descriptor);
info->addDeviceDescriptor(descriptor);
}
emit devicesDiscovered(deviceClassId, deviceDescriptors);
return Device::DeviceErrorAsync;
info->finish(Device::DeviceErrorNoError);
}
Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Action &action)
void DevicePluginWs2812fx::executeAction(DeviceActionInfo *info)
{
if (device->deviceClassId() == ws2812fxDeviceClassId ) {
Device *device = info->device();
Action action = info->action();
QByteArray command;
if (action.actionTypeId() == ws2812fxPowerActionTypeId) {
@ -128,7 +125,7 @@ Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Ac
command.append("0");
}
command.append("\r\n");
return sendCommand(device, action.id(), command, CommandType::Brightness);
return sendCommand(info, command, CommandType::Brightness);
}
if (action.actionTypeId() == ws2812fxBrightnessActionTypeId) {
@ -136,7 +133,7 @@ Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Ac
command.append("b ");
command.append(action.param(ws2812fxBrightnessActionBrightnessParamTypeId).value().toString());
command.append("\r\n");
return sendCommand(device, action.id(), command, CommandType::Brightness);
return sendCommand(info, command, CommandType::Brightness);
}
if (action.actionTypeId() == ws2812fxSpeedActionTypeId) {
@ -144,7 +141,7 @@ Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Ac
command.append("s ");
command.append(action.param(ws2812fxSpeedActionSpeedParamTypeId).value().toString());
command.append("\r\n");
return sendCommand(device, action.id(), command, CommandType::Speed);
return sendCommand(info, command, CommandType::Speed);
}
if (action.actionTypeId() == ws2812fxColorActionTypeId) {
@ -154,7 +151,7 @@ Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Ac
command.append("c ");
command.append(QString(color.name()).remove("#"));
command.append("\r\n");
return sendCommand(device, action.id(), command, CommandType::Color);
return sendCommand(info, command, CommandType::Color);
}
if (action.actionTypeId() == ws2812fxColorTemperatureActionTypeId) {
@ -166,7 +163,7 @@ Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Ac
command.append("c ");
command.append(QString(color.name()).remove("#"));
command.append("\r\n");
return sendCommand(device, action.id(), command, CommandType::Color);
return sendCommand(info, command, CommandType::Color);
}
if (action.actionTypeId() == ws2812fxEffectModeActionTypeId) {
@ -291,11 +288,8 @@ Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Ac
command.append(QString::number(FX_MODE_CUSTOM_3));
}
command.append("\r\n");
return sendCommand(device, action.id(), command, CommandType::Mode);
return sendCommand(info, command, CommandType::Mode);
}
return Device::DeviceErrorActionTypeNotFound;
}
return Device::DeviceErrorDeviceClassNotFound;
}
@ -328,7 +322,7 @@ void DevicePluginWs2812fx::onReadyRead()
if (data.contains("mode")) {
if (m_pendingActions.contains(CommandType::Mode)) {
emit actionExecutionFinished(m_pendingActions.value(CommandType::Mode), Device::DeviceErrorNoError);
m_pendingActions.take(CommandType::Mode)->finish(Device::DeviceErrorNoError);
}
QString mode = data.split('-').at(1);
mode.remove(0, 1);
@ -338,7 +332,7 @@ void DevicePluginWs2812fx::onReadyRead()
}
if (data.contains("brightness")) {
if (m_pendingActions.contains(CommandType::Brightness)) {
emit actionExecutionFinished(m_pendingActions.value(CommandType::Brightness), Device::DeviceErrorNoError);
m_pendingActions.take(CommandType::Brightness)->finish(Device::DeviceErrorNoError);
}
QString rawBrightness = data.split(':').at(1);
rawBrightness.remove(" ");
@ -355,7 +349,7 @@ void DevicePluginWs2812fx::onReadyRead()
}
if (data.contains("speed")) {
if (m_pendingActions.contains(CommandType::Speed)) {
emit actionExecutionFinished(m_pendingActions.value(CommandType::Speed), Device::DeviceErrorNoError);
m_pendingActions.take(CommandType::Speed)->finish(Device::DeviceErrorNoError);
}
QString rawSpeed = data.split(':').at(1);
rawSpeed.remove(" ");
@ -367,7 +361,7 @@ void DevicePluginWs2812fx::onReadyRead()
}
if (data.contains("color")) {
if (m_pendingActions.contains(CommandType::Color)) {
emit actionExecutionFinished(m_pendingActions.value(CommandType::Color), Device::DeviceErrorNoError);
m_pendingActions.take(CommandType::Color)->finish(Device::DeviceErrorNoError);
}
QString rawColor = data.split(':').at(1);
rawColor.remove(" ");
@ -411,16 +405,15 @@ void DevicePluginWs2812fx::onSerialError(QSerialPort::SerialPortError error)
}
}
Device::DeviceError DevicePluginWs2812fx::sendCommand(Device* device, ActionId actionId, const QByteArray &command, CommandType commandType)
void DevicePluginWs2812fx::sendCommand(DeviceActionInfo *info, const QByteArray &command, CommandType commandType)
{
qDebug(dcWs2812fx()) << "Sending command" << command;
QSerialPort *serialPort = m_serialPorts.value(device);
QSerialPort *serialPort = m_serialPorts.value(info->device());
if (!serialPort)
return Device::DeviceErrorDeviceNotFound;
return info->finish(Device::DeviceErrorDeviceNotFound);
if (serialPort->write(command) != command.length()) {
qCWarning(dcWs2812fx) << "Error writing to serial port";
return Device::DeviceErrorHardwareFailure;
return info->finish(Device::DeviceErrorHardwareFailure);
}
m_pendingActions.insert(commandType, actionId);
return Device::DeviceErrorAsync;
m_pendingActions.insert(commandType, info);
}

View File

@ -101,10 +101,10 @@ class DevicePluginWs2812fx : public DevicePlugin
public:
explicit DevicePluginWs2812fx();
Device::DeviceSetupStatus setupDevice(Device *device) override;
void setupDevice(DeviceSetupInfo *info) override;
void deviceRemoved(Device *device) override;
Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
Device::DeviceError executeAction(Device *device, const Action &action) override;
void discoverDevices(DeviceDiscoveryInfo *info) override;
void executeAction(DeviceActionInfo *info) override;
private:
enum CommandType {
@ -116,10 +116,10 @@ private:
QHash<Device *, QSerialPort *> m_serialPorts;
QList<QString> m_usedInterfaces;
QHash<CommandType, ActionId> m_pendingActions;
QHash<CommandType, DeviceActionInfo*> m_pendingActions;
QTimer *m_reconnectTimer = nullptr;
Device::DeviceError sendCommand(Device *device, ActionId actionId, const QByteArray &command, CommandType commandType);
void sendCommand(DeviceActionInfo *info, const QByteArray &command, CommandType commandType);
private slots:
void onReadyRead();