package main import ( "fmt" "log" "runtime" "unsafe" ndk "git.wow.st/gmp/android-go/android27" ) var ( tag *ndk.Char ) 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 sensorLoop() { // the Android Looper is thread-local runtime.LockOSThread() pkg := ndk.CharWithGoString("st.wow.git.sensors") manager := ndk.ASensorManagerGetInstanceForPackage(pkg) pkg.Free() sens := ndk.ASensorManagerGetDefaultSensor(manager, ndk.ASENSOR_TYPE_ACCELEROMETER) stp := ndk.ASensorGetStringType(sens) labchan <- "sensor" + stp.String() var looper *ndk.ALooper var queue *ndk.ASensorEventQueue looper_id := 1 var rate ndk.Int32_t = 60 setup := func() { looper = ndk.ALooperForThread() if (looper == nil) { labchan <- "no looper for thread" looper = ndk.ALooperPrepare(ndk.ALOOPER_PREPARE_ALLOW_NON_CALLBACKS) } queue = ndk.ASensorManagerCreateEventQueue(manager, looper, looper_id) ndk.ASensorEventQueueEnableSensor(queue, sens) ndk.ASensorEventQueueSetEventRate(queue, sens, 1000000/rate) // microseconds } setup() labchan <- "polling accelerometer" for { var zero ndk.Int id := (int)(ndk.ALooperPollOnce(-1, &zero, &zero, (*unsafe.Pointer)(unsafe.Pointer(nil)))) // poll forever if (id == ndk.ALOOPER_POLL_ERROR) { // set up a new looper labchan <- "getting a new looper" setup() continue } if (id == looper_id) { var event ndk.ASensorEvent if (ndk.ASensorEventQueueGetEvents(queue, &event, 1) != 1) { continue } accel := (&event).Acceleration() senschan <- vector{float64(accel.X()), float64(accel.Y()), float64(accel.Z())} } } } func init() { tag = ndk.CharWithGoString("gio") log.Print("Android starting") go apiLevel() go sensorLoop() }