2010-07-15 06:41:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <android/sensor.h>
|
|
|
|
#include <gui/Sensor.h>
|
|
|
|
#include <gui/SensorManager.h>
|
|
|
|
#include <gui/SensorEventQueue.h>
|
2010-09-14 06:17:30 +00:00
|
|
|
#include <utils/Looper.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
|
2011-08-16 00:10:23 +00:00
|
|
|
static nsecs_t sStartTime = 0;
|
|
|
|
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
int receiver(int fd, int events, void* data)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
|
|
|
sp<SensorEventQueue> q((SensorEventQueue*)data);
|
|
|
|
ssize_t n;
|
|
|
|
ASensorEvent buffer[8];
|
2010-11-06 02:12:58 +00:00
|
|
|
|
|
|
|
static nsecs_t oldTimeStamp = 0;
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
while ((n = q->read(buffer, 8)) > 0) {
|
|
|
|
for (int i=0 ; i<n ; i++) {
|
2011-09-13 05:16:01 +00:00
|
|
|
float t;
|
2010-11-06 02:12:58 +00:00
|
|
|
if (oldTimeStamp) {
|
2011-09-13 05:16:01 +00:00
|
|
|
t = float(buffer[i].timestamp - oldTimeStamp) / s2ns(1);
|
2011-08-16 00:10:23 +00:00
|
|
|
} else {
|
2011-09-13 05:16:01 +00:00
|
|
|
t = float(buffer[i].timestamp - sStartTime) / s2ns(1);
|
2010-11-06 02:12:58 +00:00
|
|
|
}
|
|
|
|
oldTimeStamp = buffer[i].timestamp;
|
2011-09-13 05:16:01 +00:00
|
|
|
|
|
|
|
if (buffer[i].type == Sensor::TYPE_ACCELEROMETER) {
|
|
|
|
printf("%lld\t%8f\t%8f\t%8f\t%f\n",
|
|
|
|
buffer[i].timestamp,
|
|
|
|
buffer[i].data[0], buffer[i].data[1], buffer[i].data[2],
|
|
|
|
1.0/t);
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n<0 && n != -EAGAIN) {
|
|
|
|
printf("error reading events (%s)\n", strerror(-n));
|
|
|
|
}
|
2010-09-14 06:17:30 +00:00
|
|
|
return 1;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
SensorManager& mgr(SensorManager::getInstance());
|
|
|
|
|
|
|
|
Sensor const* const* list;
|
|
|
|
ssize_t count = mgr.getSensorList(&list);
|
2010-09-14 06:17:30 +00:00
|
|
|
printf("numSensors=%d\n", int(count));
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
sp<SensorEventQueue> q = mgr.createEventQueue();
|
|
|
|
printf("queue=%p\n", q.get());
|
|
|
|
|
2011-08-16 00:10:23 +00:00
|
|
|
Sensor const* accelerometer = mgr.getDefaultSensor(Sensor::TYPE_ACCELEROMETER);
|
2010-07-15 06:41:37 +00:00
|
|
|
printf("accelerometer=%p (%s)\n",
|
|
|
|
accelerometer, accelerometer->getName().string());
|
2011-08-16 00:10:23 +00:00
|
|
|
|
|
|
|
sStartTime = systemTime();
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
q->enableSensor(accelerometer);
|
|
|
|
|
2011-09-13 05:16:01 +00:00
|
|
|
q->setEventRate(accelerometer, ms2ns(10));
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
sp<Looper> loop = new Looper(false);
|
|
|
|
loop->addFd(q->getFd(), 0, ALOOPER_EVENT_INPUT, receiver, q.get());
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
//printf("about to poll...\n");
|
2010-09-14 06:17:30 +00:00
|
|
|
int32_t ret = loop->pollOnce(-1);
|
2010-07-15 06:41:37 +00:00
|
|
|
switch (ret) {
|
2010-09-14 06:17:30 +00:00
|
|
|
case ALOOPER_POLL_WAKE:
|
|
|
|
//("ALOOPER_POLL_WAKE\n");
|
|
|
|
break;
|
2010-07-15 06:41:37 +00:00
|
|
|
case ALOOPER_POLL_CALLBACK:
|
|
|
|
//("ALOOPER_POLL_CALLBACK\n");
|
|
|
|
break;
|
|
|
|
case ALOOPER_POLL_TIMEOUT:
|
|
|
|
printf("ALOOPER_POLL_TIMEOUT\n");
|
|
|
|
break;
|
|
|
|
case ALOOPER_POLL_ERROR:
|
|
|
|
printf("ALOOPER_POLL_TIMEOUT\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("ugh? poll returned %d\n", ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|