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

Commit ebc61d1

Browse files
committed
Merge pull request #118 from Gamadril/master
Added support for tpm2 protocol. One frame used for all LEDs
2 parents 3d859fb + 8d51eb0 commit ebc61d1

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

libsrc/leddevice/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SET(Leddevice_HEADERS
2929
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
3030
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
3131
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
32+
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.h
3233
)
3334

3435
SET(Leddevice_SOURCES
@@ -45,6 +46,7 @@ SET(Leddevice_SOURCES
4546
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
4647
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp
4748
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.cpp
49+
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.cpp
4850
)
4951

5052
if(ENABLE_SPIDEV)

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "LedDeviceTest.h"
3030
#include "LedDeviceHyperionUsbasp.h"
3131
#include "LedDevicePhilipsHue.h"
32+
#include "LedDeviceTpm2.h"
3233

3334
LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
3435
{
@@ -171,6 +172,15 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
171172
const std::string output = deviceConfig["output"].asString();
172173
device = new LedDeviceTest(output);
173174
}
175+
else if (type == "tpm2")
176+
{
177+
const std::string output = deviceConfig["output"].asString();
178+
const unsigned rate = deviceConfig["rate"].asInt();
179+
180+
LedDeviceTpm2* deviceTpm2 = new LedDeviceTpm2(output, rate);
181+
deviceTpm2->open();
182+
device = deviceTpm2;
183+
}
174184
else
175185
{
176186
std::cout << "Unable to create device " << type << std::endl;

libsrc/leddevice/LedDeviceTpm2.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 "LedDeviceTpm2.h"
13+
14+
LedDeviceTpm2::LedDeviceTpm2(const std::string& outputDevice, const unsigned baudrate) :
15+
LedRs232Device(outputDevice, baudrate),
16+
_ledBuffer(0)
17+
{
18+
// empty
19+
}
20+
21+
int LedDeviceTpm2::write(const std::vector<ColorRgb> &ledValues)
22+
{
23+
if (_ledBuffer.size() == 0)
24+
{
25+
_ledBuffer.resize(5 + 3*ledValues.size());
26+
_ledBuffer[0] = 0xC9; // block-start byte
27+
_ledBuffer[1] = 0xDA; // DATA frame
28+
_ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // frame size high byte
29+
_ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // frame size low byte
30+
_ledBuffer.back() = 0x36; // block-end byte
31+
}
32+
33+
// write data
34+
memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
35+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
36+
}
37+
38+
int LedDeviceTpm2::switchOff()
39+
{
40+
memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 5);
41+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
42+
}

libsrc/leddevice/LedDeviceTpm2.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
// STL includes
4+
#include <string>
5+
6+
// hyperion incluse
7+
#include "LedRs232Device.h"
8+
9+
///
10+
/// Implementation of the LedDevice interface for writing to serial device using tpm2 protocol.
11+
///
12+
class LedDeviceTpm2 : public LedRs232Device
13+
{
14+
public:
15+
///
16+
/// Constructs the LedDevice for attached serial device using supporting tpm2 protocol
17+
/// All LEDs in the stripe are handled as one frame
18+
///
19+
/// @param outputDevice The name of the output device (eg '/dev/ttyAMA0')
20+
/// @param baudrate The used baudrate for writing to the output device
21+
///
22+
LedDeviceTpm2(const std::string& outputDevice, 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+
/// The buffer containing the packed RGB values
37+
std::vector<uint8_t> _ledBuffer;
38+
};

0 commit comments

Comments
 (0)