diff --git a/doc/datasheets/APA102_LED.pdf b/doc/datasheets/APA102_LED.pdf new file mode 100644 index 00000000..2a457631 Binary files /dev/null and b/doc/datasheets/APA102_LED.pdf differ diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index 4304c874..19f1d96e 100755 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -57,6 +57,7 @@ if(ENABLE_SPIDEV) ${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.h ${CURRENT_SOURCE_DIR}/LedDeviceP9813.h ${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h + ${CURRENT_SOURCE_DIR}/LedDeviceAPA102.h ) SET(Leddevice_SOURCES ${Leddevice_SOURCES} @@ -65,6 +66,7 @@ if(ENABLE_SPIDEV) ${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.cpp ${CURRENT_SOURCE_DIR}/LedDeviceP9813.cpp ${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp + ${CURRENT_SOURCE_DIR}/LedDeviceAPA102.cpp ) endif(ENABLE_SPIDEV) diff --git a/libsrc/leddevice/LedDeviceAPA102.cpp b/libsrc/leddevice/LedDeviceAPA102.cpp new file mode 100644 index 00000000..2a2f5e8c --- /dev/null +++ b/libsrc/leddevice/LedDeviceAPA102.cpp @@ -0,0 +1,42 @@ + +// STL includes +#include +#include +#include + +// Linux includes +#include +#include + +// hyperion local includes +#include "LedDeviceAPA102.h" + +LedDeviceAPA102::LedDeviceAPA102(const std::string& outputDevice, const unsigned baudrate) : + LedSpiDevice(outputDevice, baudrate, 500000), + _ledBuffer(0) +{ + // empty +} + +int LedDeviceAPA102::write(const std::vector &ledValues) +{ + const unsigned int mLedCount = (ledValues.size() * 4) + 8; + if(_ledBuffer.size() != mLedCount){ + _ledBuffer.resize(mLedCount, 0x00); + } + + for (unsigned iLed=1; iLed<=ledValues.size(); ++iLed) { + const ColorRgb& rgb = ledValues[iLed]; + _ledBuffer[iLed*4] = 0xFF; + _ledBuffer[iLed*4+1] = rgb.red; + _ledBuffer[iLed*4+2] = rgb.green; + _ledBuffer[iLed*4+3] = rgb.blue; + } + + return writeBytes(_ledBuffer.size(), _ledBuffer.data()); +} + +int LedDeviceAPA102::switchOff() +{ + return write(std::vector(_ledBuffer.size(), ColorRgb{0,0,0})); +} diff --git a/libsrc/leddevice/LedDeviceAPA102.h b/libsrc/leddevice/LedDeviceAPA102.h new file mode 100644 index 00000000..3cc4e518 --- /dev/null +++ b/libsrc/leddevice/LedDeviceAPA102.h @@ -0,0 +1,42 @@ +#pragma once + +// STL includes +#include + +// hyperion incluse +#include "LedSpiDevice.h" + +/// +/// Implementation of the LedDevice interface for writing to APA102 led device. +/// +/// APA102 is +/// +class LedDeviceAPA102 : public LedSpiDevice +{ +public: + /// + /// Constructs the LedDevice for a string containing leds of the type APA102 + /// + /// @param outputDevice The name of the output device (eg '/etc/SpiDev.0.0') + /// @param baudrate The used baudrate for writing to the output device + /// + LedDeviceAPA102(const std::string& outputDevice, + const unsigned baudrate); + + /// + /// 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(); + +private: + + /// The buffer containing the packed RGB values + std::vector _ledBuffer; + +}; diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index fb602b02..3208b393 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -14,6 +14,7 @@ #include "LedDeviceLpd8806.h" #include "LedDeviceP9813.h" #include "LedDeviceWs2801.h" + #include "LedDeviceAPA102.h" #endif #ifdef ENABLE_TINKERFORGE @@ -86,6 +87,16 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) device = deviceP9813; } + else if (type == "apa102") + { + const std::string output = deviceConfig["output"].asString(); + const unsigned rate = deviceConfig["rate"].asInt(); + + LedDeviceAPA102* deviceAPA102 = new LedDeviceAPA102(output, rate); + deviceAPA102->open(); + + device = deviceAPA102; + } else if (type == "ws2801" || type == "lightberry") { const std::string output = deviceConfig["output"].asString();