fixed multiple MIDI devices S/N

This commit is contained in:
2024-12-26 14:55:55 +01:00
9 changed files with 142 additions and 36 deletions

View File

@@ -10,25 +10,6 @@ import (
"github.com/mattrtaylor/go-rtmidi"
)
/*
DMXConnect
DMXUSBProtocol.go
Copyright (c) Valentin Boulanger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// MIDIDriver represents how the protocol is defined
type MIDIDriver struct {
peripherals map[string]Peripheral // The list of peripherals
@@ -109,7 +90,8 @@ func (d *MIDIDriver) 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)
sn := strings.ToLower(strings.Replace(name, " ", "_", -1))
midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn)
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(d.peripherals, midiPeripherals)

View File

@@ -2,15 +2,17 @@ package hardware
// MIDIPeripheral contains the data of a MIDI peripheral
type MIDIPeripheral struct {
name string // The name of the peripheral
location int // The location of the peripheral
name string // The name of the peripheral
location int // The location of the peripheral
serialNumber string // The S/N of the peripheral
}
// NewMIDIPeripheral creates a new MIDI peripheral
func NewMIDIPeripheral(name string, location int) *MIDIPeripheral {
func NewMIDIPeripheral(name string, location int, serialNumber string) *MIDIPeripheral {
return &MIDIPeripheral{
name: name,
location: location,
name: name,
location: location,
serialNumber: serialNumber,
}
}
@@ -44,5 +46,6 @@ func (p *MIDIPeripheral) GetInfo() PeripheralInfo {
return PeripheralInfo{
Name: p.name,
ProtocolName: "MIDI",
SerialNumber: p.serialNumber,
}
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand"
"strings"
)
// OS2LDriver represents how the protocol is defined
@@ -27,7 +28,7 @@ func (d *OS2LDriver) Initialize() error {
// 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))
randomSerialNumber := strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber)
d.peripherals[randomSerialNumber] = peripheral
return peripheral, nil

View File

@@ -152,11 +152,11 @@ func (h *HardwareManager) Scan(ctx context.Context) error {
return fmt.Errorf("No peripherals driver registered")
}
for _, driver := range h.drivers {
finder := driver
driverCopy := driver
go func() {
err := driver.Scan(ctx)
err := driverCopy.Scan(ctx)
if err != nil {
fmt.Printf("Unable to scan peripherals with the %s driver: %s\n", finder.GetName(), err)
fmt.Printf("Unable to scan peripherals with the %s driver: %s\n", driverCopy.GetName(), err)
return
}
}()