From 9ddbf81810f7412b370954274fb6c94fc034bea1 Mon Sep 17 00:00:00 2001 From: redPanther Date: Thu, 1 Dec 2016 16:17:14 +0100 Subject: [PATCH] merge adalight and adalightapa102 (#303) if you want to use adalightapa102 driver, select adalight and set lightberry_apa102_mode : true in device config. Default is standard adalight --- libsrc/leddevice/CMakeLists.txt | 2 - libsrc/leddevice/LedDeviceAdalight.cpp | 47 +++++++++++++--- libsrc/leddevice/LedDeviceAdalight.h | 2 + libsrc/leddevice/LedDeviceAdalightApa102.cpp | 54 ------------------- libsrc/leddevice/LedDeviceAdalightApa102.h | 36 ------------- libsrc/leddevice/LedDeviceFactory.cpp | 2 - libsrc/leddevice/LedDeviceSchemas.qrc | 1 - libsrc/leddevice/schemas/schema-adalight.json | 7 +++ .../schemas/schema-adalightapa102.json | 32 ----------- 9 files changed, 50 insertions(+), 133 deletions(-) delete mode 100644 libsrc/leddevice/LedDeviceAdalightApa102.cpp delete mode 100644 libsrc/leddevice/LedDeviceAdalightApa102.h delete mode 100644 libsrc/leddevice/schemas/schema-adalightapa102.json diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index f8b7d927..414f8588 100755 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -17,7 +17,6 @@ SET(Leddevice_QT_HEADERS ${CURRENT_HEADER_DIR}/LedDevice.h ${CURRENT_SOURCE_DIR}/ProviderRs232.h ${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h - ${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.h ${CURRENT_SOURCE_DIR}/LedDeviceAtmoOrb.h ${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h ${CURRENT_SOURCE_DIR}/ProviderHID.h @@ -53,7 +52,6 @@ SET(Leddevice_SOURCES ${CURRENT_SOURCE_DIR}/ProviderHID.cpp ${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp - ${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.cpp ${CURRENT_SOURCE_DIR}/LedDeviceAtmoOrb.cpp ${CURRENT_SOURCE_DIR}/LedDeviceRawHID.cpp ${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp diff --git a/libsrc/leddevice/LedDeviceAdalight.cpp b/libsrc/leddevice/LedDeviceAdalight.cpp index ba80b1a2..4e6a15a4 100644 --- a/libsrc/leddevice/LedDeviceAdalight.cpp +++ b/libsrc/leddevice/LedDeviceAdalight.cpp @@ -2,8 +2,10 @@ LedDeviceAdalight::LedDeviceAdalight(const QJsonObject &deviceConfig) : ProviderRs232() + , _ligthBerryAPA102Mode(false) { _deviceReady = init(deviceConfig); + _ligthBerryAPA102Mode = deviceConfig["lightberry_apa102_mode"].toBool(false); } LedDevice* LedDeviceAdalight::construct(const QJsonObject &deviceConfig) @@ -15,23 +17,56 @@ bool LedDeviceAdalight::init(const QJsonObject &deviceConfig) { ProviderRs232::init(deviceConfig); - _ledBuffer.resize(6 + _ledRGBCount); + // create ledBuffer + unsigned int bufferSize = 6; // 6 bytes header + unsigned int totalLedCount = _ledCount; + + if (_ligthBerryAPA102Mode) + { + const unsigned int startFrameSize = 4; + const unsigned int bytesPerRGBLed = 4; + const unsigned int endFrameSize = std::max(((_ledCount + 15) / 16), bytesPerRGBLed); + bufferSize += (_ledCount * bytesPerRGBLed) + startFrameSize + endFrameSize ; + } + else + { + totalLedCount -= 1; + bufferSize += _ledRGBCount; + } + + _ledBuffer.resize(bufferSize, 0x00); _ledBuffer[0] = 'A'; _ledBuffer[1] = 'd'; _ledBuffer[2] = 'a'; - _ledBuffer[3] = (((unsigned int)_ledCount - 1) >> 8) & 0xFF; // LED count high byte - _ledBuffer[4] = ((unsigned int)_ledCount - 1) & 0xFF; // LED count low byte + _ledBuffer[3] = (((unsigned int)(totalLedCount)) >> 8) & 0xFF; // LED count high byte + _ledBuffer[4] = ((unsigned int)(totalLedCount)) & 0xFF; // LED count low byte _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum + Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount, - _ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] - ); + _ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] + ); return true; } int LedDeviceAdalight::write(const std::vector & ledValues) { - memcpy(6 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3); + if(_ligthBerryAPA102Mode) + { + for (signed iLed=1; iLed<=_ledCount; iLed++) + { + const ColorRgb& rgb = ledValues[iLed-1]; + _ledBuffer[iLed*4+6] = 0xFF; + _ledBuffer[iLed*4+1+6] = rgb.red; + _ledBuffer[iLed*4+2+6] = rgb.green; + _ledBuffer[iLed*4+3+6] = rgb.blue; + } + } + else + { + memcpy(6 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3); + } + return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } diff --git a/libsrc/leddevice/LedDeviceAdalight.h b/libsrc/leddevice/LedDeviceAdalight.h index b44627e8..3e5120e4 100644 --- a/libsrc/leddevice/LedDeviceAdalight.h +++ b/libsrc/leddevice/LedDeviceAdalight.h @@ -30,5 +30,7 @@ private: /// @return Zero on succes else negative /// virtual int write(const std::vector & ledValues); + + bool _ligthBerryAPA102Mode; }; diff --git a/libsrc/leddevice/LedDeviceAdalightApa102.cpp b/libsrc/leddevice/LedDeviceAdalightApa102.cpp deleted file mode 100644 index 030dd8a8..00000000 --- a/libsrc/leddevice/LedDeviceAdalightApa102.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "LedDeviceAdalightApa102.h" - -LedDeviceAdalightApa102::LedDeviceAdalightApa102(const QJsonObject &deviceConfig) - : ProviderRs232() -{ - _deviceReady = init(deviceConfig); -} - -LedDevice* LedDeviceAdalightApa102::construct(const QJsonObject &deviceConfig) -{ - return new LedDeviceAdalightApa102(deviceConfig); -} - -bool LedDeviceAdalightApa102::init(const QJsonObject &deviceConfig) -{ - ProviderRs232::init(deviceConfig); - - const unsigned int startFrameSize = 4; - const unsigned int endFrameSize = std::max(((_ledCount + 15) / 16), 4); - const unsigned int mLedCount = (_ledCount * 4) + startFrameSize + endFrameSize; - if(_ledBuffer.size() != mLedCount+6) - { - _ledBuffer.resize(mLedCount+6, 0x00); - _ledBuffer[0] = 'A'; - _ledBuffer[1] = 'd'; - _ledBuffer[2] = 'a'; - _ledBuffer[3] = (((unsigned int)(_ledCount)) >> 8) & 0xFF; // LED count high byte - _ledBuffer[4] = ((unsigned int)(_ledCount)) & 0xFF; // LED count low byte - _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum - Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount, - _ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] ); - } - - return true; -} - -//comparing to ws2801 adalight, the following changes were needed: -// 1- differnt data frame (4 bytes instead of 3) -// 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd -int LedDeviceAdalightApa102::write(const std::vector & ledValues) -{ - for (signed iLed=1; iLed<=_ledCount; iLed++) - { - const ColorRgb& rgb = ledValues[iLed-1]; - _ledBuffer[iLed*4+6] = 0xFF; - _ledBuffer[iLed*4+1+6] = rgb.red; - _ledBuffer[iLed*4+2+6] = rgb.green; - _ledBuffer[iLed*4+3+6] = rgb.blue; - } - - // write data - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} - diff --git a/libsrc/leddevice/LedDeviceAdalightApa102.h b/libsrc/leddevice/LedDeviceAdalightApa102.h deleted file mode 100644 index 00c59a44..00000000 --- a/libsrc/leddevice/LedDeviceAdalightApa102.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -// hyperion include -#include "LedDeviceAdalight.h" - -/// -/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102. -/// -class LedDeviceAdalightApa102 : public ProviderRs232 -{ - Q_OBJECT - -public: - /// - /// Constructs the LedDevice for attached Adalight device - /// - /// @param outputDevice The name of the output device (eg '/dev/ttyS0') - /// @param baudrate The used baudrate for writing to the output device - /// - LedDeviceAdalightApa102(const QJsonObject &deviceConfig); - - /// create leddevice when type in config is set to this type - static LedDevice* construct(const QJsonObject &deviceConfig); - - virtual bool init(const QJsonObject &deviceConfig); - -private: - /// - /// 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); -}; - diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index a04a2a6e..4434b8b2 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -45,7 +45,6 @@ #include "LedDevicePhilipsHue.h" #include "LedDeviceTpm2.h" #include "LedDeviceAtmo.h" -#include "LedDeviceAdalightApa102.h" #include "LedDeviceAtmoOrb.h" #include "LedDeviceUdpH801.h" @@ -69,7 +68,6 @@ LedDevice * LedDeviceFactory::construct(const QJsonObject & deviceConfig, const #define REGISTER(className) LedDevice::addToDeviceMap(QString(#className).toLower().toStdString(), LedDevice##className::construct); // rs232 devices REGISTER(Adalight); - REGISTER(AdalightApa102); REGISTER(Sedu); REGISTER(DMX); REGISTER(Tpm2); diff --git a/libsrc/leddevice/LedDeviceSchemas.qrc b/libsrc/leddevice/LedDeviceSchemas.qrc index ea442fde..efff45fd 100644 --- a/libsrc/leddevice/LedDeviceSchemas.qrc +++ b/libsrc/leddevice/LedDeviceSchemas.qrc @@ -1,6 +1,5 @@ - schemas/schema-adalightapa102.json schemas/schema-adalight.json schemas/schema-apa102.json schemas/schema-atmo.json diff --git a/libsrc/leddevice/schemas/schema-adalight.json b/libsrc/leddevice/schemas/schema-adalight.json index 339c5230..cbfd3745 100644 --- a/libsrc/leddevice/schemas/schema-adalight.json +++ b/libsrc/leddevice/schemas/schema-adalight.json @@ -26,6 +26,13 @@ "default": 5000, "append" : "ms", "propertyOrder" : 4 + }, + "lightberry_apa102_mode": { + "type": "boolean", + "format": "checkbox", + "title":"LightBerry APA102 Mode", + "default": false, + "propertyOrder" : 5 } }, "additionalProperties": true diff --git a/libsrc/leddevice/schemas/schema-adalightapa102.json b/libsrc/leddevice/schemas/schema-adalightapa102.json deleted file mode 100644 index 339c5230..00000000 --- a/libsrc/leddevice/schemas/schema-adalightapa102.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "type":"object", - "required":true, - "properties":{ - "output": { - "type": "string", - "title":"Output path", - "propertyOrder" : 1 - }, - "rate": { - "type": "integer", - "title":"Baudrate", - "default": 1000000, - "propertyOrder" : 2 - }, - "delayAfterConnect": { - "type": "integer", - "title":"Delay after connect", - "default": 250, - "append" : "ms", - "propertyOrder" : 3 - }, - "rewriteTime": { - "type": "integer", - "title":"refresh time", - "default": 5000, - "append" : "ms", - "propertyOrder" : 4 - } - }, - "additionalProperties": true -}