Fix warnings in AMBXController.cpp

This commit is contained in:
Adam Honse 2025-07-02 00:22:52 -05:00
parent 804490399f
commit eecdd4befc

View file

@ -19,43 +19,43 @@ AMBXController::AMBXController(const char* path)
initialized = false; initialized = false;
usb_context = nullptr; usb_context = nullptr;
dev_handle = nullptr; dev_handle = nullptr;
location = "USB: "; location = "USB: ";
location += path; location += path;
// Initialize libusb in this instance // Initialize libusb in this instance
if(libusb_init(&usb_context) < 0) if(libusb_init(&usb_context) < 0)
{ {
return; return;
} }
// Get the device list // Get the device list
libusb_device** device_list; libusb_device** device_list;
ssize_t device_count = libusb_get_device_list(usb_context, &device_list); ssize_t device_count = libusb_get_device_list(usb_context, &device_list);
if(device_count < 0) if(device_count < 0)
{ {
return; return;
} }
for(ssize_t i = 0; i < device_count; i++) for(ssize_t i = 0; i < device_count; i++)
{ {
libusb_device* device = device_list[i]; libusb_device* device = device_list[i];
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
if(libusb_get_device_descriptor(device, &desc) != LIBUSB_SUCCESS) if(libusb_get_device_descriptor(device, &desc) != LIBUSB_SUCCESS)
{ {
continue; continue;
} }
if(desc.idVendor == AMBX_VID && desc.idProduct == AMBX_PID) if(desc.idVendor == AMBX_VID && desc.idProduct == AMBX_PID)
{ {
uint8_t bus = libusb_get_bus_number(device); uint8_t bus = libusb_get_bus_number(device);
uint8_t address = libusb_get_device_address(device); uint8_t address = libusb_get_device_address(device);
char current_path[32]; char current_path[32];
snprintf(current_path, sizeof(current_path), "%d-%d", bus, address); snprintf(current_path, sizeof(current_path), "%d-%d", bus, address);
if(strcmp(path, current_path) == 0) if(strcmp(path, current_path) == 0)
{ {
// Try to open this device // Try to open this device
@ -63,16 +63,16 @@ AMBXController::AMBXController(const char* path)
{ {
continue; continue;
} }
// Try to detach the kernel driver if attached // Try to detach the kernel driver if attached
if(libusb_kernel_driver_active(dev_handle, 0)) if(libusb_kernel_driver_active(dev_handle, 0))
{ {
libusb_detach_kernel_driver(dev_handle, 0); libusb_detach_kernel_driver(dev_handle, 0);
} }
// Set auto-detach for Windows compatibility // Set auto-detach for Windows compatibility
libusb_set_auto_detach_kernel_driver(dev_handle, 1); libusb_set_auto_detach_kernel_driver(dev_handle, 1);
// Claim the interface // Claim the interface
if(libusb_claim_interface(dev_handle, 0) != LIBUSB_SUCCESS) if(libusb_claim_interface(dev_handle, 0) != LIBUSB_SUCCESS)
{ {
@ -80,26 +80,26 @@ AMBXController::AMBXController(const char* path)
dev_handle = nullptr; dev_handle = nullptr;
continue; continue;
} }
// Get string descriptor for serial number if available // Get string descriptor for serial number if available
if(desc.iSerialNumber != 0) if(desc.iSerialNumber != 0)
{ {
unsigned char serial_str[256]; unsigned char serial_str[256];
int serial_result = libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber, int serial_result = libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber,
serial_str, sizeof(serial_str)); serial_str, sizeof(serial_str));
if(serial_result > 0) if(serial_result > 0)
{ {
serial = std::string(reinterpret_cast<char*>(serial_str), serial_result); serial = std::string(reinterpret_cast<char*>(serial_str), serial_result);
} }
} }
// Successfully opened and claimed the device // Successfully opened and claimed the device
initialized = true; initialized = true;
break; break;
} }
} }
} }
libusb_free_device_list(device_list, 1); libusb_free_device_list(device_list, 1);
} }
@ -108,29 +108,29 @@ AMBXController::~AMBXController()
if(initialized) if(initialized)
{ {
// Turn off all lights before closing // Turn off all lights before closing
unsigned int led_ids[5] = unsigned int led_ids[5] =
{ {
AMBX_LIGHT_LEFT, AMBX_LIGHT_LEFT,
AMBX_LIGHT_RIGHT, AMBX_LIGHT_RIGHT,
AMBX_LIGHT_WALL_LEFT, AMBX_LIGHT_WALL_LEFT,
AMBX_LIGHT_WALL_CENTER, AMBX_LIGHT_WALL_CENTER,
AMBX_LIGHT_WALL_RIGHT AMBX_LIGHT_WALL_RIGHT
}; };
RGBColor colors[5] = { 0, 0, 0, 0, 0 }; RGBColor colors[5] = { 0, 0, 0, 0, 0 };
SetLEDColors(led_ids, colors, 5); SetLEDColors(led_ids, colors, 5);
} }
if(dev_handle != nullptr) if(dev_handle != nullptr)
{ {
// Release the interface // Release the interface
libusb_release_interface(dev_handle, 0); libusb_release_interface(dev_handle, 0);
// Close the device // Close the device
libusb_close(dev_handle); libusb_close(dev_handle);
dev_handle = nullptr; dev_handle = nullptr;
} }
if(usb_context != nullptr) if(usb_context != nullptr)
{ {
libusb_exit(usb_context); libusb_exit(usb_context);
@ -159,7 +159,7 @@ void AMBXController::SendPacket(unsigned char* packet, unsigned int size)
{ {
return; return;
} }
int actual_length = 0; int actual_length = 0;
libusb_interrupt_transfer(dev_handle, AMBX_ENDPOINT_OUT, packet, size, &actual_length, 100); libusb_interrupt_transfer(dev_handle, AMBX_ENDPOINT_OUT, packet, size, &actual_length, 100);
} }
@ -170,19 +170,19 @@ void AMBXController::SetLEDColor(unsigned int led, RGBColor color)
{ {
return; return;
} }
unsigned char color_buf[6] = unsigned char color_buf[6] =
{ {
AMBX_PACKET_HEADER, AMBX_PACKET_HEADER,
static_cast<unsigned char>(led), static_cast<unsigned char>(led),
AMBX_SET_COLOR, AMBX_SET_COLOR,
RGBGetRValue(color), (unsigned char)RGBGetRValue(color),
RGBGetGValue(color), (unsigned char)RGBGetGValue(color),
RGBGetBValue(color) (unsigned char)RGBGetBValue(color)
}; };
SendPacket(color_buf, 6); SendPacket(color_buf, 6);
std::this_thread::sleep_for(std::chrono::milliseconds(2)); std::this_thread::sleep_for(std::chrono::milliseconds(2));
} }