|
| 1 | +// stl includes |
| 2 | +#include <exception> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +// Local Hyperion includes |
| 6 | +#include "LedDeviceHyperionUsbasp.h" |
| 7 | + |
| 8 | +// Static constants which define the Hyperion Usbasp device |
| 9 | +uint16_t LedDeviceHyperionUsbasp::_usbVendorId = 0x16c0; |
| 10 | +uint16_t LedDeviceHyperionUsbasp::_usbProductId = 0x05dc; |
| 11 | +std::string LedDeviceHyperionUsbasp::_usbProductDescription = "Hyperion led controller"; |
| 12 | + |
| 13 | + |
| 14 | +LedDeviceHyperionUsbasp::LedDeviceHyperionUsbasp(uint8_t writeLedsCommand) : |
| 15 | + LedDevice(), |
| 16 | + _writeLedsCommand(writeLedsCommand), |
| 17 | + _libusbContext(nullptr), |
| 18 | + _deviceHandle(nullptr), |
| 19 | + _ledCount(256) |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | +LedDeviceHyperionUsbasp::~LedDeviceHyperionUsbasp() |
| 24 | +{ |
| 25 | + if (_deviceHandle != nullptr) |
| 26 | + { |
| 27 | + libusb_release_interface(_deviceHandle, 0); |
| 28 | + libusb_attach_kernel_driver(_deviceHandle, 0); |
| 29 | + libusb_close(_deviceHandle); |
| 30 | + |
| 31 | + _deviceHandle = nullptr; |
| 32 | + } |
| 33 | + |
| 34 | + if (_libusbContext != nullptr) |
| 35 | + { |
| 36 | + libusb_exit(_libusbContext); |
| 37 | + _libusbContext = nullptr; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +int LedDeviceHyperionUsbasp::open() |
| 42 | +{ |
| 43 | + int error; |
| 44 | + |
| 45 | + // initialize the usb context |
| 46 | + if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS) |
| 47 | + { |
| 48 | + std::cerr << "Error while initializing USB context(" << error << "): " << libusb_error_name(error) << std::endl; |
| 49 | + _libusbContext = nullptr; |
| 50 | + return -1; |
| 51 | + } |
| 52 | + //libusb_set_debug(_libusbContext, 3); |
| 53 | + std::cout << "USB context initialized" << std::endl; |
| 54 | + |
| 55 | + // retrieve the list of usb devices |
| 56 | + libusb_device ** deviceList; |
| 57 | + ssize_t deviceCount = libusb_get_device_list(_libusbContext, &deviceList); |
| 58 | + |
| 59 | + // iterate the list of devices |
| 60 | + for (ssize_t i = 0 ; i < deviceCount; ++i) |
| 61 | + { |
| 62 | + // try to open and initialize the device |
| 63 | + error = testAndOpen(deviceList[i]); |
| 64 | + |
| 65 | + if (error == 0) |
| 66 | + { |
| 67 | + // a device was sucessfully opened. break from list |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // free the device list |
| 73 | + libusb_free_device_list(deviceList, 1); |
| 74 | + |
| 75 | + if (_deviceHandle == nullptr) |
| 76 | + { |
| 77 | + std::cerr << "No " << _usbProductDescription << " has been found" << std::endl; |
| 78 | + } |
| 79 | + |
| 80 | + return _deviceHandle == nullptr ? -1 : 0; |
| 81 | +} |
| 82 | + |
| 83 | +int LedDeviceHyperionUsbasp::testAndOpen(libusb_device * device) |
| 84 | +{ |
| 85 | + libusb_device_descriptor deviceDescriptor; |
| 86 | + int error = libusb_get_device_descriptor(device, &deviceDescriptor); |
| 87 | + if (error != LIBUSB_SUCCESS) |
| 88 | + { |
| 89 | + std::cerr << "Error while retrieving device descriptor(" << error << "): " << libusb_error_name(error) << std::endl; |
| 90 | + return -1; |
| 91 | + } |
| 92 | + |
| 93 | + if (deviceDescriptor.idVendor == _usbVendorId && |
| 94 | + deviceDescriptor.idProduct == _usbProductId && |
| 95 | + deviceDescriptor.iProduct != 0 && |
| 96 | + getString(device, deviceDescriptor.iProduct) == _usbProductDescription) |
| 97 | + { |
| 98 | + // get the hardware address |
| 99 | + int busNumber = libusb_get_bus_number(device); |
| 100 | + int addressNumber = libusb_get_device_address(device); |
| 101 | + |
| 102 | + std::cout << _usbProductDescription << " found: bus=" << busNumber << " address=" << addressNumber << std::endl; |
| 103 | + |
| 104 | + try |
| 105 | + { |
| 106 | + _deviceHandle = openDevice(device); |
| 107 | + std::cout << _usbProductDescription << " successfully opened" << std::endl; |
| 108 | + return 0; |
| 109 | + } |
| 110 | + catch(int e) |
| 111 | + { |
| 112 | + _deviceHandle = nullptr; |
| 113 | + std::cerr << "Unable to open " << _usbProductDescription << ". Searching for other device(" << e << "): " << libusb_error_name(e) << std::endl; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return -1; |
| 118 | +} |
| 119 | + |
| 120 | +int LedDeviceHyperionUsbasp::write(const std::vector<ColorRgb> &ledValues) |
| 121 | +{ |
| 122 | + _ledCount = ledValues.size(); |
| 123 | + |
| 124 | + int nbytes = libusb_control_transfer( |
| 125 | + _deviceHandle, // device handle |
| 126 | + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, // request type |
| 127 | + _writeLedsCommand, // request |
| 128 | + 0, // value |
| 129 | + 0, // index |
| 130 | + (uint8_t *) ledValues.data(), // data |
| 131 | + (3*_ledCount) & 0xffff, // length |
| 132 | + 5000); // timeout |
| 133 | + |
| 134 | + // Disabling interupts for a little while on the device results in a PIPE error. All seems to keep functioning though... |
| 135 | + if(nbytes < 0 && nbytes != LIBUSB_ERROR_PIPE) |
| 136 | + { |
| 137 | + std::cerr << "Error while writing data to " << _usbProductDescription << " (" << libusb_error_name(nbytes) << ")" << std::endl; |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +int LedDeviceHyperionUsbasp::switchOff() |
| 145 | +{ |
| 146 | + std::vector<ColorRgb> ledValues(_ledCount, ColorRgb::BLACK); |
| 147 | + return write(ledValues); |
| 148 | +} |
| 149 | + |
| 150 | +libusb_device_handle * LedDeviceHyperionUsbasp::openDevice(libusb_device *device) |
| 151 | +{ |
| 152 | + libusb_device_handle * handle = nullptr; |
| 153 | + |
| 154 | + int error = libusb_open(device, &handle); |
| 155 | + if (error != LIBUSB_SUCCESS) |
| 156 | + { |
| 157 | + std::cerr << "unable to open device(" << error << "): " << libusb_error_name(error) << std::endl; |
| 158 | + throw error; |
| 159 | + } |
| 160 | + |
| 161 | + // detach kernel driver if it is active |
| 162 | + if (libusb_kernel_driver_active(handle, 0) == 1) |
| 163 | + { |
| 164 | + error = libusb_detach_kernel_driver(handle, 0); |
| 165 | + if (error != LIBUSB_SUCCESS) |
| 166 | + { |
| 167 | + std::cerr << "unable to detach kernel driver(" << error << "): " << libusb_error_name(error) << std::endl; |
| 168 | + libusb_close(handle); |
| 169 | + throw error; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + error = libusb_claim_interface(handle, 0); |
| 174 | + if (error != LIBUSB_SUCCESS) |
| 175 | + { |
| 176 | + std::cerr << "unable to claim interface(" << error << "): " << libusb_error_name(error) << std::endl; |
| 177 | + libusb_attach_kernel_driver(handle, 0); |
| 178 | + libusb_close(handle); |
| 179 | + throw error; |
| 180 | + } |
| 181 | + |
| 182 | + return handle; |
| 183 | +} |
| 184 | + |
| 185 | +std::string LedDeviceHyperionUsbasp::getString(libusb_device * device, int stringDescriptorIndex) |
| 186 | +{ |
| 187 | + libusb_device_handle * handle = nullptr; |
| 188 | + |
| 189 | + int error = libusb_open(device, &handle); |
| 190 | + if (error != LIBUSB_SUCCESS) |
| 191 | + { |
| 192 | + throw error; |
| 193 | + } |
| 194 | + |
| 195 | + char buffer[256]; |
| 196 | + error = libusb_get_string_descriptor_ascii(handle, stringDescriptorIndex, reinterpret_cast<unsigned char *>(buffer), sizeof(buffer)); |
| 197 | + if (error <= 0) |
| 198 | + { |
| 199 | + libusb_close(handle); |
| 200 | + throw error; |
| 201 | + } |
| 202 | + |
| 203 | + libusb_close(handle); |
| 204 | + return std::string(buffer, error); |
| 205 | +} |
0 commit comments