diff --git a/bridge b/bridge new file mode 100755 index 0000000..d63c806 Binary files /dev/null and b/bridge differ diff --git a/cmd/bridge/ant.go b/cmd/bridge/ant.go index 7623d6b..dcdf315 100644 --- a/cmd/bridge/ant.go +++ b/cmd/bridge/ant.go @@ -10,6 +10,11 @@ import ( "github.com/half2me/antgo/driver/usb" ) +const ( + antDataPageMask = 0x7F // Mask for data page number (7 bits, clears toggle bit) + antDataPageCyclingPower = 0x10 // Data page 16 = Cycling Power Feature page +) + func resetAndWait(drv io.ReadWriter) error { if _, err := drv.Write(ant.SystemResetMessage()); err != nil { return err @@ -61,8 +66,8 @@ func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32, switch msg.DeviceType() { case ant.DEVICE_TYPE_POWER: // Check data page first - page := msg[4] & 0x7F // mask toggle bit - if page != 0x10 { + page := msg[4] & antDataPageMask + if page != antDataPageCyclingPower { continue } cur := ant.PowerMessage(msg) diff --git a/cmd/bridge/bluetooth.go b/cmd/bridge/bluetooth.go index 2156f61..b00788d 100644 --- a/cmd/bridge/bluetooth.go +++ b/cmd/bridge/bluetooth.go @@ -8,6 +8,13 @@ import ( "tinygo.org/x/bluetooth" ) +const ( + bleCyclingPowerServiceUUID = 0x1818 // BLE Cycling Power Service UUID + bleCyclingPowerFeatureCharacteristic = 0x2A65 // BLE Cycling Power Feature characteristic + bleCyclingPowerMeasurementCharacteristic = 0x2A63 // BLE Cycling Power Measurement characteristic + bleSignalDetectedFlag = 0x20 // Signal Detected bit in BLE payload header +) + var adapter = bluetooth.DefaultAdapter func must(err error) { @@ -22,15 +29,15 @@ func ExposeBluetooth(events <-chan Event) { var powerChar bluetooth.Characteristic must(adapter.AddService(&bluetooth.Service{ - UUID: bluetooth.New16BitUUID(0x1818), + UUID: bluetooth.New16BitUUID(bleCyclingPowerServiceUUID), Characteristics: []bluetooth.CharacteristicConfig{ { - UUID: bluetooth.New16BitUUID(0x2A65), // Cycling Power Feature + UUID: bluetooth.New16BitUUID(bleCyclingPowerFeatureCharacteristic), Flags: bluetooth.CharacteristicReadPermission, Value: []byte{0, 0, 0, 0}, }, { - UUID: bluetooth.New16BitUUID(0x2A63), // Cycling Power Measurement + UUID: bluetooth.New16BitUUID(bleCyclingPowerMeasurementCharacteristic), Flags: bluetooth.CharacteristicNotifyPermission, Handle: &powerChar, }, @@ -39,7 +46,7 @@ func ExposeBluetooth(events <-chan Event) { must(adapter.DefaultAdvertisement().Configure(bluetooth.AdvertisementOptions{ LocalName: "LinuxAntBtBridge", - ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(0x1818)}, + ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(bleCyclingPowerServiceUUID)}, })) must(adapter.DefaultAdvertisement().Start()) logMsg("BLE advertising...") @@ -67,7 +74,7 @@ func ExposeBluetooth(events <-chan Event) { lastCompletedRev = elapsedTicks - fracRev*timePerRev payload := make([]byte, 8) - binary.LittleEndian.PutUint16(payload[0:], 0x20) + binary.LittleEndian.PutUint16(payload[0:], bleSignalDetectedFlag) binary.LittleEndian.PutUint16(payload[2:], watts) binary.LittleEndian.PutUint16(payload[4:], uint16(completedRevs)) binary.LittleEndian.PutUint16(payload[6:], uint16(lastCompletedRev))