Merge pull request #212 from ssoerensen/master

Added support for the APA102 LED Strip

Former-commit-id: 0e63a61fd8667458dbfb3c2b0d0c975f8cf615c1
This commit is contained in:
poljvd 2014-12-15 19:46:25 +01:00
commit c063d42ab4
5 changed files with 97 additions and 0 deletions

Binary file not shown.

View File

@ -57,6 +57,7 @@ if(ENABLE_SPIDEV)
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.h ${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.h
${CURRENT_SOURCE_DIR}/LedDeviceP9813.h ${CURRENT_SOURCE_DIR}/LedDeviceP9813.h
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h ${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h
${CURRENT_SOURCE_DIR}/LedDeviceAPA102.h
) )
SET(Leddevice_SOURCES SET(Leddevice_SOURCES
${Leddevice_SOURCES} ${Leddevice_SOURCES}
@ -65,6 +66,7 @@ if(ENABLE_SPIDEV)
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.cpp ${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.cpp
${CURRENT_SOURCE_DIR}/LedDeviceP9813.cpp ${CURRENT_SOURCE_DIR}/LedDeviceP9813.cpp
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp ${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAPA102.cpp
) )
endif(ENABLE_SPIDEV) endif(ENABLE_SPIDEV)

View File

@ -0,0 +1,42 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// 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<ColorRgb> &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<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0}));
}

View File

@ -0,0 +1,42 @@
#pragma once
// STL includes
#include <string>
// 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<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
/// The buffer containing the packed RGB values
std::vector<uint8_t> _ledBuffer;
};

View File

@ -14,6 +14,7 @@
#include "LedDeviceLpd8806.h" #include "LedDeviceLpd8806.h"
#include "LedDeviceP9813.h" #include "LedDeviceP9813.h"
#include "LedDeviceWs2801.h" #include "LedDeviceWs2801.h"
#include "LedDeviceAPA102.h"
#endif #endif
#ifdef ENABLE_TINKERFORGE #ifdef ENABLE_TINKERFORGE
@ -86,6 +87,16 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
device = deviceP9813; 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") else if (type == "ws2801" || type == "lightberry")
{ {
const std::string output = deviceConfig["output"].asString(); const std::string output = deviceConfig["output"].asString();