Add buttons to cancel connection attempts and disconnect from
connected devices.
This commit is contained in:
parent
a123228e30
commit
f04f3b1ffb
35
main.go
35
main.go
|
@ -93,6 +93,7 @@ func eventloop() {
|
|||
var periph ble.Peripheral
|
||||
periphs := make([]ble.Peripheral, 0)
|
||||
btns := make([]*widget.Button, 0)
|
||||
backBtn := &widget.Button{}
|
||||
|
||||
var page, offpage, scanpage, connpage, hrpage func()
|
||||
|
||||
|
@ -121,6 +122,7 @@ func eventloop() {
|
|||
b.StopScan()
|
||||
periph = periphs[i]
|
||||
b.Connect(periph)
|
||||
state = "connecting"
|
||||
page = connpage
|
||||
w.Invalidate()
|
||||
}
|
||||
|
@ -139,7 +141,19 @@ func eventloop() {
|
|||
c3 := f.Rigid(gtx, func() {
|
||||
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() {
|
||||
|
@ -154,7 +168,19 @@ func eventloop() {
|
|||
l.Alignment = text.Middle
|
||||
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
|
||||
|
@ -170,13 +196,16 @@ func eventloop() {
|
|||
fmt.Printf("UpdateState: %s\n", e.State)
|
||||
state = e.State
|
||||
if state != "powered on" {
|
||||
periphs = periphs[:0]
|
||||
page = offpage
|
||||
} else {
|
||||
|
||||
if Config.Autoconnect != "" && b.Connect(ble.Peripheral{Identifier: Config.Autoconnect}) {
|
||||
state = "connecting"
|
||||
page = connpage
|
||||
} else {
|
||||
periphs = periphs[:0]
|
||||
state = "scanning"
|
||||
page = scanpage
|
||||
b.Scan()
|
||||
}
|
||||
|
@ -186,10 +215,12 @@ func eventloop() {
|
|||
periphs = append(periphs, e.Peripheral)
|
||||
btns = append(btns, &widget.Button{})
|
||||
if e.Peripheral.Identifier == Config.Autoconnect && b.Connect(e.Peripheral) {
|
||||
state = "connecting"
|
||||
page = connpage
|
||||
}
|
||||
case ble.ConnectEvent:
|
||||
fmt.Printf("Connect event\n")
|
||||
b.StopScan()
|
||||
state = "connected"
|
||||
periph = e.Peripheral
|
||||
Config.Autoconnect = periph.Identifier
|
||||
|
|
Loading…
Reference in New Issue
Block a user