From 599afe675f79ed006ec8a35499f918480c1f3473 Mon Sep 17 00:00:00 2001 From: "T. van der Zwan" Date: Tue, 10 Dec 2013 19:28:27 +0000 Subject: [PATCH] Added first implementation of Paintpack device Former-commit-id: 0ef7025a3ad67aebb0268e888b26b1d5095e27d8 --- libsrc/hyperion/CMakeLists.txt | 3 + libsrc/hyperion/Hyperion.cpp | 8 ++ libsrc/hyperion/device/LedDevicePaintpack.cpp | 77 +++++++++++++++++++ libsrc/hyperion/device/LedDevicePaintpack.h | 59 ++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 libsrc/hyperion/device/LedDevicePaintpack.cpp create mode 100644 libsrc/hyperion/device/LedDevicePaintpack.h diff --git a/libsrc/hyperion/CMakeLists.txt b/libsrc/hyperion/CMakeLists.txt index 8a70c46a..a75b23bd 100644 --- a/libsrc/hyperion/CMakeLists.txt +++ b/libsrc/hyperion/CMakeLists.txt @@ -49,6 +49,7 @@ SET(Hyperion_HEADERS ${CURRENT_SOURCE_DIR}/device/LedDeviceLpd6803.h ${CURRENT_SOURCE_DIR}/device/LedDeviceLpd8806.h ${CURRENT_SOURCE_DIR}/device/LedDeviceLightpack.h + ${CURRENT_SOURCE_DIR}/device/LedDevicePaintpack.h ${CURRENT_SOURCE_DIR}/device/LedDeviceMultiLightpack.h ) @@ -74,6 +75,7 @@ SET(Hyperion_SOURCES ${CURRENT_SOURCE_DIR}/device/LedDeviceLpd8806.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceAdalight.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceLightpack.cpp + ${CURRENT_SOURCE_DIR}/device/LedDevicePaintpack.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceMultiLightpack.cpp ) @@ -95,6 +97,7 @@ add_library(hyperion target_link_libraries(hyperion hyperion-utils + hidapi-libusb serialport ${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev ${CMAKE_THREAD_LIBS_INIT} diff --git a/libsrc/hyperion/Hyperion.cpp b/libsrc/hyperion/Hyperion.cpp index 8c746ef9..b9ae565e 100644 --- a/libsrc/hyperion/Hyperion.cpp +++ b/libsrc/hyperion/Hyperion.cpp @@ -22,6 +22,7 @@ #include "device/LedDeviceTest.h" #include "device/LedDeviceWs2801.h" #include "device/LedDeviceAdalight.h" +#include "device/LedDevicePaintpack.h" #include "device/LedDeviceLightpack.h" #include "device/LedDeviceMultiLightpack.h" @@ -95,6 +96,13 @@ LedDevice* Hyperion::createDevice(const Json::Value& deviceConfig) device = deviceLightpack; } + else if (type == "paintpack") + { + LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack(); + devicePainLightpack->open(); + + device = devicePainLightpack; + } else if (type == "multi-lightpack") { LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack(); diff --git a/libsrc/hyperion/device/LedDevicePaintpack.cpp b/libsrc/hyperion/device/LedDevicePaintpack.cpp new file mode 100644 index 00000000..7ed0c847 --- /dev/null +++ b/libsrc/hyperion/device/LedDevicePaintpack.cpp @@ -0,0 +1,77 @@ + +// Hyperion includes +#include "LedDevicePaintpack.h" + +LedDevicePaintpack::LedDevicePaintpack() : + LedDevice(), + _deviceHandle(nullptr) +{ + // 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& 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; + for (const ColorRgb & ledValue : ledValues) + { + *bufIt = ledValue.red; + ++bufIt; + *bufIt = ledValue.green; + ++bufIt; + *bufIt = ledValue.blue; + ++bufIt; + } + + return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size()); +} + +int LedDevicePaintpack::switchOff() +{ + std::fill(_ledBuffer.begin()+3, _ledBuffer.end(), uint8_t(0)); + return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size()); +} diff --git a/libsrc/hyperion/device/LedDevicePaintpack.h b/libsrc/hyperion/device/LedDevicePaintpack.h new file mode 100644 index 00000000..8247bfb6 --- /dev/null +++ b/libsrc/hyperion/device/LedDevicePaintpack.h @@ -0,0 +1,59 @@ +#pragma once + +// STL includes +#include + +// libusb include +#include + +// Hyperion includes +#include + +/// +/// LedDevice implementation for a paintpack device () +/// +class LedDevicePaintpack : public LedDevice +{ +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(); + + /// + /// Writes the RGB-Color values to the leds. + /// + /// @param[in] ledValues The RGB-color per led + /// + /// @return Zero on success else negative + /// + virtual int write(const std::vector& ledValues); + + /// + /// Switch the leds off + /// + /// @return Zero on success else negative + /// + virtual int switchOff(); + +private: + /// libusb device handle + hid_device * _deviceHandle; + + /// buffer for led data + std::vector _ledBuffer; + + +};