Disentangle LedDevice/LinearColorSmoothing, Bug Fixes & Test support (#654)

* Handle Exceptions in main & Pythoninit

* Have SSDPDiscover generic again

* Have SSDPDiscover generic again

* Change Info- to Debug logs as technical service messages

* Nanoleaf - When switched on, ensure UDP mode

* Include SQL Database in Cross-Compile instructions

* Fix Clazy (QT code checker) and clang Warnings

* Stop LedDevice:write for disabled device

* Nanoleaf: Fix uint printfs

* NanoLeaf: Fix indents to tabs

* NanoLeaf - Add debug verbosity switches

* Device switchability support, FileDevice with timestamp support

* Nanoleaf Light Panels now support External Control V2

* Enhance LedDeviceFile by Timestamp + fix readyness

* Stop color stream, if LedDevice disabled

* Nanoleaf - remove switchability

* Fix MultiColorAdjustment, if led-range is greater lednum

* Fix logging

* LedFileDevice/LedDevice - add testing support

* New "Led Test" effect

* LedDeviceFile - Add chrono include + Allow Led rewrites for testing

* Stabilize Effects for LedDevices where latchtime = 0

* Update LedDeviceFile, allow latchtime = 0

* Distangle LinearColorSmoothing and LEDDevice, Fix Effect configuration updates

* Updates LedDeviceFile - Initialize via Open

* Updates LedDeviceNanoleaf - Initialize via Open, Remove throwing exceptions

* Updates ProviderUDP - Remove throwing exceptions

* Framebuffer - Use precise timer

* TestSpi - Align to LedDevice updates

* Pretty Print CrossCompileHowTo as markdown-file

* Ensure that output is only written when LedDevice is ready

* Align APA102 Device to new device staging

* Logger - Remove clang warnings on extra semicolon

* Devices SPI - Align to Device stages and methods

* Fix cppcheck and clang findings

* Add Code-Template for new Devices

* Align devices to stages and methods, clean-up some code

* Allow to reopen LedDevice without restart

* Revert change "Remove Connect (PriorityMuxer::visiblePriorityChanged -> Hyperion::update) due to double writes"

* Remove visiblePriorityChanged from LedDevice to decouple LedDevice from hyperion logic

* Expose LedDevice getLedCount and align signedness
This commit is contained in:
LordGrey
2020-02-10 15:21:58 +01:00
committed by GitHub
parent 1aba51e85c
commit ed5455458b
107 changed files with 2980 additions and 1551 deletions

View File

@@ -16,11 +16,108 @@ LedDeviceHyperionUsbasp::LedDeviceHyperionUsbasp(const QJsonObject &deviceConfig
, _libusbContext(nullptr)
, _deviceHandle(nullptr)
{
init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDeviceHyperionUsbasp::~LedDeviceHyperionUsbasp()
{
}
LedDevice* LedDeviceHyperionUsbasp::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceHyperionUsbasp(deviceConfig);
}
bool LedDeviceHyperionUsbasp::init(const QJsonObject &deviceConfig)
{
bool isInitOK = LedDevice::init(deviceConfig);
QString ledType = deviceConfig["ledType"].toString("ws2801");
if (ledType != "ws2801" && ledType != "ws2812")
{
QString errortext = QString ("Invalid ledType; must be 'ws2801' or 'ws2812'.");
this->setInError(errortext);
isInitOK = false;
}
else
{
_writeLedsCommand = (ledType == "ws2801") ? CMD_WRITE_WS2801 : CMD_WRITE_WS2812;
}
return isInitOK;
}
int LedDeviceHyperionUsbasp::open()
{
int retval = -1;
QString errortext;
_deviceReady = false;
// General initialisation and configuration of LedDevice
if ( init(_devConfig) )
{
int error;
// initialize the usb context
if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS)
{
//Error(_log, "Error while initializing USB context(%d):%s", error, libusb_error_name(error));
errortext = QString ("Error while initializing USB context(%1):%2").arg( error).arg(libusb_error_name(error));
_libusbContext = nullptr;
}
else
{
//libusb_set_debug(_libusbContext, 3);
Debug(_log, "USB context initialized");
// retrieve the list of usb devices
libusb_device ** deviceList;
ssize_t deviceCount = libusb_get_device_list(_libusbContext, &deviceList);
// iterate the list of devices
for (ssize_t i = 0 ; i < deviceCount; ++i)
{
// try to open and initialize the device
error = testAndOpen(deviceList[i]);
if (error == 0)
{
// a device was sucessfully opened. break from list
break;
}
}
// free the device list
libusb_free_device_list(deviceList, 1);
if (_deviceHandle == nullptr)
{
//Error(_log, "No %s has been found", QSTRING_CSTR(_usbProductDescription));
errortext = QString ("No %1 has been found").arg( _usbProductDescription);
}
else
{
// Everything is OK -> enable device
_deviceReady = true;
setEnable(true);
retval = 0;
}
}
// On error/exceptions, set LedDevice in error
if ( retval < 0 )
{
this->setInError( errortext );
}
}
return retval;
}
void LedDeviceHyperionUsbasp::close()
{
LedDevice::close();
// LedDevice specific closing activites
if (_deviceHandle != nullptr)
{
libusb_release_interface(_deviceHandle, 0);
@@ -37,69 +134,6 @@ LedDeviceHyperionUsbasp::~LedDeviceHyperionUsbasp()
}
}
bool LedDeviceHyperionUsbasp::init(const QJsonObject &deviceConfig)
{
LedDevice::init(deviceConfig);
QString ledType = deviceConfig["ledType"].toString("ws2801");
if (ledType != "ws2801" && ledType != "ws2812")
{
throw std::runtime_error("HyperionUsbasp: invalid ledType; must be 'ws2801' or 'ws2812'.");
}
_writeLedsCommand = (ledType == "ws2801") ? CMD_WRITE_WS2801 : CMD_WRITE_WS2812;
return true;
}
LedDevice* LedDeviceHyperionUsbasp::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceHyperionUsbasp(deviceConfig);
}
int LedDeviceHyperionUsbasp::open()
{
int error;
// initialize the usb context
if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS)
{
Error(_log, "Error while initializing USB context(%d):%s", error, libusb_error_name(error));
_libusbContext = nullptr;
return -1;
}
//libusb_set_debug(_libusbContext, 3);
Debug(_log, "USB context initialized");
// retrieve the list of usb devices
libusb_device ** deviceList;
ssize_t deviceCount = libusb_get_device_list(_libusbContext, &deviceList);
// iterate the list of devices
for (ssize_t i = 0 ; i < deviceCount; ++i)
{
// try to open and initialize the device
error = testAndOpen(deviceList[i]);
if (error == 0)
{
// a device was sucessfully opened. break from list
break;
}
}
// free the device list
libusb_free_device_list(deviceList, 1);
if (_deviceHandle == nullptr)
{
Error(_log, "No %s has been found", QSTRING_CSTR(_usbProductDescription));
}
return _deviceHandle == nullptr ? -1 : 0;
}
int LedDeviceHyperionUsbasp::testAndOpen(libusb_device * device)
{
libusb_device_descriptor deviceDescriptor;
@@ -121,6 +155,7 @@ int LedDeviceHyperionUsbasp::testAndOpen(libusb_device * device)
Info(_log, "%s found: bus=%d address=%d", QSTRING_CSTR(_usbProductDescription), busNumber, addressNumber);
// TODO: Check, if exceptions via try/catch need to be replaced in Qt environment
try
{
_deviceHandle = openDevice(device);

View File

@@ -27,14 +27,14 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceHyperionUsbasp(const QJsonObject &deviceConfig);
explicit LedDeviceHyperionUsbasp(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
bool init(const QJsonObject &deviceConfig);
bool init(const QJsonObject &deviceConfig) override;
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
@@ -42,16 +42,23 @@ public:
///
/// Destructor of the LedDevice; closes the output device if it is open
///
virtual ~LedDeviceHyperionUsbasp();
virtual ~LedDeviceHyperionUsbasp() override;
public slots:
///
/// Closes the output device.
/// Includes switching-off the device and stopping refreshes
///
virtual void close() override;
protected:
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
int open();
int open() override;
protected:
///
/// Writes the RGB-Color values to the leds.
///
@@ -59,7 +66,7 @@ protected:
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
virtual int write(const std::vector<ColorRgb>& ledValues) override;
///
/// Test if the device is a Hyperion Usbasp device

View File

@@ -43,16 +43,118 @@ LedDeviceLightpack::LedDeviceLightpack(const QString & serialNumber)
, _bitsPerChannel(-1)
, _hwLedCount(-1)
{
_deviceReady = false;
}
LedDeviceLightpack::LedDeviceLightpack(const QJsonObject &deviceConfig)
: LedDevice()
, _libusbContext(nullptr)
, _deviceHandle(nullptr)
, _busNumber(-1)
, _addressNumber(-1)
, _firmwareVersion({-1,-1})
, _bitsPerChannel(-1)
, _hwLedCount(-1)
{
init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDeviceLightpack::~LedDeviceLightpack()
{
}
LedDevice* LedDeviceLightpack::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceLightpack(deviceConfig);
}
bool LedDeviceLightpack::init(const QJsonObject &deviceConfig)
{
bool isInitOK = LedDevice::init(deviceConfig);
_serialNumber = deviceConfig["output"].toString("");
return isInitOK;
}
int LedDeviceLightpack::open()
{
int retval = -1;
QString errortext;
_deviceReady = false;
// General initialisation and configuration of LedDevice
if ( init(_devConfig) )
{
int error;
// initialize the usb context
if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS)
{
//Error(_log, "Error while initializing USB context(%d): %s", error, libusb_error_name(error));
errortext = QString ("Error while initializing USB context(%1):%2").arg( error).arg(libusb_error_name(error));
_libusbContext = nullptr;
}
else
{
//libusb_set_debug(_libusbContext, 3);
Debug(_log, "USB context initialized");
// retrieve the list of usb devices
libusb_device ** deviceList;
ssize_t deviceCount = libusb_get_device_list(_libusbContext, &deviceList);
// iterate the list of devices
for (ssize_t i = 0 ; i < deviceCount; ++i)
{
// try to open and initialize the device
error = testAndOpen(deviceList[i], _serialNumber);
if (error == 0)
{
// a device was sucessfully opened. break from list
break;
}
}
// free the device list
libusb_free_device_list(deviceList, 1);
if (_deviceHandle == nullptr)
{
if (_serialNumber.isEmpty())
{
//Warning(_log, "No Lightpack device has been found");
errortext = QString ("No Lightpack devices were found");
}
else
{
//Error(_log,"No Lightpack device has been found with serial %", QSTRING_CSTR(_serialNumber));
errortext = QString ("No Lightpack device has been found with serial %1").arg( _serialNumber);
}
}
else
{
// Everything is OK -> enable device
_deviceReady = true;
setEnable(true);
retval = 0;
}
}
// On error/exceptions, set LedDevice in error
if ( retval < 0 )
{
this->setInError( errortext );
}
}
return retval;
}
void LedDeviceLightpack::close()
{
LedDevice::close();
// LedDevice specific closing activites
if (_deviceHandle != nullptr)
{
libusb_release_interface(_deviceHandle, LIGHTPACK_INTERFACE);
@@ -69,68 +171,6 @@ LedDeviceLightpack::~LedDeviceLightpack()
}
}
bool LedDeviceLightpack::init(const QJsonObject &deviceConfig)
{
LedDevice::init(deviceConfig);
_serialNumber = deviceConfig["output"].toString("");
return true;
}
LedDevice* LedDeviceLightpack::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceLightpack(deviceConfig);
}
int LedDeviceLightpack::open()
{
int error;
// initialize the usb context
if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS)
{
Error(_log, "Error while initializing USB context(%d): %s", error, libusb_error_name(error));
_libusbContext = nullptr;
return -1;
}
//libusb_set_debug(_libusbContext, 3);
Debug(_log, "USB context initialized");
// retrieve the list of usb devices
libusb_device ** deviceList;
ssize_t deviceCount = libusb_get_device_list(_libusbContext, &deviceList);
// iterate the list of devices
for (ssize_t i = 0 ; i < deviceCount; ++i)
{
// try to open and initialize the device
error = testAndOpen(deviceList[i], _serialNumber);
if (error == 0)
{
// a device was sucessfully opened. break from list
break;
}
}
// free the device list
libusb_free_device_list(deviceList, 1);
if (_deviceHandle == nullptr)
{
if (_serialNumber.isEmpty())
{
Warning(_log, "No Lightpack device has been found");
}
else
{
Error(_log,"No Lightpack device has been found with serial %s", QSTRING_CSTR(_serialNumber));
}
}
return _deviceHandle == nullptr ? -1 : 0;
}
int LedDeviceLightpack::testAndOpen(libusb_device * device, const QString & requestedSerialNumber)
{
libusb_device_descriptor deviceDescriptor;
@@ -154,6 +194,7 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const QString & requ
QString serialNumber;
if (deviceDescriptor.iSerialNumber != 0)
{
// TODO: Check, if exceptions via try/catch need to be replaced in Qt environment
try
{
serialNumber = LedDeviceLightpack::getString(device, deviceDescriptor.iSerialNumber);
@@ -171,6 +212,7 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const QString & requ
if (requestedSerialNumber.isEmpty() || requestedSerialNumber == serialNumber)
{
// This is it!
// TODO: Check, if exceptions via try/catch need to be replaced in Qt environment
try
{
_deviceHandle = openDevice(device);
@@ -252,7 +294,7 @@ int LedDeviceLightpack::write(const std::vector<ColorRgb> &ledValues)
int LedDeviceLightpack::write(const ColorRgb * ledValues, int size)
{
int count = qMin(_hwLedCount, _ledCount);
int count = qMin(_hwLedCount, static_cast<int>( _ledCount));
for (int i = 0; i < count ; ++i)
{
@@ -275,8 +317,13 @@ int LedDeviceLightpack::write(const ColorRgb * ledValues, int size)
int LedDeviceLightpack::switchOff()
{
unsigned char buf[1] = {CMD_OFF_ALL};
return writeBytes(buf, sizeof(buf)) == sizeof(buf);
int rc = LedDevice::switchOff();
if ( _deviceReady )
{
unsigned char buf[1] = {CMD_OFF_ALL};
rc = writeBytes(buf, sizeof(buf)) == sizeof(buf);
}
return rc;
}
const QString &LedDeviceLightpack::getSerialNumber() const
@@ -284,11 +331,6 @@ const QString &LedDeviceLightpack::getSerialNumber() const
return _serialNumber;
}
int LedDeviceLightpack::getLedCount() const
{
return _ledCount;
}
int LedDeviceLightpack::writeBytes(uint8_t *data, int size)
{
// std::cout << "Writing " << size << " bytes: ";

View File

@@ -26,14 +26,14 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceLightpack(const QJsonObject &deviceConfig);
explicit LedDeviceLightpack(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
bool init(const QJsonObject &deviceConfig);
bool init(const QJsonObject &deviceConfig) override;
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
@@ -41,14 +41,14 @@ public:
///
/// Destructor of the LedDevice; closes the output device if it is open
///
virtual ~LedDeviceLightpack();
virtual ~LedDeviceLightpack() override;
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
int open();
int open() override;
///
/// Writes the RGB-Color values to the leds.
@@ -65,13 +65,19 @@ public:
///
/// @return Zero on success else negative
///
virtual int switchOff();
virtual int switchOff() override;
/// Get the serial of the Lightpack
const QString & getSerialNumber() const;
/// Get the number of leds
int getLedCount() const;
public slots:
///
/// Closes the output device.
/// Includes switching-off the device and stopping refreshes
///
virtual void close() override;
protected:
private:
///

View File

@@ -21,7 +21,8 @@ LedDeviceMultiLightpack::LedDeviceMultiLightpack(const QJsonObject &deviceConfig
: LedDevice()
, _lightpacks()
{
LedDevice::init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDeviceMultiLightpack::~LedDeviceMultiLightpack()
@@ -39,39 +40,58 @@ LedDevice* LedDeviceMultiLightpack::construct(const QJsonObject &deviceConfig)
int LedDeviceMultiLightpack::open()
{
// retrieve a list with Lightpack serials
QStringList serialList = getLightpackSerials();
int retval = -1;
QString errortext;
_deviceReady = false;
// sort the list of Lightpacks based on the serial to get a fixed order
std::sort(_lightpacks.begin(), _lightpacks.end(), compareLightpacks);
// open each lightpack device
foreach (auto serial , serialList)
// General initialisation and configuration of LedDevice
if ( init(_devConfig) )
{
LedDeviceLightpack * device = new LedDeviceLightpack(serial);
int error = device->open();
// retrieve a list with Lightpack serials
QStringList serialList = getLightpackSerials();
if (error == 0)
// sort the list of Lightpacks based on the serial to get a fixed order
std::sort(_lightpacks.begin(), _lightpacks.end(), compareLightpacks);
// open each lightpack device
foreach (auto serial , serialList)
{
_lightpacks.push_back(device);
LedDeviceLightpack * device = new LedDeviceLightpack(serial);
int error = device->open();
if (error == 0)
{
_lightpacks.push_back(device);
}
else
{
//Error(_log, "Error while creating Lightpack device with serial %s", QSTRING_CSTR(serial));
errortext = QString ("Error while creating Lightpack device with serial %1").arg( serial );
delete device;
}
}
if (_lightpacks.size() == 0)
{
//Warning(_log, "No Lightpack devices were found");
errortext = QString ("No Lightpack devices were found");
}
else
{
Error(_log, "Error while creating Lightpack device with serial %s", QSTRING_CSTR(serial));
delete device;
Info(_log, "%d Lightpack devices were found", _lightpacks.size());
// Everything is OK -> enable device
_deviceReady = true;
setEnable(true);
retval = 0;
}
// On error/exceptions, set LedDevice in error
if ( retval < 0 )
{
this->setInError( errortext );
}
}
if (_lightpacks.size() == 0)
{
Warning(_log, "No Lightpack devices were found");
}
else
{
Info(_log, "%d Lightpack devices were found", _lightpacks.size());
}
return _lightpacks.size() > 0 ? 0 : -1;
return retval;
}
int LedDeviceMultiLightpack::write(const std::vector<ColorRgb> &ledValues)
@@ -81,7 +101,7 @@ int LedDeviceMultiLightpack::write(const std::vector<ColorRgb> &ledValues)
for (LedDeviceLightpack * device : _lightpacks)
{
int count = qMin(device->getLedCount(), size);
int count = qMin(static_cast<int>( device->getLedCount()), size);
if (count > 0)
{
@@ -151,6 +171,7 @@ QStringList LedDeviceMultiLightpack::getLightpackSerials()
QString serialNumber;
if (deviceDescriptor.iSerialNumber != 0)
{
// TODO: Check, if exceptions via try/catch need to be replaced in Qt environment
try
{
serialNumber = LedDeviceMultiLightpack::getString(deviceList[i], deviceDescriptor.iSerialNumber);

View File

@@ -22,29 +22,30 @@ public:
///
/// Constructs specific LedDevice
///
LedDeviceMultiLightpack(const QJsonObject &);
explicit LedDeviceMultiLightpack(const QJsonObject &);
///
/// Destructor of the LedDevice; closes the output device if it is open
///
virtual ~LedDeviceMultiLightpack();
virtual ~LedDeviceMultiLightpack() override;
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
///
virtual int switchOff() override;
protected:
///
/// Opens and configures the output device7
///
/// @return Zero on succes else negative
///
int open();
int open() override;
///
/// Switch the leds off
///
/// @return Zero on success else negative
///
virtual int switchOff();
private:
///
@@ -54,7 +55,7 @@ private:
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
virtual int write(const std::vector<ColorRgb>& ledValues) override;
static QStringList getLightpackSerials();
static QString getString(libusb_device * device, int stringDescriptorIndex);

View File

@@ -4,12 +4,10 @@
LedDevicePaintpack::LedDevicePaintpack(const QJsonObject &deviceConfig)
: ProviderHID()
{
ProviderHID::init(deviceConfig);
_useFeature = false;
_devConfig = deviceConfig;
_deviceReady = false;
_ledBuffer.resize(_ledRGBCount + 2, uint8_t(0));
_ledBuffer[0] = 3;
_ledBuffer[1] = 0;
_useFeature = false;
}
LedDevice* LedDevicePaintpack::construct(const QJsonObject &deviceConfig)
@@ -17,6 +15,17 @@ LedDevice* LedDevicePaintpack::construct(const QJsonObject &deviceConfig)
return new LedDevicePaintpack(deviceConfig);
}
bool LedDevicePaintpack::init(const QJsonObject &deviceConfig)
{
bool isInitOK = ProviderHID::init(deviceConfig);
_ledBuffer.resize(_ledRGBCount + 2, uint8_t(0));
_ledBuffer[0] = 3;
_ledBuffer[1] = 0;
return isInitOK;
}
int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues)
{
auto bufIt = _ledBuffer.begin()+2;

View File

@@ -14,11 +14,18 @@ public:
///
/// @param deviceConfig json device config
///
LedDevicePaintpack(const QJsonObject &deviceConfig);
explicit LedDevicePaintpack(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
/// Writes the RGB-Color values to the leds.
@@ -27,5 +34,5 @@ private:
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
virtual int write(const std::vector<ColorRgb>& ledValues) override;
};

View File

@@ -4,9 +4,10 @@
LedDeviceRawHID::LedDeviceRawHID(const QJsonObject &deviceConfig)
: ProviderHID()
{
ProviderHID::init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
_useFeature = true;
_ledBuffer.resize(_ledRGBCount);
}
LedDevice* LedDeviceRawHID::construct(const QJsonObject &deviceConfig)
@@ -14,14 +15,18 @@ LedDevice* LedDeviceRawHID::construct(const QJsonObject &deviceConfig)
return new LedDeviceRawHID(deviceConfig);
}
bool LedDeviceRawHID::init(const QJsonObject &deviceConfig)
{
bool isInitOK = ProviderHID::init(deviceConfig);
_ledBuffer.resize(_ledRGBCount);
return isInitOK;
}
int LedDeviceRawHID::write(const std::vector<ColorRgb> & ledValues)
{
// write data
memcpy(_ledBuffer.data(), ledValues.data(), _ledRGBCount);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
void LedDeviceRawHID::rewriteLeds()
{
writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@@ -18,14 +18,17 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceRawHID(const QJsonObject &deviceConfig);
explicit LedDeviceRawHID(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
private slots:
/// Write the last data to the leds again
void rewriteLeds();
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -34,5 +37,5 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);
virtual int write(const std::vector<ColorRgb> & ledValues) override;
};

View File

@@ -10,26 +10,22 @@
#include "ProviderHID.h"
ProviderHID::ProviderHID()
: _useFeature(false)
, _deviceHandle(nullptr)
, _blockedForDelay(false)
: _VendorId(0)
, _ProductId(0)
, _useFeature(false)
, _deviceHandle(nullptr)
, _delayAfterConnect_ms (0)
, _blockedForDelay(false)
{
}
ProviderHID::~ProviderHID()
{
if (_deviceHandle != nullptr)
{
hid_close(_deviceHandle);
_deviceHandle = nullptr;
}
hid_exit();
}
bool ProviderHID::init(const QJsonObject &deviceConfig)
{
LedDevice::init(deviceConfig);
bool isInitOK = LedDevice::init(deviceConfig);
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
auto VendorIdString = deviceConfig["VID"].toString("0x2341").toStdString();
@@ -39,64 +35,94 @@ bool ProviderHID::init(const QJsonObject &deviceConfig)
_VendorId = std::stoul(VendorIdString, nullptr, 16);
_ProductId = std::stoul(ProductIdString, nullptr, 16);
return true;
return isInitOK;
}
int ProviderHID::open()
{
// Initialize the usb context
int error = hid_init();
if (error != 0)
{
Error(_log, "Error while initializing the hidapi context");
return -1;
}
Debug(_log,"Hidapi initialized");
int retval = -1;
QString errortext;
_deviceReady = false;
// Open the device
Info(_log, "Opening device: VID %04hx PID %04hx\n", _VendorId, _ProductId);
_deviceHandle = hid_open(_VendorId, _ProductId, nullptr);
if (_deviceHandle == nullptr)
if ( init(_devConfig) )
{
// Failed to open the device
Error(_log,"Failed to open HID device. Maybe your PID/VID setting is wrong? Make sure to add a udev rule/use sudo.");
// http://www.signal11.us/oss/hidapi/
/*
std::cout << "Showing a list of all available HID devices:" << std::endl;
auto devs = hid_enumerate(0x00, 0x00);
auto cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf("\n");
cur_dev = cur_dev->next;
// Initialize the usb context
int error = hid_init();
if (error != 0)
{
//Error(_log, "Error while initializing the hidapi context");
errortext = "Error while initializing the hidapi context";
}
hid_free_enumeration(devs);
*/
return -1;
}
else
{
Info(_log,"Opened HID device successful");
}
// Wait after device got opened if enabled
if (_delayAfterConnect_ms > 0)
{
_blockedForDelay = true;
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
}
else
{
Debug(_log,"Hidapi initialized");
return 0;
// Open the device
Info(_log, "Opening device: VID %04hx PID %04hx\n", _VendorId, _ProductId);
_deviceHandle = hid_open(_VendorId, _ProductId, nullptr);
if (_deviceHandle == nullptr)
{
// Failed to open the device
Error(_log,"Failed to open HID device. Maybe your PID/VID setting is wrong? Make sure to add a udev rule/use sudo.");
errortext = "Failed to open HID device";
// http://www.signal11.us/oss/hidapi/
/*
std::cout << "Showing a list of all available HID devices:" << std::endl;
auto devs = hid_enumerate(0x00, 0x00);
auto cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf("\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
*/
}
else
{
Info(_log,"Opened HID device successful");
// Everything is OK -> enable device
_deviceReady = true;
setEnable(true);
retval = 0;
}
// Wait after device got opened if enabled
if (_delayAfterConnect_ms > 0)
{
_blockedForDelay = true;
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
}
}
// On error/exceptions, set LedDevice in error
if ( retval < 0 )
{
this->setInError( errortext );
}
}
return retval;
}
void ProviderHID::close()
{
LedDevice::close();
// LedDevice specific closing activites
if (_deviceHandle != nullptr)
{
hid_close(_deviceHandle);
_deviceHandle = nullptr;
}
hid_exit();
}
int ProviderHID::writeBytes(const unsigned size, const uint8_t * data)
{

View File

@@ -24,22 +24,30 @@ public:
///
/// Destructor of the LedDevice; closes the output device if it is open
///
virtual ~ProviderHID();
virtual ~ProviderHID() override;
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig) override;
public slots:
///
/// Closes the output device.
/// Includes switching-off the device and stopping refreshes
///
virtual void close() override;
protected:
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
int open();
protected:
int open() override;
/**
* Writes the given bytes to the HID-device and
*

View File

@@ -242,11 +242,6 @@ const std::string &LedDeviceLightpackHidapi::getSerialNumber() const
return _serialNumber;
}
int LedDeviceLightpackHidapi::getLedCount() const
{
return _ledCount;
}
int LedDeviceLightpackHidapi::writeBytes(uint8_t *data, int size)
{
// std::cout << "Writing " << size << " bytes: ";

View File

@@ -57,9 +57,6 @@ public:
/// Get the serial of the Lightpack
const std::string & getSerialNumber() const;
/// Get the number of leds
int getLedCount() const;
private:
///
/// Writes the RGB-Color values to the leds.