Added support for tpm2 protocol. One frame used for all LEDs

Former-commit-id: 5b2ed33a50d90999c6f9508ba3782ad73838fb56
This commit is contained in:
Gamadril 2014-06-15 02:19:09 +02:00
parent 98dfe7997f
commit 1d046ab35a
4 changed files with 92 additions and 0 deletions

View File

@ -29,6 +29,7 @@ SET(Leddevice_HEADERS
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.h
)
SET(Leddevice_SOURCES
@ -45,6 +46,7 @@ SET(Leddevice_SOURCES
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.cpp
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.cpp
)
if(ENABLE_SPIDEV)

View File

@ -29,6 +29,7 @@
#include "LedDeviceTest.h"
#include "LedDeviceHyperionUsbasp.h"
#include "LedDevicePhilipsHue.h"
#include "LedDeviceTpm2.h"
LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
{
@ -171,6 +172,15 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
const std::string output = deviceConfig["output"].asString();
device = new LedDeviceTest(output);
}
else if (type == "tpm2")
{
const std::string output = deviceConfig["output"].asString();
const unsigned rate = deviceConfig["rate"].asInt();
LedDeviceTpm2* deviceTpm2 = new LedDeviceTpm2(output, rate);
deviceTpm2->open();
device = deviceTpm2;
}
else
{
std::cout << "Unable to create device " << type << std::endl;

View File

@ -0,0 +1,42 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceTpm2.h"
LedDeviceTpm2::LedDeviceTpm2(const std::string& outputDevice, const unsigned baudrate) :
LedRs232Device(outputDevice, baudrate),
_ledBuffer(0)
{
// empty
}
int LedDeviceTpm2::write(const std::vector<ColorRgb> &ledValues)
{
if (_ledBuffer.size() == 0)
{
_ledBuffer.resize(5 + 3*ledValues.size());
_ledBuffer[0] = 0xC9; // block-start byte
_ledBuffer[1] = 0xDA; // DATA frame
_ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // LED count high byte
_ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // LED count low byte
_ledBuffer.back() = 0x36; // block-end byte
}
// write data
memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
int LedDeviceTpm2::switchOff()
{
memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 5);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@ -0,0 +1,38 @@
#pragma once
// STL includes
#include <string>
// hyperion incluse
#include "LedRs232Device.h"
///
/// Implementation of the LedDevice interface for writing to serial device using tpm2 protocol.
///
class LedDeviceTpm2 : public LedRs232Device
{
public:
///
/// Constructs the LedDevice for attached serial device using supporting tpm2 protocol
/// All LEDs in the stripe are handled as one frame
///
/// @param outputDevice The name of the output device (eg '/dev/ttyAMA0')
/// @param baudrate The used baudrate for writing to the output device
///
LedDeviceTpm2(const std::string& outputDevice, const unsigned baudrate);
///
/// Writes the led color values to the led-device
///
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
/// The buffer containing the packed RGB values
std::vector<uint8_t> _ledBuffer;
};