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+
7338int 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}
0 commit comments