Add autocnnect and connection timeouts.

This commit is contained in:
Greg 2019-10-24 17:00:23 -04:00
parent 7fa3860f6c
commit 11dd9c3eb4

48
main.go
View File

@ -15,8 +15,8 @@ import (
"gioui.org/app" "gioui.org/app"
"gioui.org/io/system" "gioui.org/io/system"
"gioui.org/layout" "gioui.org/layout"
"gioui.org/unit"
"gioui.org/text" "gioui.org/text"
"gioui.org/unit"
"gioui.org/widget" "gioui.org/widget"
"gioui.org/widget/material" "gioui.org/widget/material"
@ -67,18 +67,27 @@ func eventloop() {
margin := layout.UniformInset(unit.Dp(10)) margin := layout.UniformInset(unit.Dp(10))
b := ble.NewBLE() b := ble.NewBLE()
b.Scan()
state := "starting" state := "starting"
var periph ble.Peripheral var periph ble.Peripheral
periphs := make([]ble.Peripheral, 0) periphs := make([]ble.Peripheral, 0)
btns := make([]*widget.Button, 0) btns := make([]*widget.Button, 0)
var autoconnectID string var autoconnectID string // should load from config file
autoconnectID = "93D3A64F-1664-497D-8B01-77951DB8E0F3"
var page, scanpage, connpage, hrpage func() var page, offpage, scanpage, connpage, hrpage func()
f := &layout.Flex{Axis: layout.Vertical} f := &layout.Flex{Axis: layout.Vertical}
offpage = func() {
c1 := f.Rigid(gtx, func() {
th.Body1("Heart Rate Monitor").Layout(gtx)
})
c2 := f.Rigid(gtx, func() {
th.Body1("Bluetooth is Powered Off").Layout(gtx)
})
f.Layout(gtx, c1, c2)
}
scanpage = func() { scanpage = func() {
c1 := f.Rigid(gtx, func() { c1 := f.Rigid(gtx, func() {
th.Body1("Heart Rate Monitor").Layout(gtx) th.Body1("Heart Rate Monitor").Layout(gtx)
@ -91,18 +100,14 @@ func eventloop() {
lst.Layout(gtx, len(periphs), func(i int) { lst.Layout(gtx, len(periphs), func(i int) {
th.Button(periphs[i].Name).Layout(gtx, btns[i]) th.Button(periphs[i].Name).Layout(gtx, btns[i])
if btns[i].Clicked(gtx) { if btns[i].Clicked(gtx) {
periph = periphs[i]
b.StopScan() b.StopScan()
b.Connect(periph) b.Connect(periphs[i])
page = connpage page = connpage
w.Invalidate() w.Invalidate()
} }
}) })
}) })
lst := &layout.List{Axis: layout.Vertical}
lst.Layout(gtx, 1, func(i int) {
f.Layout(gtx, c1, c2, c3) f.Layout(gtx, c1, c2, c3)
})
} }
connpage = func() { connpage = func() {
@ -133,7 +138,7 @@ func eventloop() {
f.Layout(gtx, c1, c2, c3) f.Layout(gtx, c1, c2, c3)
} }
page = scanpage page = offpage
tick := time.NewTicker(time.Second / 30) tick := time.NewTicker(time.Second / 30)
events := b.Events() events := b.Events()
@ -144,21 +149,40 @@ func eventloop() {
switch e := e.(type) { switch e := e.(type) {
case ble.UpdateStateEvent: case ble.UpdateStateEvent:
state = e.State state = e.State
if state != "powered on" {
page = offpage
} else {
if autoconnectID != "" {
b.Connect(ble.Peripheral{Identifier: autoconnectID})
page = connpage
} else {
page = scanpage
b.Scan()
}
}
w.Invalidate()
case ble.DiscoverEvent: case ble.DiscoverEvent:
fmt.Printf("found %s\n", e.Peripheral.Identifier) fmt.Printf("found %s\n", e.Peripheral.Identifier)
periphs = append(periphs, e.Peripheral) periphs = append(periphs, e.Peripheral)
//btns = append(btns, th.Button(e.Peripheral.Name))
btns = append(btns, &widget.Button{}) btns = append(btns, &widget.Button{})
fmt.Printf("len(periphs) = %d\n", len(periphs))
if e.Peripheral.Identifier == autoconnectID { if e.Peripheral.Identifier == autoconnectID {
b.Connect(e.Peripheral) b.Connect(e.Peripheral)
} }
case ble.ConnectEvent: case ble.ConnectEvent:
fmt.Printf("Connect event\n") fmt.Printf("Connect event\n")
state = "connected" state = "connected"
periph = e.Peripheral
ble.DiscoverServices(e.Peripheral) ble.DiscoverServices(e.Peripheral)
page = hrpage page = hrpage
w.Invalidate() w.Invalidate()
case ble.ConnectTimeoutEvent:
fmt.Printf("Connect timeout\n")
state = "timeout"
autoconnectID = ""
page = scanpage
b.Scan()
w.Invalidate()
} }
case <-tick.C: case <-tick.C:
w.Invalidate() w.Invalidate()