hyperion.ng/libsrc/leddevice/LedDeviceAPA102.cpp
wayland 63d0f6652e Rewrote the class to make it work with apa102 led strips
Former-commit-id: 9f63b95fb635b15e550d9368c7c8dbc632572bc5
2014-12-11 13:48:14 +01:00

43 lines
981 B
C++

// 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}));
}