workaround for when the trainer stops transmitting. ListenForTrainer now

restarts after not having received data for 10 seconds.
This commit is contained in:
Erik Brakkee 2026-06-14 14:58:22 +02:00
parent 0d49c6f51f
commit c75c2ea0e1
2 changed files with 41 additions and 27 deletions

View File

@ -34,7 +34,6 @@ func resetAndWait(drv io.ReadWriter) error {
func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
events chan<- Event) {
defer close(events)
drv, err := usb.GetDevice(gousb.ID(vendorId), gousb.ID(productId))
if err != nil {
@ -53,10 +52,17 @@ func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
logMsg("ant: scan mode started")
msgs := make(chan ant.BroadcastMessage, 100)
go device.DumpBroadcastMessages(context.Background(), drv, msgs)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
device.DumpBroadcastMessages(ctx, drv, msgs)
logMsg("ant: dump broadcast messages ended")
}()
logMsg("ant: listening...")
for msg := range msgs {
for {
select {
case msg := <-msgs:
if deviceNumber != 0 && msg.DeviceNumber() != deviceNumber {
logPrintf("ant: received msg from ignored device %v\n", msg.DeviceNumber())
continue
@ -84,5 +90,9 @@ func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
logPrintf("ant: dropped event, buffer full\n")
}
}
case <-time.After(10 * time.Second):
logMsg("ant: no more messages arrived, restarting")
return
}
}
}

View File

@ -81,7 +81,11 @@ This allows these trainers to be used with modern devices that don't support ANT
}
events := make(chan Event, 1)
go ListenForTrainer(uint16(c.vendorID), uint16(c.productID), c.deviceNumber, events)
go func() {
for {
ListenForTrainer(uint16(c.vendorID), uint16(c.productID), c.deviceNumber, events)
}
}()
ExposeBluetooth(events)
return nil