Files
powerpods/goTool/cmd_ble_rtt.go
T
skrueckenandCursor 92a13d7fd0 Add BLE host transport selectable via DIP8 alongside UART.
Master can speak the same framed protobuf protocol over NimBLE or UART; goTool gains -ble, ble-rtt, and ble-stream timing checks for ESP-NOW coexistence.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 16:11:10 +02:00

78 lines
1.8 KiB
Go

package main
import (
"flag"
"fmt"
"math"
"sort"
"time"
)
func runBleRtt(h hostConn, args []string) error {
fs := flag.NewFlagSet("ble-rtt", flag.ExitOnError)
count := fs.Int("count", 50, "number of measured VERSION round-trips")
settle := fs.Float64("settle", 0, "extra settle seconds before measurement (0 = already settled on connect)")
warmup := fs.Int("warmup", 5, "discarded warm-up round-trips")
if err := fs.Parse(args); err != nil {
return err
}
if *count < 1 {
return fmt.Errorf("--count must be >= 1")
}
if *warmup < 0 {
return fmt.Errorf("--warmup must be >= 0")
}
if *settle > 0 {
fmt.Printf("settling %.1fs…\n", *settle)
time.Sleep(time.Duration(*settle * float64(time.Second)))
}
total := *warmup + *count
samples := make([]float64, 0, *count)
for i := 0; i < total; i++ {
t0 := time.Now()
if _, err := hostGetVersion(h); err != nil {
return fmt.Errorf("VERSION round-trip %d: %w", i, err)
}
ms := float64(time.Since(t0).Microseconds()) / 1000.0
if i >= *warmup {
samples = append(samples, ms)
}
}
sort.Float64s(samples)
fmt.Printf("ble-rtt (VERSION): n=%d warmup=%d\n", len(samples), *warmup)
fmt.Printf(" min=%.3f median=%.3f mean=%.3f max=%.3f ms\n",
samples[0], percentile(samples, 0.5), mean(samples), samples[len(samples)-1])
return nil
}
func mean(v []float64) float64 {
if len(v) == 0 {
return math.NaN()
}
var s float64
for _, x := range v {
s += x
}
return s / float64(len(v))
}
func percentile(sorted []float64, p float64) float64 {
if len(sorted) == 0 {
return math.NaN()
}
if len(sorted) == 1 {
return sorted[0]
}
idx := p * float64(len(sorted)-1)
lo := int(idx)
hi := lo + 1
if hi >= len(sorted) {
return sorted[lo]
}
frac := idx - float64(lo)
return sorted[lo]*(1-frac) + sorted[hi]*frac
}