diff --git a/cmd/bridge/ant.go b/cmd/bridge/ant.go index 85f08cd..fcb1b46 100644 --- a/cmd/bridge/ant.go +++ b/cmd/bridge/ant.go @@ -27,7 +27,8 @@ func resetAndWait(drv io.ReadWriter) error { } } -func listenForAntDevice(vendorId gousb.ID, productId gousb.ID, deviceNumber uint32) { +func ListenForTrainer(vendorId gousb.ID, productId gousb.ID, deviceNumber uint32, + events chan<- Event) { drv, err := usb.GetDevice(vendorId, productId) if err != nil { panic(err) @@ -58,9 +59,18 @@ func listenForAntDevice(vendorId gousb.ID, productId gousb.ID, deviceNumber uint switch msg.DeviceType() { case ant.DEVICE_TYPE_POWER: + // Check data page first + page := msg[4] & 0x7F // mask toggle bit + if page != 0x10 { + continue + } cur := ant.PowerMessage(msg) fmt.Printf("%v: power: %dW cadence: %d rpm\n", msg.DeviceNumber(), cur.InstantaneousPower(), cur.InstantaneousCadence()) + events <- Event{ + Power: cur.InstantaneousPower(), + Cadence: cur.InstantaneousCadence(), + } } } } diff --git a/cmd/bridge/bluetooth.go b/cmd/bridge/bluetooth.go index 2cdc95d..fccf437 100644 --- a/cmd/bridge/bluetooth.go +++ b/cmd/bridge/bluetooth.go @@ -17,7 +17,7 @@ func must(err error) { } } -func ExposeBluetooth() { +func ExposeBluetooth(events <-chan Event) { must(adapter.Enable()) var powerChar bluetooth.Characteristic @@ -49,14 +49,13 @@ func ExposeBluetooth() { var lastCompletedRev float64 // time in 1/1024s of last completed revolution lastUpdate := time.Now() var elapsedTicks float64 - for { + for event := range events { now := time.Now() deltaT := now.Sub(lastUpdate).Seconds() lastUpdate = now - sec := now.Second() - watts := uint16(100 + sec) - rpm := float64(50 + sec) + watts := event.Power + rpm := float64(event.Cadence) totalRevs += rpm / 60.0 * deltaT elapsedTicks += deltaT * 1024.0 @@ -77,7 +76,5 @@ func ExposeBluetooth() { powerChar.Write(payload) fmt.Printf("watts: %d rpm: %.0f completedRevs: %.0f lastRevTime: %.0f\n", watts, rpm, completedRevs, lastCompletedRev) - - time.Sleep(250 * time.Millisecond) } } diff --git a/cmd/bridge/event.go b/cmd/bridge/event.go new file mode 100644 index 0000000..4cfd2bf --- /dev/null +++ b/cmd/bridge/event.go @@ -0,0 +1,6 @@ +package main + +type Event struct { + Power uint16 + Cadence uint8 +} diff --git a/cmd/bridge/main.go b/cmd/bridge/main.go index ccc5421..2c17076 100644 --- a/cmd/bridge/main.go +++ b/cmd/bridge/main.go @@ -1,26 +1,20 @@ package main import ( - "fmt" "os/exec" "github.com/google/gousb" ) -func oldMain() { +func main() { + exec.Command("hciconfig", "hci0", "down").Run() + exec.Command("hciconfig", "hci0", "up").Run() var vendorId gousb.ID = 0x0fcf var productId gousb.ID = 0x1008 var deviceNumber uint32 = 3001 - listenForAntDevice(vendorId, productId, deviceNumber) - - fmt.Println("channel closed") -} - -func main() { - exec.Command("hciconfig", "hci0", "down").Run() - exec.Command("hciconfig", "hci0", "up").Run() - - ExposeBluetooth() + events := make(chan Event) + go ListenForTrainer(vendorId, productId, deviceNumber, events) + ExposeBluetooth(events) }