Corrected APA102 USB (adalight) led device

I have removed the 'hack' that allowed to use APA102 with original
version of adalight. I have modified adalight code and placed it into
dependencies folder. This change is not backward compatible, so it won't
work with original adalight code.
The reason for the change is that last leds were not acting as they
should (last led red). Additionally with this change and new arduino
code, performance is  lot better and lights change much smoother.
I have also changed switchOff method that requires different data sent
to apa102 to turn all leds off.

Enjoy :)
Jacek


Former-commit-id: 624fe6c429aee896b150d23289f0be19e040474d
This commit is contained in:
tociek 2015-11-12 00:22:11 +01:00
parent bd1cdac78a
commit 7551a06cf4
3 changed files with 32 additions and 11 deletions

Binary file not shown.

View File

@ -3,7 +3,6 @@
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
// Linux includes
#include <fcntl.h>
@ -18,24 +17,23 @@ LedDeviceAdalightApa102::LedDeviceAdalightApa102(const std::string& outputDevice
_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
// see dependencies folder for arduino sketch for APA102
int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
{
ledCount = ledValues.size();
const unsigned int startFrameSize = 4;
const unsigned int endFrameSize = std::max<unsigned int>(((ledValues.size() + 15) / 16), 4);
const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
if(_ledBuffer.size() != mLedCount){
_ledBuffer.resize(mLedCount, 0xFF);
if(_ledBuffer.size() != mLedCount+6){
_ledBuffer.resize(mLedCount+6, 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[3] = (((unsigned int)(ledValues.size())) >> 8) & 0xFF; // LED count high byte
_ledBuffer[4] = ((unsigned int)(ledValues.size())) & 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-1];
_ledBuffer[iLed*4+6] = 0xFF;
@ -51,4 +49,23 @@ int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
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());
}

View File

@ -32,13 +32,17 @@ public:
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);
virtual int switchOff();
private slots:
/// Write the last data to the leds again
void rewriteLeds();
private:
/// The buffer containing the packed RGB values
std::vector<uint8_t> _ledBuffer;
unsigned int ledCount;
/// 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