From b5b3610b701625b48f7261b0a8d5bbd57f7bc022 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 26 May 2026 16:28:13 +0200 Subject: [PATCH] bluetooth with fake data. --- cmd/bridge/main.go | 72 +++++++++++++++++++++++++++++++++++++++++++++- go.mod | 20 +++++++++++-- go.sum | 43 +++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/cmd/bridge/main.go b/cmd/bridge/main.go index 953aa26..3df10a0 100644 --- a/cmd/bridge/main.go +++ b/cmd/bridge/main.go @@ -1,12 +1,16 @@ package main import ( + "encoding/binary" "fmt" + "os/exec" + "time" "github.com/google/gousb" + "tinygo.org/x/bluetooth" ) -func main() { +func oldMain() { var vendorId gousb.ID = 0x0fcf var productId gousb.ID = 0x1008 @@ -16,3 +20,69 @@ func main() { fmt.Println("channel closed") } + +var adapter = bluetooth.DefaultAdapter + +func main() { + exec.Command("hciconfig", "hci0", "down").Run() + exec.Command("hciconfig", "hci0", "up").Run() + + 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()) + fmt.Println("BLE advertising...") + var crankRevs uint16 + var lastCrankEventTime uint16 + + for { + sec := time.Now().Second() + watts := uint16(100 + sec) + rpm := uint16(50 + sec) + + // advance crank event time based on rpm + // time between crank events = 1024*60/rpm ticks + crankEventInterval := uint16(1024 * 60 / rpm) + lastCrankEventTime += crankEventInterval + crankRevs++ + + // flags: bit 5 = crank revolution data present + payload := make([]byte, 8) + binary.LittleEndian.PutUint16(payload[0:], 0x20) + binary.LittleEndian.PutUint16(payload[2:], watts) + binary.LittleEndian.PutUint16(payload[4:], crankRevs) + binary.LittleEndian.PutUint16(payload[6:], lastCrankEventTime) + + powerChar.Write(payload) + fmt.Printf("watts: %d rpm: %d crankRevs: %d eventTime: %d\n", watts, rpm, crankRevs, lastCrankEventTime) + + time.Sleep(time.Second) + } +} + +func must(err error) { + if err != nil { + panic(err) + } +} diff --git a/go.mod b/go.mod index f412cb8..7cb20c2 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,22 @@ module git.wamblee.org/antplusbridge go 1.26.2 -require github.com/half2me/antgo v0.0.0-20240825222428-e6b9ddc9c32f +require ( + github.com/half2me/antgo v0.0.0-20240825222428-e6b9ddc9c32f + tinygo.org/x/bluetooth v0.15.0 +) -require github.com/google/gousb v1.1.3 // indirect +require ( + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/godbus/dbus/v5 v5.1.0 // indirect + github.com/google/gousb v1.1.3 + github.com/saltosystems/winrt-go v0.0.0-20260317170058-9c2fec580d96 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect + github.com/soypat/cyw43439 v0.1.0 // indirect + github.com/soypat/lneto v0.1.0 // indirect + github.com/soypat/seqs v0.0.0-20250124201400-0d65bc7c1710 // indirect + github.com/tinygo-org/cbgo v0.0.4 // indirect + github.com/tinygo-org/pio v0.3.0 // indirect + golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d // indirect + golang.org/x/sys v0.11.0 // indirect +) diff --git a/go.sum b/go.sum index 5f5e401..6c70954 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,47 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/gousb v1.1.3 h1:xt6M5TDsGSZ+rlomz5Si5Hmd/Fvbmo2YCJHN+yGaK4o= github.com/google/gousb v1.1.3/go.mod h1:GGWUkK0gAXDzxhwrzetW592aOmkkqSGcj5KLEgmCVUg= github.com/half2me/antgo v0.0.0-20240825222428-e6b9ddc9c32f h1:T1EVnxPi3CqwmJdOs/s76vplnG8t0MEFmwo3bvHrZAo= github.com/half2me/antgo v0.0.0-20240825222428-e6b9ddc9c32f/go.mod h1:BXp/M+7//3V0GY2ytRJw4vcxZve8mQ0zRVuow+fANkE= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/saltosystems/winrt-go v0.0.0-20260317170058-9c2fec580d96 h1:IXxzj3yjfDNXZJ35foY+RpFShqPsZZ81hhCckgfh5PI= +github.com/saltosystems/winrt-go v0.0.0-20260317170058-9c2fec580d96/go.mod h1:CIltaIm7qaANUIvzr0Vmz71lmQMAIbGJ7cvgzX7FMfA= +github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/soypat/cyw43439 v0.1.0 h1:3Nyqg2LSndhCYgCr2VXuL2nn73vyaJXAnD02veMoLvA= +github.com/soypat/cyw43439 v0.1.0/go.mod h1:R2uSILRwSPmcmmKy5Z0FtK4ypgiPf5YqK+F+IKmXqxc= +github.com/soypat/lneto v0.1.0 h1:VAHCJ33hvC3wDqhM0Vm7w0k6vwNsOCAsQ8XTrXJpS7I= +github.com/soypat/lneto v0.1.0/go.mod h1:g/8Lk+hIsMZydyWDJjK2YfsCuG6jA5mWCO6U+4S7w1U= +github.com/soypat/seqs v0.0.0-20250124201400-0d65bc7c1710 h1:Y9fBuiR/urFY/m76+SAZTxk2xAOS2n85f+H1CugajeA= +github.com/soypat/seqs v0.0.0-20250124201400-0d65bc7c1710/go.mod h1:oCVCNGCHMKoBj97Zp9znLbQ1nHxpkmOY9X+UAGzOxc8= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q= +github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/tinygo-org/cbgo v0.0.4 h1:3D76CRYbH03Rudi8sEgs/YO0x3JIMdyq8jlQtk/44fU= +github.com/tinygo-org/cbgo v0.0.4/go.mod h1:7+HgWIHd4nbAz0ESjGlJ1/v9LDU1Ox8MGzP9mah/fLk= +github.com/tinygo-org/pio v0.3.0 h1:opEnOtw58KGB4RJD3/n/Rd0/djYGX3DeJiXLI6y/yDI= +github.com/tinygo-org/pio v0.3.0/go.mod h1:wf6c6lKZp+pQOzKKcpzchmRuhiMc27ABRuo7KVnaMFU= +golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0= +golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +tinygo.org/x/bluetooth v0.15.0 h1:hLn8+iZFXvVxBzPIdZfvc6TD8JP32ixF22lCEWHAbIo= +tinygo.org/x/bluetooth v0.15.0/go.mod h1:meayNB+9rC1igTUNmNU7KftlSEzrFHe37rBSQZjHN8Y=