diff --git a/include/leddevice/LedDevice.h b/include/leddevice/LedDevice.h index 83abd080..5a07443b 100644 --- a/include/leddevice/LedDevice.h +++ b/include/leddevice/LedDevice.h @@ -1,9 +1,13 @@ #pragma once +#include +#include + // STL incldues #include -#include +#include #include +#include // Utility includes #include @@ -32,17 +36,10 @@ public: /// virtual ~LedDevice() {} - /// - /// Writes the RGB-Color values to the leds. - /// - /// @param[in] ledValues The RGB-color per led - /// - /// @return Zero on success else negative - /// - virtual int write(const std::vector& ledValues) = 0; - /// Switch the leds off - virtual int switchOff() = 0; + virtual int switchOff(); + + virtual int setLedValues(const std::vector& ledValues); /// /// Opens and configures the output device @@ -58,6 +55,15 @@ public: static Json::Value getLedDeviceSchemas(); protected: + /// + /// Writes the RGB-Color values to the leds. + /// + /// @param[in] ledValues The RGB-color per led + /// + /// @return Zero on success else negative + /// + virtual int write(const std::vector& ledValues) = 0; + /// The common Logger instance for all LedDevices Logger * _log; diff --git a/libsrc/hyperion/Hyperion.cpp b/libsrc/hyperion/Hyperion.cpp index a44942ca..6c8f1b5a 100644 --- a/libsrc/hyperion/Hyperion.cpp +++ b/libsrc/hyperion/Hyperion.cpp @@ -1012,9 +1012,9 @@ void Hyperion::update() // Write the data to the device if (_deviceSmooth->enabled()) - _deviceSmooth->write(_ledBuffer); + _deviceSmooth->setLedValues(_ledBuffer); else - _device->write(_ledBuffer); + _device->setLedValues(_ledBuffer); // Start the timeout-timer if (priorityInfo.timeoutTime_ms == -1) diff --git a/libsrc/hyperion/LinearColorSmoothing.cpp b/libsrc/hyperion/LinearColorSmoothing.cpp index bbdc8875..184894b3 100644 --- a/libsrc/hyperion/LinearColorSmoothing.cpp +++ b/libsrc/hyperion/LinearColorSmoothing.cpp @@ -112,7 +112,7 @@ void LinearColorSmoothing::queueColors(const std::vector & ledColors) { // No output delay => immediate write if ( _writeToLedsEnable ) - _ledDevice->write(ledColors); + _ledDevice->setLedValues(ledColors); } else { @@ -125,7 +125,7 @@ void LinearColorSmoothing::queueColors(const std::vector & ledColors) { if ( _outputQueue.size() > _outputDelay || !_writeToLedsEnable ) { - _ledDevice->write(_outputQueue.front()); + _ledDevice->setLedValues(_outputQueue.front()); _outputQueue.pop_front(); } } diff --git a/libsrc/leddevice/LedDevice.cpp b/libsrc/leddevice/LedDevice.cpp index 88349b19..dce4d656 100644 --- a/libsrc/leddevice/LedDevice.cpp +++ b/libsrc/leddevice/LedDevice.cpp @@ -67,3 +67,18 @@ Json::Value LedDevice::getLedDeviceSchemas() return result; } + + +int LedDevice::setLedValues(const std::vector& ledValues) +{ + _ledCount = ledValues.size(); + return write(ledValues); +} + +int LedDevice::switchOff() +{ + return write(std::vector(_ledCount, ColorRgb::BLACK )); +} + + + diff --git a/libsrc/leddevice/LedDeviceAPA102.cpp b/libsrc/leddevice/LedDeviceAPA102.cpp index b2314b85..07fd0f21 100644 --- a/libsrc/leddevice/LedDeviceAPA102.cpp +++ b/libsrc/leddevice/LedDeviceAPA102.cpp @@ -1,21 +1,9 @@ - -// STL includes -#include -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceAPA102.h" LedDeviceAPA102::LedDeviceAPA102(const Json::Value &deviceConfig) : ProviderSpi(deviceConfig) { - _latchTime_ns = 500000; + _latchTime_ns = 500000; // fixed latchtime } LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig) @@ -25,7 +13,6 @@ LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig) int LedDeviceAPA102::write(const std::vector &ledValues) { - _ledCount = ledValues.size(); const unsigned int startFrameSize = 4; const unsigned int endFrameSize = std::max(((_ledCount + 15) / 16), 4); const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize; @@ -48,8 +35,3 @@ int LedDeviceAPA102::write(const std::vector &ledValues) return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } - -int LedDeviceAPA102::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} diff --git a/libsrc/leddevice/LedDeviceAPA102.h b/libsrc/leddevice/LedDeviceAPA102.h index d7411429..9dd742e0 100644 --- a/libsrc/leddevice/LedDeviceAPA102.h +++ b/libsrc/leddevice/LedDeviceAPA102.h @@ -1,11 +1,8 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderSpi.h" -#include + /// /// Implementation of the LedDevice interface for writing to APA102 led device. @@ -23,7 +20,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); - +private: /// /// Writes the led color values to the led-device /// @@ -31,7 +28,4 @@ public: /// @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/LedDeviceAdalight.cpp b/libsrc/leddevice/LedDeviceAdalight.cpp index 2963e0c1..d0758c43 100644 --- a/libsrc/leddevice/LedDeviceAdalight.cpp +++ b/libsrc/leddevice/LedDeviceAdalight.cpp @@ -1,16 +1,4 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceAdalight.h" -#include LedDeviceAdalight::LedDeviceAdalight(const Json::Value &deviceConfig) : ProviderRs232(deviceConfig) @@ -60,16 +48,6 @@ int LedDeviceAdalight::write(const std::vector & ledValues) return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } -int LedDeviceAdalight::switchOff() -{ - // restart the timer - _timer.start(); - - // write data - memset(6 + _ledBuffer.data(), 0, _ledBuffer.size()-6); - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} - void LedDeviceAdalight::rewriteLeds() { writeBytes(_ledBuffer.size(), _ledBuffer.data()); diff --git a/libsrc/leddevice/LedDeviceAdalight.h b/libsrc/leddevice/LedDeviceAdalight.h index 35a9f4af..fa3b177e 100644 --- a/libsrc/leddevice/LedDeviceAdalight.h +++ b/libsrc/leddevice/LedDeviceAdalight.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // Qt includes #include @@ -27,6 +24,11 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private slots: + /// Write the last data to the leds again + void rewriteLeds(); + +protected: /// /// Writes the led color values to the led-device /// @@ -35,14 +37,6 @@ public: /// virtual int write(const std::vector & ledValues); - /// Switch the leds off - virtual int switchOff(); - -private slots: - /// Write the last data to the leds again - void rewriteLeds(); - -protected: /// Timer object which makes sure that led data is written at a minimum rate /// The Adalight device will switch off when it does not receive data at least /// every 15 seconds diff --git a/libsrc/leddevice/LedDeviceAdalightApa102.cpp b/libsrc/leddevice/LedDeviceAdalightApa102.cpp index 0bb35fe9..8f900f7a 100644 --- a/libsrc/leddevice/LedDeviceAdalightApa102.cpp +++ b/libsrc/leddevice/LedDeviceAdalightApa102.cpp @@ -1,14 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceAdalightApa102.h" LedDeviceAdalightApa102::LedDeviceAdalightApa102(const Json::Value &deviceConfig) @@ -27,30 +16,24 @@ LedDevice* LedDeviceAdalightApa102::construct(const Json::Value &deviceConfig) // 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) { - _ledCount = ledValues.size(); 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){ + if(_ledBuffer.size() != mLedCount+6) + { _ledBuffer.resize(mLedCount+6, 0x00); _ledBuffer[0] = 'A'; _ledBuffer[1] = 'd'; _ledBuffer[2] = 'a'; - _ledBuffer[3] = (((unsigned int)(ledValues.size())) >> 8) & 0xFF; // LED count high byte - _ledBuffer[4] = ((unsigned int)(ledValues.size())) & 0xFF; // LED count low byte + _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", - ledValues.size(), - _ledBuffer[0], - _ledBuffer[1], - _ledBuffer[2], - _ledBuffer[3], - _ledBuffer[4], - _ledBuffer[5] - ); + 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] ); } - for (signed iLed=1; iLed<=_ledCount; iLed++) { + 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; @@ -65,19 +48,3 @@ int LedDeviceAdalightApa102::write(const std::vector & ledValues) return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } -int LedDeviceAdalightApa102::switchOff() -{ - for (signed iLed=1; iLed<=_ledCount; iLed++) { - _ledBuffer[iLed*4+6] = 0xFF; - _ledBuffer[iLed*4+1+6] = 0x00; - _ledBuffer[iLed*4+2+6] = 0x00; - _ledBuffer[iLed*4+3+6] = 0x00; - } - - // restart the timer - _timer.start(); - - // write data - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); - -} diff --git a/libsrc/leddevice/LedDeviceAdalightApa102.h b/libsrc/leddevice/LedDeviceAdalightApa102.h index d452d771..f8f03b39 100644 --- a/libsrc/leddevice/LedDeviceAdalightApa102.h +++ b/libsrc/leddevice/LedDeviceAdalightApa102.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion include #include "LedDeviceAdalight.h" @@ -25,6 +22,7 @@ public: /// create leddevice when type in config is set to this type static LedDevice* construct(const Json::Value &deviceConfig); +protected: /// /// Writes the led color values to the led-device /// @@ -32,8 +30,5 @@ public: /// @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/LedDeviceAtmo.cpp b/libsrc/leddevice/LedDeviceAtmo.cpp index b4467a8d..61267645 100644 --- a/libsrc/leddevice/LedDeviceAtmo.cpp +++ b/libsrc/leddevice/LedDeviceAtmo.cpp @@ -21,19 +21,14 @@ int LedDeviceAtmo::write(const std::vector &ledValues) { // The protocol is shomehow limited. we always need to send exactly 5 channels + header // (19 bytes) for the hardware to recognize the data - if (ledValues.size() != 5) + if (_ledCount != 5) { - Error( _log, "%d channels configured. This should always be 5!", ledValues.size()); + Error( _log, "%d channels configured. This should always be 5!", _ledCount); return 0; } // write data - memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * sizeof(ColorRgb)); + memcpy(4 + _ledBuffer.data(), ledValues.data(), _ledCount * sizeof(ColorRgb)); return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } -int LedDeviceAtmo::switchOff() -{ - memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 4); - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} diff --git a/libsrc/leddevice/LedDeviceAtmo.h b/libsrc/leddevice/LedDeviceAtmo.h index 8bb54cdf..dac05e4a 100644 --- a/libsrc/leddevice/LedDeviceAtmo.h +++ b/libsrc/leddevice/LedDeviceAtmo.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderRs232.h" @@ -22,6 +19,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -29,7 +27,4 @@ public: /// @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/LedDeviceAtmoOrb.cpp b/libsrc/leddevice/LedDeviceAtmoOrb.cpp index 79c339a1..70e40fdd 100644 --- a/libsrc/leddevice/LedDeviceAtmoOrb.cpp +++ b/libsrc/leddevice/LedDeviceAtmoOrb.cpp @@ -167,30 +167,11 @@ void LedDeviceAtmoOrb::sendCommand(const QByteArray &bytes) _udpSocket->writeDatagram(datagram.data(), datagram.size(), _groupAddress, _multiCastGroupPort); } -int LedDeviceAtmoOrb::switchOff() { - for (unsigned int i = 0; i < _orbIds.size(); i++) +int LedDeviceAtmoOrb::switchOff() +{ + for (auto orbId : _orbIds) { - QByteArray bytes; - bytes.resize(5 + _numLeds * 3); - bytes.fill('\0'); - - // Command identifier: C0FFEE - bytes[0] = 0xC0; - bytes[1] = 0xFF; - bytes[2] = 0xEE; - - // Command type - bytes[3] = 1; - - // Orb ID - bytes[4] = _orbIds[i]; - - // RED / GREEN / BLUE - bytes[5] = 0; - bytes[6] = 0; - bytes[7] = 0; - - sendCommand(bytes); + setColor(orbId, ColorRgb::BLACK, 1); } return 0; } diff --git a/libsrc/leddevice/LedDeviceAtmoOrb.h b/libsrc/leddevice/LedDeviceAtmoOrb.h index e76fe067..20694a4c 100644 --- a/libsrc/leddevice/LedDeviceAtmoOrb.h +++ b/libsrc/leddevice/LedDeviceAtmoOrb.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // Qt includes #include #include @@ -65,6 +62,9 @@ public: /// virtual ~LedDeviceAtmoOrb(); + virtual int switchOff(); + +private: /// /// Sends the given led-color values to the Orbs /// @@ -73,9 +73,6 @@ public: /// virtual int write(const std::vector &ledValues); - virtual int switchOff(); - -private: /// QNetworkAccessManager object for sending requests. QNetworkAccessManager *_manager; diff --git a/libsrc/leddevice/LedDeviceFadeCandy.cpp b/libsrc/leddevice/LedDeviceFadeCandy.cpp index 671bde35..74d0a83e 100644 --- a/libsrc/leddevice/LedDeviceFadeCandy.cpp +++ b/libsrc/leddevice/LedDeviceFadeCandy.cpp @@ -85,11 +85,10 @@ bool LedDeviceFadeCandy::tryConnect() int LedDeviceFadeCandy::write( const std::vector & ledValues ) { - ssize_t nrLedValues = ledValues.size(); - ssize_t led_data_size = nrLedValues * 3; // 3 color bytes + ssize_t led_data_size = _ledCount * 3; // 3 color bytes ssize_t opc_data_size = led_data_size + OPC_HEADER_SIZE; - if (nrLedValues > MAX_NUM_LEDS) + if (_ledCount > MAX_NUM_LEDS) { Error(_log, "fadecandy/opc: Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS); return -1; @@ -122,15 +121,6 @@ int LedDeviceFadeCandy::transferData() return -2; } - -int LedDeviceFadeCandy::switchOff() -{ - for ( int idx=OPC_HEADER_SIZE; idx < _opc_data.size(); idx++ ) - _opc_data[idx] = 0; - - return ( transferData()<0 ? -1 : 0 ); -} - int LedDeviceFadeCandy::sendSysEx(uint8_t systemId, uint8_t commandId, QByteArray msg) { if ( isConnected() ) diff --git a/libsrc/leddevice/LedDeviceFadeCandy.h b/libsrc/leddevice/LedDeviceFadeCandy.h index cd55958c..cf1d38a7 100644 --- a/libsrc/leddevice/LedDeviceFadeCandy.h +++ b/libsrc/leddevice/LedDeviceFadeCandy.h @@ -1,8 +1,6 @@ #pragma once // STL/Qt includes -#include -#include #include // Leddevice includes @@ -65,9 +63,6 @@ public: /// virtual int write(const std::vector & ledValues); - /// Switch the leds off - virtual int switchOff(); - private: QTcpSocket _client; std::string _host; diff --git a/libsrc/leddevice/LedDeviceFile.cpp b/libsrc/leddevice/LedDeviceFile.cpp index 93ad3022..2233d131 100644 --- a/libsrc/leddevice/LedDeviceFile.cpp +++ b/libsrc/leddevice/LedDeviceFile.cpp @@ -1,5 +1,3 @@ - -// Local-Hyperion includes #include "LedDeviceFile.h" LedDeviceFile::LedDeviceFile(const Json::Value &deviceConfig) @@ -41,8 +39,3 @@ int LedDeviceFile::write(const std::vector & ledValues) return 0; } - -int LedDeviceFile::switchOff() -{ - return 0; -} diff --git a/libsrc/leddevice/LedDeviceFile.h b/libsrc/leddevice/LedDeviceFile.h index 282a7110..5afbb40a 100644 --- a/libsrc/leddevice/LedDeviceFile.h +++ b/libsrc/leddevice/LedDeviceFile.h @@ -1,6 +1,6 @@ #pragma once -// STL includes0 +// STL includes #include // Leddevice includes @@ -35,6 +35,7 @@ public: /// @return true if success virtual bool setConfig(const Json::Value &deviceConfig); +protected: /// /// Writes the given led-color values to the output stream /// @@ -44,10 +45,6 @@ public: /// virtual int write(const std::vector & ledValues); - /// Switch the leds off - virtual int switchOff(); - -private: /// The outputstream std::ofstream _ofs; }; diff --git a/libsrc/leddevice/LedDeviceHyperionUsbasp.cpp b/libsrc/leddevice/LedDeviceHyperionUsbasp.cpp index f3aa6aaa..4e845260 100644 --- a/libsrc/leddevice/LedDeviceHyperionUsbasp.cpp +++ b/libsrc/leddevice/LedDeviceHyperionUsbasp.cpp @@ -137,8 +137,6 @@ int LedDeviceHyperionUsbasp::testAndOpen(libusb_device * device) int LedDeviceHyperionUsbasp::write(const std::vector &ledValues) { - _ledCount = ledValues.size(); - int nbytes = libusb_control_transfer( _deviceHandle, // device handle LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, // request type @@ -159,12 +157,6 @@ int LedDeviceHyperionUsbasp::write(const std::vector &ledValues) return 0; } -int LedDeviceHyperionUsbasp::switchOff() -{ - std::vector ledValues(_ledCount, ColorRgb::BLACK); - return write(ledValues); -} - libusb_device_handle * LedDeviceHyperionUsbasp::openDevice(libusb_device *device) { Logger * log = Logger::getInstance("LedDevice"); diff --git a/libsrc/leddevice/LedDeviceHyperionUsbasp.h b/libsrc/leddevice/LedDeviceHyperionUsbasp.h index 7e9d0da2..8a68c1b2 100644 --- a/libsrc/leddevice/LedDeviceHyperionUsbasp.h +++ b/libsrc/leddevice/LedDeviceHyperionUsbasp.h @@ -52,6 +52,7 @@ public: /// int open(); +protected: /// /// Writes the RGB-Color values to the leds. /// @@ -61,14 +62,6 @@ public: /// virtual int write(const std::vector& ledValues); - /// - /// Switch the leds off - /// - /// @return Zero on success else negative - /// - virtual int switchOff(); - -private: /// /// Test if the device is a Hyperion Usbasp device /// @@ -80,7 +73,6 @@ private: static std::string getString(libusb_device * device, int stringDescriptorIndex); -private: /// command to write the leds uint8_t _writeLedsCommand; @@ -93,5 +85,5 @@ private: /// Usb device identifiers static uint16_t _usbVendorId; static uint16_t _usbProductId; - static std::string _usbProductDescription; + static std::string _usbProductDescription; }; diff --git a/libsrc/leddevice/LedDeviceLightpack-hidapi.cpp b/libsrc/leddevice/LedDeviceLightpack-hidapi.cpp index 476d3509..afe89083 100644 --- a/libsrc/leddevice/LedDeviceLightpack-hidapi.cpp +++ b/libsrc/leddevice/LedDeviceLightpack-hidapi.cpp @@ -39,8 +39,8 @@ LedDeviceLightpackHidapi::LedDeviceLightpackHidapi() , _serialNumber("") , _firmwareVersion({-1,-1}) , _bitsPerChannel(-1) + , _hwLedCount(-1) { - _ledCount = -1; } LedDeviceLightpackHidapi::~LedDeviceLightpackHidapi() @@ -165,11 +165,11 @@ int LedDeviceLightpackHidapi::testAndOpen(hid_device_info *device, const std::st // determine the number of leds if (_firmwareVersion.majorVersion == 4) { - _ledCount = 8; + _hwLedCount = 8; } else { - _ledCount = 10; + _hwLedCount = 10; } // determine the bits per channel @@ -184,7 +184,7 @@ int LedDeviceLightpackHidapi::testAndOpen(hid_device_info *device, const std::st } // set the led buffer size (repport id + command + 6 bytes per led) - _ledBuffer = std::vector(2 + _ledCount * 6, 0); + _ledBuffer = std::vector(2 + _hwLedCount * 6, 0); _ledBuffer[0] = 0x0; // report id _ledBuffer[1] = CMD_UPDATE_LEDS; @@ -202,16 +202,15 @@ int LedDeviceLightpackHidapi::testAndOpen(hid_device_info *device, const std::st return -1; } -int LedDeviceLightpackHidapi::write(const std::vector &ledValues) +int LedDeviceLightpack::write(const std::vector &ledValues) { - return write(ledValues.data(), ledValues.size()); + return write(ledValues.data(), _ledCount); } -int LedDeviceLightpackHidapi::write(const ColorRgb * ledValues, int size) +int LedDeviceLightpack::write(const ColorRgb * ledValues, int size) { - int count = std::min(_ledCount, size); - - for (int i = 0; i < count ; ++i) + int count = std::min(_hwLedCount,size); + for (int i=0; i& ledValues); - /// /// Writes the RGB-Color values to the leds. /// @@ -67,6 +58,15 @@ public: int getLedCount() const; private: + /// + /// Writes the RGB-Color values to the leds. + /// + /// @param[in] ledValues The RGB-color per led + /// + /// @return Zero on success else negative + /// + virtual int write(const std::vector& ledValues); + /// /// Test if the device is a (or the) lightpack we are looking for /// @@ -86,7 +86,6 @@ private: int minorVersion; }; -private: /// libusb device handle hid_device * _deviceHandle; @@ -97,11 +96,8 @@ private: Version _firmwareVersion; /// the number of leds of the device - int _ledCount; + int _hwLedCount; /// the number of bits per channel int _bitsPerChannel; - - /// buffer for led data - std::vector _ledBuffer; }; diff --git a/libsrc/leddevice/LedDeviceLightpack.cpp b/libsrc/leddevice/LedDeviceLightpack.cpp index 80677244..f1be443b 100644 --- a/libsrc/leddevice/LedDeviceLightpack.cpp +++ b/libsrc/leddevice/LedDeviceLightpack.cpp @@ -41,12 +41,12 @@ LedDeviceLightpack::LedDeviceLightpack(const std::string & serialNumber) , _serialNumber(serialNumber) , _firmwareVersion({-1,-1}) , _bitsPerChannel(-1) + , _hwLedCount(-1) { - _ledCount = -1; } LedDeviceLightpack::LedDeviceLightpack(const Json::Value &deviceConfig) - : LedDeviceLightpack() + : LedDevice() { setConfig(deviceConfig); } @@ -207,11 +207,11 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & // determine the number of leds if (_firmwareVersion.majorVersion == 4) { - _ledCount = 8; + _hwLedCount = 8; } else { - _ledCount = 10; + _hwLedCount = 10; } // determine the bits per channel @@ -226,7 +226,7 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & } // set the led buffer size (command + 6 bytes per led) - _ledBuffer = std::vector(1 + _ledCount * 6, 0); + _ledBuffer = std::vector(1 + _hwLedCount * 6, 0); _ledBuffer[0] = CMD_UPDATE_LEDS; // return success @@ -251,7 +251,7 @@ int LedDeviceLightpack::write(const std::vector &ledValues) int LedDeviceLightpack::write(const ColorRgb * ledValues, int size) { - int count = std::min(_ledCount, size); + int count = std::min(_hwLedCount, _ledCount); for (int i = 0; i < count ; ++i) { diff --git a/libsrc/leddevice/LedDeviceLightpack.h b/libsrc/leddevice/LedDeviceLightpack.h index 8c530094..c35ffe8b 100644 --- a/libsrc/leddevice/LedDeviceLightpack.h +++ b/libsrc/leddevice/LedDeviceLightpack.h @@ -1,9 +1,7 @@ #pragma once // stl includes -#include #include -#include // libusb include #include @@ -52,15 +50,6 @@ public: /// int open(); - /// - /// Writes the RGB-Color values to the leds. - /// - /// @param[in] ledValues The RGB-color per led - /// - /// @return Zero on success else negative - /// - virtual int write(const std::vector& ledValues); - /// /// Writes the RGB-Color values to the leds. /// @@ -85,6 +74,15 @@ public: int getLedCount() const; private: + /// + /// Writes the RGB-Color values to the leds. + /// + /// @param[in] ledValues The RGB-color per led + /// + /// @return Zero on success else negative + /// + virtual int write(const std::vector& ledValues); + /// /// Test if the device is a (or the) lightpack we are looking for /// @@ -107,7 +105,6 @@ private: static libusb_device_handle * openDevice(libusb_device * device); static std::string getString(libusb_device * device, int stringDescriptorIndex); -private: /// libusb context libusb_context * _libusbContext; @@ -128,4 +125,7 @@ private: /// the number of bits per channel int _bitsPerChannel; + + /// count of real hardware leds + int _hwLedCount; }; diff --git a/libsrc/leddevice/LedDeviceLpd6803.cpp b/libsrc/leddevice/LedDeviceLpd6803.cpp index 37559ef7..2d55f4e6 100644 --- a/libsrc/leddevice/LedDeviceLpd6803.cpp +++ b/libsrc/leddevice/LedDeviceLpd6803.cpp @@ -1,13 +1,3 @@ -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceLpd6803.h" LedDeviceLpd6803::LedDeviceLpd6803(const Json::Value &deviceConfig) @@ -22,7 +12,7 @@ LedDevice* LedDeviceLpd6803::construct(const Json::Value &deviceConfig) int LedDeviceLpd6803::write(const std::vector &ledValues) { - unsigned messageLength = 4 + 2*ledValues.size() + ledValues.size()/8 + 1; + unsigned messageLength = 4 + 2*_ledCount + _ledCount/8 + 1; // Reconfigure if the current connfiguration does not match the required configuration if (messageLength != _ledBuffer.size()) { @@ -31,7 +21,7 @@ int LedDeviceLpd6803::write(const std::vector &ledValues) } // Copy the colors from the ColorRgb vector to the Ldp6803 data vector - for (unsigned iLed=0; iLed &ledValues) } // Write the data - if (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) - { - return -1; - } - return 0; -} - -int LedDeviceLpd6803::switchOff() -{ - return write(std::vector(_ledBuffer.size(), ColorRgb{0,0,0})); + return (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) ? -1 : 0; } diff --git a/libsrc/leddevice/LedDeviceLpd6803.h b/libsrc/leddevice/LedDeviceLpd6803.h index ff228912..72acb763 100644 --- a/libsrc/leddevice/LedDeviceLpd6803.h +++ b/libsrc/leddevice/LedDeviceLpd6803.h @@ -27,6 +27,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -34,7 +35,4 @@ public: /// @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/LedDeviceLpd8806.cpp b/libsrc/leddevice/LedDeviceLpd8806.cpp index 1c95d752..afd231b3 100644 --- a/libsrc/leddevice/LedDeviceLpd8806.cpp +++ b/libsrc/leddevice/LedDeviceLpd8806.cpp @@ -1,13 +1,3 @@ -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceLpd8806.h" LedDeviceLpd8806::LedDeviceLpd8806(const Json::Value &deviceConfig) @@ -22,19 +12,20 @@ LedDevice* LedDeviceLpd8806::construct(const Json::Value &deviceConfig) int LedDeviceLpd8806::write(const std::vector &ledValues) { - const unsigned clearSize = ledValues.size()/32+1; + const unsigned clearSize = _ledCount/32+1; + unsigned messageLength = 3*_ledCount + clearSize; // Reconfigure if the current connfiguration does not match the required configuration - if (3*ledValues.size() + clearSize != _ledBuffer.size()) + if (messageLength != _ledBuffer.size()) { // Initialise the buffer - _ledBuffer.resize(3*ledValues.size() + clearSize, 0x00); + _ledBuffer.resize(messageLength, 0x00); // Perform an initial reset to start accepting data on the first led writeBytes(clearSize, _ledBuffer.data()); } // Copy the colors from the ColorRgb vector to the Ldp8806 data vector - for (unsigned iLed=0; iLed &ledValues) } // Write the data - if (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) - { - return -1; - } - return 0; -} - -int LedDeviceLpd8806::switchOff() -{ - return write(std::vector(_ledBuffer.size(), ColorRgb{0,0,0})); + return (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) ? -1 : 0; } diff --git a/libsrc/leddevice/LedDeviceLpd8806.h b/libsrc/leddevice/LedDeviceLpd8806.h index e4b7803a..3d3498ab 100644 --- a/libsrc/leddevice/LedDeviceLpd8806.h +++ b/libsrc/leddevice/LedDeviceLpd8806.h @@ -88,6 +88,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -95,7 +96,4 @@ public: /// @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/LedDeviceMultiLightpack.h b/libsrc/leddevice/LedDeviceMultiLightpack.h index 858e9cd9..dcd7f50a 100644 --- a/libsrc/leddevice/LedDeviceMultiLightpack.h +++ b/libsrc/leddevice/LedDeviceMultiLightpack.h @@ -41,6 +41,14 @@ public: /// int open(); + /// + /// Switch the leds off + /// + /// @return Zero on success else negative + /// + virtual int switchOff(); + +private: /// /// Writes the RGB-Color values to the leds. /// @@ -50,18 +58,9 @@ public: /// virtual int write(const std::vector& ledValues); - /// - /// Switch the leds off - /// - /// @return Zero on success else negative - /// - virtual int switchOff(); - -private: static std::list getLightpackSerials(); static std::string getString(libusb_device * device, int stringDescriptorIndex); -private: /// buffer for led data std::vector _lightpacks; }; diff --git a/libsrc/leddevice/LedDeviceP9813.cpp b/libsrc/leddevice/LedDeviceP9813.cpp index 1305aa09..aa9878e4 100644 --- a/libsrc/leddevice/LedDeviceP9813.cpp +++ b/libsrc/leddevice/LedDeviceP9813.cpp @@ -1,14 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceP9813.h" LedDeviceP9813::LedDeviceP9813(const Json::Value &deviceConfig) @@ -23,10 +12,9 @@ LedDevice* LedDeviceP9813::construct(const Json::Value &deviceConfig) int LedDeviceP9813::write(const std::vector &ledValues) { - if (_ledCount != (signed)ledValues.size()) + if (_ledBuffer.size() == 0) { _ledBuffer.resize(ledValues.size() * 4 + 8, 0x00); - _ledCount = ledValues.size(); } uint8_t * dataPtr = _ledBuffer.data(); @@ -41,11 +29,6 @@ int LedDeviceP9813::write(const std::vector &ledValues) return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } -int LedDeviceP9813::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} - uint8_t LedDeviceP9813::calculateChecksum(const ColorRgb & color) const { uint8_t res = 0; diff --git a/libsrc/leddevice/LedDeviceP9813.h b/libsrc/leddevice/LedDeviceP9813.h index 06b01b4d..86a6ba78 100644 --- a/libsrc/leddevice/LedDeviceP9813.h +++ b/libsrc/leddevice/LedDeviceP9813.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion include #include "ProviderSpi.h" @@ -22,6 +19,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -30,10 +28,6 @@ public: /// virtual int write(const std::vector &ledValues); - /// Switch the leds off - virtual int switchOff(); - -private: /// /// Calculates the required checksum for one led /// diff --git a/libsrc/leddevice/LedDevicePaintpack.cpp b/libsrc/leddevice/LedDevicePaintpack.cpp index 15ed5c05..145ebe6c 100644 --- a/libsrc/leddevice/LedDevicePaintpack.cpp +++ b/libsrc/leddevice/LedDevicePaintpack.cpp @@ -1,5 +1,3 @@ - -// Hyperion includes #include "LedDevicePaintpack.h" // Use out report HID device @@ -16,30 +14,24 @@ LedDevice* LedDevicePaintpack::construct(const Json::Value &deviceConfig) int LedDevicePaintpack::write(const std::vector & ledValues) { - if (_ledBuffer.size() < 2 + ledValues.size()*3) + unsigned newSize = 3*_ledCount + 2; + if (_ledBuffer.size() < newSize) { - _ledBuffer.resize(2 + ledValues.size()*3, uint8_t(0)); + _ledBuffer.resize(newSize, uint8_t(0)); _ledBuffer[0] = 3; _ledBuffer[1] = 0; } auto bufIt = _ledBuffer.begin()+2; - for (const ColorRgb & ledValue : ledValues) + for (const ColorRgb & color : ledValues) { - *bufIt = ledValue.red; + *bufIt = color.red; ++bufIt; - *bufIt = ledValue.green; + *bufIt = color.green; ++bufIt; - *bufIt = ledValue.blue; + *bufIt = color.blue; ++bufIt; } return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } - - -int LedDevicePaintpack::switchOff() -{ - std::fill(_ledBuffer.begin() + 2, _ledBuffer.end(), uint8_t(0)); - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} diff --git a/libsrc/leddevice/LedDevicePaintpack.h b/libsrc/leddevice/LedDevicePaintpack.h index 8a0c54fa..0b06c971 100644 --- a/libsrc/leddevice/LedDevicePaintpack.h +++ b/libsrc/leddevice/LedDevicePaintpack.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // Hyperion includes #include "ProviderHID.h" @@ -22,6 +19,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the RGB-Color values to the leds. /// @@ -30,11 +28,4 @@ public: /// @return Zero on success else negative /// virtual int write(const std::vector& ledValues); - - /// - /// Switch the leds off - /// - /// @return Zero on success else negative - /// - virtual int switchOff(); }; diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 66652946..eeaed98b 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -1,9 +1,6 @@ // Local-Hyperion includes #include "LedDevicePhilipsHue.h" -// jsoncpp includes -#include - // qt includes #include #include @@ -205,11 +202,11 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) // Save light states if not done before. if (!areStatesSaved()) { - saveStates((unsigned int) ledValues.size()); - switchOn((unsigned int) ledValues.size()); + saveStates((unsigned int) _ledCount); + switchOn((unsigned int) _ledCount); } // If there are less states saved than colors given, then maybe something went wrong before. - if (lights.size() != ledValues.size()) + if (lights.size() != (unsigned)_ledCount) { restoreStates(); return 0; diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index 5d374a24..b355cf89 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -1,13 +1,9 @@ #pragma once -// STL includes -#include - // Qt includes -#include -#include #include #include + // Leddevice includes #include @@ -136,6 +132,14 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); + /// Restores the original state of the leds. + virtual int switchOff(); + +private slots: + /// Restores the status of all lights. + void restoreStates(); + +private: /// /// Sends the given led-color values via put request to the hue system /// @@ -145,14 +149,6 @@ public: /// virtual int write(const std::vector & ledValues); - /// Restores the original state of the leds. - virtual int switchOff(); - -private slots: - /// Restores the status of all lights. - void restoreStates(); - -private: /// Array to save the lamps. std::vector lights; /// Ip address of the bridge diff --git a/libsrc/leddevice/LedDevicePiBlaster.cpp b/libsrc/leddevice/LedDevicePiBlaster.cpp index b4775ee0..31e52772 100644 --- a/libsrc/leddevice/LedDevicePiBlaster.cpp +++ b/libsrc/leddevice/LedDevicePiBlaster.cpp @@ -3,10 +3,6 @@ #include #include - -// jsoncpp includes -#include - // QT includes #include @@ -116,7 +112,7 @@ int LedDevicePiBlaster::write(const std::vector & ledValues) for (unsigned int i=0; i < TABLE_SZ; i++ ) { valueIdx = _gpio_to_led[ i ]; - if ( (valueIdx >= 0) && (valueIdx < (signed) ledValues.size()) ) + if ( (valueIdx >= 0) && (valueIdx < _ledCount) ) { double pwmDutyCycle = 0.0; switch (_gpio_to_color[ i ]) @@ -157,24 +153,3 @@ int LedDevicePiBlaster::write(const std::vector & ledValues) return 0; } -int LedDevicePiBlaster::switchOff() -{ - // Attempt to open if not yet opened - if (_fid == nullptr && open() < 0) - { - return -1; - } - - int valueIdx = -1; - for (unsigned int i=0; i < TABLE_SZ; i++ ) - { - valueIdx = _gpio_to_led[ i ]; - if (valueIdx >= 0) - { - fprintf(_fid, "%i=%f\n", i, 0.0); - fflush(_fid); - } - } - - return 0; -} diff --git a/libsrc/leddevice/LedDevicePiBlaster.h b/libsrc/leddevice/LedDevicePiBlaster.h index d1189e5b..cd252504 100644 --- a/libsrc/leddevice/LedDevicePiBlaster.h +++ b/libsrc/leddevice/LedDevicePiBlaster.h @@ -1,12 +1,5 @@ - #pragma once -// STL includes -#include - -// jsoncpp includes -#include - // Hyperion-Leddevice includes #include @@ -40,6 +33,7 @@ public: /// int open(); +private: /// /// Writes the colors to the PiBlaster device /// @@ -49,15 +43,6 @@ public: /// int write(const std::vector &ledValues); - /// - /// Switches off the leds - /// - /// @return Zero on success else negative - /// - int switchOff(); - -private: - /// The name of the output device (very likely '/dev/pi-blaster') std::string _deviceName; diff --git a/libsrc/leddevice/LedDeviceRawHID.cpp b/libsrc/leddevice/LedDeviceRawHID.cpp index 3ea3cc37..51e3834b 100644 --- a/libsrc/leddevice/LedDeviceRawHID.cpp +++ b/libsrc/leddevice/LedDeviceRawHID.cpp @@ -1,14 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceRawHID.h" // Use feature report HID device @@ -35,25 +24,17 @@ LedDevice* LedDeviceRawHID::construct(const Json::Value &deviceConfig) int LedDeviceRawHID::write(const std::vector & ledValues) { // Resize buffer if required - if (_ledBuffer.size() < ledValues.size() * 3) { - _ledBuffer.resize(3 * ledValues.size()); + unsigned bufferSize = _ledCount * 3; + if (_ledBuffer.size() < bufferSize) + { + _ledBuffer.resize(bufferSize); } // restart the timer _timer.start(); // write data - memcpy(_ledBuffer.data(), ledValues.data(), ledValues.size() * 3); - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} - -int LedDeviceRawHID::switchOff() -{ - // restart the timer - _timer.start(); - - // write data - std::fill(_ledBuffer.begin(), _ledBuffer.end(), uint8_t(0)); + memcpy(_ledBuffer.data(), ledValues.data(), bufferSize); return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } diff --git a/libsrc/leddevice/LedDeviceRawHID.h b/libsrc/leddevice/LedDeviceRawHID.h index 53a3569f..f77944ef 100644 --- a/libsrc/leddevice/LedDeviceRawHID.h +++ b/libsrc/leddevice/LedDeviceRawHID.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // Qt includes #include @@ -27,6 +24,11 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private slots: + /// Write the last data to the leds again + void rewriteLeds(); + +private: /// /// Writes the led color values to the led-device /// @@ -35,14 +37,6 @@ public: /// virtual int write(const std::vector & ledValues); - /// Switch the leds off - virtual int switchOff(); - -private slots: - /// Write the last data to the leds again - void rewriteLeds(); - -private: /// Timer object which makes sure that led data is written at a minimum rate /// The RawHID device will switch off when it does not receive data at least /// every 15 seconds diff --git a/libsrc/leddevice/LedDeviceSedu.cpp b/libsrc/leddevice/LedDeviceSedu.cpp index 6bdc70c4..f4e0681c 100644 --- a/libsrc/leddevice/LedDeviceSedu.cpp +++ b/libsrc/leddevice/LedDeviceSedu.cpp @@ -1,14 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceSedu.h" struct FrameSpec @@ -20,7 +9,6 @@ struct FrameSpec LedDeviceSedu::LedDeviceSedu(const Json::Value &deviceConfig) : ProviderRs232(deviceConfig) { - // empty } LedDevice* LedDeviceSedu::construct(const Json::Value &deviceConfig) @@ -34,7 +22,7 @@ int LedDeviceSedu::write(const std::vector &ledValues) { std::vector frameSpecs{{0xA1, 256}, {0xA2, 512}, {0xB0, 768}, {0xB1, 1536}, {0xB2, 3072} }; - const unsigned reqColorChannels = ledValues.size() * sizeof(ColorRgb); + const unsigned reqColorChannels = _ledCount * sizeof(ColorRgb); for (const FrameSpec& frameSpec : frameSpecs) { @@ -59,9 +47,3 @@ int LedDeviceSedu::write(const std::vector &ledValues) memcpy(_ledBuffer.data()+2, ledValues.data(), ledValues.size() * sizeof(ColorRgb)); return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } - -int LedDeviceSedu::switchOff() -{ - memset(_ledBuffer.data()+2, 0, _ledBuffer.size()-3); - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} diff --git a/libsrc/leddevice/LedDeviceSedu.h b/libsrc/leddevice/LedDeviceSedu.h index 9a67f1de..d189872d 100644 --- a/libsrc/leddevice/LedDeviceSedu.h +++ b/libsrc/leddevice/LedDeviceSedu.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderRs232.h" @@ -22,6 +19,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -29,7 +27,4 @@ public: /// @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/LedDeviceSk6812SPI.cpp b/libsrc/leddevice/LedDeviceSk6812SPI.cpp index e68e6441..d9cab065 100644 --- a/libsrc/leddevice/LedDeviceSk6812SPI.cpp +++ b/libsrc/leddevice/LedDeviceSk6812SPI.cpp @@ -1,14 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceSk6812SPI.h" LedDeviceSk6812SPI::LedDeviceSk6812SPI(const Json::Value &deviceConfig) @@ -32,13 +21,7 @@ LedDevice* LedDeviceSk6812SPI::construct(const Json::Value &deviceConfig) bool LedDeviceSk6812SPI::setConfig(const Json::Value &deviceConfig) { - ProviderSpi::setConfig(deviceConfig); - - _baudRate_Hz = deviceConfig.get("rate",3000000).asInt(); - if ( (_baudRate_Hz < 2050000) || (_baudRate_Hz > 4000000) ) - { - Warning(_log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz); - } + ProviderSpi::setConfig(deviceConfig,3000000); _whiteAlgorithm = deviceConfig.get("white_algorithm","").asString(); @@ -47,21 +30,21 @@ bool LedDeviceSk6812SPI::setConfig(const Json::Value &deviceConfig) int LedDeviceSk6812SPI::write(const std::vector &ledValues) { - _ledCount = ledValues.size(); - -// 4 colours, 4 spi bytes per colour + 3 frame end latch bytes -#define COLOURS_PER_LED 4 -#define SPI_BYTES_PER_COLOUR 4 -#define SPI_BYTES_PER_LED COLOURS_PER_LED * SPI_BYTES_PER_COLOUR + // 4 colours, 4 spi bytes per colour + 3 frame end latch bytes + #define COLOURS_PER_LED 4 + #define SPI_BYTES_PER_COLOUR 4 + #define SPI_BYTES_PER_LED COLOURS_PER_LED * SPI_BYTES_PER_COLOUR unsigned spi_size = _ledCount * SPI_BYTES_PER_LED + 3; - if(_ledBuffer.size() != spi_size){ - _ledBuffer.resize(spi_size, 0x00); + if(_ledBuffer.size() != spi_size) + { + _ledBuffer.resize(spi_size, 0x00); } unsigned spi_ptr = 0; - for (const ColorRgb& color : ledValues) { + for (const ColorRgb& color : ledValues) + { Rgb_to_Rgbw(color, &_temp_rgbw, _whiteAlgorithm); uint32_t colorBits = ((uint32_t)_temp_rgbw.red << 24) + @@ -74,15 +57,11 @@ int LedDeviceSk6812SPI::write(const std::vector &ledValues) colorBits >>= 2; } spi_ptr += SPI_BYTES_PER_LED; - } + } + _ledBuffer[spi_ptr++] = 0; _ledBuffer[spi_ptr++] = 0; _ledBuffer[spi_ptr++] = 0; return writeBytes(spi_size, _ledBuffer.data()); } - -int LedDeviceSk6812SPI::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} diff --git a/libsrc/leddevice/LedDeviceSk6812SPI.h b/libsrc/leddevice/LedDeviceSk6812SPI.h index 29ab4772..c1336f49 100644 --- a/libsrc/leddevice/LedDeviceSk6812SPI.h +++ b/libsrc/leddevice/LedDeviceSk6812SPI.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderSpi.h" @@ -29,6 +26,7 @@ public: /// @return true if success bool setConfig(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -37,10 +35,6 @@ public: /// virtual int write(const std::vector &ledValues); - /// Switch the leds off - virtual int switchOff(); - -private: std::string _whiteAlgorithm; uint8_t bitpair_to_byte[4]; diff --git a/libsrc/leddevice/LedDeviceTinkerforge.cpp b/libsrc/leddevice/LedDeviceTinkerforge.cpp index 59ffaf37..882d60db 100644 --- a/libsrc/leddevice/LedDeviceTinkerforge.cpp +++ b/libsrc/leddevice/LedDeviceTinkerforge.cpp @@ -10,10 +10,10 @@ static const unsigned MAX_NUM_LEDS = 320; static const unsigned MAX_NUM_LEDS_SETTABLE = 16; LedDeviceTinkerforge::LedDeviceTinkerforge(const Json::Value &deviceConfig) - : LedDevice() - , _ipConnection(nullptr) - , _ledStrip(nullptr) - , _colorChannelSize(0) + : LedDevice() + , _ipConnection(nullptr) + , _ledStrip(nullptr) + , _colorChannelSize(0) { setConfig(deviceConfig); } @@ -82,21 +82,19 @@ int LedDeviceTinkerforge::open() int LedDeviceTinkerforge::write(const std::vector &ledValues) { - unsigned nrLedValues = ledValues.size(); - - if (nrLedValues > MAX_NUM_LEDS) + if ((unsigned)_ledCount > MAX_NUM_LEDS) { Error(_log,"Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS); return -1; } - if (_colorChannelSize < nrLedValues) + if (_colorChannelSize < (unsigned)_ledCount) { - _redChannel.resize(nrLedValues, uint8_t(0)); - _greenChannel.resize(nrLedValues, uint8_t(0)); - _blueChannel.resize(nrLedValues, uint8_t(0)); + _redChannel.resize(_ledCount, uint8_t(0)); + _greenChannel.resize(_ledCount, uint8_t(0)); + _blueChannel.resize(_ledCount, uint8_t(0)); } - _colorChannelSize = nrLedValues; + _colorChannelSize = _ledCount; auto redIt = _redChannel.begin(); auto greenIt = _greenChannel.begin(); @@ -115,15 +113,6 @@ int LedDeviceTinkerforge::write(const std::vector &ledValues) return transferLedData(_ledStrip, 0, _colorChannelSize, _redChannel.data(), _greenChannel.data(), _blueChannel.data()); } -int LedDeviceTinkerforge::switchOff() -{ - std::fill(_redChannel.begin(), _redChannel.end(), 0); - std::fill(_greenChannel.begin(), _greenChannel.end(), 0); - std::fill(_blueChannel.begin(), _blueChannel.end(), 0); - - return transferLedData(_ledStrip, 0, _colorChannelSize, _redChannel.data(), _greenChannel.data(), _blueChannel.data()); -} - int LedDeviceTinkerforge::transferLedData(LEDStrip *ledStrip, unsigned index, unsigned length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel) { if (length == 0 || index >= length || length > MAX_NUM_LEDS) diff --git a/libsrc/leddevice/LedDeviceTinkerforge.h b/libsrc/leddevice/LedDeviceTinkerforge.h index 4f3c6dc7..562be57c 100644 --- a/libsrc/leddevice/LedDeviceTinkerforge.h +++ b/libsrc/leddevice/LedDeviceTinkerforge.h @@ -1,4 +1,3 @@ - #pragma once // STL includes @@ -42,6 +41,7 @@ public: /// int open(); +private: /// /// Writes the colors to the led strip bricklet /// @@ -51,14 +51,6 @@ public: /// virtual int write(const std::vector &ledValues); - /// - /// Switches off the leds - /// - /// @return Zero on success else negative - /// - virtual int switchOff(); - -private: /// /// Writes the data to the led strip blicklet int transferLedData(LEDStrip *ledstrip, unsigned int index, unsigned int length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel); diff --git a/libsrc/leddevice/LedDeviceTpm2.cpp b/libsrc/leddevice/LedDeviceTpm2.cpp index e15045c4..503116f5 100644 --- a/libsrc/leddevice/LedDeviceTpm2.cpp +++ b/libsrc/leddevice/LedDeviceTpm2.cpp @@ -1,12 +1,5 @@ - -// STL includes -#include -#include -#include - -// hyperion local includes #include "LedDeviceTpm2.h" -#include + LedDeviceTpm2::LedDeviceTpm2(const Json::Value &deviceConfig) : ProviderRs232(deviceConfig) @@ -22,21 +15,15 @@ int LedDeviceTpm2::write(const std::vector &ledValues) { if (_ledBuffer.size() == 0) { - _ledBuffer.resize(5 + 3*ledValues.size()); + _ledBuffer.resize(5 + 3*_ledCount); _ledBuffer[0] = 0xC9; // block-start byte _ledBuffer[1] = 0xDA; // DATA frame - _ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // frame size high byte - _ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // frame size low byte + _ledBuffer[2] = ((3 * _ledCount) >> 8) & 0xFF; // frame size high byte + _ledBuffer[3] = (3 * _ledCount) & 0xFF; // frame size low byte _ledBuffer.back() = 0x36; // block-end byte } // write data - memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3); - return writeBytes(_ledBuffer.size(), _ledBuffer.data()); -} - -int LedDeviceTpm2::switchOff() -{ - memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 5); + memcpy(4 + _ledBuffer.data(), ledValues.data(), _ledCount * 3); return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } diff --git a/libsrc/leddevice/LedDeviceTpm2.h b/libsrc/leddevice/LedDeviceTpm2.h index 39e0a3ae..b1d71a7e 100644 --- a/libsrc/leddevice/LedDeviceTpm2.h +++ b/libsrc/leddevice/LedDeviceTpm2.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderRs232.h" @@ -22,6 +19,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -29,7 +27,4 @@ public: /// @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/LedDeviceTpm2net.cpp b/libsrc/leddevice/LedDeviceTpm2net.cpp index b6f7881c..6b229b2c 100644 --- a/libsrc/leddevice/LedDeviceTpm2net.cpp +++ b/libsrc/leddevice/LedDeviceTpm2net.cpp @@ -1,18 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include -#include - -#include -#include - -// hyperion local includes #include "LedDeviceTpm2net.h" LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) @@ -23,8 +8,8 @@ LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig) { - ProviderUdp::setConfig(deviceConfig,50200,104000); - _tpm2_max = deviceConfig.get("max-packet",170).asInt(); + ProviderUdp::setConfig(deviceConfig, TPM2_DEFAULT_PORT, 104000); + _tpm2_max = deviceConfig.get("max-packet", 170).asInt(); return true; } @@ -43,7 +28,6 @@ int LedDeviceTpm2net::write(const std::vector &ledValues) int retVal = 0; - _ledCount = ledValues.size(); _tpm2ByteCount = 3 * _ledCount; _tpm2TotalPackets = 1 + _tpm2ByteCount / _tpm2_max; diff --git a/libsrc/leddevice/LedDeviceTpm2net.h b/libsrc/leddevice/LedDeviceTpm2net.h index f3ced7ca..6ed55eee 100644 --- a/libsrc/leddevice/LedDeviceTpm2net.h +++ b/libsrc/leddevice/LedDeviceTpm2net.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion includes #include "ProviderUdp.h" @@ -31,6 +28,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -39,7 +37,6 @@ public: /// virtual int write(const std::vector &ledValues); -private: int _tpm2_max; int _tpm2ByteCount; int _tpm2TotalPackets; diff --git a/libsrc/leddevice/LedDeviceUdp.cpp b/libsrc/leddevice/LedDeviceUdp.cpp deleted file mode 100644 index 1d2bfb37..00000000 --- a/libsrc/leddevice/LedDeviceUdp.cpp +++ /dev/null @@ -1,213 +0,0 @@ - -// Local-Hyperion includes -#include "LedDeviceUdp.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct addrinfo hints, *servinfo, *p; -//char udpbuffer[1024]; -int sockfd; -int ledprotocol; -unsigned leds_per_pkt; -int update_number; -int fragment_number; - -LedDeviceUdp::LedDeviceUdp(const std::string& output, const unsigned protocol, const unsigned maxPacket) -: LedDevice() -{ - std::string hostname; - std::string port; - ledprotocol = protocol; - leds_per_pkt = ((maxPacket-4)/3); - if (leds_per_pkt <= 0) { - leds_per_pkt = 200; - } - - int got_colon=0; - for (unsigned int i=0; iai_next) { - if ((sockfd = socket(p->ai_family, p->ai_socktype, - p->ai_protocol)) == -1) { - Error(_log,"talker: socket %s", strerror(errno)); -// perror("talker: socket"); - continue; - } - - break; - } - - if (p == NULL) { - Error(_log,"talker: failed to create socket"); - assert(p!=NULL); - } -} - -LedDeviceUdp::~LedDeviceUdp() -{ - // empty -} - -int LedDeviceUdp::write(const std::vector & ledValues) -{ - _ledCount = ledValues.size(); - - char udpbuffer[4096]; - int udpPtr=0; - - update_number++; - update_number &= 0xf; - - if (ledprotocol == 0) - { - int i=0; - for (const ColorRgb& color : ledValues) - { - if (i<4090) - { - udpbuffer[i++] = color.red; - udpbuffer[i++] = color.green; - udpbuffer[i++] = color.blue; - } - } - sendto(sockfd, udpbuffer, i, 0, p->ai_addr, p->ai_addrlen); - } - if (ledprotocol == 1) - { -#define MAXLEDperFRAG 450 - for (int frag=0; frag<4; frag++) - { - udpPtr=0; - udpbuffer[udpPtr++] = 0; - udpbuffer[udpPtr++] = 0; - udpbuffer[udpPtr++] = (frag*MAXLEDperFRAG)/256; // high byte - udpbuffer[udpPtr++] = (frag*MAXLEDperFRAG)%256; // low byte - int ct=0; - for (int this_led = frag*300; ((this_led<_ledCount) && (ct++ 7) - { - sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen); - } - } - } - if (ledprotocol == 2) - { - udpPtr = 0; - unsigned int ledCtr = 0; - fragment_number = 0; - udpbuffer[udpPtr++] = update_number & 0xf; - udpbuffer[udpPtr++] = fragment_number++; - udpbuffer[udpPtr++] = ledCtr/256; // high byte - udpbuffer[udpPtr++] = ledCtr%256; // low byte - - for (const ColorRgb& color : ledValues) - { - if (udpPtr<4090) { - udpbuffer[udpPtr++] = color.red; - udpbuffer[udpPtr++] = color.green; - udpbuffer[udpPtr++] = color.blue; - } - ledCtr++; - if ( (ledCtr % leds_per_pkt == 0) || (ledCtr == ledValues.size()) ) { - sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen); - memset(udpbuffer, 0, sizeof udpbuffer); - udpPtr = 0; - udpbuffer[udpPtr++] = update_number & 0xf; - udpbuffer[udpPtr++] = fragment_number++; - udpbuffer[udpPtr++] = ledCtr/256; // high byte - udpbuffer[udpPtr++] = ledCtr%256; // low byte - } - } - } - - if (ledprotocol == 3) - { - udpPtr = 0; - unsigned int ledCtr = 0; - unsigned int fragments = 1; - unsigned int datasize = ledValues.size() * 3; - if (ledValues.size() > leds_per_pkt) { - fragments = (ledValues.size() / leds_per_pkt) + 1; - } - fragment_number = 1; - udpbuffer[udpPtr++] = 0x9C; - udpbuffer[udpPtr++] = 0xDA; - udpbuffer[udpPtr++] = datasize/256; // high byte - udpbuffer[udpPtr++] = datasize%256; // low byte - udpbuffer[udpPtr++] = fragment_number++; - udpbuffer[udpPtr++] = fragments; - - for (const ColorRgb& color : ledValues) - { - if (udpPtr<4090) - { - udpbuffer[udpPtr++] = color.red; - udpbuffer[udpPtr++] = color.green; - udpbuffer[udpPtr++] = color.blue; - } - ledCtr++; - if ( (ledCtr % leds_per_pkt == 0) || (ledCtr == ledValues.size()) ) - { - udpbuffer[udpPtr++] = 0x36; - sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen); - memset(udpbuffer, 0, sizeof udpbuffer); - udpPtr = 0; - udpbuffer[udpPtr++] = 0x9C; - udpbuffer[udpPtr++] = 0xDA; - udpbuffer[udpPtr++] = datasize/256; // high byte - udpbuffer[udpPtr++] = datasize%256; // low byte - udpbuffer[udpPtr++] = fragment_number++; - udpbuffer[udpPtr++] = fragments; - } - } - } - - return 0; -} - -int LedDeviceUdp::switchOff() -{ -// return write(std::vector(_ledCount, ColorRgb{0,0,0})); - return 0; -} diff --git a/libsrc/leddevice/LedDeviceUdp.h b/libsrc/leddevice/LedDeviceUdp.h deleted file mode 100644 index 3ddcd2cf..00000000 --- a/libsrc/leddevice/LedDeviceUdp.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -// Leddevice includes -#include - -/// -/// Implementation of the LedDevice that write the led-colors via udp -/// -/// -class LedDeviceUdp : public LedDevice -{ -public: - /// - /// Constructs the test-device, which opens an output stream to the file - /// - LedDeviceUdp(const std::string& output, const unsigned protocol, const unsigned maxPacket); - - /// - /// Destructor of this test-device - /// - virtual ~LedDeviceUdp(); - - /// - /// Writes the given led-color values to the output stream - /// - /// @param ledValues The color-value per led - /// - /// @return Zero on success else negative - /// - virtual int write(const std::vector & ledValues); - - /// Switch the leds off - virtual int switchOff(); - -}; diff --git a/libsrc/leddevice/LedDeviceUdpE131.cpp b/libsrc/leddevice/LedDeviceUdpE131.cpp index ac6e7588..abf26fdc 100644 --- a/libsrc/leddevice/LedDeviceUdpE131.cpp +++ b/libsrc/leddevice/LedDeviceUdpE131.cpp @@ -1,16 +1,5 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include #include - #include -#include // hyperion local includes #include "LedDeviceUdpE131.h" @@ -83,17 +72,12 @@ void LedDeviceUdpE131::prepare(const unsigned this_universe, const unsigned this int LedDeviceUdpE131::write(const std::vector &ledValues) { - int retVal = 0; - + int retVal = 0; int _thisChannelCount = 0; - - _e131_seq++; - + int _dmxChannelCount = 3 * _ledCount; const uint8_t * rawdata = reinterpret_cast(ledValues.data()); - _ledCount = ledValues.size(); - - int _dmxChannelCount = 3 * _ledCount; + _e131_seq++; for (int rawIdx = 0; rawIdx < _dmxChannelCount; rawIdx++) { @@ -113,7 +97,7 @@ int LedDeviceUdpE131::write(const std::vector &ledValues) { #undef e131debug #if e131debug - printf ( "send packet: rawidx %d dmxchannelcount %d universe: %d, packetsz %d\n" + Debug (_log, "send packet: rawidx %d dmxchannelcount %d universe: %d, packetsz %d" , rawIdx , _dmxChannelCount , _e131_universe + rawIdx / DMX_MAX diff --git a/libsrc/leddevice/LedDeviceUdpE131.h b/libsrc/leddevice/LedDeviceUdpE131.h index fb26aa8a..d11f7563 100644 --- a/libsrc/leddevice/LedDeviceUdpE131.h +++ b/libsrc/leddevice/LedDeviceUdpE131.h @@ -1,25 +1,22 @@ #pragma once -// STL includes -#include - // hyperion includes #include "ProviderUdp.h" #include -/* -* -* https://raw.githubusercontent.com/forkineye/ESPixelStick/master/_E131.h -* Project: E131 - E.131 (sACN) library for Arduino -* Copyright (c) 2015 Shelby Merrick -* http://www.forkineye.com -* -* This program is provided free for you to use in any way that you wish, -* subject to the laws and regulations where you are using it. Due diligence -* is strongly suggested before using this code. Please give credit where due. -* -*/ +/** + * + * https://raw.githubusercontent.com/forkineye/ESPixelStick/master/_E131.h + * Project: E131 - E.131 (sACN) library for Arduino + * Copyright (c) 2015 Shelby Merrick + * http://www.forkineye.com + * + * This program is provided free for you to use in any way that you wish, + * subject to the laws and regulations where you are using it. Due diligence + * is strongly suggested before using this code. Please give credit where due. + * + **/ #define E131_DEFAULT_PORT 5568 @@ -49,37 +46,39 @@ #define E131_DMP_DATA 125 /* E1.31 Packet Structure */ -typedef union { - struct { - /* Root Layer */ - uint16_t preamble_size; - uint16_t postamble_size; - uint8_t acn_id[12]; - uint16_t root_flength; - uint32_t root_vector; - char cid[16]; +typedef union +{ + struct + { + /* Root Layer */ + uint16_t preamble_size; + uint16_t postamble_size; + uint8_t acn_id[12]; + uint16_t root_flength; + uint32_t root_vector; + char cid[16]; - /* Frame Layer */ - uint16_t frame_flength; - uint32_t frame_vector; - char source_name[64]; - uint8_t priority; - uint16_t reserved; - uint8_t sequence_number; - uint8_t options; - uint16_t universe; + /* Frame Layer */ + uint16_t frame_flength; + uint32_t frame_vector; + char source_name[64]; + uint8_t priority; + uint16_t reserved; + uint8_t sequence_number; + uint8_t options; + uint16_t universe; - /* DMP Layer */ - uint16_t dmp_flength; - uint8_t dmp_vector; - uint8_t type; - uint16_t first_address; - uint16_t address_increment; - uint16_t property_value_count; - uint8_t property_values[513]; - } __attribute__((packed)); + /* DMP Layer */ + uint16_t dmp_flength; + uint8_t dmp_vector; + uint8_t type; + uint16_t first_address; + uint16_t address_increment; + uint16_t property_value_count; + uint8_t property_values[513]; + } __attribute__((packed)); - uint8_t raw[638]; + uint8_t raw[638]; } e131_packet_t; /* defined parameters from http://tsp.esta.org/tsp/documents/docs/BSR_E1-31-20xx_CP-2014-1009r2.pdf */ @@ -119,6 +118,7 @@ public: static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -127,7 +127,6 @@ public: /// virtual int write(const std::vector &ledValues); -private: void prepare(const unsigned this_universe, const unsigned this_dmxChannelCount); e131_packet_t e131_packet; diff --git a/libsrc/leddevice/LedDeviceUdpH801.cpp b/libsrc/leddevice/LedDeviceUdpH801.cpp index 8d859b04..dc2ab816 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.cpp +++ b/libsrc/leddevice/LedDeviceUdpH801.cpp @@ -1,11 +1,3 @@ -// STL includes -#include -#include -#include - -#include - -// hyperion local includes #include "LedDeviceUdpH801.h" LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) @@ -20,7 +12,8 @@ bool LedDeviceUdpH801::setConfig(const Json::Value &deviceConfig) ProviderUdp::setConfig(deviceConfig, 10000000, 30977, "255.255.255.255"); _ids.clear(); - for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++) { + for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++) + { QString id(deviceConfig["lightIds"][i].asCString()); _ids.push_back(id.toInt(nullptr, 16)); } @@ -54,9 +47,3 @@ int LedDeviceUdpH801::write(const std::vector &ledValues) return writeBytes(_message.size(), reinterpret_cast(_message.data())); } - -int LedDeviceUdpH801::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0, 0, 0})); -} - diff --git a/libsrc/leddevice/LedDeviceUdpH801.h b/libsrc/leddevice/LedDeviceUdpH801.h index c5f4c87a..6d8ce510 100644 --- a/libsrc/leddevice/LedDeviceUdpH801.h +++ b/libsrc/leddevice/LedDeviceUdpH801.h @@ -1,12 +1,5 @@ #pragma once -#include -#include -#include - -// STL includes -#include - // hyperion includes #include "ProviderUdp.h" @@ -41,6 +34,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -48,7 +42,4 @@ public: /// @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/LedDeviceUdpRaw.cpp b/libsrc/leddevice/LedDeviceUdpRaw.cpp index 82c60a36..13ea4d48 100644 --- a/libsrc/leddevice/LedDeviceUdpRaw.cpp +++ b/libsrc/leddevice/LedDeviceUdpRaw.cpp @@ -1,20 +1,9 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceUdpRaw.h" LedDeviceUdpRaw::LedDeviceUdpRaw(const Json::Value &deviceConfig) : ProviderUdp() { - ProviderUdp::setConfig(deviceConfig, 500000, 5568); + setConfig(deviceConfig, 500000, 5568); } LedDevice* LedDeviceUdpRaw::construct(const Json::Value &deviceConfig) @@ -24,8 +13,6 @@ LedDevice* LedDeviceUdpRaw::construct(const Json::Value &deviceConfig) int LedDeviceUdpRaw::write(const std::vector &ledValues) { - _ledCount = ledValues.size(); - const unsigned dataLen = _ledCount * sizeof(ColorRgb); const uint8_t * dataPtr = reinterpret_cast(ledValues.data()); diff --git a/libsrc/leddevice/LedDeviceUdpRaw.h b/libsrc/leddevice/LedDeviceUdpRaw.h index ff75b29c..8287879d 100644 --- a/libsrc/leddevice/LedDeviceUdpRaw.h +++ b/libsrc/leddevice/LedDeviceUdpRaw.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderUdp.h" diff --git a/libsrc/leddevice/LedDeviceWS2812b.cpp b/libsrc/leddevice/LedDeviceWS2812b.cpp index 8df215bf..4fddb107 100644 --- a/libsrc/leddevice/LedDeviceWS2812b.cpp +++ b/libsrc/leddevice/LedDeviceWS2812b.cpp @@ -307,8 +307,6 @@ int LedDeviceWS2812b::write(const std::vector &ledValues) clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timeStart); #endif - _ledCount = ledValues.size(); - // Read data from LEDBuffer[], translate it into wire format, and write to PWMWaveform unsigned int colorBits = 0; // Holds the GRB color before conversion to wire bit pattern unsigned int wireBit = 1; // Holds the current bit we will set in PWMWaveform, start with 1 and skip the other two for speed @@ -454,11 +452,6 @@ int LedDeviceWS2812b::write(const std::vector &ledValues) return 0; } -int LedDeviceWS2812b::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} - LedDeviceWS2812b::~LedDeviceWS2812b() { // Exit cleanly, freeing memory and stopping the DMA & PWM engines diff --git a/libsrc/leddevice/LedDeviceWS2812b.h b/libsrc/leddevice/LedDeviceWS2812b.h index 1d8b60ff..c2c2b6cb 100644 --- a/libsrc/leddevice/LedDeviceWS2812b.h +++ b/libsrc/leddevice/LedDeviceWS2812b.h @@ -151,6 +151,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &); +private: /// /// Writes the led color values to the led-device /// @@ -159,10 +160,6 @@ public: /// virtual int write(const std::vector &ledValues); - /// Switch the leds off - virtual int switchOff(); - -private: page_map_t *page_map; // This will hold the page map, which we'll allocate uint8_t *virtbase; // Pointer to some virtual memory that will be allocated diff --git a/libsrc/leddevice/LedDeviceWS281x.cpp b/libsrc/leddevice/LedDeviceWS281x.cpp index 98e03e11..512fe9fd 100644 --- a/libsrc/leddevice/LedDeviceWS281x.cpp +++ b/libsrc/leddevice/LedDeviceWS281x.cpp @@ -1,4 +1,3 @@ -#include #include #include "LedDeviceWS281x.h" @@ -86,23 +85,6 @@ int LedDeviceWS281x::write(const std::vector &ledValues) return ws2811_render(&_led_string) ? -1 : 0; } -// Turn off the LEDs by sending 000000's -// TODO Allow optional power switch out another gpio, if this code handles it can -// make it more likely we don't accidentally drive data into an off strip -int LedDeviceWS281x::switchOff() -{ - if (!_initialized) - { - return -1; - } - - int idx = 0; - while (idx < _led_string.channel[_channel].count) - _led_string.channel[_channel].leds[idx++] = 0; - - return ws2811_render(&_led_string) ? -1 : 0; -} - // Destructor LedDeviceWS281x::~LedDeviceWS281x() { diff --git a/libsrc/leddevice/LedDeviceWS281x.h b/libsrc/leddevice/LedDeviceWS281x.h index b622981b..650d5da7 100644 --- a/libsrc/leddevice/LedDeviceWS281x.h +++ b/libsrc/leddevice/LedDeviceWS281x.h @@ -28,6 +28,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -36,10 +37,6 @@ public: /// virtual int write(const std::vector &ledValues); - /// Switch the leds off - virtual int switchOff(); - -private: ws2811_t _led_string; int _channel; bool _initialized; diff --git a/libsrc/leddevice/LedDeviceWs2801.cpp b/libsrc/leddevice/LedDeviceWs2801.cpp index b3d5e81a..bbf6a80a 100644 --- a/libsrc/leddevice/LedDeviceWs2801.cpp +++ b/libsrc/leddevice/LedDeviceWs2801.cpp @@ -1,14 +1,3 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceWs2801.h" LedDeviceWs2801::LedDeviceWs2801(const Json::Value &deviceConfig) @@ -23,15 +12,9 @@ LedDevice* LedDeviceWs2801::construct(const Json::Value &deviceConfig) int LedDeviceWs2801::write(const std::vector &ledValues) { - _ledCount = ledValues.size(); - - const unsigned dataLen = ledValues.size() * sizeof(ColorRgb); + const unsigned dataLen = _ledCount * sizeof(ColorRgb); const uint8_t * dataPtr = reinterpret_cast(ledValues.data()); return writeBytes(dataLen, dataPtr); } -int LedDeviceWs2801::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} diff --git a/libsrc/leddevice/LedDeviceWs2801.h b/libsrc/leddevice/LedDeviceWs2801.h index 7961fea3..c034d051 100644 --- a/libsrc/leddevice/LedDeviceWs2801.h +++ b/libsrc/leddevice/LedDeviceWs2801.h @@ -1,9 +1,5 @@ #pragma once -// STL includes -#include - -// hyperion incluse #include "ProviderSpi.h" /// @@ -22,6 +18,7 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); +protected: /// /// Writes the led color values to the led-device /// @@ -29,7 +26,4 @@ public: /// @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/LedDeviceWs2812SPI.cpp b/libsrc/leddevice/LedDeviceWs2812SPI.cpp index 9985ce92..b4e55d75 100644 --- a/libsrc/leddevice/LedDeviceWs2812SPI.cpp +++ b/libsrc/leddevice/LedDeviceWs2812SPI.cpp @@ -1,28 +1,15 @@ - -// STL includes -#include -#include -#include - -// Linux includes -#include -#include - -// hyperion local includes #include "LedDeviceWs2812SPI.h" LedDeviceWs2812SPI::LedDeviceWs2812SPI(const Json::Value &deviceConfig) - : ProviderSpi(deviceConfig) + : ProviderSpi() , bitpair_to_byte { 0b10001000, 0b10001100, 0b11001000, 0b11001100, } - { setConfig(deviceConfig); - } LedDevice* LedDeviceWs2812SPI::construct(const Json::Value &deviceConfig) @@ -32,20 +19,14 @@ LedDevice* LedDeviceWs2812SPI::construct(const Json::Value &deviceConfig) bool LedDeviceWs2812SPI::setConfig(const Json::Value &deviceConfig) { - ProviderSpi::setConfig(deviceConfig); + ProviderSpi::setConfig(deviceConfig,3000000); + WarningIf(( _baudRate_Hz < 2050000 || _baudRate_Hz > 4000000 ), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz); - _baudRate_Hz = deviceConfig.get("rate",3000000).asInt(); - if ( (_baudRate_Hz < 2050000) || (_baudRate_Hz > 4000000) ) - { - Warning(_log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz); - } - return true; + return true; } int LedDeviceWs2812SPI::write(const std::vector &ledValues) { - _ledCount = ledValues.size(); - // 3 colours, 4 spi bytes per colour + 3 frame end latch bytes const int SPI_BYTES_PER_LED = 3 * 4; unsigned spi_size = _ledCount * SPI_BYTES_PER_LED + 3; @@ -56,7 +37,7 @@ int LedDeviceWs2812SPI::write(const std::vector &ledValues) } unsigned spi_ptr = 0; - for (unsigned i=0; i< (unsigned)_ledCount; ++i) + for (unsigned i=0; i<(unsigned)_ledCount; ++i) { uint32_t colorBits = ((unsigned int)ledValues[i].red << 16) | ((unsigned int)ledValues[i].green << 8) @@ -75,8 +56,3 @@ int LedDeviceWs2812SPI::write(const std::vector &ledValues) return writeBytes(spi_size, _ledBuffer.data()); } - -int LedDeviceWs2812SPI::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} diff --git a/libsrc/leddevice/LedDeviceWs2812SPI.h b/libsrc/leddevice/LedDeviceWs2812SPI.h index 592bf9d0..ca0c9924 100644 --- a/libsrc/leddevice/LedDeviceWs2812SPI.h +++ b/libsrc/leddevice/LedDeviceWs2812SPI.h @@ -1,8 +1,5 @@ #pragma once -// STL includes -#include - // hyperion incluse #include "ProviderSpi.h" @@ -22,13 +19,14 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); - /// - /// Sets configuration - /// - /// @param deviceConfig the json device config - /// @return true if success - bool setConfig(const Json::Value &deviceConfig); + /// + /// Sets configuration + /// + /// @param deviceConfig the json device config + /// @return true if success + bool setConfig(const Json::Value &deviceConfig); +private: /// /// Writes the led color values to the led-device /// @@ -37,9 +35,5 @@ public: /// virtual int write(const std::vector &ledValues); - /// Switch the leds off - virtual int switchOff(); - -private: uint8_t bitpair_to_byte[4]; }; diff --git a/libsrc/leddevice/ProviderHID.h b/libsrc/leddevice/ProviderHID.h index 4dbb9255..87c4e36f 100644 --- a/libsrc/leddevice/ProviderHID.h +++ b/libsrc/leddevice/ProviderHID.h @@ -55,7 +55,7 @@ protected: // HID VID and PID unsigned short _VendorId; unsigned short _ProductId; - bool _useFeature; + bool _useFeature; /// libusb device handle hid_device * _deviceHandle; diff --git a/libsrc/leddevice/ProviderSpi.cpp b/libsrc/leddevice/ProviderSpi.cpp index 37e98fb7..2cab0e8f 100644 --- a/libsrc/leddevice/ProviderSpi.cpp +++ b/libsrc/leddevice/ProviderSpi.cpp @@ -18,7 +18,10 @@ ProviderSpi::ProviderSpi(const Json::Value &deviceConfig) : LedDevice() , _fid(-1) { - setConfig(deviceConfig); + if (deviceConfig != Json::nullValue) + { + setConfig(deviceConfig); + } memset(&_spi, 0, sizeof(_spi)); } @@ -27,10 +30,10 @@ ProviderSpi::~ProviderSpi() // close(_fid); } -bool ProviderSpi::setConfig(const Json::Value &deviceConfig) +bool ProviderSpi::setConfig(const Json::Value &deviceConfig, int defaultBaudRateHz) { _deviceName = deviceConfig.get("output","/dev/spidev0.0").asString(); - _baudRate_Hz = deviceConfig.get("rate",1000000).asInt(); + _baudRate_Hz = deviceConfig.get("rate",defaultBaudRateHz).asInt(); _latchTime_ns = deviceConfig.get("latchtime",0).asInt(); _spiMode = deviceConfig.get("spimode",SPI_MODE_0).asInt(); _spiDataInvert = deviceConfig.get("invert",false).asBool(); diff --git a/libsrc/leddevice/ProviderSpi.h b/libsrc/leddevice/ProviderSpi.h index 9425b621..1388f4a1 100644 --- a/libsrc/leddevice/ProviderSpi.h +++ b/libsrc/leddevice/ProviderSpi.h @@ -17,14 +17,14 @@ public: /// /// @param deviceConfig json device config /// - ProviderSpi(const Json::Value &deviceConfig); + ProviderSpi(const Json::Value &deviceConfig=Json::nullValue); /// /// Sets configuration /// /// @param deviceConfig the json device config /// @return true if success - virtual bool setConfig(const Json::Value &deviceConfig); + virtual bool setConfig(const Json::Value &deviceConfig, int defaultBaudRateHz=1000000); /// /// Destructor of the LedDevice; closes the output device if it is open diff --git a/libsrc/leddevice/ProviderUdp.cpp b/libsrc/leddevice/ProviderUdp.cpp index bd564433..b1d96450 100644 --- a/libsrc/leddevice/ProviderUdp.cpp +++ b/libsrc/leddevice/ProviderUdp.cpp @@ -95,8 +95,3 @@ int ProviderUdp::writeBytes(const unsigned size, const uint8_t * data) return retVal; } -int ProviderUdp::switchOff() -{ - return write(std::vector(_ledCount, ColorRgb{0,0,0})); -} - diff --git a/libsrc/leddevice/ProviderUdp.h b/libsrc/leddevice/ProviderUdp.h index 7d76e16a..fad8167e 100644 --- a/libsrc/leddevice/ProviderUdp.h +++ b/libsrc/leddevice/ProviderUdp.h @@ -36,9 +36,6 @@ public: /// int open(); - /// Switch the leds off - virtual int switchOff(); - protected: /// /// Writes the given bytes/bits to the SPI-device and sleeps the latch time to ensure that the diff --git a/test/TestSpi.cpp b/test/TestSpi.cpp index 47aa6627..c7e8de23 100644 --- a/test/TestSpi.cpp +++ b/test/TestSpi.cpp @@ -61,7 +61,7 @@ void setColor(char* colorStr) LedDeviceWs2801 ledDevice(deviceConfig); ledDevice.open(); - ledDevice.write(buff); + ledDevice.setLedValues(buff); } bool _running = true; @@ -110,7 +110,7 @@ void doCircle() data[curLed_2] = color_2; - ledDevice.write(data); + ledDevice.setLedValues(data); nanosleep(&loopTime, NULL); } @@ -119,7 +119,7 @@ void doCircle() data[curLed_1] = ColorRgb::BLACK; data[curLed_2] = ColorRgb::BLACK; - ledDevice.write(data); + ledDevice.setLedValues(data); } #include