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

Commit 60d2efe

Browse files
committed
Merge pull request #393 from LightberryEu/master
APA102 implementation for Adalight
2 parents f594935 + ba786d5 commit 60d2efe

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

libsrc/leddevice/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ include_directories(
1515
SET(Leddevice_QT_HEADERS
1616
${CURRENT_SOURCE_DIR}/LedRs232Device.h
1717
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h
18+
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.h
1819
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.h
1920
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
2021
)
@@ -40,6 +41,7 @@ SET(Leddevice_SOURCES
4041
${CURRENT_SOURCE_DIR}/LedRs232Device.cpp
4142

4243
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
44+
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.cpp
4345
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.cpp
4446
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
4547
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp

libsrc/leddevice/LedDeviceAdalight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private slots:
4040
/// Write the last data to the leds again
4141
void rewriteLeds();
4242

43-
private:
43+
protected:
4444
/// The buffer containing the packed RGB values
4545
std::vector<uint8_t> _ledBuffer;
4646

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+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
// STL includes
4+
#include <string>
5+
6+
// Qt includes
7+
#include <QTimer>
8+
9+
// hyperion incluse
10+
#include "LedDeviceAdalight.h"
11+
12+
///
13+
/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102.
14+
///
15+
class LedDeviceAdalightApa102 : public LedDeviceAdalight
16+
{
17+
Q_OBJECT
18+
19+
public:
20+
///
21+
/// Constructs the LedDevice for attached Adalight device
22+
///
23+
/// @param outputDevice The name of the output device (eg '/dev/ttyS0')
24+
/// @param baudrate The used baudrate for writing to the output device
25+
///
26+
LedDeviceAdalightApa102(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms);
27+
28+
///
29+
/// Writes the led color values to the led-device
30+
///
31+
/// @param ledValues The color-value per led
32+
/// @return Zero on succes else negative
33+
///
34+
virtual int write(const std::vector<ColorRgb> & ledValues);
35+
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;
46+
};

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "LedDevicePhilipsHue.h"
3434
#include "LedDeviceTpm2.h"
3535
#include "LedDeviceAtmo.h"
36+
#include "LedDeviceAdalightApa102.h"
3637

3738
#ifdef ENABLE_WS2812BPWM
3839
#include "LedDeviceWS2812b.h"
@@ -58,6 +59,17 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
5859

5960
device = deviceAdalight;
6061
}
62+
else if (type == "adalightapa102")
63+
{
64+
const std::string output = deviceConfig["output"].asString();
65+
const unsigned rate = deviceConfig["rate"].asInt();
66+
const int delay_ms = deviceConfig["delayAfterConnect"].asInt();
67+
68+
LedDeviceAdalightApa102* deviceAdalightApa102 = new LedDeviceAdalightApa102(output, rate, delay_ms);
69+
deviceAdalightApa102->open();
70+
71+
device = deviceAdalightApa102;
72+
}
6173
else if (type == "ambiled")
6274
{
6375
const std::string output = deviceConfig["output"].asString();

0 commit comments

Comments
 (0)