sensorservicetest.cpp revision 16bcf66afa333f8d3f3b835ed556e4fce8fa35bf
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <android/sensor.h> 18#include <gui/Sensor.h> 19#include <gui/SensorManager.h> 20#include <gui/SensorEventQueue.h> 21#include <utils/Looper.h> 22 23using namespace android; 24 25static nsecs_t sStartTime = 0; 26 27 28int receiver(int fd, int events, void* data) 29{ 30 sp<SensorEventQueue> q((SensorEventQueue*)data); 31 ssize_t n; 32 ASensorEvent buffer[8]; 33 34 static nsecs_t oldTimeStamp = 0; 35 36 while ((n = q->read(buffer, 8)) > 0) { 37 for (int i=0 ; i<n ; i++) { 38 if (buffer[i].type == Sensor::TYPE_ACCELEROMETER) { 39 printf("time=%lld, value=<%5.1f,%5.1f,%5.1f>\n", 40 buffer[i].timestamp, 41 buffer[i].acceleration.x, 42 buffer[i].acceleration.y, 43 buffer[i].acceleration.z); 44 } 45 46 if (oldTimeStamp) { 47 float t = float(buffer[i].timestamp - oldTimeStamp) / s2ns(1); 48 printf("%f ms (%f Hz)\n", t*1000, 1.0/t); 49 } else { 50 float t = float(buffer[i].timestamp - sStartTime) / s2ns(1); 51 printf("first event: %f ms\n", t*1000); 52 } 53 oldTimeStamp = buffer[i].timestamp; 54 } 55 } 56 if (n<0 && n != -EAGAIN) { 57 printf("error reading events (%s)\n", strerror(-n)); 58 } 59 return 1; 60} 61 62 63int main(int argc, char** argv) 64{ 65 SensorManager& mgr(SensorManager::getInstance()); 66 67 Sensor const* const* list; 68 ssize_t count = mgr.getSensorList(&list); 69 printf("numSensors=%d\n", int(count)); 70 71 sp<SensorEventQueue> q = mgr.createEventQueue(); 72 printf("queue=%p\n", q.get()); 73 74 Sensor const* accelerometer = mgr.getDefaultSensor(Sensor::TYPE_ACCELEROMETER); 75 printf("accelerometer=%p (%s)\n", 76 accelerometer, accelerometer->getName().string()); 77 78 sStartTime = systemTime(); 79 80 q->enableSensor(accelerometer); 81 82 q->setEventRate(accelerometer, ms2ns(200)); 83 84 sp<Looper> loop = new Looper(false); 85 loop->addFd(q->getFd(), 0, ALOOPER_EVENT_INPUT, receiver, q.get()); 86 87 do { 88 //printf("about to poll...\n"); 89 int32_t ret = loop->pollOnce(-1); 90 switch (ret) { 91 case ALOOPER_POLL_WAKE: 92 //("ALOOPER_POLL_WAKE\n"); 93 break; 94 case ALOOPER_POLL_CALLBACK: 95 //("ALOOPER_POLL_CALLBACK\n"); 96 break; 97 case ALOOPER_POLL_TIMEOUT: 98 printf("ALOOPER_POLL_TIMEOUT\n"); 99 break; 100 case ALOOPER_POLL_ERROR: 101 printf("ALOOPER_POLL_TIMEOUT\n"); 102 break; 103 default: 104 printf("ugh? poll returned %d\n", ret); 105 break; 106 } 107 } while (1); 108 109 110 return 0; 111} 112