hyperion.ng/libsrc/leddevice/LedDeviceMultiLightpack.cpp
Paulchen Panther 464de381a0 JsonCpp to QTJson (Final Part) (#273)
* Update CMakeLists.txt

* Update Hyperion.h

* Update LedDevice.h

* Update LedDeviceFactory.h

* Update Hyperion.cpp

* Update LedString.cpp

* Update JsonClientConnection.cpp

* Update LedDeviceAdalight.cpp

* Update LedDeviceAdalight.h

* Update LedDeviceAPA102.cpp

* Update LedDeviceAdalightApa102.h

* Update LedDeviceAdalightApa102.cpp

* Update LedDeviceAPA102.h

* Update LedDeviceAtmo.cpp

* Update LedDeviceAtmo.h

* Update LedDeviceAtmoOrb.cpp

* Update LedDeviceAtmoOrb.h

* Update LedDeviceDMX.cpp

* Update LedDeviceDMX.h

* Update LedDeviceFactory.cpp

* Update LedDeviceFadeCandy.cpp

* Update LedDeviceFadeCandy.h

* Update LedDeviceFile.cpp

* Update LedDeviceFile.h

* Update LedDeviceHyperionUsbasp.cpp

* Update LedDeviceHyperionUsbasp.h

* Update LedDeviceLightpack.cpp

* Update LedDeviceLightpack.h

* Update LedDeviceLpd6803.cpp

* Update LedDeviceLpd6803.h

* Update LedDeviceLpd8806.cpp

* Update LedDeviceLpd8806.h

* Update LedDeviceMultiLightpack.cpp

* Update LedDeviceMultiLightpack.h

* Update LedDeviceP9813.cpp

* Update LedDeviceP9813.h

* Update LedDevicePaintpack.cpp

* Update LedDevicePaintpack.h

* Update LedDevicePhilipsHue.cpp

* Update LedDevicePhilipsHue.h

* Update LedDevicePiBlaster.cpp

* Update LedDevicePiBlaster.h

* Update LedDeviceRawHID.cpp

* Update LedDeviceRawHID.h

* Update LedDeviceSedu.cpp

* Update LedDeviceSedu.h

* Update LedDeviceSk6812SPI.cpp

* Update LedDeviceSk6812SPI.h

* Update LedDeviceTinkerforge.cpp

* Update LedDeviceTinkerforge.h

* Update LedDeviceTpm2.cpp

* Update LedDeviceTpm2.h

* Update LedDeviceTpm2net.cpp

* Update LedDeviceTpm2net.h

* Update LedDeviceUdpE131.cpp

* Update LedDeviceUdpE131.h

* Update LedDeviceUdpH801.cpp

* Update LedDeviceUdpH801.h

* Update LedDeviceUdpRaw.cpp

* Update LedDeviceUdpRaw.h

* Update LedDeviceWs2801.cpp

* Update LedDeviceWs2801.h

* Update LedDeviceWS2812b.cpp

* Update LedDeviceWS2812b.h

* Update LedDeviceWs2812SPI.cpp

* Update LedDeviceWs2812SPI.h

* Update LedDeviceWS281x.cpp

* Update LedDeviceWS281x.h

* Update ProviderHID.cpp

* Update ProviderHID.h

* Update ProviderRs232.cpp

* Update ProviderRs232.h

* Update ProviderSpi.cpp

* Update ProviderSpi.h

* Update ProviderUdp.cpp

* Update ProviderUdp.h

* Update LedDevice.cpp

* Update CMakeLists.txt

* Update hyperiond.cpp

* Update hyperiond.h

* Update TestSpi.cpp

* Delete AUTHORS

* Delete CMakeLists.txt

* Delete LICENSE

* Delete json_batchallocator.h

* Delete json_internalarray.inl

* Delete json_internalmap.inl

* Delete json_reader.cpp

* Delete json_tool.h

* Delete json_value.cpp

* Delete json_valueiterator.inl

* Delete json_writer.cpp

* Delete sconscript

* Delete autolink.h

* Delete config.h

* Delete features.h

* Delete forwards.h

* Delete json.h

* Delete reader.h

* Delete value.h

* Delete writer.h
2016-10-13 21:59:58 +02:00

197 lines
4.7 KiB
C++

// stl includes
#include <exception>
#include <cstring>
#include <algorithm>
// Local Hyperion includes
#include "LedDeviceMultiLightpack.h"
// from USB_ID.h (http://code.google.com/p/light-pack/source/browse/CommonHeaders/USB_ID.h)
#define USB_OLD_VENDOR_ID 0x03EB
#define USB_OLD_PRODUCT_ID 0x204F
#define USB_VENDOR_ID 0x1D50
#define USB_PRODUCT_ID 0x6022
bool compareLightpacks(LedDeviceLightpack * lhs, LedDeviceLightpack * rhs)
{
return lhs->getSerialNumber() < rhs->getSerialNumber();
}
LedDeviceMultiLightpack::LedDeviceMultiLightpack(const QJsonObject &)
: LedDevice()
, _lightpacks()
{
}
LedDeviceMultiLightpack::~LedDeviceMultiLightpack()
{
for (LedDeviceLightpack * device : _lightpacks)
{
delete device;
}
}
LedDevice* LedDeviceMultiLightpack::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceMultiLightpack(deviceConfig);
}
int LedDeviceMultiLightpack::open()
{
// retrieve a list with Lightpack serials
std::list<std::string> serialList = getLightpackSerials();
// 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
for (const std::string & serial : serialList)
{
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", serial.c_str());
delete device;
}
}
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;
}
int LedDeviceMultiLightpack::write(const std::vector<ColorRgb> &ledValues)
{
const ColorRgb * data = ledValues.data();
int size = ledValues.size();
for (LedDeviceLightpack * device : _lightpacks)
{
int count = std::min(device->getLedCount(), size);
if (count > 0)
{
device->write(data, count);
data += count;
size -= count;
}
else
{
Warning(_log, "Unable to write data to Lightpack device: no more led data available");
}
}
return 0;
}
int LedDeviceMultiLightpack::switchOff()
{
for (LedDeviceLightpack * device : _lightpacks)
{
device->switchOff();
}
return 0;
}
std::list<std::string> LedDeviceMultiLightpack::getLightpackSerials()
{
std::list<std::string> serialList;
Logger * log = Logger::getInstance("LedDevice");
Debug(log, "Getting list of Lightpack serials");
// initialize the usb context
libusb_context * libusbContext;
int error = libusb_init(&libusbContext);
if (error != LIBUSB_SUCCESS)
{
Error(log,"Error while initializing USB context(%d): %s", error, libusb_error_name(error));
libusbContext = nullptr;
return serialList;
}
//libusb_set_debug(_libusbContext, 3);
Info(log, "USB context initialized in multi Lightpack device");
// 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)
{
libusb_device_descriptor deviceDescriptor;
error = libusb_get_device_descriptor(deviceList[i], &deviceDescriptor);
if (error != LIBUSB_SUCCESS)
{
Error(log, "Error while retrieving device descriptor(%d): %s", error, libusb_error_name(error));
continue;
}
if ((deviceDescriptor.idVendor == USB_VENDOR_ID && deviceDescriptor.idProduct == USB_PRODUCT_ID) ||
(deviceDescriptor.idVendor == USB_OLD_VENDOR_ID && deviceDescriptor.idProduct == USB_OLD_PRODUCT_ID))
{
Info(log, "Found a lightpack device. Retrieving serial...");
// get the serial number
std::string serialNumber;
if (deviceDescriptor.iSerialNumber != 0)
{
try
{
serialNumber = LedDeviceMultiLightpack::getString(deviceList[i], deviceDescriptor.iSerialNumber);
}
catch (int e)
{
Error(log,"Unable to retrieve serial number(%d): %s", e, libusb_error_name(e));
continue;
}
}
Error(log, "Lightpack device found with serial %s", serialNumber.c_str());;
serialList.push_back(serialNumber);
}
}
// free the device list
libusb_free_device_list(deviceList, 1);
libusb_exit(libusbContext);
return serialList;
}
std::string LedDeviceMultiLightpack::getString(libusb_device * device, int stringDescriptorIndex)
{
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);
return std::string(buffer, error);
}