From 12ed4151e7ae60fcc577ca677184ccae7a457dfe Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 17 Sep 2019 10:42:01 -0400 Subject: [PATCH] Add sensors example. --- .gitignore | 1 + examples/sensors/android.go | 73 ++++++++++++++++++++++++++++ examples/sensors/main.go | 96 +++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 examples/sensors/android.go create mode 100644 examples/sensors/main.go diff --git a/.gitignore b/.gitignore index 9b7d4d3..773c991 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.ast +*.apk diff --git a/examples/sensors/android.go b/examples/sensors/android.go new file mode 100644 index 0000000..8001e46 --- /dev/null +++ b/examples/sensors/android.go @@ -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() + }() +} diff --git a/examples/sensors/main.go b/examples/sensors/main.go new file mode 100644 index 0000000..5b11635 --- /dev/null +++ b/examples/sensors/main.go @@ -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) + } + } + } +} +