2015-03-30 11:53:15 -07:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-08-12 10:16:13 -07:00
|
|
|
// +build android
|
|
|
|
|
2015-03-30 11:53:15 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
#include <android/sensor.h>
|
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
#define GO_ANDROID_SENSOR_LOOPER_ID 100
|
2015-03-30 11:53:15 -07:00
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
ASensorEventQueue* queue = NULL;
|
|
|
|
ALooper* looper = NULL;
|
2015-03-30 11:53:15 -07:00
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
void GoAndroid_createManager() {
|
|
|
|
ASensorManager* manager = ASensorManager_getInstance();
|
|
|
|
looper = ALooper_forThread();
|
2015-03-30 11:53:15 -07:00
|
|
|
if (looper == NULL) {
|
|
|
|
looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
|
|
|
|
}
|
2015-09-04 00:17:01 -07:00
|
|
|
queue = ASensorManager_createEventQueue(manager, looper, GO_ANDROID_SENSOR_LOOPER_ID, NULL, NULL);
|
2015-03-30 11:53:15 -07:00
|
|
|
}
|
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
int GoAndroid_enableSensor(int s, int32_t usec) {
|
2015-03-30 11:53:15 -07:00
|
|
|
ASensorManager* manager = ASensorManager_getInstance();
|
|
|
|
const ASensor* sensor = ASensorManager_getDefaultSensor(manager, s);
|
|
|
|
if (sensor == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-04 00:17:01 -07:00
|
|
|
ASensorEventQueue_enableSensor(queue, sensor);
|
|
|
|
ASensorEventQueue_setEventRate(queue, sensor, usec);
|
2015-03-30 11:53:15 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
void GoAndroid_disableSensor(int s) {
|
2015-03-30 11:53:15 -07:00
|
|
|
ASensorManager* manager = ASensorManager_getInstance();
|
|
|
|
const ASensor* sensor = ASensorManager_getDefaultSensor(manager, s);
|
2015-09-04 00:17:01 -07:00
|
|
|
ASensorEventQueue_disableSensor(queue, sensor);
|
2015-03-30 11:53:15 -07:00
|
|
|
}
|
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
int GoAndroid_readQueue(int n, int32_t* types, int64_t* timestamps, float* vectors) {
|
2015-03-30 11:53:15 -07:00
|
|
|
int id;
|
|
|
|
int events;
|
|
|
|
ASensorEvent event;
|
2015-04-07 15:56:44 -04:00
|
|
|
int i = 0;
|
2015-04-07 17:19:45 -04:00
|
|
|
// Try n times read from the event queue.
|
|
|
|
// If anytime timeout occurs, don't retry to read and immediately return.
|
|
|
|
// Consume the event queue entirely between polls.
|
2015-09-02 19:23:47 -07:00
|
|
|
while (i < n && (id = ALooper_pollAll(-1, NULL, &events, NULL)) >= 0) {
|
2015-09-04 00:17:01 -07:00
|
|
|
if (id != GO_ANDROID_SENSOR_LOOPER_ID) {
|
2015-03-30 11:53:15 -07:00
|
|
|
continue;
|
|
|
|
}
|
2015-09-04 00:17:01 -07:00
|
|
|
while (i < n && ASensorEventQueue_getEvents(queue, &event, 1)) {
|
2015-04-07 15:56:44 -04:00
|
|
|
types[i] = event.type;
|
|
|
|
timestamps[i] = event.timestamp;
|
|
|
|
vectors[i*3] = event.vector.x;
|
|
|
|
vectors[i*3+1] = event.vector.y;
|
|
|
|
vectors[i*3+2] = event.vector.z;
|
|
|
|
i++;
|
2015-03-30 11:53:15 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 15:56:44 -04:00
|
|
|
return i;
|
2015-03-30 11:53:15 -07:00
|
|
|
}
|
|
|
|
|
2015-09-04 00:17:01 -07:00
|
|
|
void GoAndroid_destroyManager() {
|
2015-03-30 11:53:15 -07:00
|
|
|
ASensorManager* manager = ASensorManager_getInstance();
|
2015-09-04 00:17:01 -07:00
|
|
|
ASensorManager_destroyEventQueue(manager, queue);
|
|
|
|
ALooper_release(looper);
|
2015-03-30 11:53:15 -07:00
|
|
|
}
|