Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Add RawHID Device + HID API Device #407

Merged
merged 5 commits into from
Nov 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libsrc/leddevice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ SET(Leddevice_QT_HEADERS
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.h
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.h
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
${CURRENT_SOURCE_DIR}/LedHIDDevice.h
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.h
)

SET(Leddevice_HEADERS
Expand All @@ -39,10 +41,12 @@ SET(Leddevice_SOURCES
${CURRENT_SOURCE_DIR}/LedDeviceFactory.cpp

${CURRENT_SOURCE_DIR}/LedRs232Device.cpp
${CURRENT_SOURCE_DIR}/LedHIDDevice.cpp

${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.cpp
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.cpp
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp
Expand Down
2 changes: 1 addition & 1 deletion libsrc/leddevice/LedDeviceAdalight.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Qt includes
#include <QTimer>

// hyperion incluse
// hyperion include
#include "LedRs232Device.h"

///
Expand Down
26 changes: 25 additions & 1 deletion libsrc/leddevice/LedDeviceFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "LedDeviceAdalight.h"
#include "LedDeviceAmbiLed.h"
#include "LedDeviceRawHID.h"
#include "LedDeviceLightpack.h"
#include "LedDeviceMultiLightpack.h"
#include "LedDevicePaintpack.h"
Expand Down Expand Up @@ -147,6 +148,21 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
device = deviceTinkerforge;
}
#endif
else if (type == "rawhid")
{
const int delay_ms = deviceConfig["delayAfterConnect"].asInt();
auto VendorIdString = deviceConfig.get("VID", "0x2341").asString();
auto ProductIdString = deviceConfig.get("PID", "0x8036").asString();

// Convert HEX values to integer
auto VendorId = std::stoul(VendorIdString, nullptr, 16);
auto ProductId = std::stoul(ProductIdString, nullptr, 16);

LedDeviceRawHID* deviceHID = new LedDeviceRawHID(VendorId, ProductId, delay_ms);
deviceHID->open();

device = deviceHID;
}
else if (type == "lightpack")
{
const std::string output = deviceConfig.get("output", "").asString();
Expand All @@ -165,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;
Expand Down
63 changes: 14 additions & 49 deletions libsrc/leddevice/LedDevicePaintpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
// 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()
int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues)
{
if (_deviceHandle != nullptr)
if (_ledBuffer.size() < 2 + ledValues.size()*3)
{
hid_close(_deviceHandle);
_deviceHandle = nullptr;
_ledBuffer.resize(2 + ledValues.size()*3, uint8_t(0));
_ledBuffer[0] = 3;
_ledBuffer[1] = 0;
}

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;
Expand All @@ -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());
}
26 changes: 3 additions & 23 deletions libsrc/leddevice/LedDevicePaintpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -49,11 +34,6 @@ class LedDevicePaintpack : public LedDevice
virtual int switchOff();

private:
/// libusb device handle
hid_device * _deviceHandle;

/// buffer for led data
std::vector<uint8_t> _ledBuffer;


};
57 changes: 57 additions & 0 deletions libsrc/leddevice/LedDeviceRawHID.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>

// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>

// hyperion local includes
#include "LedDeviceRawHID.h"

// Use feature report HID device
LedDeviceRawHID::LedDeviceRawHID(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms) :
LedHIDDevice(VendorId, ProductId, delayAfterConnect_ms, true),
_ledBuffer(0),
_timer()
{
// setup the timer
_timer.setSingleShot(false);
_timer.setInterval(5000);
connect(&_timer, SIGNAL(timeout()), this, SLOT(rewriteLeds()));

// start the timer
_timer.start();
}

int LedDeviceRawHID::write(const std::vector<ColorRgb> & ledValues)
{
// Resize buffer if required
if (_ledBuffer.size() < ledValues.size() * 3) {
_ledBuffer.resize(3 * ledValues.size());
}

// restart the timer
_timer.start();

// write data
memcpy(_ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

int LedDeviceRawHID::switchOff()
{
// restart the timer
_timer.start();

// write data
std::fill(_ledBuffer.begin(), _ledBuffer.end(), uint8_t(0));
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

void LedDeviceRawHID::rewriteLeds()
{
writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
48 changes: 48 additions & 0 deletions libsrc/leddevice/LedDeviceRawHID.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

// STL includes
#include <string>

// Qt includes
#include <QTimer>

// hyperion include
#include "LedHIDDevice.h"

///
/// Implementation of the LedDevice interface for writing to an RawHID led device.
///
class LedDeviceRawHID : public LedHIDDevice
{
Q_OBJECT

public:
///
/// Constructs the LedDevice for attached RawHID device
///
LedDeviceRawHID(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms);

///
/// Writes the led color values to the led-device
///
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);

/// Switch the leds off
virtual int switchOff();

private slots:
/// Write the last data to the leds again
void rewriteLeds();

private:
/// The buffer containing the packed RGB values
std::vector<uint8_t> _ledBuffer;

/// Timer object which makes sure that led data is written at a minimum rate
/// The RawHID device will switch off when it does not receive data at least
/// every 15 seconds
QTimer _timer;
};
Loading