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

Commit 0e63a61

Browse files
committed
Merge pull request #212 from ssoerensen/master
Added support for the APA102 LED Strip
2 parents e25fabd + 9f63b95 commit 0e63a61

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

doc/datasheets/APA102_LED.pdf

276 KB
Binary file not shown.

libsrc/leddevice/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ if(ENABLE_SPIDEV)
5757
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.h
5858
${CURRENT_SOURCE_DIR}/LedDeviceP9813.h
5959
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h
60+
${CURRENT_SOURCE_DIR}/LedDeviceAPA102.h
6061
)
6162
SET(Leddevice_SOURCES
6263
${Leddevice_SOURCES}
@@ -65,6 +66,7 @@ if(ENABLE_SPIDEV)
6566
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.cpp
6667
${CURRENT_SOURCE_DIR}/LedDeviceP9813.cpp
6768
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp
69+
${CURRENT_SOURCE_DIR}/LedDeviceAPA102.cpp
6870
)
6971
endif(ENABLE_SPIDEV)
7072

libsrc/leddevice/LedDeviceAPA102.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 "LedDeviceAPA102.h"
13+
14+
LedDeviceAPA102::LedDeviceAPA102(const std::string& outputDevice, const unsigned baudrate) :
15+
LedSpiDevice(outputDevice, baudrate, 500000),
16+
_ledBuffer(0)
17+
{
18+
// empty
19+
}
20+
21+
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
22+
{
23+
const unsigned int mLedCount = (ledValues.size() * 4) + 8;
24+
if(_ledBuffer.size() != mLedCount){
25+
_ledBuffer.resize(mLedCount, 0x00);
26+
}
27+
28+
for (unsigned iLed=1; iLed<=ledValues.size(); ++iLed) {
29+
const ColorRgb& rgb = ledValues[iLed];
30+
_ledBuffer[iLed*4] = 0xFF;
31+
_ledBuffer[iLed*4+1] = rgb.red;
32+
_ledBuffer[iLed*4+2] = rgb.green;
33+
_ledBuffer[iLed*4+3] = rgb.blue;
34+
}
35+
36+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
37+
}
38+
39+
int LedDeviceAPA102::switchOff()
40+
{
41+
return write(std::vector<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0}));
42+
}

libsrc/leddevice/LedDeviceAPA102.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
// STL includes
4+
#include <string>
5+
6+
// hyperion incluse
7+
#include "LedSpiDevice.h"
8+
9+
///
10+
/// Implementation of the LedDevice interface for writing to APA102 led device.
11+
///
12+
/// APA102 is
13+
///
14+
class LedDeviceAPA102 : public LedSpiDevice
15+
{
16+
public:
17+
///
18+
/// Constructs the LedDevice for a string containing leds of the type APA102
19+
///
20+
/// @param outputDevice The name of the output device (eg '/etc/SpiDev.0.0')
21+
/// @param baudrate The used baudrate for writing to the output device
22+
///
23+
LedDeviceAPA102(const std::string& outputDevice,
24+
const unsigned baudrate);
25+
26+
///
27+
/// Writes the led color values to the led-device
28+
///
29+
/// @param ledValues The color-value per led
30+
/// @return Zero on succes else negative
31+
///
32+
virtual int write(const std::vector<ColorRgb> &ledValues);
33+
34+
/// Switch the leds off
35+
virtual int switchOff();
36+
37+
private:
38+
39+
/// The buffer containing the packed RGB values
40+
std::vector<uint8_t> _ledBuffer;
41+
42+
};

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "LedDeviceLpd8806.h"
1515
#include "LedDeviceP9813.h"
1616
#include "LedDeviceWs2801.h"
17+
#include "LedDeviceAPA102.h"
1718
#endif
1819

1920
#ifdef ENABLE_TINKERFORGE
@@ -86,6 +87,16 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
8687

8788
device = deviceP9813;
8889
}
90+
else if (type == "apa102")
91+
{
92+
const std::string output = deviceConfig["output"].asString();
93+
const unsigned rate = deviceConfig["rate"].asInt();
94+
95+
LedDeviceAPA102* deviceAPA102 = new LedDeviceAPA102(output, rate);
96+
deviceAPA102->open();
97+
98+
device = deviceAPA102;
99+
}
89100
else if (type == "ws2801" || type == "lightberry")
90101
{
91102
const std::string output = deviceConfig["output"].asString();

0 commit comments

Comments
 (0)