From beca61081b6c46e2cee7e193681962d40643e3cf Mon Sep 17 00:00:00 2001 From: Henry Seurer Date: Mon, 18 Nov 2024 13:58:04 -0800 Subject: [PATCH] Added go.mod --- go.mod | 18 ++++++++++++++++++ messages.go | 48 ++++++++++++++++++------------------------------ 2 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7fec217 --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module farm-messages + +go 1.23 + +require tinygo.org/x/bluetooth v0.10.0 + +require ( + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/godbus/dbus/v5 v5.1.0 // indirect + github.com/saltosystems/winrt-go v0.0.0-20240509164145-4f7860a3bd2b // indirect + github.com/sirupsen/logrus v1.9.3 // indirect + github.com/soypat/cyw43439 v0.0.0-20240609122733-da9153086796 // indirect + github.com/soypat/seqs v0.0.0-20240527012110-1201bab640ef // indirect + github.com/tinygo-org/cbgo v0.0.4 // indirect + github.com/tinygo-org/pio v0.0.0-20231216154340-cd888eb58899 // indirect + golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 // indirect + golang.org/x/sys v0.11.0 // indirect +) diff --git a/messages.go b/messages.go index 55fff57..ab189ec 100644 --- a/messages.go +++ b/messages.go @@ -35,6 +35,7 @@ import ( "strings" "sync/atomic" "time" + "tinygo.org/x/bluetooth" ) type BuildVersion struct { @@ -237,39 +238,39 @@ const maxInt64 = float64(math.MaxInt64 - 512) func (t *ThrottleEntry) ForAttempt(attempt float64, minTime time.Duration, maxTime time.Duration) time.Duration { // Zero-values are nonsensical, so we use // them to apply defaults - min := t.Min - if min <= 0 { - min = minTime + minValue := t.Min + if minValue <= 0 { + minValue = minTime } - max := t.Max - if max <= 0 { - max = maxTime + maxValue := t.Max + if maxValue <= 0 { + maxValue = maxTime } - if min >= max { + if minValue >= maxValue { // short-circuit - return max + return maxValue } factor := t.Factor if factor <= 0 { factor = 2 } //calculate this duration - minFloat := float64(min) + minFloat := float64(minValue) durationFloat := minFloat * math.Pow(factor, attempt) if t.Jitter { durationFloat = rand.Float64()*(durationFloat-minFloat) + minFloat } //ensure float64 won't overflow int64 if durationFloat > maxInt64 { - return max + return maxValue } dur := time.Duration(durationFloat) //keep within bounds - if dur < min { - return min + if dur < minValue { + return minValue } - if dur > max { - return max + if dur > maxValue { + return maxValue } return dur } @@ -966,23 +967,10 @@ type StatusMessage struct { States map[string]bool `json:"states,omitempty"` } -type ManufacturerDataElement struct { - // The company ID, which must be one of the assigned company IDs. - // The full list is in here: - // https://www.bluetooth.com/specifications/assigned-numbers/ - // The list can also be viewed here: - // https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/company_identifiers/company_identifiers.yaml - // The value 0xffff can also be used for testing. - CompanyID uint16 `json:"company-id,omitempty"` - - // The value, which can be any value but can't be very large. - Data []byte `json:"data,omitempty"` -} - type BleAdvertisementPayload struct { - LocalName string `json:"localName,omitempty"` - Bytes []byte `json:"bytes,omitempty"` - ManufacturerData []ManufacturerDataElement `json:"manufacturerData,omitempty"` + LocalName string `json:"localName,omitempty"` + Bytes []byte `json:"bytes,omitempty"` + ManufacturerData []bluetooth.ManufacturerDataElement `json:"manufacturerData,omitempty"` } type BleDevice struct {