diff --git a/bridge b/bridge index 68324eb..d517d58 100755 Binary files a/bridge and b/bridge differ diff --git a/cmd/bridge/ant.go b/cmd/bridge/ant.go index 1f7cd77..7623d6b 100644 --- a/cmd/bridge/ant.go +++ b/cmd/bridge/ant.go @@ -28,6 +28,8 @@ 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 { panic(err) @@ -66,9 +68,13 @@ func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32, cur := ant.PowerMessage(msg) logPrintf("ant: %v: power: %dW cadence: %d rpm\n", msg.DeviceNumber(), cur.InstantaneousPower(), cur.InstantaneousCadence()) - events <- Event{ + select { + case events <- Event{ Power: cur.InstantaneousPower(), Cadence: cur.InstantaneousCadence(), + }: + default: + logPrintf("ant: dropped event, buffer full\n") } } } diff --git a/cmd/bridge/cli.go b/cmd/bridge/cli.go index 49e5af9..eb0cee8 100644 --- a/cmd/bridge/cli.go +++ b/cmd/bridge/cli.go @@ -73,7 +73,7 @@ This allows these trainers to be used with modern devices that don't support ANT logPrintf("hciconfig up failed: %v\n", err) } - events := make(chan Event) + events := make(chan Event, 1) go ListenForTrainer(uint16(c.vendorID), uint16(c.productID), c.deviceNumber, events) ExposeBluetooth(events) diff --git a/cmd/bridge/log.go b/cmd/bridge/log.go index 4f18690..20db863 100644 --- a/cmd/bridge/log.go +++ b/cmd/bridge/log.go @@ -2,15 +2,20 @@ package main import ( "fmt" + "sync" "time" ) +var logMu sync.Mutex + func logMsg(msg string) { - fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " " + msg) + logPrintf("%s\n", msg) } func logPrintf(format string, a ...any) { + logMu.Lock() fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" "+format, a...) + logMu.Unlock() } func logMsgf(format string, a ...any) {