all: update NewAdvertisementInterval to accept time.Duration

This makes the API a bit more natural and (if needed) allows for a
slightly greater precision in specifying the advertisement interval.
This commit is contained in:
Ayke van Laethem 2020-06-04 16:53:17 +02:00
parent d07cf38d66
commit e1e010d8ec
No known key found for this signature in database
GPG key ID: E97FF5335DFDFDED
6 changed files with 19 additions and 12 deletions

View file

@ -13,7 +13,7 @@ func main() {
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "Go Bluetooth",
Interval: bluetooth.NewAdvertisementInterval(100),
Interval: bluetooth.NewAdvertisementInterval(100 * time.Millisecond),
}))
must("start adv", adv.Start())

View file

@ -18,7 +18,7 @@ func main() {
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "Go HRS",
ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(0x2A37)},
Interval: bluetooth.NewAdvertisementInterval(100),
Interval: bluetooth.NewAdvertisementInterval(100 * time.Millisecond),
}))
must("start adv", adv.Start())

View file

@ -25,7 +25,7 @@ func main() {
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "LED colors",
Interval: bluetooth.NewAdvertisementInterval(100),
Interval: bluetooth.NewAdvertisementInterval(100 * time.Millisecond),
}))
must("start adv", adv.Start())

View file

@ -8,6 +8,8 @@ package main
// Code to interact with a raw terminal is in separate files with build tags.
import (
"time"
"github.com/tinygo-org/bluetooth"
)
@ -25,7 +27,7 @@ func main() {
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "NUS", // Nordic UART Service
ServiceUUIDs: []bluetooth.UUID{serviceUUID},
Interval: bluetooth.NewAdvertisementInterval(100),
Interval: bluetooth.NewAdvertisementInterval(100 * time.Millisecond),
}))
must("start adv", adv.Start())

16
gap.go
View file

@ -1,6 +1,9 @@
package bluetooth
import "errors"
import (
"errors"
"time"
)
var (
errScanning = errors.New("bluetooth: a scan is already in progress")
@ -28,11 +31,12 @@ type AdvertisementOptions struct {
// AdvertisementInterval is the advertisement interval in 0.625µs units.
type AdvertisementInterval uint32
// NewAdvertisementInterval returns a new advertisement interval, based on an
// interval in milliseconds.
func NewAdvertisementInterval(intervalMillis uint32) AdvertisementInterval {
// Convert an interval to units of
return AdvertisementInterval(intervalMillis * 8 / 5)
// NewAdvertisementInterval returns a new advertisement interval. Advertisement
// intervals are in units of 0.625µs, so the precision will not be greater than
// that.
func NewAdvertisementInterval(interval time.Duration) AdvertisementInterval {
// Convert an interval to units of 0.625µs.
return AdvertisementInterval(uint64(interval / (625 * time.Microsecond)))
}
// Connection is a numeric identifier that indicates a connection handle.

View file

@ -5,6 +5,7 @@ package bluetooth
import (
"device/arm"
"runtime/volatile"
"time"
)
/*
@ -86,8 +87,8 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
scanParams := C.ble_gap_scan_params_t{}
scanParams.set_bitfield_extended(0)
scanParams.set_bitfield_active(0)
scanParams.interval = 100 * 1000 / 625 // 100ms in 625µs units
scanParams.window = 100 * 1000 / 625 // 100ms in 625µs units
scanParams.interval = uint16(NewAdvertisementInterval(100 * time.Millisecond))
scanParams.window = uint16(NewAdvertisementInterval(100 * time.Millisecond))
scanParams.timeout = C.BLE_GAP_SCAN_TIMEOUT_UNLIMITED
scanReportBufferInfo := C.ble_data_t{
p_data: &scanReportBuffer.data[0],