mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
599afe675f
Former-commit-id: 0ef7025a3ad67aebb0268e888b26b1d5095e27d8
78 lines
1.6 KiB
C++
78 lines
1.6 KiB
C++
|
|
// 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<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;
|
|
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());
|
|
}
|