refactor: replace magic hex constants with named constants

- Add BLE spec constants for service/characteristic UUIDs and signal flag
- Add ANT+ spec constants for data page mask and cycling power page
- Add descriptive comments explaining each constant's origin and purpose
This commit is contained in:
Erik Brakkee 2026-05-26 21:58:16 +00:00
parent 443a88e0f1
commit 72987c5935
3 changed files with 19 additions and 7 deletions

BIN
bridge Executable file

Binary file not shown.

View File

@ -10,6 +10,11 @@ import (
"github.com/half2me/antgo/driver/usb" "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 { func resetAndWait(drv io.ReadWriter) error {
if _, err := drv.Write(ant.SystemResetMessage()); err != nil { if _, err := drv.Write(ant.SystemResetMessage()); err != nil {
return err return err
@ -61,8 +66,8 @@ func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
switch msg.DeviceType() { switch msg.DeviceType() {
case ant.DEVICE_TYPE_POWER: case ant.DEVICE_TYPE_POWER:
// Check data page first // Check data page first
page := msg[4] & 0x7F // mask toggle bit page := msg[4] & antDataPageMask
if page != 0x10 { if page != antDataPageCyclingPower {
continue continue
} }
cur := ant.PowerMessage(msg) cur := ant.PowerMessage(msg)

View File

@ -8,6 +8,13 @@ import (
"tinygo.org/x/bluetooth" "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 var adapter = bluetooth.DefaultAdapter
func must(err error) { func must(err error) {
@ -22,15 +29,15 @@ func ExposeBluetooth(events <-chan Event) {
var powerChar bluetooth.Characteristic var powerChar bluetooth.Characteristic
must(adapter.AddService(&bluetooth.Service{ must(adapter.AddService(&bluetooth.Service{
UUID: bluetooth.New16BitUUID(0x1818), UUID: bluetooth.New16BitUUID(bleCyclingPowerServiceUUID),
Characteristics: []bluetooth.CharacteristicConfig{ Characteristics: []bluetooth.CharacteristicConfig{
{ {
UUID: bluetooth.New16BitUUID(0x2A65), // Cycling Power Feature UUID: bluetooth.New16BitUUID(bleCyclingPowerFeatureCharacteristic),
Flags: bluetooth.CharacteristicReadPermission, Flags: bluetooth.CharacteristicReadPermission,
Value: []byte{0, 0, 0, 0}, Value: []byte{0, 0, 0, 0},
}, },
{ {
UUID: bluetooth.New16BitUUID(0x2A63), // Cycling Power Measurement UUID: bluetooth.New16BitUUID(bleCyclingPowerMeasurementCharacteristic),
Flags: bluetooth.CharacteristicNotifyPermission, Flags: bluetooth.CharacteristicNotifyPermission,
Handle: &powerChar, Handle: &powerChar,
}, },
@ -39,7 +46,7 @@ func ExposeBluetooth(events <-chan Event) {
must(adapter.DefaultAdvertisement().Configure(bluetooth.AdvertisementOptions{ must(adapter.DefaultAdvertisement().Configure(bluetooth.AdvertisementOptions{
LocalName: "LinuxAntBtBridge", LocalName: "LinuxAntBtBridge",
ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(0x1818)}, ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(bleCyclingPowerServiceUUID)},
})) }))
must(adapter.DefaultAdvertisement().Start()) must(adapter.DefaultAdvertisement().Start())
logMsg("BLE advertising...") logMsg("BLE advertising...")
@ -67,7 +74,7 @@ func ExposeBluetooth(events <-chan Event) {
lastCompletedRev = elapsedTicks - fracRev*timePerRev lastCompletedRev = elapsedTicks - fracRev*timePerRev
payload := make([]byte, 8) 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[2:], watts)
binary.LittleEndian.PutUint16(payload[4:], uint16(completedRevs)) binary.LittleEndian.PutUint16(payload[4:], uint16(completedRevs))
binary.LittleEndian.PutUint16(payload[6:], uint16(lastCompletedRev)) binary.LittleEndian.PutUint16(payload[6:], uint16(lastCompletedRev))