From 3595709099397d8c72a16b973b17925262af248a Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sun, 8 Nov 2015 16:32:53 +0100 Subject: [PATCH] Adapted Paintpack to HIDDevice API Former-commit-id: 06101f0e99fbe99f8994193cea337b400acbafe3 --- libsrc/leddevice/LedDeviceFactory.cpp | 10 +++- libsrc/leddevice/LedDevicePaintpack.cpp | 65 ++++++------------------- libsrc/leddevice/LedDevicePaintpack.h | 26 ++-------- 3 files changed, 27 insertions(+), 74 deletions(-) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index d5d40611..85f665a5 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -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; diff --git a/libsrc/leddevice/LedDevicePaintpack.cpp b/libsrc/leddevice/LedDevicePaintpack.cpp index 7ed0c847..db87bf49 100644 --- a/libsrc/leddevice/LedDevicePaintpack.cpp +++ b/libsrc/leddevice/LedDevicePaintpack.cpp @@ -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 & 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& 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& 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()); } diff --git a/libsrc/leddevice/LedDevicePaintpack.h b/libsrc/leddevice/LedDevicePaintpack.h index 327dc123..3ed91eab 100644 --- a/libsrc/leddevice/LedDevicePaintpack.h +++ b/libsrc/leddevice/LedDevicePaintpack.h @@ -3,34 +3,19 @@ // STL includes #include -// libusb include -#include - // Hyperion includes -#include +#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 _ledBuffer; - - };