diff --git a/main.go b/main.go index 69eb42b..a104799 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,8 @@ import ( "gioui.org/app" "gioui.org/io/system" "gioui.org/layout" - "gioui.org/unit" "gioui.org/text" + "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" @@ -67,18 +67,27 @@ func eventloop() { margin := layout.UniformInset(unit.Dp(10)) b := ble.NewBLE() - b.Scan() state := "starting" var periph ble.Peripheral - periphs := make([]ble.Peripheral,0) - btns := make([]*widget.Button,0) + periphs := make([]ble.Peripheral, 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} + 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() { c1 := f.Rigid(gtx, func() { th.Body1("Heart Rate Monitor").Layout(gtx) @@ -91,18 +100,14 @@ func eventloop() { lst.Layout(gtx, len(periphs), func(i int) { th.Button(periphs[i].Name).Layout(gtx, btns[i]) if btns[i].Clicked(gtx) { - periph = periphs[i] b.StopScan() - b.Connect(periph) + b.Connect(periphs[i]) page = connpage 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() { @@ -126,14 +131,14 @@ func eventloop() { th.Body1(periph.Name).Layout(gtx) }) c3 := f.Rigid(gtx, func() { - l := th.H1(fmt.Sprintf("%d",b.HR())) + l := th.H1(fmt.Sprintf("%d", b.HR())) l.Alignment = text.Middle l.Layout(gtx) }) f.Layout(gtx, c1, c2, c3) } - page = scanpage + page = offpage tick := time.NewTicker(time.Second / 30) events := b.Events() @@ -144,23 +149,42 @@ func eventloop() { switch e := e.(type) { case ble.UpdateStateEvent: 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: fmt.Printf("found %s\n", e.Peripheral.Identifier) periphs = append(periphs, e.Peripheral) - //btns = append(btns, th.Button(e.Peripheral.Name)) btns = append(btns, &widget.Button{}) - fmt.Printf("len(periphs) = %d\n", len(periphs)) if e.Peripheral.Identifier == autoconnectID { b.Connect(e.Peripheral) } case ble.ConnectEvent: fmt.Printf("Connect event\n") state = "connected" + periph = e.Peripheral ble.DiscoverServices(e.Peripheral) page = hrpage 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() case e := <-w.Events(): switch e := e.(type) {