Add sensors example.

This commit is contained in:
Greg 2019-09-17 10:42:01 -04:00
parent 7edd18c8dc
commit 12ed4151e7
3 changed files with 170 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.ast *.ast
*.apk

View File

@ -0,0 +1,73 @@
//+build android
package main
import "C"
import (
"fmt"
"log"
"os"
ndk "git.wow.st/gmp/android-go/android27"
"gioui.org/ui/app"
)
var (
tag *ndk.Char
)
func mkHomeDir() {
msg := ndk.CharWithGoString("getting datadir")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
godir,_ := app.DataDir()
labchan <- fmt.Sprintf("DataDir is %s", godir)
msg = ndk.CharWithGoString("ok. calling mkdir")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
if _, err := os.Stat(godir); !os.IsNotExist(err) {
labchan <- "already exists"
return
}
if err := os.MkdirAll(godir, 0700); err != nil {
fmt.Sprintf("Error creating directory: %s", err)
labchan <- fmt.Sprintf("Error creating directory: %s", err)
} else {
labchan <- "created"
}
}
func apiLevel() {
msg := ndk.CharWithGoString("hello log world")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
apilevel := ndk.AndroidGetDeviceApiLevel()
message := fmt.Sprintf("API level is %d", apilevel)
labchan <- message
msg = ndk.CharWithGoString(message)
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
}
func getSensors() {
pkg := ndk.CharWithGoString("st.wow.git.jni")
sm := ndk.ASensorManagerGetInstanceForPackage(pkg)
pkg.Free()
sens := ndk.ASensorManagerGetDefaultSensor(sm, ndk.ASENSOR_TYPE_ACCELEROMETER)
stp := ndk.ASensorGetStringType(sens)
labchan <- "sensor" + stp.String()
}
func init() {
tag = ndk.CharWithGoString("gio")
log.Print("Android starting")
go func() {
apiLevel()
mkHomeDir()
getSensors()
}()
}

96
examples/sensors/main.go Normal file
View File

@ -0,0 +1,96 @@
// +build darwin linux
package main
import (
"log"
"image/color"
"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
"gioui.org/ui/text"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/font/gofont/goregular"
)
var (
face text.Face
white = color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}
gray = color.RGBA{A: 0xff, R: 0xb0, G: 0xb0, B: 0xb0}
black = color.RGBA{A: 0xff, R: 0x00, G: 0x00, B: 0x00}
labchan chan string
)
func main() {
labchan = make(chan string)
log.Print("Staring event loop")
go eventloop()
app.Main()
log.Print("App closed")
}
func diffInsets(x, y app.Insets) bool {
return x.Top != y.Top ||
x.Bottom != y.Bottom ||
x.Left != y.Left ||
x.Right != y.Right
}
func eventloop() {
w := app.NewWindow(
app.WithWidth(ui.Dp(400)),
app.WithHeight(ui.Dp(400)),
app.WithTitle("Hello"))
q := w.Queue()
var c ui.Config
ops := new(ui.Ops)
//var dims layout.Dimensions
var cs layout.Constraints
var faces measure.Faces
regular, err := sfnt.Parse(goregular.TTF)
if err != nil { log.Fatal("Cannot parse font.") }
face = faces.For(regular, ui.Sp(16))
sysinset := &layout.Inset{}
margin := layout.UniformInset(ui.Dp(10))
labels := []*text.Label{}
list := &layout.List{Axis: layout.Vertical}
resetSysinset := func(x app.Insets) {
sysinset.Top = x.Top
sysinset.Bottom = x.Bottom
sysinset.Left = x.Left
sysinset.Right = x.Right
}
for {
select {
case x := <-labchan:
labels = append(labels,&text.Label{Face: face, Text: x})
case e := <-w.Events():
switch e := e.(type) {
case app.DestroyEvent:
return
case app.UpdateEvent:
c = &e.Config
ops.Reset()
faces.Reset(c)
resetSysinset(e.Insets)
cs = layout.RigidConstraints(e.Size)
cs = sysinset.Begin(c, ops, cs)
cs = margin.Begin(c, ops, cs)
for list.Init(c, q, ops, cs, len(labels)); list.More(); list.Next() {
list.End(labels[list.Index()].Layout(ops, list.Constraints()))
}
sysinset.End(margin.End(list.Layout()))
w.Update(ops)
}
}
}
}