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")
{
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();
device = devicePainLightpack;

View File

@ -2,61 +2,25 @@
// Hyperion includes
#include "LedDevicePaintpack.h"
LedDevicePaintpack::LedDevicePaintpack() :
LedDevice(),
_deviceHandle(nullptr)
// Use out report HID device
LedDevicePaintpack::LedDevicePaintpack(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms) :
LedHIDDevice(VendorId, ProductId, delayAfterConnect_ms, false),
_ledBuffer(0)
{
// empty
}
int LedDevicePaintpack::open()
int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues)
{
// initialize the usb context
int error = hid_init();
if (error != 0)
if (_ledBuffer.size() < 2 + ledValues.size()*3)
{
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;
_ledBuffer.resize(2 + ledValues.size()*3, uint8_t(0));
_ledBuffer[0] = 3;
_ledBuffer[1] = 0;
}
return 0;
}
LedDevicePaintpack::~LedDevicePaintpack()
{
if (_deviceHandle != nullptr)
{
hid_close(_deviceHandle);
_deviceHandle = nullptr;
}
hid_exit();
}
int LedDevicePaintpack::write(const std::vector<ColorRgb>& ledValues)
{
if (_ledBuffer.size() < 3 + ledValues.size()*3)
{
_ledBuffer.resize(3 + ledValues.size()*3, uint8_t(0));
_ledBuffer[0] = 0;
_ledBuffer[1] = 3;
_ledBuffer[2] = 0;
}
auto bufIt = _ledBuffer.begin()+3;
auto bufIt = _ledBuffer.begin()+2;
for (const ColorRgb & ledValue : ledValues)
{
*bufIt = ledValue.red;
@ -67,11 +31,12 @@ int LedDevicePaintpack::write(const std::vector<ColorRgb>& ledValues)
++bufIt;
}
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size());
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
int LedDevicePaintpack::switchOff()
{
std::fill(_ledBuffer.begin()+3, _ledBuffer.end(), uint8_t(0));
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size());
std::fill(_ledBuffer.begin() + 2, _ledBuffer.end(), uint8_t(0));
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

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