Add buttons to cancel connection attempts and disconnect from

connected devices.
This commit is contained in:
Greg 2019-10-25 14:04:08 -04:00
parent a123228e30
commit f04f3b1ffb

35
main.go
View File

@ -93,6 +93,7 @@ func eventloop() {
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)
backBtn := &widget.Button{}
var page, offpage, scanpage, connpage, hrpage func() var page, offpage, scanpage, connpage, hrpage func()
@ -121,6 +122,7 @@ func eventloop() {
b.StopScan() b.StopScan()
periph = periphs[i] periph = periphs[i]
b.Connect(periph) b.Connect(periph)
state = "connecting"
page = connpage page = connpage
w.Invalidate() w.Invalidate()
} }
@ -139,7 +141,19 @@ func eventloop() {
c3 := f.Rigid(gtx, func() { c3 := f.Rigid(gtx, func() {
th.Body1(periph.Name).Layout(gtx) th.Body1(periph.Name).Layout(gtx)
}) })
f.Layout(gtx, c1, c2, c3) c4 := f.Rigid(gtx, func() {
th.Button("Cancel").Layout(gtx, backBtn)
if backBtn.Clicked(gtx) {
ble.CancelConnection(periph)
periphs = periphs[:0]
Config.Autoconnect = ""
saveConfig()
b.Scan()
state = "scanning"
page = scanpage
}
})
f.Layout(gtx, c1, c2, c3, c4)
} }
hrpage = func() { hrpage = func() {
@ -154,7 +168,19 @@ func eventloop() {
l.Alignment = text.Middle l.Alignment = text.Middle
l.Layout(gtx) l.Layout(gtx)
}) })
f.Layout(gtx, c1, c2, c3) c4 := f.Rigid(gtx, func() {
th.Button("Stop").Layout(gtx, backBtn)
if backBtn.Clicked(gtx) {
ble.Disconnect(periph)
periphs = periphs[:0]
Config.Autoconnect = ""
saveConfig()
b.Scan()
state = "scanning"
page = scanpage
}
})
f.Layout(gtx, c1, c2, c3, c4)
} }
page = offpage page = offpage
@ -170,13 +196,16 @@ func eventloop() {
fmt.Printf("UpdateState: %s\n", e.State) fmt.Printf("UpdateState: %s\n", e.State)
state = e.State state = e.State
if state != "powered on" { if state != "powered on" {
periphs = periphs[:0]
page = offpage page = offpage
} else { } else {
if Config.Autoconnect != "" && b.Connect(ble.Peripheral{Identifier: Config.Autoconnect}) { if Config.Autoconnect != "" && b.Connect(ble.Peripheral{Identifier: Config.Autoconnect}) {
state = "connecting"
page = connpage page = connpage
} else { } else {
periphs = periphs[:0] periphs = periphs[:0]
state = "scanning"
page = scanpage page = scanpage
b.Scan() b.Scan()
} }
@ -186,10 +215,12 @@ func eventloop() {
periphs = append(periphs, e.Peripheral) periphs = append(periphs, e.Peripheral)
btns = append(btns, &widget.Button{}) btns = append(btns, &widget.Button{})
if e.Peripheral.Identifier == Config.Autoconnect && b.Connect(e.Peripheral) { if e.Peripheral.Identifier == Config.Autoconnect && b.Connect(e.Peripheral) {
state = "connecting"
page = connpage page = connpage
} }
case ble.ConnectEvent: case ble.ConnectEvent:
fmt.Printf("Connect event\n") fmt.Printf("Connect event\n")
b.StopScan()
state = "connected" state = "connected"
periph = e.Peripheral periph = e.Peripheral
Config.Autoconnect = periph.Identifier Config.Autoconnect = periph.Identifier