Adapted Paintpack to HIDDevice API

Former-commit-id: 06101f0e99fbe99f8994193cea337b400acbafe3
This commit is contained in:
NicoHood 2015-11-08 16:32:53 +01:00
parent a7d9a44dcc
commit 3595709099
3 changed files with 27 additions and 74 deletions

View File

@ -181,7 +181,15 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
} }
else if (type == "paintpack") else if (type == "paintpack")
{ {
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack(); const int delay_ms = deviceConfig["delayAfterConnect"].asInt();
auto VendorIdString = deviceConfig.get("VID", "0x0EBF").asString();
auto ProductIdString = deviceConfig.get("PID", "0x0025").asString();
// Convert HEX values to integer
auto VendorId = std::stoul(VendorIdString, nullptr, 16);
auto ProductId = std::stoul(ProductIdString, nullptr, 16);
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack(VendorId, ProductId, delay_ms);
devicePainLightpack->open(); devicePainLightpack->open();
device = devicePainLightpack; device = devicePainLightpack;

View File

@ -2,61 +2,25 @@
// Hyperion includes // Hyperion includes
#include "LedDevicePaintpack.h" #include "LedDevicePaintpack.h"
LedDevicePaintpack::LedDevicePaintpack() : // Use out report HID device
LedDevice(), LedDevicePaintpack::LedDevicePaintpack(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms) :
_deviceHandle(nullptr) LedHIDDevice(VendorId, ProductId, delayAfterConnect_ms, false),
_ledBuffer(0)
{ {
// empty // empty
} }
int LedDevicePaintpack::open()
{
// initialize the usb context
int error = hid_init();
if (error != 0)
{
std::cerr << "Error while initializing the hidapi context" << std::endl;
return -1;
}
std::cout << "Hidapi initialized" << std::endl;
// Initialise the paintpack device
const unsigned short Paintpack_VendorId = 0x0ebf;
const unsigned short Paintpack_ProductId = 0x0025;
_deviceHandle = hid_open(Paintpack_VendorId, Paintpack_ProductId, nullptr);
if (_deviceHandle == nullptr)
{
// Failed to open the device
std::cerr << "Failed to open HID Paintpakc device " << std::endl;
return -1;
}
return 0;
}
LedDevicePaintpack::~LedDevicePaintpack()
{
if (_deviceHandle != nullptr)
{
hid_close(_deviceHandle);
_deviceHandle = nullptr;
}
hid_exit();
}
int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues) int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues)
{ {
if (_ledBuffer.size() < 3 + ledValues.size()*3) if (_ledBuffer.size() < 2 + ledValues.size()*3)
{ {
_ledBuffer.resize(3 + ledValues.size()*3, uint8_t(0)); _ledBuffer.resize(2 + ledValues.size()*3, uint8_t(0));
_ledBuffer[0] = 3;
_ledBuffer[0] = 0; _ledBuffer[1] = 0;
_ledBuffer[1] = 3;
_ledBuffer[2] = 0;
} }
auto bufIt = _ledBuffer.begin()+3; auto bufIt = _ledBuffer.begin()+2;
for (const ColorRgb & ledValue : ledValues) for (const ColorRgb & ledValue : ledValues)
{ {
*bufIt = ledValue.red; *bufIt = ledValue.red;
@ -67,11 +31,12 @@ int LedDevicePaintpack::write(const std::vector<ColorRgb>& ledValues)
++bufIt; ++bufIt;
} }
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDevicePaintpack::switchOff() int LedDevicePaintpack::switchOff()
{ {
std::fill(_ledBuffer.begin()+3, _ledBuffer.end(), uint8_t(0)); std::fill(_ledBuffer.begin() + 2, _ledBuffer.end(), uint8_t(0));
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }

View File

@ -3,34 +3,19 @@
// STL includes // STL includes
#include <vector> #include <vector>
// libusb include
#include <hidapi/hidapi.h>
// Hyperion includes // Hyperion includes
#include <leddevice/LedDevice.h> #include "LedHIDDevice.h"
/// ///
/// LedDevice implementation for a paintpack device () /// LedDevice implementation for a paintpack device ()
/// ///
class LedDevicePaintpack : public LedDevice class LedDevicePaintpack : public LedHIDDevice
{ {
public: public:
/** /**
* Constructs the paintpack device * Constructs the paintpack device
*/ */
LedDevicePaintpack(); LedDevicePaintpack(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms);
/**
* Destructs the paintpack device, closes USB connection if open
*/
virtual ~LedDevicePaintpack();
/**
* Opens the Paintpack device
*
* @return Zero on succes else negative
*/
int open();
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
@ -49,11 +34,6 @@ public:
virtual int switchOff(); virtual int switchOff();
private: private:
/// libusb device handle
hid_device * _deviceHandle;
/// buffer for led data /// buffer for led data
std::vector<uint8_t> _ledBuffer; std::vector<uint8_t> _ledBuffer;
}; };