Update to use DiscoverServiceEvent, DiscoverCharacteristicEvent, and
UpdateValue event.
This commit is contained in:
parent
11dd9c3eb4
commit
43cf503d77
31
main.go
31
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -49,6 +50,15 @@ func main() {
|
||||||
app.Main()
|
app.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hrDecode(x []byte) int {
|
||||||
|
flags := x[0]
|
||||||
|
if flags&0x80 != 0 { // uint16 format
|
||||||
|
return int(binary.BigEndian.Uint16(x[1:2]))
|
||||||
|
} else {
|
||||||
|
return int(x[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func eventloop() {
|
func eventloop() {
|
||||||
w := app.NewWindow(
|
w := app.NewWindow(
|
||||||
app.Size(unit.Dp(400), unit.Dp(400)),
|
app.Size(unit.Dp(400), unit.Dp(400)),
|
||||||
|
@ -69,6 +79,7 @@ func eventloop() {
|
||||||
b := ble.NewBLE()
|
b := ble.NewBLE()
|
||||||
|
|
||||||
state := "starting"
|
state := "starting"
|
||||||
|
var hr int
|
||||||
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)
|
||||||
|
@ -131,7 +142,7 @@ func eventloop() {
|
||||||
th.Body1(periph.Name).Layout(gtx)
|
th.Body1(periph.Name).Layout(gtx)
|
||||||
})
|
})
|
||||||
c3 := f.Rigid(gtx, func() {
|
c3 := f.Rigid(gtx, func() {
|
||||||
l := th.H1(fmt.Sprintf("%d", b.HR()))
|
l := th.H1(fmt.Sprintf("%d", hr))
|
||||||
l.Alignment = text.Middle
|
l.Alignment = text.Middle
|
||||||
l.Layout(gtx)
|
l.Layout(gtx)
|
||||||
})
|
})
|
||||||
|
@ -162,7 +173,7 @@ func eventloop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.Invalidate()
|
w.Invalidate()
|
||||||
case ble.DiscoverEvent:
|
case ble.DiscoverPeripheralEvent:
|
||||||
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, &widget.Button{})
|
btns = append(btns, &widget.Button{})
|
||||||
|
@ -173,7 +184,7 @@ func eventloop() {
|
||||||
fmt.Printf("Connect event\n")
|
fmt.Printf("Connect event\n")
|
||||||
state = "connected"
|
state = "connected"
|
||||||
periph = e.Peripheral
|
periph = e.Peripheral
|
||||||
ble.DiscoverServices(e.Peripheral)
|
e.Peripheral.DiscoverServices()
|
||||||
page = hrpage
|
page = hrpage
|
||||||
w.Invalidate()
|
w.Invalidate()
|
||||||
case ble.ConnectTimeoutEvent:
|
case ble.ConnectTimeoutEvent:
|
||||||
|
@ -183,6 +194,20 @@ func eventloop() {
|
||||||
page = scanpage
|
page = scanpage
|
||||||
b.Scan()
|
b.Scan()
|
||||||
w.Invalidate()
|
w.Invalidate()
|
||||||
|
case ble.DiscoverServiceEvent:
|
||||||
|
fmt.Printf("DiscoverService %s\n", e.Gatt.UUID)
|
||||||
|
if e.Gatt.UUID == "180D" {
|
||||||
|
fmt.Printf("Found HRM Service\n")
|
||||||
|
e.Peripheral.DiscoverCharacteristics(e.Service)
|
||||||
|
}
|
||||||
|
case ble.DiscoverCharacteristicEvent:
|
||||||
|
fmt.Printf("DiscoverCharacteristic %s\n", e.Gatt.UUID)
|
||||||
|
if e.Gatt.UUID == "2A37" {
|
||||||
|
fmt.Printf("Found heart rate value\n")
|
||||||
|
e.Peripheral.SetNotifyValue(e.Characteristic)
|
||||||
|
}
|
||||||
|
case ble.UpdateValueEvent:
|
||||||
|
hr = hrDecode(e.Data)
|
||||||
}
|
}
|
||||||
case <-tick.C:
|
case <-tick.C:
|
||||||
w.Invalidate()
|
w.Invalidate()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user