add OS2L device creation

This commit is contained in:
2024-12-23 17:22:37 +01:00
parent 7cf222c4f9
commit 037735fb85
14 changed files with 476 additions and 292 deletions

View File

@@ -29,7 +29,7 @@ var (
// HardwareManager is the class who manages the hardware
type HardwareManager struct {
finders map[string]PeripheralFinder // The map of peripherals finders
drivers map[string]PeripheralDriver // The map of peripherals finders
peripherals []Peripheral // The current list of peripherals
deviceChangedEvent chan struct{} // The event when the devices list changed
ctx context.Context
@@ -38,7 +38,7 @@ type HardwareManager struct {
// NewHardwareManager creates a new HardwareManager
func NewHardwareManager() *HardwareManager {
return &HardwareManager{
finders: make(map[string]PeripheralFinder),
drivers: make(map[string]PeripheralDriver),
peripherals: make([]Peripheral, 0),
deviceChangedEvent: make(chan struct{}),
}
@@ -118,38 +118,45 @@ func (h *HardwareManager) Start(ctx context.Context) error {
return nil
}
// RegisterFinder registers a new peripherals finder
func (h *HardwareManager) RegisterFinder(finder PeripheralFinder) {
h.finders[finder.GetName()] = finder
fmt.Printf("Success registered the %s finder\n", finder.GetName())
// GetDriver returns a register driver
func (h *HardwareManager) GetDriver(driverName string) (PeripheralDriver, error) {
driver, exists := h.drivers[driverName]
if !exists {
return nil, fmt.Errorf("Unable to locate the '%s' driver", driverName)
}
return driver, nil
}
// GetPeripheral gets the peripheral object from the parent finder
func (h *HardwareManager) GetPeripheral(finderName string, peripheralID string) (Peripheral, bool) {
// Get the parent finder
parentFinder := h.finders[finderName]
// If no finder found, return false
if parentFinder == nil {
fmt.Println("Unable to get the finder")
// RegisterDriver registers a new peripherals driver
func (h *HardwareManager) RegisterDriver(driver PeripheralDriver) {
h.drivers[driver.GetName()] = driver
fmt.Printf("Success registered the %s driver\n", driver.GetName())
}
// GetPeripheral gets the peripheral object from the parent driver
func (h *HardwareManager) GetPeripheral(driverName string, peripheralID string) (Peripheral, bool) {
// Get the driver
parentDriver := h.drivers[driverName]
// If no driver found, return false
if parentDriver == nil {
fmt.Println("Unable to get the driver")
return nil, false
}
fmt.Println("Finder ok, returning the peripheral")
// Contact the finder to get the device
return parentFinder.GetPeripheral(peripheralID)
// Contact the driver to get the device
return parentDriver.GetPeripheral(peripheralID)
}
// Scan scans all the peripherals for the registered finders
func (h *HardwareManager) Scan(ctx context.Context) error {
if len(h.finders) == 0 {
return fmt.Errorf("No peripherals finder registered")
if len(h.drivers) == 0 {
return fmt.Errorf("No peripherals driver registered")
}
for _, finder := range h.finders {
finder := finder
for _, driver := range h.drivers {
finder := driver
go func() {
err := finder.Scan(ctx)
err := driver.Scan(ctx)
if err != nil {
fmt.Printf("Unable to scan peripherals with the %s finder: %s\n", finder.GetName(), err)
fmt.Printf("Unable to scan peripherals with the %s driver: %s\n", finder.GetName(), err)
return
}
}()