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

Commit bd36530

Browse files
committed
Added Raw HID device
1 parent 6284152 commit bd36530

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

libsrc/leddevice/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ SET(Leddevice_QT_HEADERS
1818
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.h
1919
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.h
2020
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
21+
${CURRENT_SOURCE_DIR}/LedHIDDevice.h
22+
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.h
2123
)
2224

2325
SET(Leddevice_HEADERS
@@ -39,10 +41,12 @@ SET(Leddevice_SOURCES
3941
${CURRENT_SOURCE_DIR}/LedDeviceFactory.cpp
4042

4143
${CURRENT_SOURCE_DIR}/LedRs232Device.cpp
44+
${CURRENT_SOURCE_DIR}/LedHIDDevice.cpp
4245

4346
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
4447
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.cpp
4548
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.cpp
49+
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.cpp
4650
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
4751
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp
4852
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "LedDeviceAdalight.h"
2525
#include "LedDeviceAmbiLed.h"
26+
#include "LedDeviceRawHID.h"
2627
#include "LedDeviceLightpack.h"
2728
#include "LedDeviceMultiLightpack.h"
2829
#include "LedDevicePaintpack.h"
@@ -147,6 +148,21 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
147148
device = deviceTinkerforge;
148149
}
149150
#endif
151+
else if (type == "rawhid")
152+
{
153+
const int delay_ms = deviceConfig["delayAfterConnect"].asInt();
154+
auto VendorIdString = deviceConfig.get("VID", "0x2341").asString();
155+
auto ProductIdString = deviceConfig.get("PID", "0x8036").asString();
156+
157+
// Convert HEX values to integer
158+
auto VendorId = std::stoul(VendorIdString, nullptr, 16);
159+
auto ProductId = std::stoul(ProductIdString, nullptr, 16);
160+
161+
LedDeviceRawHID* deviceHID = new LedDeviceRawHID(VendorId, ProductId, delay_ms);
162+
deviceHID->open();
163+
164+
device = deviceHID;
165+
}
150166
else if (type == "lightpack")
151167
{
152168
const std::string output = deviceConfig.get("output", "").asString();

libsrc/leddevice/LedDeviceRawHID.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 "LedDeviceRawHID.h"
13+
14+
// Use feature report HID device
15+
LedDeviceRawHID::LedDeviceRawHID(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms) :
16+
LedHIDDevice(VendorId, ProductId, delayAfterConnect_ms, true),
17+
_ledBuffer(0),
18+
_timer()
19+
{
20+
// setup the timer
21+
_timer.setSingleShot(false);
22+
_timer.setInterval(5000);
23+
connect(&_timer, SIGNAL(timeout()), this, SLOT(rewriteLeds()));
24+
25+
// start the timer
26+
_timer.start();
27+
}
28+
29+
int LedDeviceRawHID::write(const std::vector<ColorRgb> & ledValues)
30+
{
31+
// Resize buffer if required
32+
if (_ledBuffer.size() < ledValues.size() * 3) {
33+
_ledBuffer.resize(3 * ledValues.size());
34+
}
35+
36+
// restart the timer
37+
_timer.start();
38+
39+
// write data
40+
memcpy(_ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
41+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
42+
}
43+
44+
int LedDeviceRawHID::switchOff()
45+
{
46+
// restart the timer
47+
_timer.start();
48+
49+
// write data
50+
std::fill(_ledBuffer.begin(), _ledBuffer.end(), uint8_t(0));
51+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
52+
}
53+
54+
void LedDeviceRawHID::rewriteLeds()
55+
{
56+
writeBytes(_ledBuffer.size(), _ledBuffer.data());
57+
}

libsrc/leddevice/LedDeviceRawHID.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
// STL includes
4+
#include <string>
5+
6+
// Qt includes
7+
#include <QTimer>
8+
9+
// hyperion include
10+
#include "LedHIDDevice.h"
11+
12+
///
13+
/// Implementation of the LedDevice interface for writing to an RawHID led device.
14+
///
15+
class LedDeviceRawHID : public LedHIDDevice
16+
{
17+
Q_OBJECT
18+
19+
public:
20+
///
21+
/// Constructs the LedDevice for attached RawHID device
22+
///
23+
LedDeviceRawHID(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms);
24+
25+
///
26+
/// Writes the led color values to the led-device
27+
///
28+
/// @param ledValues The color-value per led
29+
/// @return Zero on succes else negative
30+
///
31+
virtual int write(const std::vector<ColorRgb> & ledValues);
32+
33+
/// Switch the leds off
34+
virtual int switchOff();
35+
36+
private slots:
37+
/// Write the last data to the leds again
38+
void rewriteLeds();
39+
40+
private:
41+
/// The buffer containing the packed RGB values
42+
std::vector<uint8_t> _ledBuffer;
43+
44+
/// Timer object which makes sure that led data is written at a minimum rate
45+
/// The RawHID device will switch off when it does not receive data at least
46+
/// every 15 seconds
47+
QTimer _timer;
48+
};

0 commit comments

Comments
 (0)