device

package
v0.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 13 Imported by: 0

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

View Source
const (
	DisplayWidth  = 640
	DisplayHeight = 48
	FrameSize     = DisplayWidth * DisplayHeight * 4 // RGBA
)

FrameConstants defines the display dimensions and buffer sizes.

Variables

View Source
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.

View Source
var (
	ErrPermissionDenied = errors.New("USB permission denied")
	ErrDeviceBusy       = errors.New("device busy")
)

Additional sentinel errors for actionable UI messages.

Functions

func NewDeviceError

func NewDeviceError(op string, err error) error

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

type DeviceError struct {
	Op  string
	Err error
}

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

type DeviceInfo struct {
	Manufacturer string
	Product      string
	VendorID     uint16
	ProductID    uint16
}

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 NewMockDevice

func NewMockDevice() *MockDevice

NewMockDevice creates a new mock device.

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) ReadTouch

func (m *MockDevice) ReadTouch(ctx context.Context) ([]touch.Event, error)

ReadTouch returns mock touch events.

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) ReadTouch

func (n *NexusDevice) ReadTouch(ctx context.Context) ([]touch.Event, error)

ReadTouch reads touch events from the device.

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL