Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit ba786d5

Browse files
author
tociek
committed
Final adjustments for APA102 via Adalight
1 parent 7fb9e8e commit ba786d5

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
// STL includes
3+
#include <cstring>
4+
#include <cstdio>
5+
#include <iostream>
6+
7+
// Linux includes
8+
#include <fcntl.h>
9+
#include <sys/ioctl.h>
10+
11+
// hyperion local includes
12+
#include "LedDeviceAdalightApa102.h"
13+
14+
LedDeviceAdalightApa102::LedDeviceAdalightApa102(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) :
15+
LedDeviceAdalight(outputDevice, baudrate, delayAfterConnect_ms),
16+
_ledBuffer(0),
17+
_timer()
18+
{
19+
}
20+
//comparing to ws2801 adalight, the following changes were needed:
21+
// 1- differnt data frame (4 bytes instead of 3)
22+
// 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd
23+
int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
24+
{
25+
const unsigned int startFrameSize = 4;
26+
const unsigned int endFrameSize = (ledValues.size() + 63) / 64 * 4;
27+
const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
28+
if(_ledBuffer.size() != mLedCount){
29+
_ledBuffer.resize(mLedCount, 0x00);
30+
_ledBuffer[0] = 'A';
31+
_ledBuffer[1] = 'd';
32+
_ledBuffer[2] = 'a';
33+
_ledBuffer[3] = (((unsigned int)(ledValues.size() * 1.33) - 1) >> 8) & 0xFF; // LED count high byte
34+
_ledBuffer[4] = ((unsigned int)(ledValues.size() * 1.33) - 1) & 0xFF; // LED count low byte
35+
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
36+
}
37+
38+
for (unsigned iLed=1; iLed<=ledValues.size(); iLed++) {
39+
const ColorRgb& rgb = ledValues[iLed];
40+
_ledBuffer[iLed*4+6] = 0xFF;
41+
_ledBuffer[iLed*4+1+6] = rgb.red;
42+
_ledBuffer[iLed*4+2+6] = rgb.green;
43+
_ledBuffer[iLed*4+3+6] = rgb.blue;
44+
}
45+
46+
// restart the timer
47+
_timer.start();
48+
49+
// write data
50+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
51+
}
52+
53+

libsrc/leddevice/LedDeviceAdalightApa102.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "LedDeviceAdalight.h"
1111

1212
///
13-
/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102 led strips.
13+
/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102.
1414
///
1515
class LedDeviceAdalightApa102 : public LedDeviceAdalight
1616
{
@@ -33,4 +33,14 @@ class LedDeviceAdalightApa102 : public LedDeviceAdalight
3333
///
3434
virtual int write(const std::vector<ColorRgb> & ledValues);
3535

36+
37+
38+
private:
39+
/// The buffer containing the packed RGB values
40+
std::vector<uint8_t> _ledBuffer;
41+
42+
/// Timer object which makes sure that led data is written at a minimum rate
43+
/// The Adalight device will switch off when it does not receive data at least
44+
/// every 15 seconds
45+
QTimer _timer;
3646
};

0 commit comments

Comments
 (0)