generated from thinkode/modelRepository
23-os2l (#32)
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:
@@ -2,23 +2,32 @@ package hardware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// OS2LFinder represents how the protocol is defined
|
||||
type OS2LFinder struct {
|
||||
registeredPeripherals map[string]OS2LPeripheral // The list of found peripherals
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
|
||||
saved map[string]*OS2LPeripheral // The list of saved peripherals
|
||||
|
||||
onArrival func(p PeripheralInfo) // When a peripheral arrives
|
||||
onRemoval func(p PeripheralInfo) // When a peripheral goes away
|
||||
}
|
||||
|
||||
// NewOS2LFinder creates a new OS2L finder
|
||||
func NewOS2LFinder() *OS2LFinder {
|
||||
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder created")
|
||||
return &OS2LFinder{
|
||||
registeredPeripherals: make(map[string]OS2LPeripheral),
|
||||
saved: make(map[string]*OS2LPeripheral),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,37 +37,92 @@ func (f *OS2LFinder) Initialize() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnArrival is the callback function when a new peripheral arrives
|
||||
func (f *OS2LFinder) OnArrival(cb func(p PeripheralInfo)) {
|
||||
f.onArrival = cb
|
||||
}
|
||||
|
||||
// OnRemoval i the callback when a peripheral goes away
|
||||
func (f *OS2LFinder) OnRemoval(cb func(p PeripheralInfo)) {
|
||||
f.onRemoval = cb
|
||||
}
|
||||
|
||||
// RegisterPeripheral registers a new peripheral
|
||||
func (f *OS2LFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
|
||||
// Create a random serial number for this peripheral
|
||||
peripheralData.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
// If the SerialNumber is empty, generate another one
|
||||
if peripheralData.SerialNumber == "" {
|
||||
peripheralData.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
|
||||
}
|
||||
|
||||
// Create a new OS2L peripheral
|
||||
peripheral, err := NewOS2LPeripheral(peripheralData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to create the OS2L peripheral: %w", err)
|
||||
}
|
||||
// Set the event callback
|
||||
peripheral.SetEventCallback(func(event any) {
|
||||
runtime.EventsEmit(ctx, string(PeripheralEventEmitted), peripheralData.SerialNumber, event)
|
||||
})
|
||||
|
||||
f.saved[peripheralData.SerialNumber] = peripheral
|
||||
log.Trace().Str("file", "OS2LFinder").Str("serialNumber", peripheralData.SerialNumber).Msg("OS2L peripheral created")
|
||||
|
||||
os2lPeripheral, err := NewOS2LPeripheral(peripheralData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to create the OS2L peripheral: %v", err)
|
||||
}
|
||||
// Connect this peripheral
|
||||
err = os2lPeripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
// New OS2L peripheral has arrived
|
||||
if f.onArrival != nil {
|
||||
f.onArrival(peripheral.GetInfo())
|
||||
}
|
||||
|
||||
f.registeredPeripherals[peripheralData.SerialNumber] = *os2lPeripheral
|
||||
log.Trace().Any("periph", &os2lPeripheral).Str("file", "OS2LFinder").Str("peripheralName", peripheralData.Name).Msg("OS2L peripheral has been created")
|
||||
f.wg.Add(1)
|
||||
go func() {
|
||||
defer f.wg.Done()
|
||||
// Connect the OS2L peripheral
|
||||
err = peripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "OS2LFinder").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", "OS2LFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the OS2L peripheral")
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// Emits the event in the hardware
|
||||
runtime.EventsEmit(ctx, "LOAD_PERIPHERAL", peripheralData)
|
||||
|
||||
return peripheralData.SerialNumber, nil
|
||||
}
|
||||
|
||||
// UnregisterPeripheral unregisters an existing peripheral
|
||||
func (f *OS2LFinder) UnregisterPeripheral(peripheralID string) error {
|
||||
peripheral, registered := f.registeredPeripherals[peripheralID]
|
||||
if registered {
|
||||
err := peripheral.Disconnect()
|
||||
func (f *OS2LFinder) UnregisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if peripheral, detected := f.saved[peripheralData.SerialNumber]; detected {
|
||||
// Deactivating peripheral
|
||||
err := peripheral.Deactivate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to deactivate the peripheral")
|
||||
return nil
|
||||
}
|
||||
// Disconnecting peripheral
|
||||
err = peripheral.Disconnect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
delete(f.registeredPeripherals, peripheralID)
|
||||
|
||||
// The OS2L peripheral has gone
|
||||
f.onRemoval(peripheralData)
|
||||
|
||||
delete(f.saved, peripheralData.SerialNumber)
|
||||
runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -68,9 +132,9 @@ func (f *OS2LFinder) GetName() string {
|
||||
}
|
||||
|
||||
// GetPeripheralSettings gets the peripheral settings
|
||||
func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
|
||||
func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]any, error) {
|
||||
// Return the specified peripheral
|
||||
peripheral, found := f.registeredPeripherals[peripheralID]
|
||||
peripheral, found := f.saved[peripheralID]
|
||||
if !found {
|
||||
log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the OS2L finder")
|
||||
return nil, fmt.Errorf("unable to found the peripheral")
|
||||
@@ -80,24 +144,49 @@ func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]inte
|
||||
}
|
||||
|
||||
// SetPeripheralSettings sets the peripheral settings
|
||||
func (f *OS2LFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error {
|
||||
func (f *OS2LFinder) SetPeripheralSettings(ctx context.Context, peripheralID string, settings map[string]any) error {
|
||||
// Return the specified peripheral
|
||||
peripheral, found := f.registeredPeripherals[peripheralID]
|
||||
peripheral, found := f.saved[peripheralID]
|
||||
if !found {
|
||||
log.Error().Str("file", "OS2LFinder").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", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
|
||||
return peripheral.SetSettings(settings)
|
||||
|
||||
// Set the peripheral settings
|
||||
return peripheral.SetSettings(ctx, settings)
|
||||
}
|
||||
|
||||
// Start starts the finder
|
||||
func (f *OS2LFinder) Start(ctx context.Context) error {
|
||||
// No peripherals to scan here
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops this finder
|
||||
func (f *OS2LFinder) Stop() error {
|
||||
// WaitStop stops the finder
|
||||
func (f *OS2LFinder) WaitStop() error {
|
||||
log.Trace().Str("file", "OS2LFinder").Msg("stopping the OS2L finder...")
|
||||
|
||||
// Close the channel
|
||||
// close(f.scanChannel)
|
||||
|
||||
// Wait for all the peripherals to close
|
||||
log.Trace().Str("file", "OS2LFinder").Msg("closing all OS2L peripherals")
|
||||
var errs []error
|
||||
for registeredPeripheralSN, registeredPeripheral := range f.saved {
|
||||
err := registeredPeripheral.WaitStop()
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Waiting internal tasks
|
||||
f.wg.Wait()
|
||||
|
||||
// Returning errors
|
||||
if len(errs) > 0 {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user