show event signal on the interface

This commit is contained in:
2025-11-13 13:01:06 +01:00
parent d730cf4f1c
commit 18a3a716ef
8 changed files with 69 additions and 16 deletions

View File

@@ -24,25 +24,32 @@ type OS2LMessage struct {
type OS2LPeripheral struct {
wg sync.WaitGroup
info PeripheralInfo // The basic info for this peripheral
serverIP string // OS2L server IP
serverPort int // OS2L server port
info PeripheralInfo // The basic info for this peripheral
serverIP string // OS2L server IP
serverPort int // OS2L server port
listener net.Listener // Net listener (TCP)
listenerCancel context.CancelFunc // Call this function to cancel the peripheral activation
eventCallback func(any) // This callback is called for returning events
}
// NewOS2LPeripheral creates a new OS2L peripheral
func NewOS2LPeripheral(peripheralData PeripheralInfo) (*OS2LPeripheral, error) {
log.Trace().Str("file", "OS2LPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("OS2L peripheral created")
return &OS2LPeripheral{
info: peripheralData,
serverIP: "127.0.0.1",
serverPort: 9995,
listener: nil,
info: peripheralData,
serverIP: "127.0.0.1",
serverPort: 9995,
listener: nil,
eventCallback: nil,
}, nil
}
// SetEventCallback sets the callback for returning events
func (p *OS2LPeripheral) SetEventCallback(eventCallback func(any)) {
p.eventCallback = eventCallback
}
// Connect connects the MIDI peripheral
func (p *OS2LPeripheral) Connect(ctx context.Context) error {
var err error
@@ -56,14 +63,18 @@ func (p *OS2LPeripheral) Connect(ctx context.Context) error {
return nil
}
func handleMessage(raw []byte) error {
// handleMessage handles an OS2L message
func (p *OS2LPeripheral) handleMessage(raw []byte) error {
message := OS2LMessage{}
err := json.Unmarshal(raw, &message)
if err != nil {
return fmt.Errorf("Unable to parse the OS2L message: %w", err)
}
// Display the message
fmt.Printf("Event: %s, Name: %s, State: %s, ID: %d, Param: %f\n", message.Event, message.Name, message.State, message.ID, message.Param)
log.Debug().Str("event", message.Event).Str("name", message.Name).Str("state", message.State).Int("ID", int(message.ID)).Float64("param", message.Param).Msg("OS2L event received")
// Return the event to the finder
if p.eventCallback != nil {
go p.eventCallback(message)
}
return nil
}
@@ -135,7 +146,7 @@ func (p *OS2LPeripheral) Activate(ctx context.Context) error {
return // autre erreur ou EOF
}
handleMessage(buffer[:n])
p.handleMessage(buffer[:n])
}
}
}(conn)