- Move flag registration from init() to main() - Make rootCmd a local variable in main() instead of a package variable - Log errors from hciconfig down/up reset commands - Log errors from BLE power characteristic writes
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"time"
|
|
|
|
"tinygo.org/x/bluetooth"
|
|
)
|
|
|
|
var adapter = bluetooth.DefaultAdapter
|
|
|
|
func must(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func ExposeBluetooth(events <-chan Event) {
|
|
must(adapter.Enable())
|
|
|
|
var powerChar bluetooth.Characteristic
|
|
|
|
must(adapter.AddService(&bluetooth.Service{
|
|
UUID: bluetooth.New16BitUUID(0x1818),
|
|
Characteristics: []bluetooth.CharacteristicConfig{
|
|
{
|
|
UUID: bluetooth.New16BitUUID(0x2A65), // Cycling Power Feature
|
|
Flags: bluetooth.CharacteristicReadPermission,
|
|
Value: []byte{0, 0, 0, 0},
|
|
},
|
|
{
|
|
UUID: bluetooth.New16BitUUID(0x2A63), // Cycling Power Measurement
|
|
Flags: bluetooth.CharacteristicNotifyPermission,
|
|
Handle: &powerChar,
|
|
},
|
|
},
|
|
}))
|
|
|
|
must(adapter.DefaultAdvertisement().Configure(bluetooth.AdvertisementOptions{
|
|
LocalName: "LinuxAntBtBridge",
|
|
ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(0x1818)},
|
|
}))
|
|
must(adapter.DefaultAdvertisement().Start())
|
|
logMsg("BLE advertising...")
|
|
|
|
var totalRevs float64
|
|
var lastCompletedRev float64 // time in 1/1024s of last completed revolution
|
|
lastUpdate := time.Now()
|
|
var elapsedTicks float64
|
|
for event := range events {
|
|
now := time.Now()
|
|
deltaT := now.Sub(lastUpdate).Seconds()
|
|
lastUpdate = now
|
|
|
|
watts := event.Power
|
|
rpm := float64(event.Cadence)
|
|
|
|
totalRevs += rpm / 60.0 * deltaT
|
|
elapsedTicks += deltaT * 1024.0
|
|
|
|
completedRevs := math.Floor(totalRevs)
|
|
// time at which the last completed revolution occurred
|
|
// = current time - fractional part of revolution * time per revolution
|
|
fracRev := totalRevs - completedRevs
|
|
timePerRev := 1024.0 * 60.0 / rpm
|
|
lastCompletedRev = elapsedTicks - fracRev*timePerRev
|
|
|
|
payload := make([]byte, 8)
|
|
binary.LittleEndian.PutUint16(payload[0:], 0x20)
|
|
binary.LittleEndian.PutUint16(payload[2:], watts)
|
|
binary.LittleEndian.PutUint16(payload[4:], uint16(completedRevs))
|
|
binary.LittleEndian.PutUint16(payload[6:], uint16(lastCompletedRev))
|
|
|
|
if _, err := powerChar.Write(payload); err != nil {
|
|
logPrintf("bt: failed to write power characteristic: %v\n", err)
|
|
}
|
|
logPrintf("bt: watts: %d rpm: %.0f completedRevs: %.0f lastRevTime: %.0f processingTime: %v\n",
|
|
watts, rpm, completedRevs, lastCompletedRev, time.Now().Sub(now))
|
|
}
|
|
}
|