2013-11-13 20:15:53 +01:00
|
|
|
// stl includes
|
|
|
|
#include <exception>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
// Local Hyperion includes
|
|
|
|
#include "LedDeviceLightpack.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
|
|
|
|
|
|
|
|
#define LIGHTPACK_INTERFACE 0
|
|
|
|
|
|
|
|
// from commands.h (http://code.google.com/p/light-pack/source/browse/CommonHeaders/commands.h)
|
|
|
|
// Commands to device, sends it in first byte of data[]
|
|
|
|
enum COMMANDS{
|
|
|
|
CMD_UPDATE_LEDS = 1,
|
|
|
|
CMD_OFF_ALL,
|
|
|
|
CMD_SET_TIMER_OPTIONS,
|
|
|
|
CMD_SET_PWM_LEVEL_MAX_VALUE, /* deprecated */
|
|
|
|
CMD_SET_SMOOTH_SLOWDOWN,
|
|
|
|
CMD_SET_BRIGHTNESS,
|
|
|
|
|
|
|
|
CMD_NOP = 0x0F
|
|
|
|
};
|
|
|
|
|
|
|
|
// from commands.h (http://code.google.com/p/light-pack/source/browse/CommonHeaders/commands.h)
|
|
|
|
enum DATA_VERSION_INDEXES{
|
|
|
|
INDEX_FW_VER_MAJOR = 1,
|
|
|
|
INDEX_FW_VER_MINOR
|
|
|
|
};
|
|
|
|
|
2016-07-13 11:18:12 +02:00
|
|
|
LedDeviceLightpack::LedDeviceLightpack(const std::string & serialNumber)
|
|
|
|
: LedDevice()
|
|
|
|
, _libusbContext(nullptr)
|
|
|
|
, _deviceHandle(nullptr)
|
|
|
|
, _busNumber(-1)
|
|
|
|
, _addressNumber(-1)
|
|
|
|
, _serialNumber(serialNumber)
|
|
|
|
, _firmwareVersion({-1,-1})
|
|
|
|
, _bitsPerChannel(-1)
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
_ledCount = -1;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LedDeviceLightpack::~LedDeviceLightpack()
|
|
|
|
{
|
|
|
|
if (_deviceHandle != nullptr)
|
|
|
|
{
|
|
|
|
libusb_release_interface(_deviceHandle, LIGHTPACK_INTERFACE);
|
|
|
|
libusb_attach_kernel_driver(_deviceHandle, LIGHTPACK_INTERFACE);
|
|
|
|
libusb_close(_deviceHandle);
|
|
|
|
|
|
|
|
_deviceHandle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_libusbContext != nullptr)
|
|
|
|
{
|
2013-11-13 23:16:09 +01:00
|
|
|
libusb_exit(_libusbContext);
|
|
|
|
_libusbContext = nullptr;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 11:18:12 +02:00
|
|
|
int LedDeviceLightpack::open()
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
// initialize the usb context
|
|
|
|
if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(_log, "Error while initializing USB context(%d): %s", error, libusb_error_name(error));
|
2013-11-13 20:15:53 +01:00
|
|
|
_libusbContext = nullptr;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-17 15:51:42 +01:00
|
|
|
//libusb_set_debug(_libusbContext, 3);
|
2016-06-26 11:53:16 +02:00
|
|
|
Debug(_log, "USB context initialized");
|
2013-11-13 20:15:53 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
// try to open and initialize the device
|
2016-07-13 11:18:12 +02:00
|
|
|
error = testAndOpen(deviceList[i], _serialNumber);
|
2013-11-17 15:51:42 +01:00
|
|
|
|
|
|
|
if (error == 0)
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
// a device was sucessfully opened. break from list
|
|
|
|
break;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
2013-11-17 15:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// free the device list
|
|
|
|
libusb_free_device_list(deviceList, 1);
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
if (_deviceHandle == nullptr)
|
|
|
|
{
|
|
|
|
if (_serialNumber.empty())
|
|
|
|
{
|
2016-06-26 11:53:16 +02:00
|
|
|
Warning(_log, "No Lightpack device has been found");
|
2013-11-17 15:51:42 +01:00
|
|
|
}
|
|
|
|
else
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
2016-06-26 11:53:16 +02:00
|
|
|
Error(_log,"No Lightpack device has been found with serial %s", _serialNumber.c_str());
|
2013-11-17 15:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 23:29:19 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
return _deviceHandle == nullptr ? -1 : 0;
|
|
|
|
}
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-20 20:54:04 +01:00
|
|
|
int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & requestedSerialNumber)
|
2013-11-17 15:51:42 +01:00
|
|
|
{
|
|
|
|
libusb_device_descriptor deviceDescriptor;
|
|
|
|
int error = libusb_get_device_descriptor(device, &deviceDescriptor);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(_log, "Error while retrieving device descriptor(%d): %s", error, libusb_error_name(error));
|
2013-11-17 15:51:42 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((deviceDescriptor.idVendor == USB_VENDOR_ID && deviceDescriptor.idProduct == USB_PRODUCT_ID) ||
|
|
|
|
(deviceDescriptor.idVendor == USB_OLD_VENDOR_ID && deviceDescriptor.idProduct == USB_OLD_PRODUCT_ID))
|
|
|
|
{
|
2016-06-26 11:53:16 +02:00
|
|
|
Info(_log, "Found a lightpack device. Retrieving more information...");
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
// get the hardware address
|
|
|
|
int busNumber = libusb_get_bus_number(device);
|
|
|
|
int addressNumber = libusb_get_device_address(device);
|
|
|
|
|
|
|
|
// get the serial number
|
|
|
|
std::string serialNumber;
|
|
|
|
if (deviceDescriptor.iSerialNumber != 0)
|
|
|
|
{
|
2013-11-13 20:15:53 +01:00
|
|
|
try
|
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
serialNumber = LedDeviceLightpack::getString(device, deviceDescriptor.iSerialNumber);
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
catch (int e)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(_log, "unable to retrieve serial number from Lightpack device(%d): %s", e, libusb_error_name(e));
|
2013-11-17 15:51:42 +01:00
|
|
|
serialNumber = "";
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
2013-11-17 15:51:42 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 11:53:16 +02:00
|
|
|
Debug(_log,"Lightpack device found: bus=%d address=%d serial=%s", busNumber, addressNumber, serialNumber.c_str());
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
// check if this is the device we are looking for
|
2013-11-20 20:54:04 +01:00
|
|
|
if (requestedSerialNumber.empty() || requestedSerialNumber == serialNumber)
|
2013-11-17 15:51:42 +01:00
|
|
|
{
|
|
|
|
// This is it!
|
|
|
|
try
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
_deviceHandle = openDevice(device);
|
|
|
|
_serialNumber = serialNumber;
|
|
|
|
_busNumber = busNumber;
|
|
|
|
_addressNumber = addressNumber;
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2016-06-26 11:53:16 +02:00
|
|
|
Info(_log, "Lightpack device successfully opened");
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-20 20:54:04 +01:00
|
|
|
// get the firmware version
|
|
|
|
uint8_t buffer[256];
|
|
|
|
error = libusb_control_transfer(
|
|
|
|
_deviceHandle,
|
|
|
|
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
|
|
|
|
0x01,
|
|
|
|
0x0100,
|
|
|
|
0,
|
|
|
|
buffer, sizeof(buffer), 1000);
|
|
|
|
if (error < 3)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(_log, "Unable to retrieve firmware version number from Lightpack device(%d): %s", error, libusb_error_name(error));
|
2013-11-20 20:54:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_firmwareVersion.majorVersion = buffer[INDEX_FW_VER_MAJOR];
|
|
|
|
_firmwareVersion.minorVersion = buffer[INDEX_FW_VER_MINOR];
|
|
|
|
}
|
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
// FOR TESTING PURPOSE: FORCE MAJOR VERSION TO 6
|
|
|
|
_firmwareVersion.majorVersion = 6;
|
|
|
|
|
|
|
|
// disable smoothing of the chosen device
|
|
|
|
disableSmoothing();
|
|
|
|
|
|
|
|
// determine the number of leds
|
|
|
|
if (_firmwareVersion.majorVersion == 4)
|
|
|
|
{
|
|
|
|
_ledCount = 8;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
2013-11-17 15:51:42 +01:00
|
|
|
else
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
_ledCount = 10;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
// determine the bits per channel
|
|
|
|
if (_firmwareVersion.majorVersion == 6)
|
|
|
|
{
|
|
|
|
// maybe also or version 7? The firmware suggest this is only for 6... (2013-11-13)
|
|
|
|
_bitsPerChannel = 12;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_bitsPerChannel = 8;
|
|
|
|
}
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
// set the led buffer size (command + 6 bytes per led)
|
|
|
|
_ledBuffer = std::vector<uint8_t>(1 + _ledCount * 6, 0);
|
|
|
|
_ledBuffer[0] = CMD_UPDATE_LEDS;
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
// return success
|
2016-06-26 11:53:16 +02:00
|
|
|
Debug(_log, "Lightpack device opened: bus=%d address=%d serial=%s version=%s.%s.", _busNumber, _addressNumber, _serialNumber.c_str(), _firmwareVersion.majorVersion, _firmwareVersion.minorVersion );
|
2013-11-17 15:51:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch(int e)
|
|
|
|
{
|
|
|
|
_deviceHandle = nullptr;
|
2016-08-07 07:06:56 +02:00
|
|
|
Warning(_log, "Unable to open Lightpack device. Searching for other device(%d): %s", e, libusb_error_name(e));
|
2013-11-17 15:51:42 +01:00
|
|
|
}
|
2013-11-13 23:48:00 +01:00
|
|
|
}
|
2013-11-13 23:29:19 +01:00
|
|
|
}
|
2013-11-13 20:15:53 +01:00
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
return -1;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceLightpack::write(const std::vector<ColorRgb> &ledValues)
|
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
return write(ledValues.data(), ledValues.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceLightpack::write(const ColorRgb * ledValues, int size)
|
|
|
|
{
|
|
|
|
int count = std::min(_ledCount, size);
|
2013-11-13 20:15:53 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < count ; ++i)
|
|
|
|
{
|
|
|
|
const ColorRgb & color = ledValues[i];
|
|
|
|
|
|
|
|
// copy the most significant bits of the rgb values to the first three bytes
|
2013-11-13 23:12:30 +01:00
|
|
|
// offset 1 to accomodate for the command byte
|
|
|
|
_ledBuffer[6*i+1] = color.red;
|
|
|
|
_ledBuffer[6*i+2] = color.green;
|
|
|
|
_ledBuffer[6*i+3] = color.blue;
|
2013-11-13 20:15:53 +01:00
|
|
|
|
|
|
|
// leave the next three bytes on zero...
|
2013-11-17 10:21:08 +01:00
|
|
|
// 12-bit values having zeros in the lowest 4 bits which is almost correct, but it saves extra
|
2013-11-13 20:15:53 +01:00
|
|
|
// switches to determine what to do and some bit shuffling
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:16:00 +01:00
|
|
|
int error = writeBytes(_ledBuffer.data(), _ledBuffer.size());
|
|
|
|
return error >= 0 ? 0 : error;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceLightpack::switchOff()
|
|
|
|
{
|
|
|
|
unsigned char buf[1] = {CMD_OFF_ALL};
|
|
|
|
return writeBytes(buf, sizeof(buf)) == sizeof(buf);
|
|
|
|
}
|
|
|
|
|
2013-11-17 15:51:42 +01:00
|
|
|
const std::string &LedDeviceLightpack::getSerialNumber() const
|
|
|
|
{
|
|
|
|
return _serialNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceLightpack::getLedCount() const
|
|
|
|
{
|
|
|
|
return _ledCount;
|
|
|
|
}
|
|
|
|
|
2013-11-13 20:15:53 +01:00
|
|
|
int LedDeviceLightpack::writeBytes(uint8_t *data, int size)
|
|
|
|
{
|
2013-11-17 15:51:42 +01:00
|
|
|
// std::cout << "Writing " << size << " bytes: ";
|
|
|
|
// for (int i = 0; i < size ; ++i) printf("%02x ", data[i]);
|
|
|
|
// std::cout << std::endl;
|
2013-11-13 23:29:19 +01:00
|
|
|
|
2013-11-17 10:21:08 +01:00
|
|
|
int error = libusb_control_transfer(_deviceHandle,
|
|
|
|
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
|
2013-11-23 11:14:27 +01:00
|
|
|
0x09,
|
2013-11-13 20:15:53 +01:00
|
|
|
(2 << 8),
|
|
|
|
0x00,
|
2013-11-17 15:51:42 +01:00
|
|
|
data, size, 1000);
|
2013-11-17 10:21:08 +01:00
|
|
|
|
|
|
|
if (error == size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(_log, "Unable to write %d bytes to Lightpack device(%d): %s", size, error, libusb_error_name(error));
|
2013-11-17 10:21:08 +01:00
|
|
|
return error;
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceLightpack::disableSmoothing()
|
|
|
|
{
|
|
|
|
unsigned char buf[2] = {CMD_SET_SMOOTH_SLOWDOWN, 0};
|
|
|
|
return writeBytes(buf, sizeof(buf)) == sizeof(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_device_handle * LedDeviceLightpack::openDevice(libusb_device *device)
|
|
|
|
{
|
|
|
|
libusb_device_handle * handle = nullptr;
|
2016-06-26 11:53:16 +02:00
|
|
|
Logger * log = Logger::getInstance("LedDevice");
|
2013-11-13 20:15:53 +01:00
|
|
|
int error = libusb_open(device, &handle);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(log, "unable to open device(%d): %s", error, libusb_error_name(error));
|
2013-11-13 20:15:53 +01:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:13:32 +01:00
|
|
|
// detach kernel driver if it is active
|
|
|
|
if (libusb_kernel_driver_active(handle, LIGHTPACK_INTERFACE) == 1)
|
2013-11-13 20:15:53 +01:00
|
|
|
{
|
2013-11-18 21:13:32 +01:00
|
|
|
error = libusb_detach_kernel_driver(handle, LIGHTPACK_INTERFACE);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(log, "unable to detach kernel driver(%d): %s", error, libusb_error_name(error));
|
2013-11-18 21:13:32 +01:00
|
|
|
libusb_close(handle);
|
|
|
|
throw error;
|
|
|
|
}
|
2013-11-13 20:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
error = libusb_claim_interface(handle, LIGHTPACK_INTERFACE);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
2016-08-07 07:06:56 +02:00
|
|
|
Error(log, "unable to claim interface(%d): %s", error, libusb_error_name(error));
|
2013-11-16 10:38:03 +01:00
|
|
|
libusb_attach_kernel_driver(handle, LIGHTPACK_INTERFACE);
|
2013-11-15 18:16:00 +01:00
|
|
|
libusb_close(handle);
|
2013-11-13 20:15:53 +01:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LedDeviceLightpack::getString(libusb_device * device, int stringDescriptorIndex)
|
|
|
|
{
|
2013-11-16 10:38:03 +01:00
|
|
|
libusb_device_handle * handle = nullptr;
|
|
|
|
|
|
|
|
int error = libusb_open(device, &handle);
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
{
|
|
|
|
throw error;
|
|
|
|
}
|
2013-11-13 20:15:53 +01:00
|
|
|
|
|
|
|
char buffer[256];
|
2013-11-16 10:38:03 +01:00
|
|
|
error = libusb_get_string_descriptor_ascii(handle, stringDescriptorIndex, reinterpret_cast<unsigned char *>(buffer), sizeof(buffer));
|
2013-11-13 20:15:53 +01:00
|
|
|
if (error <= 0)
|
|
|
|
{
|
2013-11-16 10:38:03 +01:00
|
|
|
libusb_close(handle);
|
2013-11-13 20:15:53 +01:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2013-11-16 10:38:03 +01:00
|
|
|
libusb_close(handle);
|
2013-11-13 20:15:53 +01:00
|
|
|
return std::string(buffer, error);
|
|
|
|
}
|