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

Commit 258161c

Browse files
committed
2 parents 4cba281 + 343b381 commit 258161c

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

libsrc/leddevice/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ if(ENABLE_SPIDEV)
5555
${CURRENT_SOURCE_DIR}/LedDeviceLpd6803.h
5656
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.h
5757
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h
58+
${CURRENT_SOURCE_DIR}/LedDeviceP9813.h
5859
)
5960
SET(Leddevice_SOURCES
6061
${Leddevice_SOURCES}
6162
${CURRENT_SOURCE_DIR}/LedSpiDevice.cpp
6263
${CURRENT_SOURCE_DIR}/LedDeviceLpd6803.cpp
6364
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.cpp
6465
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp
66+
${CURRENT_SOURCE_DIR}/LedDeviceP9813.cpp
6567
)
6668
endif(ENABLE_SPIDEV)
6769

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "LedDeviceLpd6803.h"
1111
#include "LedDeviceLpd8806.h"
1212
#include "LedDeviceWs2801.h"
13+
#include "LedDeviceP9813.h"
1314
#endif
1415

1516
#include "LedDeviceAdalight.h"
@@ -145,6 +146,16 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
145146

146147
device = deviceWs2812b;
147148
}
149+
else if (type == "p9813")
150+
{
151+
const std::string output = deviceConfig["output"].asString();
152+
const unsigned rate = deviceConfig["rate"].asInt();
153+
154+
LedDeviceP9813* deviceP9813 = new LedDeviceP9813(output, rate);
155+
deviceP9813->open();
156+
157+
device = deviceP9813;
158+
}
148159
else
149160
{
150161
std::cout << "Unable to create device " << type << std::endl;

libsrc/leddevice/LedDeviceP9813.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 "LedDeviceP9813.h"
13+
14+
LedDeviceP9813::LedDeviceP9813(const std::string& outputDevice, const unsigned baudrate) :
15+
LedSpiDevice(outputDevice, baudrate, 0),
16+
mLedCount(0)
17+
{
18+
// empty
19+
}
20+
21+
int LedDeviceP9813::write(const std::vector<ColorRgb> &ledValues)
22+
{
23+
mLedCount = ledValues.size();
24+
25+
const unsigned dataLen = ledValues.size() * 4 + 8;
26+
uint8_t data[dataLen];
27+
28+
memset(data, 0x00, dataLen);
29+
30+
int j = 4;
31+
for (unsigned i = 0; i < mLedCount; i++){
32+
data[j++] = calculateChecksum(ledValues[i]);
33+
data[j++] = ledValues[i].blue;
34+
data[j++] = ledValues[i].green;
35+
data[j++] = ledValues[i].red;
36+
}
37+
38+
return writeBytes(dataLen, data);
39+
}
40+
41+
int LedDeviceP9813::switchOff()
42+
{
43+
return write(std::vector<ColorRgb>(mLedCount, ColorRgb{0,0,0}));
44+
}
45+
46+
const uint8_t LedDeviceP9813::calculateChecksum(const ColorRgb color)
47+
{
48+
uint8_t res = 0;
49+
50+
res |= (uint8_t)0x03 << 6;
51+
res |= (uint8_t)(~(color.blue >> 6) & 0x03) << 4;
52+
res |= (uint8_t)(~(color.green >> 6) & 0x03) << 2;
53+
res |= (uint8_t)(~(color.red >> 6) & 0x03);
54+
55+
return res;
56+
}

libsrc/leddevice/LedDeviceP9813.h

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

0 commit comments

Comments
 (0)