2015-10-13 23:35:27 +02:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Linux includes
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
// hyperion local includes
|
|
|
|
#include "LedDeviceAdalightApa102.h"
|
|
|
|
|
|
|
|
LedDeviceAdalightApa102::LedDeviceAdalightApa102(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) :
|
|
|
|
LedDeviceAdalight(outputDevice, baudrate, delayAfterConnect_ms),
|
|
|
|
_ledBuffer(0),
|
|
|
|
_timer()
|
|
|
|
{
|
|
|
|
}
|
2015-11-12 00:22:11 +01:00
|
|
|
// see dependencies folder for arduino sketch for APA102
|
2015-10-13 23:35:27 +02:00
|
|
|
int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
|
|
|
|
{
|
2015-11-12 00:22:11 +01:00
|
|
|
ledCount = ledValues.size();
|
2015-10-13 23:35:27 +02:00
|
|
|
const unsigned int startFrameSize = 4;
|
2015-10-30 20:08:16 +01:00
|
|
|
const unsigned int endFrameSize = std::max<unsigned int>(((ledValues.size() + 15) / 16), 4);
|
2015-10-13 23:35:27 +02:00
|
|
|
const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
|
2015-11-12 00:22:11 +01:00
|
|
|
if(_ledBuffer.size() != mLedCount+6){
|
|
|
|
_ledBuffer.resize(mLedCount+6, 0x00);
|
2015-10-13 23:35:27 +02:00
|
|
|
_ledBuffer[0] = 'A';
|
|
|
|
_ledBuffer[1] = 'd';
|
|
|
|
_ledBuffer[2] = 'a';
|
2015-11-12 00:22:11 +01:00
|
|
|
_ledBuffer[3] = (((unsigned int)(ledValues.size())) >> 8) & 0xFF; // LED count high byte
|
|
|
|
_ledBuffer[4] = ((unsigned int)(ledValues.size())) & 0xFF; // LED count low byte
|
2015-10-13 23:35:27 +02:00
|
|
|
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
|
|
|
|
}
|
2015-11-12 00:22:11 +01:00
|
|
|
|
2015-10-13 23:35:27 +02:00
|
|
|
for (unsigned iLed=1; iLed<=ledValues.size(); iLed++) {
|
2015-10-30 20:08:16 +01:00
|
|
|
const ColorRgb& rgb = ledValues[iLed-1];
|
2015-10-13 23:35:27 +02:00
|
|
|
_ledBuffer[iLed*4+6] = 0xFF;
|
|
|
|
_ledBuffer[iLed*4+1+6] = rgb.red;
|
|
|
|
_ledBuffer[iLed*4+2+6] = rgb.green;
|
|
|
|
_ledBuffer[iLed*4+3+6] = rgb.blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// restart the timer
|
|
|
|
_timer.start();
|
|
|
|
|
|
|
|
// write data
|
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
|
|
|
|
2015-11-12 00:22:11 +01:00
|
|
|
int LedDeviceAdalightApa102::switchOff()
|
|
|
|
{
|
|
|
|
for (unsigned iLed=1; iLed<=ledCount; iLed++) {
|
|
|
|
_ledBuffer[iLed*4+6] = 0xFF;
|
|
|
|
_ledBuffer[iLed*4+1+6] = 0x00;
|
|
|
|
_ledBuffer[iLed*4+2+6] = 0x00;
|
|
|
|
_ledBuffer[iLed*4+3+6] = 0x00;
|
|
|
|
}
|
|
|
|
// restart the timer
|
|
|
|
_timer.start();
|
|
|
|
|
|
|
|
// write data
|
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceAdalightApa102::rewriteLeds()
|
|
|
|
{
|
|
|
|
writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
2015-10-13 23:35:27 +02:00
|
|
|
|