Implements the OS2L feature (tested with Virtual DJ).
Graphics improvements.

Reviewed-on: #32
This commit was merged in pull request #32.
This commit is contained in:
2025-11-14 10:46:24 +00:00
parent 121a14ac61
commit 932c288a9c
18 changed files with 560 additions and 245 deletions

View File

@@ -2,6 +2,7 @@ package hardware
import (
"context"
"errors"
"fmt"
goRuntime "runtime"
"sync"
@@ -59,24 +60,25 @@ func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri
defer f.mu.Unlock()
f.saved[peripheralData.SerialNumber] = peripheralData
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
// If already detected, connect it
if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
err := peripheral.Connect(ctx)
if err != nil {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
return peripheralData.SerialNumber, nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
// Peripheral connected, activate it
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
return peripheralData.SerialNumber, nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
f.wg.Add(1)
go func() {
defer f.wg.Done()
err := peripheral.Connect(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
return
}
// Peripheral connected, activate it
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
return
}
}()
}
// Emits the event in the hardware
@@ -97,14 +99,12 @@ func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralData Pe
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to deactivate the peripheral")
return nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
// Disconnecting peripheral
err = peripheral.Disconnect()
err = peripheral.Disconnect(ctx)
if err != nil {
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
return nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
}
delete(f.saved, peripheralData.SerialNumber)
runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData)
@@ -164,27 +164,28 @@ func (f *FTDIFinder) GetName() string {
// GetPeripheralSettings gets the peripheral settings
func (f *FTDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
// Return the specified peripheral
// peripheral, found := f.registeredPeripherals[peripheralID]
// if !found {
// log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
// return nil, fmt.Errorf("unable to found the peripheral")
// }
// log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
// return peripheral.GetSettings(), nil
return make(map[string]interface{}), nil
peripheral, found := f.detected[peripheralID]
if !found {
// FTDI not detected, return the last settings saved
if savedPeripheral, isFound := f.saved[peripheralID]; isFound {
return savedPeripheral.Settings, nil
}
return nil, fmt.Errorf("unable to found the peripheral")
}
log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
return peripheral.GetSettings(), nil
}
// SetPeripheralSettings sets the peripheral settings
func (f *FTDIFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error {
func (f *FTDIFinder) SetPeripheralSettings(ctx context.Context, peripheralID string, settings map[string]any) error {
// Return the specified peripheral
// peripheral, found := f.registeredPeripherals[peripheralID]
// if !found {
// log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
// return fmt.Errorf("unable to found the peripheral")
// }
// log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
// return peripheral.SetSettings(settings)
return nil
peripheral, found := f.detected[peripheralID]
if !found {
log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
return fmt.Errorf("unable to found the peripheral")
}
log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
return peripheral.SetSettings(settings)
}
// scanPeripherals scans the FTDI peripherals
@@ -233,29 +234,26 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
f.detected[sn] = peripheral
if f.onArrival != nil {
go f.onArrival(peripheralData)
f.onArrival(peripheralData)
}
log.Info().Str("sn", sn).Str("name", peripheralData.Name).Msg("[FTDI] New peripheral detected")
// Disconnected by default
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
// If the peripheral is saved in the project => connect
if _, saved := f.saved[sn]; saved {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
err := peripheral.Connect(ctx)
if err != nil {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
log.Err(err).Str("sn", sn).Msg("unable to connect the FTDI peripheral")
return nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to activate the FTDI peripheral")
return nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
f.wg.Add(1)
go func(p PeripheralInfo) {
defer f.wg.Done()
err := peripheral.Connect(ctx)
if err != nil {
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to connect the FTDI peripheral")
return
}
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to activate the FTDI peripheral")
return
}
}(peripheralData)
}
}
}
@@ -265,11 +263,14 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
if _, still := currentMap[sn]; !still {
// Properly clean the DMX device
err := oldPeripheral.Disconnect()
err := oldPeripheral.Deactivate(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to clean the FTDI peripheral after disconnection")
log.Err(err).Str("sn", sn).Msg("unable to deactivate the FTDI peripheral after disconnection")
}
err = oldPeripheral.Disconnect(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to disconnect the FTDI peripheral after disconnection")
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), oldPeripheral.GetInfo(), PeripheralStatusDisconnected)
// Delete it from the detected list
delete(f.detected, sn)
@@ -277,7 +278,7 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
// Execute the removal callback
if f.onRemoval != nil {
go f.onRemoval(oldPeripheral.GetInfo())
f.onRemoval(oldPeripheral.GetInfo())
}
}
}
@@ -292,22 +293,22 @@ func (f *FTDIFinder) WaitStop() error {
// close(f.scanChannel)
// Wait for all the peripherals to close
// log.Trace().Str("file", "FTDIFinder").Msg("closing all FTDI peripherals")
// var errs []error
// for registeredPeripheralSN, registeredPeripheral := range f.registeredPeripherals {
// err := registeredPeripheral.WaitStop()
// if err != nil {
// errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
// }
// }
log.Trace().Str("file", "FTDIFinder").Msg("closing all FTDI peripherals")
var errs []error
for registeredPeripheralSN, registeredPeripheral := range f.detected {
err := registeredPeripheral.WaitStop()
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
}
}
// Wait for goroutines to stop
f.wg.Wait()
// Returning errors
// if len(errs) > 0 {
// return errors.Join(errs...)
// }
if len(errs) > 0 {
return errors.Join(errs...)
}
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped")
return nil
}