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:
parent
443a88e0f1
commit
72987c5935
@ -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)
|
||||
|
||||
@ -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))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user