Documentation
¶
Overview ¶
Package device provides an abstraction layer for USB device communication. It defines interfaces for interacting with the Corsair iCUE Nexus display device.
Package device provides Nexus device implementation using direct libusb access.
Index ¶
- Constants
- Variables
- func NewDeviceError(op string, err error) error
- type ConnectionConfig
- type Device
- type DeviceError
- type DeviceInfo
- type MockDevice
- func (m *MockDevice) Connect(ctx context.Context) error
- func (m *MockDevice) Disconnect() error
- func (m *MockDevice) FailNext(operation string)
- func (m *MockDevice) GetDeviceInfo() DeviceInfo
- func (m *MockDevice) GetFirmwareVersion() (string, error)
- func (m *MockDevice) GetFramesSent() int
- func (m *MockDevice) GetLastFrame() []byte
- func (m *MockDevice) Health() error
- func (m *MockDevice) IsConnected() bool
- func (m *MockDevice) ReadTouch(ctx context.Context) ([]touch.Event, error)
- func (m *MockDevice) SendFrame(ctx context.Context, data []byte) error
- func (m *MockDevice) SetBrightness(brightness int) error
- func (m *MockDevice) SimulateDisconnect()
- func (m *MockDevice) SimulateTouch(button int, pressed bool, duration time.Duration)
- type NexusDevice
- func (n *NexusDevice) Connect(ctx context.Context) error
- func (n *NexusDevice) Disconnect() error
- func (n *NexusDevice) GetDeviceInfo() DeviceInfo
- func (n *NexusDevice) GetFirmwareVersion() (string, error)
- func (n *NexusDevice) Health() error
- func (n *NexusDevice) IsConnected() bool
- func (n *NexusDevice) ReadTouch(ctx context.Context) ([]touch.Event, error)
- func (n *NexusDevice) SendFrame(ctx context.Context, data []byte) error
- func (n *NexusDevice) SetBrightness(brightness int) error
Constants ¶
const ( DisplayWidth = 640 DisplayHeight = 48 FrameSize = DisplayWidth * DisplayHeight * 4 // RGBA )
FrameConstants defines the display dimensions and buffer sizes.
Variables ¶
var ( ErrDeviceNotFound = errors.New("device not found") ErrDeviceDisconnected = errors.New("device disconnected") ErrInvalidFrame = errors.New("invalid frame data") ErrConnectionFailed = errors.New("connection failed") ErrInterfaceFailed = errors.New("failed to claim USB interface") ErrConfigFailed = errors.New("failed to configure device") ErrSendFailed = errors.New("failed to send data") )
Common errors returned by Device implementations.
var ( ErrPermissionDenied = errors.New("USB permission denied") ErrDeviceBusy = errors.New("device busy") )
Additional sentinel errors for actionable UI messages.
Functions ¶
func NewDeviceError ¶
NewDeviceError creates a new DeviceError.
Types ¶
type ConnectionConfig ¶
type ConnectionConfig struct {
VendorID uint16 // USB Vendor ID (0x1b1c for Corsair)
ProductID uint16 // USB Product ID (0x1b8e for iCUE Nexus)
ReconnectRetries int // Number of reconnection attempts
ReconnectDelay time.Duration // Delay between reconnection attempts
}
ConnectionConfig holds configuration for device connection.
type Device ¶
type Device interface {
// Connect establishes a connection to the device.
// Returns ErrDeviceNotFound if no device is found.
Connect(ctx context.Context) error
// Disconnect closes the device connection and releases resources.
Disconnect() error
// IsConnected returns the current connection status.
IsConnected() bool
// SendFrame sends a complete frame of image data to the device.
// The data must be in the correct format (640x48 pixels, RGBA).
// Returns ErrDeviceDisconnected if the device is not connected.
SendFrame(ctx context.Context, data []byte) error
// ReadTouch reads touch input events from the device (non-blocking).
// Returns empty slice if no events are available.
ReadTouch(ctx context.Context) ([]touch.Event, error)
// Health performs a health check on the device connection.
// Returns nil if healthy, error otherwise.
Health() error
// HID Feature Report methods (may not be supported by all devices)
SetBrightness(brightness int) error
GetFirmwareVersion() (string, error)
GetDeviceInfo() DeviceInfo
}
Device represents a Nexus display device with USB communication capabilities.
type DeviceError ¶
DeviceError wraps device-related errors with additional context.
func (*DeviceError) Error ¶
func (e *DeviceError) Error() string
func (*DeviceError) Unwrap ¶
func (e *DeviceError) Unwrap() error
type DeviceInfo ¶
DeviceInfo holds static information read from the USB device at connect time.
type MockDevice ¶
type MockDevice struct {
// Callbacks for testing
OnConnect func(ctx context.Context) error
OnDisconnect func() error
OnSendFrame func(ctx context.Context, data []byte) error
// contains filtered or unexported fields
}
MockDevice is a mock implementation of Device for testing.
func (*MockDevice) Connect ¶
func (m *MockDevice) Connect(ctx context.Context) error
Connect simulates connecting to a device.
func (*MockDevice) Disconnect ¶
func (m *MockDevice) Disconnect() error
Disconnect simulates disconnecting from a device.
func (*MockDevice) FailNext ¶
func (m *MockDevice) FailNext(operation string)
FailNext makes the next call to the specified operation fail.
func (*MockDevice) GetDeviceInfo ¶
func (m *MockDevice) GetDeviceInfo() DeviceInfo
GetDeviceInfo returns mock device info.
func (*MockDevice) GetFirmwareVersion ¶
func (m *MockDevice) GetFirmwareVersion() (string, error)
GetFirmwareVersion returns a mock firmware version.
func (*MockDevice) GetFramesSent ¶
func (m *MockDevice) GetFramesSent() int
GetFramesSent returns the number of frames sent.
func (*MockDevice) GetLastFrame ¶
func (m *MockDevice) GetLastFrame() []byte
GetLastFrame returns the last frame data sent.
func (*MockDevice) Health ¶
func (m *MockDevice) Health() error
Health performs a mock health check.
func (*MockDevice) IsConnected ¶
func (m *MockDevice) IsConnected() bool
IsConnected returns the mock connection status.
func (*MockDevice) SendFrame ¶
func (m *MockDevice) SendFrame(ctx context.Context, data []byte) error
SendFrame simulates sending a frame to the device.
func (*MockDevice) SetBrightness ¶
func (m *MockDevice) SetBrightness(brightness int) error
SetBrightness simulates setting device brightness.
func (*MockDevice) SimulateDisconnect ¶
func (m *MockDevice) SimulateDisconnect()
SimulateDisconnect simulates a device disconnection.
func (*MockDevice) SimulateTouch ¶
func (m *MockDevice) SimulateTouch(button int, pressed bool, duration time.Duration)
SimulateTouch adds a mock touch event.
type NexusDevice ¶
type NexusDevice struct {
// contains filtered or unexported fields
}
NexusDevice implements the Device interface via direct libusb transfers.
func NewNexusDevice ¶
func NewNexusDevice(logger *slog.Logger, config ConnectionConfig) *NexusDevice
NewNexusDevice creates a new Nexus device.
func (*NexusDevice) Connect ¶
func (n *NexusDevice) Connect(ctx context.Context) error
Connect establishes a USB connection to the device, retrying up to 3 times.
func (*NexusDevice) Disconnect ¶
func (n *NexusDevice) Disconnect() error
Disconnect closes the USB device connection.
func (*NexusDevice) GetDeviceInfo ¶
func (n *NexusDevice) GetDeviceInfo() DeviceInfo
GetDeviceInfo returns the manufacturer, product, and USB IDs read at connect time.
func (*NexusDevice) GetFirmwareVersion ¶
func (n *NexusDevice) GetFirmwareVersion() (string, error)
GetFirmwareVersion is not yet implemented via raw libusb.
func (*NexusDevice) Health ¶
func (n *NexusDevice) Health() error
Health returns an error if the device is not connected.
func (*NexusDevice) IsConnected ¶
func (n *NexusDevice) IsConnected() bool
IsConnected returns whether the device is currently connected.
func (*NexusDevice) SendFrame ¶
func (n *NexusDevice) SendFrame(ctx context.Context, data []byte) error
SendFrame sends a rendered frame to the device display.
func (*NexusDevice) SetBrightness ¶
func (n *NexusDevice) SetBrightness(brightness int) error
SetBrightness sets the display brightness (0–100).