mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
LED Device Features, Fixes and Refactoring (Resubmit PR855) (#875)
* Refactor LedDevices - Initial version * Small renamings * Add WLED as own device * Lpd8806 Remove open() method * remove dependency on Qt 5.10 * Lpd8806 Remove open() method * Update WS281x * Update WS2812SPI * Add writeBlack for WLED powerOff * WLED remove extra bracket * Allow different Nanoleaf panel numbering sequence (Feature req.#827) * build(deps): bump websocket-extensions from 0.1.3 to 0.1.4 in /docs (#826) * Bumps [websocket-extensions](https://github.com/faye/websocket-extensions-node) from 0.1.3 to 0.1.4. - [Release notes](https://github.com/faye/websocket-extensions-node/releases) - [Changelog](https://github.com/faye/websocket-extensions-node/blob/master/CHANGELOG.md) - [Commits](https://github.com/faye/websocket-extensions-node/compare/0.1.3...0.1.4) * Fix typos * Nanoleaf clean-up * Yeelight support, generalize wizard elements * Update Yeelight to handle quota in music mode * Yeelight extend rage for extraTimeDarkness for testing * Clean-up - Add commentary, Remove development debug statements * Fix brightnessSwitchOffOnMinimum typo and default value * Yeelight support restoreOriginalState, additional Fixes * WLED - Remove UDP-Port, as it is not configurable * Fix merging issue * Remove QHostAddress::operator=(const QString&)' is deprecated * Windows compile errors and (Qt 5.15 deprecation) warnings * Fix order includes * LedDeviceFile Support Qt5.7 and greater * Windows compatibility and other Fixes * Fix Qt Version compatability * Rs232 - Resolve portname from unix /dev/ style, fix DMX sub-type support * Disable WLED Wizard Button (until Wizard is available) * Yeelight updates * Add wrong log-type as per #505 * Fixes and Clean-up after clang-tidy report * Fix udpe131 not enabled for generated CID * Change timer into dynamic for Qt Thread-Affinity * Hue clean-up and diyHue workaround * Updates after review feedback by m-seker * Add "chrono" includes
This commit is contained in:
@@ -21,54 +21,59 @@ ProviderHID::ProviderHID()
|
||||
|
||||
ProviderHID::~ProviderHID()
|
||||
{
|
||||
if (_deviceHandle != nullptr)
|
||||
{
|
||||
hid_close(_deviceHandle);
|
||||
}
|
||||
hid_exit();
|
||||
}
|
||||
|
||||
bool ProviderHID::init(const QJsonObject &deviceConfig)
|
||||
{
|
||||
bool isInitOK = LedDevice::init(deviceConfig);
|
||||
bool isInitOK = false;
|
||||
|
||||
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
|
||||
auto VendorIdString = deviceConfig["VID"].toString("0x2341").toStdString();
|
||||
auto ProductIdString = deviceConfig["PID"].toString("0x8036").toStdString();
|
||||
// Initialise sub-class
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
{
|
||||
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
|
||||
auto VendorIdString = deviceConfig["VID"].toString("0x2341").toStdString();
|
||||
auto ProductIdString = deviceConfig["PID"].toString("0x8036").toStdString();
|
||||
|
||||
// Convert HEX values to integer
|
||||
_VendorId = std::stoul(VendorIdString, nullptr, 16);
|
||||
_ProductId = std::stoul(ProductIdString, nullptr, 16);
|
||||
// Convert HEX values to integer
|
||||
_VendorId = std::stoul(VendorIdString, nullptr, 16);
|
||||
_ProductId = std::stoul(ProductIdString, nullptr, 16);
|
||||
|
||||
// Initialize the USB context
|
||||
if ( hid_init() != 0)
|
||||
{
|
||||
this->setInError("Error initializing the HIDAPI context");
|
||||
isInitOK = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug(_log,"HIDAPI initialized");
|
||||
isInitOK = true;
|
||||
}
|
||||
}
|
||||
return isInitOK;
|
||||
}
|
||||
|
||||
int ProviderHID::open()
|
||||
{
|
||||
int retval = -1;
|
||||
QString errortext;
|
||||
_deviceReady = false;
|
||||
_isDeviceReady = false;
|
||||
|
||||
if ( init(_devConfig) )
|
||||
// Open the device
|
||||
Info(_log, "Opening device: VID %04hx PID %04hx\n", _VendorId, _ProductId);
|
||||
_deviceHandle = hid_open(_VendorId, _ProductId, nullptr);
|
||||
|
||||
if (_deviceHandle == nullptr)
|
||||
{
|
||||
// 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";
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug(_log,"Hidapi initialized");
|
||||
// Failed to open the device
|
||||
this->setInError( "Failed to open HID device. Maybe your PID/VID setting is wrong? Make sure to add a udev rule/use sudo." );
|
||||
|
||||
// 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/
|
||||
/*
|
||||
// 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;
|
||||
@@ -83,45 +88,38 @@ int ProviderHID::open()
|
||||
}
|
||||
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 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Info(_log,"Opened HID device successful");
|
||||
// Everything is OK -> enable device
|
||||
_isDeviceReady = true;
|
||||
retval = 0;
|
||||
}
|
||||
|
||||
// Wait after device got opened if enabled
|
||||
if (_delayAfterConnect_ms > 0)
|
||||
{
|
||||
_blockedForDelay = true;
|
||||
QTimer::singleShot(_delayAfterConnect_ms, this, &ProviderHID::unblockAfterDelay );
|
||||
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void ProviderHID::close()
|
||||
int ProviderHID::close()
|
||||
{
|
||||
LedDevice::close();
|
||||
int retval = 0;
|
||||
_isDeviceReady = false;
|
||||
|
||||
// LedDevice specific closing activites
|
||||
// LedDevice specific closing activities
|
||||
if (_deviceHandle != nullptr)
|
||||
{
|
||||
hid_close(_deviceHandle);
|
||||
_deviceHandle = nullptr;
|
||||
}
|
||||
|
||||
hid_exit();
|
||||
return retval;
|
||||
}
|
||||
|
||||
int ProviderHID::writeBytes(const unsigned size, const uint8_t * data)
|
||||
@@ -138,7 +136,7 @@ int ProviderHID::writeBytes(const unsigned size, const uint8_t * data)
|
||||
// Try again in 3 seconds
|
||||
int delay_ms = 3000;
|
||||
_blockedForDelay = true;
|
||||
QTimer::singleShot(delay_ms, this, SLOT(unblockAfterDelay()));
|
||||
QTimer::singleShot(delay_ms, this, &ProviderHID::unblockAfterDelay );
|
||||
Debug(_log,"Device blocked for %d ms", delay_ms);
|
||||
}
|
||||
// Return here, to not write led data if the device should be blocked after connect
|
||||
@@ -188,3 +186,38 @@ void ProviderHID::unblockAfterDelay()
|
||||
Debug(_log,"Device unblocked");
|
||||
_blockedForDelay = false;
|
||||
}
|
||||
|
||||
QJsonObject ProviderHID::discover()
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
|
||||
QJsonArray deviceList;
|
||||
|
||||
// Discover HID Devices
|
||||
auto devs = hid_enumerate(0x00, 0x00);
|
||||
|
||||
if ( devs != nullptr )
|
||||
{
|
||||
auto cur_dev = devs;
|
||||
while (cur_dev)
|
||||
{
|
||||
QJsonObject deviceInfo;
|
||||
deviceInfo.insert("manufacturer",QString::fromWCharArray(cur_dev->manufacturer_string));
|
||||
deviceInfo.insert("path",cur_dev->path);
|
||||
deviceInfo.insert("productIdentifier", QString("0x%1").arg(static_cast<ushort>(cur_dev->product_id),0,16));
|
||||
deviceInfo.insert("release_number",QString("0x%1").arg(static_cast<ushort>(cur_dev->release_number),0,16));
|
||||
deviceInfo.insert("serialNumber",QString::fromWCharArray(cur_dev->serial_number));
|
||||
deviceInfo.insert("usage_page", QString("0x%1").arg(static_cast<ushort>(cur_dev->usage_page),0,16));
|
||||
deviceInfo.insert("vendorIdentifier", QString("0x%1").arg(static_cast<ushort>(cur_dev->vendor_id),0,16));
|
||||
deviceInfo.insert("interface_number",cur_dev->interface_number);
|
||||
deviceList.append(deviceInfo);
|
||||
|
||||
cur_dev = cur_dev->next;
|
||||
}
|
||||
hid_free_enumeration(devs);
|
||||
}
|
||||
|
||||
devicesDiscovered.insert("devices", deviceList);
|
||||
return devicesDiscovered;
|
||||
}
|
||||
|
Reference in New Issue
Block a user