2014-03-09 11:36:46 +01:00
|
|
|
// stl includes
|
|
|
|
#include <exception>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
// Local Hyperion includes
|
|
|
|
#include "LedDeviceHyperionUsbasp.h"
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Constants which define the Hyperion USBasp device
|
|
|
|
namespace {
|
|
|
|
uint16_t _usbVendorId = 0x16c0;
|
|
|
|
uint16_t _usbProductId = 0x05dc;
|
|
|
|
QString _usbProductDescription = "Hyperion led controller";
|
|
|
|
}
|
2014-03-09 11:36:46 +01:00
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDeviceHyperionUsbasp::LedDeviceHyperionUsbasp(const QJsonObject &deviceConfig)
|
2016-08-14 10:46:44 +02:00
|
|
|
: LedDevice()
|
|
|
|
, _libusbContext(nullptr)
|
|
|
|
, _deviceHandle(nullptr)
|
2014-03-09 11:36:46 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
_devConfig = deviceConfig;
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
|
|
|
|
|
|
|
_activeDeviceType = deviceConfig["type"].toString("UNSPECIFIED").toLower();
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LedDeviceHyperionUsbasp::~LedDeviceHyperionUsbasp()
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
if (_libusbContext != nullptr)
|
|
|
|
{
|
|
|
|
libusb_exit(_libusbContext);
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2014-03-09 11:36:46 +01:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
LedDevice* LedDeviceHyperionUsbasp::construct(const QJsonObject &deviceConfig)
|
|
|
|
{
|
|
|
|
return new LedDeviceHyperionUsbasp(deviceConfig);
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
bool LedDeviceHyperionUsbasp::init(const QJsonObject &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
bool isInitOK = false;
|
2016-12-02 12:07:24 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Initialise sub-class
|
|
|
|
if ( LedDevice::init(deviceConfig) )
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString ledType = deviceConfig["ledType"].toString("ws2801");
|
|
|
|
if (ledType != "ws2801" && ledType != "ws2812")
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString errortext = QString ("Invalid ledType; must be 'ws2801' or 'ws2812'.");
|
|
|
|
this->setInError(errortext);
|
|
|
|
isInitOK = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
else
|
2014-03-09 11:36:46 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
_writeLedsCommand = (ledType == "ws2801") ? CMD_WRITE_WS2801 : CMD_WRITE_WS2812;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
int error;
|
|
|
|
// initialize the USB context
|
|
|
|
if ( (error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS )
|
|
|
|
{
|
|
|
|
_libusbContext = nullptr;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
QString errortext = QString ("Error while initializing USB context(%1):%2").arg(error).arg(libusb_error_name(error));
|
|
|
|
this->setInError(errortext);
|
|
|
|
isInitOK = false;
|
|
|
|
}
|
|
|
|
else
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
Debug(_log, "USB context initialized");
|
|
|
|
//libusb_set_debug(_libusbContext, 3);
|
|
|
|
|
|
|
|
// retrieve the list of USB devices
|
|
|
|
libusb_device ** deviceList;
|
|
|
|
ssize_t deviceCount = libusb_get_device_list(_libusbContext, &deviceList);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// iterate the list of devices
|
|
|
|
for (ssize_t i = 0 ; i < deviceCount; ++i)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
// try to open and initialize the device
|
|
|
|
if ( testAndOpen(deviceList[i]) == 0 )
|
|
|
|
{
|
|
|
|
_device = deviceList[i];
|
|
|
|
// a device was successfully opened. break from list
|
|
|
|
break;
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// free the device list
|
|
|
|
libusb_free_device_list(deviceList, 1);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
if (_deviceHandle == nullptr)
|
|
|
|
{
|
|
|
|
QString errortext;
|
|
|
|
errortext = QString ("No %1 has been found").arg( _usbProductDescription);
|
|
|
|
this->setInError( errortext );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isInitOK = true;
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
return isInitOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceHyperionUsbasp::open()
|
|
|
|
{
|
|
|
|
int retval = -1;
|
|
|
|
_isDeviceReady = false;
|
|
|
|
|
|
|
|
if ( libusb_open(_device, &_deviceHandle) != LIBUSB_SUCCESS )
|
|
|
|
{
|
|
|
|
QString errortext = QString ("Failed to open [%1]").arg(_usbProductDescription);
|
|
|
|
this->setInError(errortext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Everything is OK -> enable device
|
|
|
|
_isDeviceReady = true;
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
return retval;
|
|
|
|
}
|
2014-03-09 11:36:46 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
int LedDeviceHyperionUsbasp::close()
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
int retval = 0;
|
|
|
|
_isDeviceReady = false;
|
2014-03-09 11:36:46 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// LedDevice specific closing activities
|
2020-02-10 15:21:58 +01:00
|
|
|
if (_deviceHandle != nullptr)
|
2014-03-09 11:36:46 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
libusb_release_interface(_deviceHandle, 0);
|
|
|
|
libusb_attach_kernel_driver(_deviceHandle, 0);
|
|
|
|
libusb_close(_deviceHandle);
|
|
|
|
|
|
|
|
_deviceHandle = nullptr;
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return retval;
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceHyperionUsbasp::testAndOpen(libusb_device * device)
|
|
|
|
{
|
|
|
|
libusb_device_descriptor deviceDescriptor;
|
|
|
|
int error = libusb_get_device_descriptor(device, &deviceDescriptor);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-05 09:14:40 +02:00
|
|
|
Error(_log, "Error while retrieving device descriptor(%d): %s", error, libusb_error_name(error));
|
2014-03-09 11:36:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deviceDescriptor.idVendor == _usbVendorId &&
|
|
|
|
deviceDescriptor.idProduct == _usbProductId &&
|
|
|
|
deviceDescriptor.iProduct != 0 &&
|
|
|
|
getString(device, deviceDescriptor.iProduct) == _usbProductDescription)
|
|
|
|
{
|
|
|
|
// get the hardware address
|
|
|
|
int busNumber = libusb_get_bus_number(device);
|
|
|
|
int addressNumber = libusb_get_device_address(device);
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
Info(_log, "%s found: bus=%d address=%d", QSTRING_CSTR(_usbProductDescription), busNumber, addressNumber);
|
2014-03-09 11:36:46 +01:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
// TODO: Check, if exceptions via try/catch need to be replaced in Qt environment
|
2014-03-09 11:36:46 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_deviceHandle = openDevice(device);
|
2017-03-04 22:17:42 +01:00
|
|
|
Info(_log, "%s successfully opened", QSTRING_CSTR(_usbProductDescription) );
|
2014-03-09 11:36:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch(int e)
|
|
|
|
{
|
|
|
|
_deviceHandle = nullptr;
|
2017-03-04 22:17:42 +01:00
|
|
|
Error(_log, "Unable to open %s. Searching for other device(%d): %s", QSTRING_CSTR(_usbProductDescription), e, libusb_error_name(e));
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceHyperionUsbasp::write(const std::vector<ColorRgb> &ledValues)
|
|
|
|
{
|
|
|
|
int nbytes = libusb_control_transfer(
|
|
|
|
_deviceHandle, // device handle
|
2020-07-12 20:27:56 +02:00
|
|
|
static_cast<uint8_t>( LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT ), // request type
|
2014-03-09 11:36:46 +01:00
|
|
|
_writeLedsCommand, // request
|
|
|
|
0, // value
|
|
|
|
0, // index
|
|
|
|
(uint8_t *) ledValues.data(), // data
|
|
|
|
(3*_ledCount) & 0xffff, // length
|
|
|
|
5000); // timeout
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Disabling interrupts for a little while on the device results in a PIPE error. All seems to keep functioning though...
|
2014-03-09 11:36:46 +01:00
|
|
|
if(nbytes < 0 && nbytes != LIBUSB_ERROR_PIPE)
|
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
Error(_log, "Error while writing data to %s (%s)", QSTRING_CSTR(_usbProductDescription), libusb_error_name(nbytes));
|
2014-03-09 11:36:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_device_handle * LedDeviceHyperionUsbasp::openDevice(libusb_device *device)
|
|
|
|
{
|
2016-06-25 22:08:17 +02:00
|
|
|
Logger * log = Logger::getInstance("LedDevice");
|
2014-03-09 11:36:46 +01:00
|
|
|
libusb_device_handle * handle = nullptr;
|
|
|
|
|
|
|
|
int error = libusb_open(device, &handle);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-05 09:14:40 +02:00
|
|
|
Error(log, "unable to open device(%d): %s",error,libusb_error_name(error));
|
2014-03-09 11:36:46 +01:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// detach kernel driver if it is active
|
|
|
|
if (libusb_kernel_driver_active(handle, 0) == 1)
|
|
|
|
{
|
|
|
|
error = libusb_detach_kernel_driver(handle, 0);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-05 09:14:40 +02:00
|
|
|
Error(log, "unable to detach kernel driver(%d): %s",error,libusb_error_name(error));
|
2014-03-09 11:36:46 +01:00
|
|
|
libusb_close(handle);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error = libusb_claim_interface(handle, 0);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-05 09:14:40 +02:00
|
|
|
Error(log, "unable to claim interface(%d): %s", error, libusb_error_name(error));
|
2014-03-09 11:36:46 +01:00
|
|
|
libusb_attach_kernel_driver(handle, 0);
|
|
|
|
libusb_close(handle);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
QString LedDeviceHyperionUsbasp::getString(libusb_device * device, int stringDescriptorIndex)
|
2014-03-09 11:36:46 +01:00
|
|
|
{
|
|
|
|
libusb_device_handle * handle = nullptr;
|
|
|
|
|
|
|
|
int error = libusb_open(device, &handle);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
error = libusb_get_string_descriptor_ascii(handle, stringDescriptorIndex, reinterpret_cast<unsigned char *>(buffer), sizeof(buffer));
|
|
|
|
if (error <= 0)
|
|
|
|
{
|
|
|
|
libusb_close(handle);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_close(handle);
|
2017-03-04 22:17:42 +01:00
|
|
|
return QString(QByteArray(buffer, error));
|
2014-03-09 11:36:46 +01:00
|
|
|
}
|