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 <inttypes.h>
18 #include <android/sensor.h>
19 #include <sensor/Sensor.h>
20 #include <sensor/SensorManager.h>
21 #include <sensor/SensorEventQueue.h>
22 #include <utils/Looper.h>
23
24 using namespace android;
25
26 static nsecs_t sStartTime = 0;
27
28
receiver(__unused int fd,__unused int events,void * data)29 int receiver(__unused int fd, __unused int events, void* data)
30 {
31 sp<SensorEventQueue> q((SensorEventQueue*)data);
32 ssize_t n;
33 ASensorEvent buffer[8];
34
35 static nsecs_t oldTimeStamp = 0;
36
37 while ((n = q->read(buffer, 8)) > 0) {
38 for (int i=0 ; i<n ; i++) {
39 float t;
40 if (oldTimeStamp) {
41 t = float(buffer[i].timestamp - oldTimeStamp) / s2ns(1);
42 } else {
43 t = float(buffer[i].timestamp - sStartTime) / s2ns(1);
44 }
45 oldTimeStamp = buffer[i].timestamp;
46
47 if (buffer[i].type == Sensor::TYPE_ACCELEROMETER) {
48 printf("%" PRId64 "\t%8f\t%8f\t%8f\t%f\n",
49 buffer[i].timestamp,
50 buffer[i].data[0], buffer[i].data[1], buffer[i].data[2],
51 1.0/t);
52 }
53
54 }
55 }
56 if (n<0 && n != -EAGAIN) {
57 printf("error reading events (%s)\n", strerror(-n));
58 }
59 return 1;
60 }
61
62
main()63 int main()
64 {
65 SensorManager& mgr = SensorManager::getInstanceForPackage(String16("Sensor Service Test"));
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(10));
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