2014-12-03 09:35:10 +01:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
2015-10-30 20:07:21 +01:00
|
|
|
#include <algorithm>
|
2014-12-03 09:35:10 +01:00
|
|
|
|
|
|
|
// 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),
|
2014-12-11 13:48:14 +01:00
|
|
|
_ledBuffer(0)
|
2014-12-03 09:35:10 +01:00
|
|
|
{
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
|
|
|
|
{
|
2015-08-05 10:07:31 +02:00
|
|
|
const unsigned int startFrameSize = 4;
|
2015-10-30 20:07:21 +01:00
|
|
|
const unsigned int endFrameSize = std::max<unsigned int>(((ledValues.size() + 15) / 16), 4);
|
2015-08-05 10:07:31 +02:00
|
|
|
const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
|
2014-12-11 13:48:14 +01:00
|
|
|
if(_ledBuffer.size() != mLedCount){
|
2015-11-04 14:05:09 +01:00
|
|
|
_ledBuffer.resize(mLedCount, 0xFF);
|
2015-11-04 14:08:33 +01:00
|
|
|
_ledBuffer[0] = 0x00;
|
|
|
|
_ledBuffer[1] = 0x00;
|
|
|
|
_ledBuffer[2] = 0x00;
|
|
|
|
_ledBuffer[3] = 0x00;
|
2015-11-04 14:05:09 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 13:48:14 +01:00
|
|
|
for (unsigned iLed=1; iLed<=ledValues.size(); ++iLed) {
|
2015-10-30 20:07:21 +01:00
|
|
|
const ColorRgb& rgb = ledValues[iLed-1];
|
2014-12-11 13:48:14 +01:00
|
|
|
_ledBuffer[iLed*4] = 0xFF;
|
|
|
|
_ledBuffer[iLed*4+1] = rgb.red;
|
|
|
|
_ledBuffer[iLed*4+2] = rgb.green;
|
|
|
|
_ledBuffer[iLed*4+3] = rgb.blue;
|
|
|
|
}
|
2014-12-03 09:35:10 +01:00
|
|
|
|
2014-12-11 13:48:14 +01:00
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
2014-12-03 09:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceAPA102::switchOff()
|
|
|
|
{
|
2014-12-11 13:48:14 +01:00
|
|
|
return write(std::vector<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0}));
|
2014-12-03 09:35:10 +01:00
|
|
|
}
|