diff --git a/cmd/bridge/bluetooth.go b/cmd/bridge/bluetooth.go index 383791e..9980f5e 100644 --- a/cmd/bridge/bluetooth.go +++ b/cmd/bridge/bluetooth.go @@ -61,6 +61,7 @@ func ExposeBluetooth(events <-chan Event) { lastUpdateTime := event.Now var lastCompletedRevs float64 = 0.0 var lastRevTicks float64 = 0.0 // time in 1/1024s of last completed revolution + prevRpm := float64(event.Cadence) for event := range events { now := event.Now @@ -69,6 +70,7 @@ func ExposeBluetooth(events <-chan Event) { watts := event.Power rpm := float64(event.Cadence) + avgRpm := (rpm + prevRpm) / 2 totalRevs += rpm / 60.0 * deltaT elapsedTicks += deltaT * 1024.0 @@ -81,7 +83,7 @@ func ExposeBluetooth(events <-chan Event) { // time at which the last completed revolution occurred // = current time - fractional part of revolution * time per revolution fracRev := totalRevs - completedRevs - ticksPerRev := 1024.0 * 60.0 / rpm + ticksPerRev := 1024.0 * 60.0 / avgRpm lastRevTicks = elapsedTicks - fracRev*ticksPerRev lastCompletedRevs = completedRevs @@ -90,11 +92,12 @@ func ExposeBluetooth(events <-chan Event) { // No change we need to emit the same data ss before. } + prevRpm = rpm payload := make([]byte, 8) binary.LittleEndian.PutUint16(payload[0:], bleSignalDetectedFlag) binary.LittleEndian.PutUint16(payload[2:], watts) binary.LittleEndian.PutUint16(payload[4:], uint16(lastCompletedRevs)) - binary.LittleEndian.PutUint16(payload[6:], uint16(lastRevTicks)) + binary.Little\Endian.PutUint16(payload[6:], uint16(lastRevTicks)) if _, err := powerChar.Write(payload); err != nil { logPrintf("bt: failed to write power characteristic: %v\n", err)