fix project life and plug-and-replay on peripherals

This commit is contained in:
2025-11-11 20:13:27 +01:00
parent 621c1922ca
commit b094019ed9
12 changed files with 326 additions and 262 deletions

View File

@@ -2,14 +2,12 @@ package hardware
import (
"context"
_ "embed"
"sync"
"unsafe"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
/*
@@ -28,12 +26,12 @@ type FTDIPeripheral struct {
}
// NewFTDIPeripheral creates a new FTDI peripheral
func NewFTDIPeripheral(info PeripheralInfo) (*FTDIPeripheral, error) {
func NewFTDIPeripheral(info PeripheralInfo) *FTDIPeripheral {
log.Info().Str("file", "FTDIPeripheral").Str("name", info.Name).Str("s/n", info.SerialNumber).Msg("FTDI peripheral created")
return &FTDIPeripheral{
info: info,
dmxSender: nil,
}, nil
}
}
// Connect connects the FTDI peripheral
@@ -47,13 +45,11 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error {
p.dmxSender = C.dmx_create()
// Connect the FTDI
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "connecting")
serialNumber := C.CString(p.info.SerialNumber)
defer C.free(unsafe.Pointer(serialNumber))
if C.dmx_connect(p.dmxSender, serialNumber) != C.DMX_OK {
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "disconnected")
log.Error().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to connect the DMX device")
return errors.Errorf("unable to connect '%s'")
return errors.Errorf("unable to connect '%s'", p.info.SerialNumber)
}
p.wg.Add(1)
@@ -63,10 +59,7 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error {
_ = p.Disconnect()
}()
// Send deactivated state (connected but not activated for now)
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "deactivated")
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device connected successfully")
return nil
}
@@ -79,6 +72,9 @@ func (p *FTDIPeripheral) Disconnect() error {
// Destroy the dmx sender
C.dmx_destroy(p.dmxSender)
// Reset the pointer to the peripheral
p.dmxSender = nil
return nil
}
@@ -93,7 +89,6 @@ func (p *FTDIPeripheral) Activate(ctx context.Context) error {
err := C.dmx_activate(p.dmxSender)
if err != C.DMX_OK {
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "deactivated")
return errors.Errorf("unable to activate the DMX sender!")
}
@@ -101,10 +96,7 @@ func (p *FTDIPeripheral) Activate(ctx context.Context) error {
C.dmx_setValue(p.dmxSender, C.uint16_t(1), C.uint8_t(255))
C.dmx_setValue(p.dmxSender, C.uint16_t(5), C.uint8_t(255))
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "activated")
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device activated successfully")
return nil
}
@@ -123,7 +115,6 @@ func (p *FTDIPeripheral) Deactivate(ctx context.Context) error {
}
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device deactivated successfully")
return nil
}