diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index 093975db..441618d2 100755 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -29,6 +29,7 @@ SET(Leddevice_HEADERS ${CURRENT_SOURCE_DIR}/LedDeviceSedu.h ${CURRENT_SOURCE_DIR}/LedDeviceTest.h ${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h + ${CURRENT_SOURCE_DIR}/LedDeviceTpm2.h ) SET(Leddevice_SOURCES @@ -45,6 +46,7 @@ SET(Leddevice_SOURCES ${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp ${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp ${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.cpp + ${CURRENT_SOURCE_DIR}/LedDeviceTpm2.cpp ) if(ENABLE_SPIDEV) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index 76eb0137..9c42384a 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -29,6 +29,7 @@ #include "LedDeviceTest.h" #include "LedDeviceHyperionUsbasp.h" #include "LedDevicePhilipsHue.h" +#include "LedDeviceTpm2.h" LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) { @@ -171,6 +172,15 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) const std::string output = deviceConfig["output"].asString(); device = new LedDeviceTest(output); } + else if (type == "tpm2") + { + const std::string output = deviceConfig["output"].asString(); + const unsigned rate = deviceConfig["rate"].asInt(); + + LedDeviceTpm2* deviceTpm2 = new LedDeviceTpm2(output, rate); + deviceTpm2->open(); + device = deviceTpm2; + } else { std::cout << "Unable to create device " << type << std::endl; diff --git a/libsrc/leddevice/LedDeviceTpm2.cpp b/libsrc/leddevice/LedDeviceTpm2.cpp new file mode 100644 index 00000000..49bb6121 --- /dev/null +++ b/libsrc/leddevice/LedDeviceTpm2.cpp @@ -0,0 +1,42 @@ + +// STL includes +#include +#include +#include + +// Linux includes +#include +#include + +// hyperion local includes +#include "LedDeviceTpm2.h" + +LedDeviceTpm2::LedDeviceTpm2(const std::string& outputDevice, const unsigned baudrate) : + LedRs232Device(outputDevice, baudrate), + _ledBuffer(0) +{ + // empty +} + +int LedDeviceTpm2::write(const std::vector &ledValues) +{ + if (_ledBuffer.size() == 0) + { + _ledBuffer.resize(5 + 3*ledValues.size()); + _ledBuffer[0] = 0xC9; // block-start byte + _ledBuffer[1] = 0xDA; // DATA frame + _ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // LED count high byte + _ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // LED count 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); + return writeBytes(_ledBuffer.size(), _ledBuffer.data()); +} diff --git a/libsrc/leddevice/LedDeviceTpm2.h b/libsrc/leddevice/LedDeviceTpm2.h new file mode 100644 index 00000000..f7ca8680 --- /dev/null +++ b/libsrc/leddevice/LedDeviceTpm2.h @@ -0,0 +1,38 @@ +#pragma once + +// STL includes +#include + +// hyperion incluse +#include "LedRs232Device.h" + +/// +/// Implementation of the LedDevice interface for writing to serial device using tpm2 protocol. +/// +class LedDeviceTpm2 : public LedRs232Device +{ +public: + /// + /// Constructs the LedDevice for attached serial device using supporting tpm2 protocol + /// All LEDs in the stripe are handled as one frame + /// + /// @param outputDevice The name of the output device (eg '/dev/ttyAMA0') + /// @param baudrate The used baudrate for writing to the output device + /// + LedDeviceTpm2(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; +};