2024-12-23 17:22:37 +01:00
|
|
|
package hardware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/rand"
|
2024-12-26 14:55:55 +01:00
|
|
|
"strings"
|
2024-12-29 13:09:46 +01:00
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-12-23 17:22:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// OS2LDriver represents how the protocol is defined
|
|
|
|
|
type OS2LDriver struct {
|
|
|
|
|
peripherals map[string]Peripheral // The list of peripherals
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewOS2LDriver creates a new OS2L driver
|
|
|
|
|
func NewOS2LDriver() *OS2LDriver {
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Trace().Str("file", "OS2LDriver").Msg("OS2L driver created")
|
2024-12-23 17:22:37 +01:00
|
|
|
return &OS2LDriver{
|
|
|
|
|
peripherals: make(map[string]Peripheral),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize initializes the MIDI driver
|
|
|
|
|
func (d *OS2LDriver) Initialize() error {
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Trace().Str("file", "OS2LDriver").Msg("OS2L driver initialized")
|
2024-12-23 17:22:37 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreatePeripheral creates a new OS2L peripheral
|
|
|
|
|
func (d *OS2LDriver) CreatePeripheral(ctx context.Context) (Peripheral, error) {
|
|
|
|
|
// Create a random serial number for this peripheral
|
2024-12-26 14:55:55 +01:00
|
|
|
randomSerialNumber := strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Trace().Str("file", "OS2LDriver").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created")
|
2024-12-23 17:22:37 +01:00
|
|
|
peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber)
|
|
|
|
|
d.peripherals[randomSerialNumber] = peripheral
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Info().Str("file", "OS2LDriver").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created and registered")
|
2024-12-23 17:22:37 +01:00
|
|
|
return peripheral, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemovePeripheral removes an OS2L dev
|
|
|
|
|
func (d *OS2LDriver) RemovePeripheral(serialNumber string) error {
|
|
|
|
|
delete(d.peripherals, serialNumber)
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Info().Str("file", "OS2LDriver").Str("serialNumber", serialNumber).Msg("OS2L peripheral removed")
|
2024-12-23 17:22:37 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetName returns the name of the driver
|
|
|
|
|
func (d *OS2LDriver) GetName() string {
|
|
|
|
|
return "OS2L"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPeripheral gets the peripheral that correspond to the specified ID
|
|
|
|
|
func (d *OS2LDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
|
|
|
|
// Return the specified peripheral
|
2024-12-29 13:09:46 +01:00
|
|
|
peripheral, found := d.peripherals[peripheralID]
|
|
|
|
|
if !found {
|
|
|
|
|
log.Error().Str("file", "OS2LDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the OS2L driver")
|
2024-12-23 17:22:37 +01:00
|
|
|
return nil, false
|
|
|
|
|
}
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Trace().Str("file", "OS2LDriver").Str("peripheralID", peripheralID).Msg("OS2L peripheral found in the driver")
|
2024-12-23 17:22:37 +01:00
|
|
|
return peripheral, true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scan scans the interfaces compatible with the MIDI protocol
|
|
|
|
|
func (d *OS2LDriver) Scan(ctx context.Context) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|