Final adjustments for APA102 via Adalight

Former-commit-id: ba786d5c4165c992ce4671e487294547c8dae8c6
This commit is contained in:
tociek 2015-10-13 23:35:27 +02:00
parent 33edcddb1b
commit 3f6c00966e
2 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1,53 @@
// 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()
{
}
//comparing to ws2801 adalight, the following changes were needed:
// 1- differnt data frame (4 bytes instead of 3)
// 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd
int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
{
const unsigned int startFrameSize = 4;
const unsigned int endFrameSize = (ledValues.size() + 63) / 64 * 4;
const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
if(_ledBuffer.size() != mLedCount){
_ledBuffer.resize(mLedCount, 0x00);
_ledBuffer[0] = 'A';
_ledBuffer[1] = 'd';
_ledBuffer[2] = 'a';
_ledBuffer[3] = (((unsigned int)(ledValues.size() * 1.33) - 1) >> 8) & 0xFF; // LED count high byte
_ledBuffer[4] = ((unsigned int)(ledValues.size() * 1.33) - 1) & 0xFF; // LED count low byte
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
}
for (unsigned iLed=1; iLed<=ledValues.size(); iLed++) {
const ColorRgb& rgb = ledValues[iLed];
_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());
}

View File

@ -10,7 +10,7 @@
#include "LedDeviceAdalight.h"
///
/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102 led strips.
/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102.
///
class LedDeviceAdalightApa102 : public LedDeviceAdalight
{
@ -33,4 +33,14 @@ public:
///
virtual int write(const std::vector<ColorRgb> & ledValues);
private:
/// The buffer containing the packed RGB values
std::vector<uint8_t> _ledBuffer;
/// Timer object which makes sure that led data is written at a minimum rate
/// The Adalight device will switch off when it does not receive data at least
/// every 15 seconds
QTimer _timer;
};