generated from thinkode/modelRepository
add OS2L device creation
This commit is contained in:
@@ -17,41 +17,41 @@ const (
|
||||
scanDelay = 4 * time.Second // Waiting delay before scanning the FTDI devices
|
||||
)
|
||||
|
||||
// FTDIFinder represents how the protocol is defined
|
||||
type FTDIFinder struct {
|
||||
// FTDIDriver represents how the protocol is defined
|
||||
type FTDIDriver struct {
|
||||
peripherals map[string]Peripheral
|
||||
}
|
||||
|
||||
// NewFTDIFinder creates a new FTDI finder
|
||||
func NewFTDIFinder() *FTDIFinder {
|
||||
return &FTDIFinder{
|
||||
// NewFTDIDriver creates a new FTDI finder
|
||||
func NewFTDIDriver() *FTDIDriver {
|
||||
return &FTDIDriver{
|
||||
peripherals: make(map[string]Peripheral),
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize initializes the FTDI finder
|
||||
func (f *FTDIFinder) Initialize() error {
|
||||
// Initialize initializes the FTDI driver
|
||||
func (d *FTDIDriver) Initialize() error {
|
||||
if goRuntime.GOOS != "windows" {
|
||||
return fmt.Errorf("<!> The FTDI finder is not compatible with your platform yet (%s)", goRuntime.GOOS)
|
||||
return fmt.Errorf("<!> The FTDI driver is not compatible with your platform yet (%s)", goRuntime.GOOS)
|
||||
}
|
||||
fmt.Println("FTDI finder initialized")
|
||||
fmt.Println("FTDI driver initialized")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetName returns the name of the finder
|
||||
func (f *FTDIFinder) GetName() string {
|
||||
// GetName returns the name of the driver
|
||||
func (d *FTDIDriver) GetName() string {
|
||||
return "FTDI"
|
||||
}
|
||||
|
||||
// GetPeripheral gets the peripheral that correspond to the specified ID
|
||||
func (f *FTDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
||||
func (d *FTDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
||||
// Return the specified peripheral
|
||||
peripheral := f.peripherals[peripheralID]
|
||||
peripheral := d.peripherals[peripheralID]
|
||||
if peripheral == nil {
|
||||
fmt.Println("Unable to get the peripheral in the finder")
|
||||
fmt.Println("Unable to get the peripheral with the driver")
|
||||
return nil, false
|
||||
}
|
||||
fmt.Println("Peripheral found in the finder")
|
||||
fmt.Println("Peripheral found by the FTDI driver")
|
||||
|
||||
return peripheral, true
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (f *FTDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
||||
var findFTDI []byte
|
||||
|
||||
// Scan scans the FTDI peripherals
|
||||
func (f *FTDIFinder) Scan(ctx context.Context) error {
|
||||
func (d *FTDIDriver) Scan(ctx context.Context) error {
|
||||
time.Sleep(scanDelay)
|
||||
|
||||
// Create a temporary file
|
||||
@@ -113,18 +113,28 @@ func (f *FTDIFinder) Scan(ctx context.Context) error {
|
||||
location = -1
|
||||
}
|
||||
// Add the peripheral to the temporary list
|
||||
peripheral, err := NewFTDIPeripheral(deviceInfo[2], deviceInfo[1], location, f.GetName())
|
||||
peripheral, err := NewFTDIPeripheral(deviceInfo[2], deviceInfo[1], location)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to create the FTDI peripheral: %v", err)
|
||||
}
|
||||
ftdiPeripherals[deviceInfo[1]] = peripheral
|
||||
}
|
||||
// Compare with the current peripherals to detect arrivals/removals
|
||||
removedList, addedList := comparePeripherals(f.peripherals, ftdiPeripherals)
|
||||
removedList, addedList := comparePeripherals(d.peripherals, ftdiPeripherals)
|
||||
// Emit the events
|
||||
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
|
||||
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
|
||||
// Store the new peripherals list
|
||||
f.peripherals = ftdiPeripherals
|
||||
d.peripherals = ftdiPeripherals
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreatePeripheral is not implemented here
|
||||
func (d *FTDIDriver) CreatePeripheral(context.Context) (Peripheral, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// RemovePeripheral is not implemented here
|
||||
func (d *FTDIDriver) RemovePeripheral(serialNumber string) error {
|
||||
return nil
|
||||
}
|
||||
@@ -26,7 +26,6 @@ type FTDIPeripheral struct {
|
||||
serialNumber string // The S/N of the FTDI peripheral
|
||||
location int // The location of the peripheral
|
||||
universesNumber int // The number of DMX universes handled by this peripheral
|
||||
finderName string // The name of the parent finder
|
||||
programName string // The temp file name of the executable
|
||||
dmxSender *exec.Cmd // The command to pilot the DMX sender program
|
||||
stdin io.WriteCloser // For writing in the DMX sender
|
||||
@@ -38,7 +37,7 @@ type FTDIPeripheral struct {
|
||||
}
|
||||
|
||||
// NewFTDIPeripheral creates a new FTDI peripheral
|
||||
func NewFTDIPeripheral(name string, serialNumber string, location int, finderName string) (*FTDIPeripheral, error) {
|
||||
func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPeripheral, error) {
|
||||
// Create a temporary file
|
||||
tempFile, err := os.CreateTemp("", "dmxSender*.exe")
|
||||
if err != nil {
|
||||
@@ -57,7 +56,6 @@ func NewFTDIPeripheral(name string, serialNumber string, location int, finderNam
|
||||
programName: tempFile.Name(),
|
||||
serialNumber: serialNumber,
|
||||
location: location,
|
||||
finderName: finderName,
|
||||
universesNumber: 1,
|
||||
disconnectChan: make(chan struct{}),
|
||||
errorsChan: make(chan error, 1),
|
||||
|
||||
@@ -29,33 +29,33 @@ import (
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// MIDIFinder represents how the protocol is defined
|
||||
type MIDIFinder struct {
|
||||
// MIDIDriver represents how the protocol is defined
|
||||
type MIDIDriver struct {
|
||||
peripherals map[string]Peripheral // The list of peripherals
|
||||
}
|
||||
|
||||
// NewMIDIFinder creates a new DMXUSB protocol
|
||||
func NewMIDIFinder() *MIDIFinder {
|
||||
return &MIDIFinder{
|
||||
// NewMIDIDriver creates a new DMXUSB protocol
|
||||
func NewMIDIDriver() *MIDIDriver {
|
||||
return &MIDIDriver{
|
||||
peripherals: make(map[string]Peripheral),
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize initializes the DMX-USB finder
|
||||
func (f *MIDIFinder) Initialize() error {
|
||||
fmt.Println("MIDI finder initialized")
|
||||
// Initialize initializes the MIDI driver
|
||||
func (d *MIDIDriver) Initialize() error {
|
||||
fmt.Println("MIDI driver initialized")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetName returns the name of the finder
|
||||
func (f *MIDIFinder) GetName() string {
|
||||
// GetName returns the name of the driver
|
||||
func (d *MIDIDriver) GetName() string {
|
||||
return "MIDI"
|
||||
}
|
||||
|
||||
// GetPeripheral gets the peripheral that correspond to the specified ID
|
||||
func (f *MIDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
||||
func (d *MIDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
||||
// Return the specified peripheral
|
||||
peripheral := f.peripherals[peripheralID]
|
||||
peripheral := d.peripherals[peripheralID]
|
||||
if peripheral == nil {
|
||||
return nil, false
|
||||
}
|
||||
@@ -84,7 +84,7 @@ func splitStringAndNumber(input string) (string, int, error) {
|
||||
}
|
||||
|
||||
// Scan scans the interfaces compatible with the MIDI protocol
|
||||
func (f *MIDIFinder) Scan(ctx context.Context) error {
|
||||
func (d *MIDIDriver) Scan(ctx context.Context) error {
|
||||
midiPeripherals := make(map[string]Peripheral)
|
||||
fmt.Println("Opening MIDI scanner port...")
|
||||
midiScanner, err := rtmidi.NewMIDIInDefault()
|
||||
@@ -109,14 +109,24 @@ func (f *MIDIFinder) Scan(ctx context.Context) error {
|
||||
}
|
||||
fmt.Printf("New MIDI device found: %s on %i\n", name, location)
|
||||
// Add the peripheral to the temporary list
|
||||
midiPeripherals[portName] = NewMIDIPeripheral(name, location, f.GetName())
|
||||
midiPeripherals[portName] = NewMIDIPeripheral(name, location)
|
||||
}
|
||||
// Compare with the current peripherals to detect arrivals/removals
|
||||
removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals)
|
||||
removedList, addedList := comparePeripherals(d.peripherals, midiPeripherals)
|
||||
// Emit the events
|
||||
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
|
||||
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
|
||||
// Store the new peripherals list
|
||||
f.peripherals = midiPeripherals
|
||||
d.peripherals = midiPeripherals
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreatePeripheral is not implemented here
|
||||
func (d *MIDIDriver) CreatePeripheral(context.Context) (Peripheral, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// RemovePeripheral is not implemented here
|
||||
func (d *MIDIDriver) RemovePeripheral(serialNumber string) error {
|
||||
return nil
|
||||
}
|
||||
@@ -4,15 +4,13 @@ package hardware
|
||||
type MIDIPeripheral struct {
|
||||
name string // The name of the peripheral
|
||||
location int // The location of the peripheral
|
||||
finderName string // The name of the parent finder
|
||||
}
|
||||
|
||||
// NewMIDIPeripheral creates a new MIDI peripheral
|
||||
func NewMIDIPeripheral(name string, location int, finderName string) *MIDIPeripheral {
|
||||
func NewMIDIPeripheral(name string, location int) *MIDIPeripheral {
|
||||
return &MIDIPeripheral{
|
||||
name: name,
|
||||
location: location,
|
||||
finderName: finderName,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
60
hardware/OS2LDriver.go
Normal file
60
hardware/OS2LDriver.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
return &OS2LDriver{
|
||||
peripherals: make(map[string]Peripheral),
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize initializes the MIDI driver
|
||||
func (d *OS2LDriver) Initialize() error {
|
||||
fmt.Println("OS2L driver initialized")
|
||||
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
|
||||
randomSerialNumber := fmt.Sprintf("%08x", rand.Intn(1<<32))
|
||||
peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber)
|
||||
d.peripherals[randomSerialNumber] = peripheral
|
||||
return peripheral, nil
|
||||
}
|
||||
|
||||
// RemovePeripheral removes an OS2L dev
|
||||
func (d *OS2LDriver) RemovePeripheral(serialNumber string) error {
|
||||
delete(d.peripherals, serialNumber)
|
||||
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
|
||||
peripheral := d.peripherals[peripheralID]
|
||||
if peripheral == nil {
|
||||
return nil, false
|
||||
}
|
||||
return peripheral, true
|
||||
}
|
||||
|
||||
// Scan scans the interfaces compatible with the MIDI protocol
|
||||
func (d *OS2LDriver) Scan(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
55
hardware/OS2LPeripheral.go
Normal file
55
hardware/OS2LPeripheral.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package hardware
|
||||
|
||||
import "fmt"
|
||||
|
||||
// OS2LPeripheral contains the data of an OS2L peripheral
|
||||
type OS2LPeripheral struct {
|
||||
name string // The name of the peripheral
|
||||
serialNumber string // The serial number of the peripheral
|
||||
}
|
||||
|
||||
// NewOS2LPeripheral creates a new OS2L peripheral
|
||||
func NewOS2LPeripheral(name string, serialNumber string) *OS2LPeripheral {
|
||||
return &OS2LPeripheral{
|
||||
name: name,
|
||||
serialNumber: serialNumber,
|
||||
}
|
||||
}
|
||||
|
||||
// Connect connects the MIDI peripheral
|
||||
func (p *OS2LPeripheral) Connect() error {
|
||||
fmt.Println("OS2L peripheral connected")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect disconnects the MIDI peripheral
|
||||
func (p *OS2LPeripheral) Disconnect() error {
|
||||
fmt.Println("OS2L peripheral disconnected")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Activate activates the MIDI peripheral
|
||||
func (p *OS2LPeripheral) Activate() error {
|
||||
fmt.Println("OS2L peripheral activated")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deactivate deactivates the MIDI peripheral
|
||||
func (p *OS2LPeripheral) Deactivate() error {
|
||||
fmt.Println("OS2L peripheral deactivated")
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDeviceProperty - not implemented for this kind of peripheral
|
||||
func (p *OS2LPeripheral) SetDeviceProperty(uint32, uint32, byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInfo gets the peripheral information
|
||||
func (p *OS2LPeripheral) GetInfo() PeripheralInfo {
|
||||
return PeripheralInfo{
|
||||
Name: p.name,
|
||||
SerialNumber: p.serialNumber,
|
||||
ProtocolName: "OS2L",
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -21,10 +21,12 @@ type PeripheralInfo struct {
|
||||
Settings []interface{} `yaml:"settings"` // Number of DMX universes handled by the peripheral
|
||||
}
|
||||
|
||||
// PeripheralFinder represents how compatible peripheral finders are implemented
|
||||
type PeripheralFinder interface {
|
||||
Initialize() error // Initializes the protocol
|
||||
GetName() string // Get the name of the finder
|
||||
GetPeripheral(string) (Peripheral, bool) // Get the peripheral
|
||||
Scan(context.Context) error // Scan for peripherals
|
||||
// PeripheralDriver represents how compatible peripheral drivers are implemented
|
||||
type PeripheralDriver interface {
|
||||
Initialize() error // Initializes the protocol
|
||||
GetName() string // Get the name of the finder
|
||||
GetPeripheral(string) (Peripheral, bool) // Get the peripheral
|
||||
Scan(context.Context) error // Scan for peripherals
|
||||
CreatePeripheral(ctx context.Context) (Peripheral, error) // Creates a new peripheral
|
||||
RemovePeripheral(serialNumber string) error // Removes a peripheral
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user