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

Commit 06101f0

Browse files
committed
Adapted Paintpack to HIDDevice API
1 parent c4bccd1 commit 06101f0

File tree

3 files changed

+26
-73
lines changed

3 files changed

+26
-73
lines changed

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
181181
}
182182
else if (type == "paintpack")
183183
{
184-
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);
185193
devicePainLightpack->open();
186194

187195
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
};

0 commit comments

Comments
 (0)