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

Commit 9691147

Browse files
committed
Merge pull request #407 from NicoHood/RawHID
Add RawHID Device + HID API Device
2 parents 2125e18 + 30e1524 commit 9691147

File tree

9 files changed

+370
-74
lines changed

9 files changed

+370
-74
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/LedDeviceAdalight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Qt includes
77
#include <QTimer>
88

9-
// hyperion incluse
9+
// hyperion include
1010
#include "LedRs232Device.h"
1111

1212
///

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 25 additions & 1 deletion
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();
@@ -165,7 +181,15 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
165181
}
166182
else if (type == "paintpack")
167183
{
168-
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack();
184+
const int delay_ms = deviceConfig["delayAfterConnect"].asInt();
185+
auto VendorIdString = deviceConfig.get("VID", "0x0EBF").asString();
186+
auto ProductIdString = deviceConfig.get("PID", "0x0025").asString();
187+
188+
// Convert HEX values to integer
189+
auto VendorId = std::stoul(VendorIdString, nullptr, 16);
190+
auto ProductId = std::stoul(ProductIdString, nullptr, 16);
191+
192+
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack(VendorId, ProductId, delay_ms);
169193
devicePainLightpack->open();
170194

171195
device = devicePainLightpack;

libsrc/leddevice/LedDevicePaintpack.cpp

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,25 @@
22
// Hyperion includes
33
#include "LedDevicePaintpack.h"
44

5-
LedDevicePaintpack::LedDevicePaintpack() :
6-
LedDevice(),
7-
_deviceHandle(nullptr)
5+
// Use out report HID device
6+
LedDevicePaintpack::LedDevicePaintpack(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms) :
7+
LedHIDDevice(VendorId, ProductId, delayAfterConnect_ms, false),
8+
_ledBuffer(0)
89
{
910
// empty
1011
}
1112

12-
int LedDevicePaintpack::open()
13-
{
14-
// initialize the usb context
15-
int error = hid_init();
16-
if (error != 0)
17-
{
18-
std::cerr << "Error while initializing the hidapi context" << std::endl;
19-
return -1;
20-
}
21-
std::cout << "Hidapi initialized" << std::endl;
22-
23-
// Initialise the paintpack device
24-
const unsigned short Paintpack_VendorId = 0x0ebf;
25-
const unsigned short Paintpack_ProductId = 0x0025;
26-
_deviceHandle = hid_open(Paintpack_VendorId, Paintpack_ProductId, nullptr);
27-
if (_deviceHandle == nullptr)
28-
{
29-
// Failed to open the device
30-
std::cerr << "Failed to open HID Paintpakc device " << std::endl;
31-
return -1;
32-
}
33-
34-
return 0;
35-
}
3613

37-
LedDevicePaintpack::~LedDevicePaintpack()
14+
int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues)
3815
{
39-
if (_deviceHandle != nullptr)
16+
if (_ledBuffer.size() < 2 + ledValues.size()*3)
4017
{
41-
hid_close(_deviceHandle);
42-
_deviceHandle = nullptr;
18+
_ledBuffer.resize(2 + ledValues.size()*3, uint8_t(0));
19+
_ledBuffer[0] = 3;
20+
_ledBuffer[1] = 0;
4321
}
4422

45-
hid_exit();
46-
}
47-
48-
int LedDevicePaintpack::write(const std::vector<ColorRgb>& ledValues)
49-
{
50-
if (_ledBuffer.size() < 3 + ledValues.size()*3)
51-
{
52-
_ledBuffer.resize(3 + ledValues.size()*3, uint8_t(0));
53-
54-
_ledBuffer[0] = 0;
55-
_ledBuffer[1] = 3;
56-
_ledBuffer[2] = 0;
57-
}
58-
59-
auto bufIt = _ledBuffer.begin()+3;
23+
auto bufIt = _ledBuffer.begin()+2;
6024
for (const ColorRgb & ledValue : ledValues)
6125
{
6226
*bufIt = ledValue.red;
@@ -67,11 +31,12 @@ int LedDevicePaintpack::write(const std::vector<ColorRgb>& ledValues)
6731
++bufIt;
6832
}
6933

70-
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size());
34+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
7135
}
7236

37+
7338
int LedDevicePaintpack::switchOff()
7439
{
75-
std::fill(_ledBuffer.begin()+3, _ledBuffer.end(), uint8_t(0));
76-
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size());
40+
std::fill(_ledBuffer.begin() + 2, _ledBuffer.end(), uint8_t(0));
41+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
7742
}

libsrc/leddevice/LedDevicePaintpack.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,19 @@
33
// STL includes
44
#include <vector>
55

6-
// libusb include
7-
#include <hidapi/hidapi.h>
8-
96
// Hyperion includes
10-
#include <leddevice/LedDevice.h>
7+
#include "LedHIDDevice.h"
118

129
///
1310
/// LedDevice implementation for a paintpack device ()
1411
///
15-
class LedDevicePaintpack : public LedDevice
12+
class LedDevicePaintpack : public LedHIDDevice
1613
{
1714
public:
1815
/**
1916
* Constructs the paintpack device
2017
*/
21-
LedDevicePaintpack();
22-
23-
/**
24-
* Destructs the paintpack device, closes USB connection if open
25-
*/
26-
virtual ~LedDevicePaintpack();
27-
28-
/**
29-
* Opens the Paintpack device
30-
*
31-
* @return Zero on succes else negative
32-
*/
33-
int open();
18+
LedDevicePaintpack(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms);
3419

3520
///
3621
/// Writes the RGB-Color values to the leds.
@@ -49,11 +34,6 @@ class LedDevicePaintpack : public LedDevice
4934
virtual int switchOff();
5035

5136
private:
52-
/// libusb device handle
53-
hid_device * _deviceHandle;
54-
5537
/// buffer for led data
5638
std::vector<uint8_t> _ledBuffer;
57-
58-
5939
};

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)