Compare commits
	
		
			No commits in common. "de1ebbe8b2ac74571bf484e989338e9ec40c4595" and "45863ab2128f68abedc7253b003b22a3b27fe703" have entirely different histories.
		
	
	
		
			de1ebbe8b2
			...
			45863ab212
		
	
		
							
								
								
									
										9
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								go.mod
									
									
									
									
									
								
							| 
						 | 
					@ -1,9 +0,0 @@
 | 
				
			||||||
module git.wow.st/gmp/hrm
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
go 1.13
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
require (
 | 
					 | 
				
			||||||
	gioui.org v0.0.0-20191024154924-88adc0ee1a0b
 | 
					 | 
				
			||||||
	git.wow.st/gmp/ble v0.0.0-20191025185913-5955e369e4eb
 | 
					 | 
				
			||||||
	gopkg.in/yaml.v2 v2.2.4
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
							
								
								
									
										34
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								go.sum
									
									
									
									
									
								
							| 
						 | 
					@ -1,34 +0,0 @@
 | 
				
			||||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
 | 
					 | 
				
			||||||
gioui.org v0.0.0-20191024154924-88adc0ee1a0b h1:h+Ot0YwjtLHkFlLVbiS3R8gS0cyk6/xkKWNlWAcXtFs=
 | 
					 | 
				
			||||||
gioui.org v0.0.0-20191024154924-88adc0ee1a0b/go.mod h1:KqFFi2Dq5gYA3FJ0sDOt8OBXoMsuxMtE8v2f0JExXAY=
 | 
					 | 
				
			||||||
git.wow.st/gmp/ble v0.0.0-20191025130954-ec02bc7bcc2b h1:qttVD1iumDo7WWzyIJAUkis+Yh8QB07RXJcbAgCfEZI=
 | 
					 | 
				
			||||||
git.wow.st/gmp/ble v0.0.0-20191025130954-ec02bc7bcc2b/go.mod h1:MiidWVofL6FTtRokC5JGBdVUhKyvZtnia5noj08r944=
 | 
					 | 
				
			||||||
git.wow.st/gmp/ble v0.0.0-20191025185913-5955e369e4eb h1:vebHnxVY7EvVzafTGFKQWR0Q3uV4jaoOTL0qS/iDP9Y=
 | 
					 | 
				
			||||||
git.wow.st/gmp/ble v0.0.0-20191025185913-5955e369e4eb/go.mod h1:MiidWVofL6FTtRokC5JGBdVUhKyvZtnia5noj08r944=
 | 
					 | 
				
			||||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
 | 
					 | 
				
			||||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
 | 
					 | 
				
			||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 | 
					 | 
				
			||||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 | 
					 | 
				
			||||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 | 
					 | 
				
			||||||
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 h1:n9HxLrNxWWtEb1cA950nuEEj3QnKbtsCJ6KjcgisNUs=
 | 
					 | 
				
			||||||
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3/go.mod h1:NOZ3BPKG0ec/BKJQgnvsSFpcKLM5xXVWnvZS97DWHgE=
 | 
					 | 
				
			||||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
 | 
					 | 
				
			||||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b h1:+qEpEAPhDZ1o0x3tHzZTQDArnOixOzGD9HUJfcg0mb4=
 | 
					 | 
				
			||||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
 | 
					 | 
				
			||||||
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
 | 
					 | 
				
			||||||
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
 | 
					 | 
				
			||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 | 
					 | 
				
			||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 | 
					 | 
				
			||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
 | 
					 | 
				
			||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
					 | 
				
			||||||
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 | 
					 | 
				
			||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
					 | 
				
			||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
					 | 
				
			||||||
							
								
								
									
										76
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								main.go
									
									
									
									
									
								
							| 
						 | 
					@ -7,6 +7,7 @@ import (
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.wow.st/gmp/ble"
 | 
						"git.wow.st/gmp/ble"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,18 +21,16 @@ import (
 | 
				
			||||||
	"gioui.org/widget"
 | 
						"gioui.org/widget"
 | 
				
			||||||
	"gioui.org/widget/material"
 | 
						"gioui.org/widget/material"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"gioui.org/font/gofont"
 | 
						_ "gioui.org/font/gofont"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type conf struct {
 | 
					type conf struct {
 | 
				
			||||||
	Autoconnect string
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var Config conf
 | 
					var Config conf
 | 
				
			||||||
var conffile string
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	conffile = path.Join(confDir, "config.yml")
 | 
						conffile := path.Join(confDir, "config.yml")
 | 
				
			||||||
	if _, err := os.Stat(conffile); os.IsNotExist(err) {
 | 
						if _, err := os.Stat(conffile); os.IsNotExist(err) {
 | 
				
			||||||
		fd, err := os.Create(conffile)
 | 
							fd, err := os.Create(conffile)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -51,14 +50,6 @@ func main() {
 | 
				
			||||||
	app.Main()
 | 
						app.Main()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func saveConfig() {
 | 
					 | 
				
			||||||
	confbytes, err := yaml.Marshal(&Config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Fatal("Cannot encode configuration: ", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	err = ioutil.WriteFile(conffile, confbytes, 0700)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func hrDecode(x []byte) int {
 | 
					func hrDecode(x []byte) int {
 | 
				
			||||||
	flags := x[0]
 | 
						flags := x[0]
 | 
				
			||||||
	if flags&0x80 != 0 { // uint16 format
 | 
						if flags&0x80 != 0 { // uint16 format
 | 
				
			||||||
| 
						 | 
					@ -73,7 +64,6 @@ func eventloop() {
 | 
				
			||||||
		app.Size(unit.Dp(400), unit.Dp(400)),
 | 
							app.Size(unit.Dp(400), unit.Dp(400)),
 | 
				
			||||||
		app.Title("HRM"),
 | 
							app.Title("HRM"),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	gofont.Register()
 | 
					 | 
				
			||||||
	th := material.NewTheme()
 | 
						th := material.NewTheme()
 | 
				
			||||||
	gtx := &layout.Context{Queue: w.Queue()}
 | 
						gtx := &layout.Context{Queue: w.Queue()}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -93,7 +83,9 @@ 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 autoconnectID string // should load from config file
 | 
				
			||||||
 | 
						//autoconnectID = "93D3A64F-1664-497D-8B01-77951DB8E0F3"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var page, offpage, scanpage, connpage, hrpage func()
 | 
						var page, offpage, scanpage, connpage, hrpage func()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -117,15 +109,11 @@ func eventloop() {
 | 
				
			||||||
		c3 := f.Rigid(gtx, func() {
 | 
							c3 := f.Rigid(gtx, func() {
 | 
				
			||||||
			lst := &layout.List{Axis: layout.Vertical}
 | 
								lst := &layout.List{Axis: layout.Vertical}
 | 
				
			||||||
			lst.Layout(gtx, len(periphs), func(i int) {
 | 
								lst.Layout(gtx, len(periphs), func(i int) {
 | 
				
			||||||
				gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
 | 
									th.Button(periphs[i].Name).Layout(gtx, btns[i])
 | 
				
			||||||
				layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
 | 
					 | 
				
			||||||
					th.Button(periphs[i].Name).Layout(gtx, btns[i])
 | 
					 | 
				
			||||||
				})
 | 
					 | 
				
			||||||
				if btns[i].Clicked(gtx) {
 | 
									if btns[i].Clicked(gtx) {
 | 
				
			||||||
					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()
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
| 
						 | 
					@ -144,19 +132,7 @@ func eventloop() {
 | 
				
			||||||
		c3 := f.Rigid(gtx, func() {
 | 
							c3 := f.Rigid(gtx, func() {
 | 
				
			||||||
			th.Body1(periph.Name).Layout(gtx)
 | 
								th.Body1(periph.Name).Layout(gtx)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		c4 := f.Rigid(gtx, func() {
 | 
							f.Layout(gtx, c1, c2, c3)
 | 
				
			||||||
			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() {
 | 
				
			||||||
| 
						 | 
					@ -171,23 +147,12 @@ func eventloop() {
 | 
				
			||||||
			l.Alignment = text.Middle
 | 
								l.Alignment = text.Middle
 | 
				
			||||||
			l.Layout(gtx)
 | 
								l.Layout(gtx)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		c4 := f.Rigid(gtx, func() {
 | 
							f.Layout(gtx, c1, c2, c3)
 | 
				
			||||||
			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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tick := time.NewTicker(time.Second / 30)
 | 
				
			||||||
	events := b.Events()
 | 
						events := b.Events()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
| 
						 | 
					@ -198,16 +163,13 @@ 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 autoconnectID != "" {
 | 
				
			||||||
						state = "connecting"
 | 
											b.Connect(ble.Peripheral{Identifier: autoconnectID})
 | 
				
			||||||
						page = connpage
 | 
											page = connpage
 | 
				
			||||||
					} else {
 | 
										} else {
 | 
				
			||||||
						periphs = periphs[:0]
 | 
					 | 
				
			||||||
						state = "scanning"
 | 
					 | 
				
			||||||
						page = scanpage
 | 
											page = scanpage
 | 
				
			||||||
						b.Scan()
 | 
											b.Scan()
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
| 
						 | 
					@ -216,26 +178,20 @@ func eventloop() {
 | 
				
			||||||
				fmt.Printf("found %s (%s)\n", e.Peripheral.Name, e.Peripheral.Identifier)
 | 
									fmt.Printf("found %s (%s)\n", e.Peripheral.Name, e.Peripheral.Identifier)
 | 
				
			||||||
				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 == autoconnectID {
 | 
				
			||||||
					state = "connecting"
 | 
										b.Connect(e.Peripheral)
 | 
				
			||||||
					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
 | 
					 | 
				
			||||||
				saveConfig()
 | 
					 | 
				
			||||||
				e.Peripheral.DiscoverServices()
 | 
									e.Peripheral.DiscoverServices()
 | 
				
			||||||
				page = hrpage
 | 
									page = hrpage
 | 
				
			||||||
			case ble.ConnectTimeoutEvent:
 | 
								case ble.ConnectTimeoutEvent:
 | 
				
			||||||
				fmt.Printf("Connect timeout\n")
 | 
									fmt.Printf("Connect timeout\n")
 | 
				
			||||||
				state = "timeout"
 | 
									state = "timeout"
 | 
				
			||||||
				periphs = periphs[:0]
 | 
									autoconnectID = ""
 | 
				
			||||||
				page = scanpage
 | 
									page = scanpage
 | 
				
			||||||
				Config.Autoconnect = ""
 | 
					 | 
				
			||||||
				saveConfig()
 | 
					 | 
				
			||||||
				b.Scan()
 | 
									b.Scan()
 | 
				
			||||||
			case ble.DiscoverServiceEvent:
 | 
								case ble.DiscoverServiceEvent:
 | 
				
			||||||
				fmt.Printf("DiscoverService %s\n", e.Gatt.UUID)
 | 
									fmt.Printf("DiscoverService %s\n", e.Gatt.UUID)
 | 
				
			||||||
| 
						 | 
					@ -253,6 +209,8 @@ func eventloop() {
 | 
				
			||||||
				hr = hrDecode(e.Data)
 | 
									hr = hrDecode(e.Data)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			w.Invalidate() // refresh on any Bluetooth event
 | 
								w.Invalidate() // refresh on any Bluetooth event
 | 
				
			||||||
 | 
							case <-tick.C:
 | 
				
			||||||
 | 
								w.Invalidate()
 | 
				
			||||||
		case e := <-w.Events():
 | 
							case e := <-w.Events():
 | 
				
			||||||
			switch e := e.(type) {
 | 
								switch e := e.(type) {
 | 
				
			||||||
			case system.DestroyEvent:
 | 
								case system.DestroyEvent:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user