From 1fb2f6be0b1c24e494992f914cb5bd91412fa0d1 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Wed, 21 Sep 2016 23:03:01 +0200 Subject: [PATCH 1/5] initial version of h801 led device --- libsrc/leddevice/CMakeLists.txt | 2 + libsrc/leddevice/LedDeviceFactory.cpp | 4 +- libsrc/leddevice/LedDeviceSchemas.qrc | 1 + libsrc/leddevice/LedDeviceUdpH801.cpp | 67 +++++++++++++++++++++++ libsrc/leddevice/LedDeviceUdpH801.h | 59 ++++++++++++++++++++ libsrc/leddevice/schemas/schema-h801.json | 27 +++++++++ 6 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 libsrc/leddevice/LedDeviceUdpH801.cpp create mode 100644 libsrc/leddevice/LedDeviceUdpH801.h create mode 100644 libsrc/leddevice/schemas/schema-h801.json diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index 4ff82221..a0b1c329 100755 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -35,6 +35,7 @@ SET(Leddevice_HEADERS ${CURRENT_SOURCE_DIR}/LedDeviceSedu.h ${CURRENT_SOURCE_DIR}/LedDeviceFile.h ${CURRENT_SOURCE_DIR}/LedDeviceUdpRaw.h + ${CURRENT_SOURCE_DIR}/LedDeviceUdpH801.h ${CURRENT_SOURCE_DIR}/LedDeviceUdpE131.h ${CURRENT_SOURCE_DIR}/ProviderUdp.h ${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h @@ -62,6 +63,7 @@ SET(Leddevice_SOURCES ${CURRENT_SOURCE_DIR}/LedDeviceFile.cpp ${CURRENT_SOURCE_DIR}/LedDeviceFadeCandy.cpp ${CURRENT_SOURCE_DIR}/LedDeviceUdpRaw.cpp + ${CURRENT_SOURCE_DIR}/LedDeviceUdpH801.cpp ${CURRENT_SOURCE_DIR}/LedDeviceUdpE131.cpp ${CURRENT_SOURCE_DIR}/ProviderUdp.cpp ${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index 5a6984e6..586c8b81 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -46,6 +46,7 @@ #include "LedDeviceAtmo.h" #include "LedDeviceAdalightApa102.h" #include "LedDeviceAtmoOrb.h" +#include "LedDeviceUdpH801.h" #ifdef ENABLE_WS2812BPWM #include "LedDeviceWS2812b.h" @@ -101,7 +102,8 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) #endif LedDevice::addToDeviceMap("philipshue", LedDevicePhilipsHue::construct); LedDevice::addToDeviceMap("atmoorb", LedDeviceAtmoOrb::construct); - + LedDevice::addToDeviceMap("h801", LedDeviceUdpH801::construct); + // direct usb LedDevice::addToDeviceMap("hyperion-usbasp", LedDeviceHyperionUsbasp::construct); LedDevice::addToDeviceMap("rawhid", LedDeviceRawHID::construct); diff --git a/libsrc/leddevice/LedDeviceSchemas.qrc b/libsrc/leddevice/LedDeviceSchemas.qrc index e35a8789..ce04eb2d 100644 --- a/libsrc/leddevice/LedDeviceSchemas.qrc +++ b/libsrc/leddevice/LedDeviceSchemas.qrc @@ -27,5 +27,6 @@ schemas/schema-ws2801.json schemas/schema-ws2812spi.json schemas/schema-ws281x.json + schemas/schema-h801.json diff --git a/libsrc/leddevice/LedDeviceUdpH801.cpp b/libsrc/leddevice/LedDeviceUdpH801.cpp new file mode 100644 index 00000000..b6c9c2b1 --- /dev/null +++ b/libsrc/leddevice/LedDeviceUdpH801.cpp @@ -0,0 +1,67 @@ +// STL includes +#include +#include +#include + +#include + +// hyperion local includes +#include "LedDeviceUdpH801.h" + +LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) : ProviderUdp(deviceConfig) +{ + setConfig(deviceConfig); +} + +bool LedDeviceUdpH801::setConfig(const Json::Value &deviceConfig) +{ + /* The H801 port is fixed */ + _port = 30977; + /* 10ms seems to be a safe default for the wait time */ + _LatchTime_ns = deviceConfig.get("latchtime", 10000000).asInt(); + + _ids.clear(); + for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++) { + QString id(deviceConfig["lightIds"][i].asCString()); + _ids.push_back(id.toInt(nullptr, 16)); + } + + _message = QByteArray(_prefix_size + _colors + _id_size * _ids.size() + _suffix_size, 0x00); + _message[0] = 0xFB; + _message[1] = 0xEB; + + for(int i=0; i<_ids.length(); i++){ + _message[_prefix_size + _colors + i * _id_size + 0] = (_ids[i] >> 0x00) & 0xFF; + _message[_prefix_size + _colors + i * _id_size + 1] = (_ids[i] >> 0x08) & 0xFF; + _message[_prefix_size + _colors + i * _id_size + 2] = (_ids[i] >> 0x10) & 0xFF; + } + + Debug( _log, "H801 using %s:%d", _address.toString().toStdString().c_str() , _port ); + + return true; +} + +LedDevice* LedDeviceUdpH801::construct(const Json::Value &deviceConfig) +{ + return new LedDeviceUdpH801(deviceConfig); +} + +int LedDeviceUdpH801::write(const std::vector &ledValues) +{ + ColorRgb color = ledValues[0]; + _message[_prefix_size + 0] = color.red; + _message[_prefix_size + 1] = color.green; + _message[_prefix_size + 2] = color.blue; + + writeBytes(_message.size(), reinterpret_cast(_message.data())); +} + +int LedDeviceUdpH801::switchOff() +{ + return write(std::vector(_ledCount, ColorRgb{0,0,0})); +} + +LedDeviceUdpH801::~LedDeviceUdpH801() +{ + ProviderUdp::~ProviderUdp(); +} diff --git a/libsrc/leddevice/LedDeviceUdpH801.h b/libsrc/leddevice/LedDeviceUdpH801.h new file mode 100644 index 00000000..56ec8fc4 --- /dev/null +++ b/libsrc/leddevice/LedDeviceUdpH801.h @@ -0,0 +1,59 @@ +#pragma once + +#include +#include +#include + +// STL includes +#include + +// hyperion includes +#include "ProviderUdp.h" + +/// +/// Implementation of the LedDevice interface for sending led colors via udp. +/// +class LedDeviceUdpH801 : public ProviderUdp +{ +protected: + QList _ids; + QByteArray _message; + const int _prefix_size = 2; + const int _colors = 5; + const int _id_size = 3; + const int _suffix_size = 1; + +public: + /// + /// Constructs specific LedDevice + /// + /// @param deviceConfig json device config + /// + LedDeviceUdpH801(const Json::Value &deviceConfig); + + /// + /// Destructor of the LedDevice; closes the output device if it is open + /// + virtual ~LedDeviceUdpH801(); + + /// + /// Sets configuration + /// + /// @param deviceConfig the json device config + /// @return true if success + bool setConfig(const Json::Value &deviceConfig); + + /// constructs leddevice + static LedDevice* construct(const Json::Value &deviceConfig); + + /// + /// Writes the led color values to the led-device + /// + /// @param ledValues The color-value per led + /// @return Zero on succes else negative + /// + virtual int write(const std::vector &ledValues); + + /// Switch the leds off + virtual int switchOff(); +}; diff --git a/libsrc/leddevice/schemas/schema-h801.json b/libsrc/leddevice/schemas/schema-h801.json new file mode 100644 index 00000000..95c574ee --- /dev/null +++ b/libsrc/leddevice/schemas/schema-h801.json @@ -0,0 +1,27 @@ +{ + "type":"object", + "required":true, + "properties":{ + "host" : { + "type": "string", + "title":"Target IP", + "default": "255.255.255.255", + "propertyOrder" : 1 + }, + "port" : { + "type": "integer", + "title":"Port", + "default": 30977, + "propertyOrder" : 2 + }, + "lightIds": { + "type": "array", + "title":"Light ids", + "items" : { + "type" : "string" + }, + "propertyOrder" : 3 + } + }, + "additionalProperties": true +} From da6dfc8ed460e6f58d1d4abace1155e0ca10109d Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Wed, 21 Sep 2016 23:07:14 +0200 Subject: [PATCH 2/5] removed useless destructor --- libsrc/leddevice/LedDeviceUdpH801.cpp | 4 ---- libsrc/leddevice/LedDeviceUdpH801.h | 5 ----- 2 files changed, 9 deletions(-) diff --git a/libsrc/leddevice/LedDeviceUdpH801.cpp b/libsrc/leddevice/LedDeviceUdpH801.cpp index b6c9c2b1..f18ee646 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.cpp +++ b/libsrc/leddevice/LedDeviceUdpH801.cpp @@ -61,7 +61,3 @@ int LedDeviceUdpH801::switchOff() return write(std::vector(_ledCount, ColorRgb{0,0,0})); } -LedDeviceUdpH801::~LedDeviceUdpH801() -{ - ProviderUdp::~ProviderUdp(); -} diff --git a/libsrc/leddevice/LedDeviceUdpH801.h b/libsrc/leddevice/LedDeviceUdpH801.h index 56ec8fc4..78bcd162 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.h +++ b/libsrc/leddevice/LedDeviceUdpH801.h @@ -31,11 +31,6 @@ public: /// LedDeviceUdpH801(const Json::Value &deviceConfig); - /// - /// Destructor of the LedDevice; closes the output device if it is open - /// - virtual ~LedDeviceUdpH801(); - /// /// Sets configuration /// From a86b3a23351511d46d40ca8cd47c7d5f08041bd4 Mon Sep 17 00:00:00 2001 From: redpanther Date: Wed, 21 Sep 2016 23:08:13 +0200 Subject: [PATCH 3/5] add ability for default values in providerudp --- libsrc/leddevice/LedDeviceTpm2net.cpp | 2 +- libsrc/leddevice/LedDeviceUdpRaw.cpp | 2 +- libsrc/leddevice/ProviderUdp.cpp | 17 +++++++++++++---- libsrc/leddevice/ProviderUdp.h | 2 +- libsrc/leddevice/schemas/schema-e131.json | 2 ++ libsrc/leddevice/schemas/schema-tpm2net.json | 14 +++++++++++--- libsrc/leddevice/schemas/schema-udpraw.json | 2 ++ 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/libsrc/leddevice/LedDeviceTpm2net.cpp b/libsrc/leddevice/LedDeviceTpm2net.cpp index 29c7a47b..7940dd0b 100644 --- a/libsrc/leddevice/LedDeviceTpm2net.cpp +++ b/libsrc/leddevice/LedDeviceTpm2net.cpp @@ -24,7 +24,7 @@ LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig) { - ProviderUdp::setConfig(deviceConfig); + ProviderUdp::setConfig(deviceConfig,50200); _LatchTime_ns = deviceConfig.get("latchtime",104000).asInt(); _tpm2_max = deviceConfig.get("max-packet",170).asInt(); return true; diff --git a/libsrc/leddevice/LedDeviceUdpRaw.cpp b/libsrc/leddevice/LedDeviceUdpRaw.cpp index 81cf892f..5dacf209 100644 --- a/libsrc/leddevice/LedDeviceUdpRaw.cpp +++ b/libsrc/leddevice/LedDeviceUdpRaw.cpp @@ -19,7 +19,7 @@ LedDeviceUdpRaw::LedDeviceUdpRaw(const Json::Value &deviceConfig) bool LedDeviceUdpRaw::setConfig(const Json::Value &deviceConfig) { - ProviderUdp::setConfig(deviceConfig); + ProviderUdp::setConfig(deviceConfig,5568); _LatchTime_ns = deviceConfig.get("latchtime",500000).asInt(); return true; diff --git a/libsrc/leddevice/ProviderUdp.cpp b/libsrc/leddevice/ProviderUdp.cpp index 03ac61f0..59a1c0c2 100644 --- a/libsrc/leddevice/ProviderUdp.cpp +++ b/libsrc/leddevice/ProviderUdp.cpp @@ -18,6 +18,7 @@ ProviderUdp::ProviderUdp(const Json::Value &deviceConfig) : LedDevice() , _LatchTime_ns(-1) + , _port(0) { setConfig(deviceConfig); _udpSocket = new QUdpSocket(); @@ -28,16 +29,18 @@ ProviderUdp::~ProviderUdp() _udpSocket->close(); } -bool ProviderUdp::setConfig(const Json::Value &deviceConfig) +bool ProviderUdp::setConfig(const Json::Value &deviceConfig, int defaultPort, std::string defaultHost) { - if (_address.setAddress( QString::fromStdString(deviceConfig["host"].asString()) ) ) + QString host = QString::fromStdString(deviceConfig.get("host",defaultHost).asString()); + + if (_address.setAddress(host) ) { Debug( _log, "Successfully parsed %s as an ip address.", deviceConfig["host"].asString().c_str()); } else { Debug( _log, "Failed to parse %s as an ip address.", deviceConfig["host"].asString().c_str()); - QHostInfo info = QHostInfo::fromName( QString::fromStdString(deviceConfig["host"].asString()) ); + QHostInfo info = QHostInfo::fromName(host); if (info.addresses().isEmpty()) { Debug( _log, "Failed to parse %s as a hostname.", deviceConfig["host"].asString().c_str()); @@ -46,7 +49,13 @@ bool ProviderUdp::setConfig(const Json::Value &deviceConfig) Debug( _log, "Successfully parsed %s as a hostname.", deviceConfig["host"].asString().c_str()); _address = info.addresses().first(); } - _port = deviceConfig["port"].asUInt(); + + _port = deviceConfig.get("port", defaultPort).asUInt(); + if ( _port<=0 || _port > 65535) + { + throw std::runtime_error("invalid target port"); + } + Debug( _log, "UDP using %s:%d", _address.toString().toStdString().c_str() , _port ); return true; diff --git a/libsrc/leddevice/ProviderUdp.h b/libsrc/leddevice/ProviderUdp.h index b5f9ec37..e3b53877 100644 --- a/libsrc/leddevice/ProviderUdp.h +++ b/libsrc/leddevice/ProviderUdp.h @@ -29,7 +29,7 @@ public: /// /// @param deviceConfig the json device config /// @return true if success - bool setConfig(const Json::Value &deviceConfig); + bool setConfig(const Json::Value &deviceConfig, int defaultPort=0, std::string defaultHost="127.0.0.1"); /// /// Opens and configures the output device diff --git a/libsrc/leddevice/schemas/schema-e131.json b/libsrc/leddevice/schemas/schema-e131.json index ef14834e..85e97e4c 100644 --- a/libsrc/leddevice/schemas/schema-e131.json +++ b/libsrc/leddevice/schemas/schema-e131.json @@ -11,6 +11,8 @@ "type": "integer", "title":"Port", "default": 5568, + "minimum" : 0, + "maximum" : 65535, "propertyOrder" : 2 }, "universe": { diff --git a/libsrc/leddevice/schemas/schema-tpm2net.json b/libsrc/leddevice/schemas/schema-tpm2net.json index f9a01219..535beef3 100644 --- a/libsrc/leddevice/schemas/schema-tpm2net.json +++ b/libsrc/leddevice/schemas/schema-tpm2net.json @@ -4,21 +4,29 @@ "properties":{ "host" : { "type": "string", - "title":"Target IP", + "title":"Target IP/hostname", "propertyOrder" : 1 }, + "port": { + "type": "integer", + "title":"Target Port", + "minimum" : 0, + "maximum" : 65535, + "default" : 50200, + "propertyOrder" : 2 + }, "latchtime": { "type": "integer", "title":"Latchtime", "default": 104000, - "propertyOrder" : 2 + "propertyOrder" : 3 }, "max-packet": { "type": "integer", "title":"Max-packet", "minimum" : 0, "default" : 170, - "propertyOrder" : 3 + "propertyOrder" : 4 } }, "additionalProperties": true diff --git a/libsrc/leddevice/schemas/schema-udpraw.json b/libsrc/leddevice/schemas/schema-udpraw.json index a9da54ef..f8115b42 100644 --- a/libsrc/leddevice/schemas/schema-udpraw.json +++ b/libsrc/leddevice/schemas/schema-udpraw.json @@ -11,6 +11,8 @@ "type": "integer", "title":"Port", "default": 5568, + "minimum" : 0, + "maximum" : 65535, "propertyOrder" : 2 } }, From 3feb905a00505b5bad434733249c3f6ab9ea02f7 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Wed, 21 Sep 2016 23:32:17 +0200 Subject: [PATCH 4/5] reformatted code and added the new defaults from @redpanther --- libsrc/leddevice/LedDeviceUdpH801.cpp | 11 ++++++----- libsrc/leddevice/LedDeviceUdpH801.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/libsrc/leddevice/LedDeviceUdpH801.cpp b/libsrc/leddevice/LedDeviceUdpH801.cpp index f18ee646..966f3bb9 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.cpp +++ b/libsrc/leddevice/LedDeviceUdpH801.cpp @@ -8,7 +8,8 @@ // hyperion local includes #include "LedDeviceUdpH801.h" -LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) : ProviderUdp(deviceConfig) +LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) + : ProviderUdp(deviceConfig) { setConfig(deviceConfig); } @@ -16,7 +17,7 @@ LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) : ProviderUd bool LedDeviceUdpH801::setConfig(const Json::Value &deviceConfig) { /* The H801 port is fixed */ - _port = 30977; + ProviderUdp::setConfig(deviceConfig, 30977, "255.255.255.255"); /* 10ms seems to be a safe default for the wait time */ _LatchTime_ns = deviceConfig.get("latchtime", 10000000).asInt(); @@ -30,13 +31,13 @@ bool LedDeviceUdpH801::setConfig(const Json::Value &deviceConfig) _message[0] = 0xFB; _message[1] = 0xEB; - for(int i=0; i<_ids.length(); i++){ + for (int i = 0; i < _ids.length(); i++) { _message[_prefix_size + _colors + i * _id_size + 0] = (_ids[i] >> 0x00) & 0xFF; _message[_prefix_size + _colors + i * _id_size + 1] = (_ids[i] >> 0x08) & 0xFF; _message[_prefix_size + _colors + i * _id_size + 2] = (_ids[i] >> 0x10) & 0xFF; } - Debug( _log, "H801 using %s:%d", _address.toString().toStdString().c_str() , _port ); + Debug(_log, "H801 using %s:%d", _address.toString().toStdString().c_str(), _port); return true; } @@ -58,6 +59,6 @@ int LedDeviceUdpH801::write(const std::vector &ledValues) int LedDeviceUdpH801::switchOff() { - return write(std::vector(_ledCount, ColorRgb{0,0,0})); + return write(std::vector(_ledCount, ColorRgb{0, 0, 0})); } diff --git a/libsrc/leddevice/LedDeviceUdpH801.h b/libsrc/leddevice/LedDeviceUdpH801.h index 78bcd162..c5f4c87a 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.h +++ b/libsrc/leddevice/LedDeviceUdpH801.h @@ -13,7 +13,7 @@ /// /// Implementation of the LedDevice interface for sending led colors via udp. /// -class LedDeviceUdpH801 : public ProviderUdp +class LedDeviceUdpH801: public ProviderUdp { protected: QList _ids; From ad785b9eba371bca0829b8dc7df80c30afdc052a Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Thu, 22 Sep 2016 00:18:46 +0200 Subject: [PATCH 5/5] implemented bare providerudp constructor and made h801 use it --- libsrc/leddevice/LedDeviceUdpH801.cpp | 5 +++-- libsrc/leddevice/ProviderUdp.cpp | 9 +++++++-- libsrc/leddevice/ProviderUdp.h | 5 +++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libsrc/leddevice/LedDeviceUdpH801.cpp b/libsrc/leddevice/LedDeviceUdpH801.cpp index 966f3bb9..f008d45d 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.cpp +++ b/libsrc/leddevice/LedDeviceUdpH801.cpp @@ -9,7 +9,7 @@ #include "LedDeviceUdpH801.h" LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) - : ProviderUdp(deviceConfig) + : ProviderUdp() { setConfig(deviceConfig); } @@ -18,6 +18,7 @@ bool LedDeviceUdpH801::setConfig(const Json::Value &deviceConfig) { /* The H801 port is fixed */ ProviderUdp::setConfig(deviceConfig, 30977, "255.255.255.255"); + /* 10ms seems to be a safe default for the wait time */ _LatchTime_ns = deviceConfig.get("latchtime", 10000000).asInt(); @@ -54,7 +55,7 @@ int LedDeviceUdpH801::write(const std::vector &ledValues) _message[_prefix_size + 1] = color.green; _message[_prefix_size + 2] = color.blue; - writeBytes(_message.size(), reinterpret_cast(_message.data())); + return writeBytes(_message.size(), reinterpret_cast(_message.data())); } int LedDeviceUdpH801::switchOff() diff --git a/libsrc/leddevice/ProviderUdp.cpp b/libsrc/leddevice/ProviderUdp.cpp index 59a1c0c2..d61ebbba 100644 --- a/libsrc/leddevice/ProviderUdp.cpp +++ b/libsrc/leddevice/ProviderUdp.cpp @@ -16,11 +16,16 @@ #include "ProviderUdp.h" ProviderUdp::ProviderUdp(const Json::Value &deviceConfig) + : ProviderUdp() +{ + setConfig(deviceConfig); +} + +ProviderUdp::ProviderUdp() : LedDevice() , _LatchTime_ns(-1) , _port(0) { - setConfig(deviceConfig); _udpSocket = new QUdpSocket(); } @@ -49,7 +54,7 @@ bool ProviderUdp::setConfig(const Json::Value &deviceConfig, int defaultPort, st Debug( _log, "Successfully parsed %s as a hostname.", deviceConfig["host"].asString().c_str()); _address = info.addresses().first(); } - + _port = deviceConfig.get("port", defaultPort).asUInt(); if ( _port<=0 || _port > 65535) { diff --git a/libsrc/leddevice/ProviderUdp.h b/libsrc/leddevice/ProviderUdp.h index e3b53877..6a81b7c1 100644 --- a/libsrc/leddevice/ProviderUdp.h +++ b/libsrc/leddevice/ProviderUdp.h @@ -19,6 +19,11 @@ public: /// ProviderUdp(const Json::Value &deviceConfig); + /// + /// Constructs specific LedDevice + /// + ProviderUdp(); + /// /// Destructor of the LedDevice; closes the output device if it is open ///