blob: 29e206914b71767006f3a3767da25abba10f3ddb [file] [log] [blame]
Mathias Agopianb1e212e2010-07-08 16:44:54 -07001/*
2 * Copyright (C) 2008 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 <stdint.h>
Elliott Hughes5f5c5462010-08-19 10:21:01 -070018#include <string.h>
Mathias Agopianb1e212e2010-07-08 16:44:54 -070019#include <sys/cdefs.h>
20#include <sys/types.h>
21
22#include <cutils/log.h>
23
24#include <hardware/sensors.h>
25
26char const* getSensorName(int type) {
27 switch(type) {
28 case SENSOR_TYPE_ACCELEROMETER:
29 return "Acc";
30 case SENSOR_TYPE_MAGNETIC_FIELD:
31 return "Mag";
32 case SENSOR_TYPE_ORIENTATION:
33 return "Ori";
34 case SENSOR_TYPE_PROXIMITY:
35 return "Prx";
36 case SENSOR_TYPE_TEMPERATURE:
37 return "Tmp";
38 case SENSOR_TYPE_LIGHT:
39 return "Lux";
40 }
41 return "ukn";
42}
43
44int main(int argc, char** argv)
45{
46 int err;
47 struct sensors_poll_device_t* device;
48 struct sensors_module_t* module;
49
50 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
51 if (err != 0) {
52 printf("hw_get_module() failed (%s)\n", strerror(-err));
53 return 0;
54 }
55
56 struct sensor_t const* list;
57 int count = module->get_sensors_list(module, &list);
58 for (int i=0 ; i<count ; i++) {
59 printf("%s\n"
60 "\tvendor: %s\n"
61 "\tversion: %d\n"
62 "\thandle: %d\n"
63 "\ttype: %d\n"
64 "\tmaxRange: %f\n"
65 "\tresolution: %f\n"
66 "\tpower: %f mA\n",
67 list[i].name,
68 list[i].vendor,
69 list[i].version,
70 list[i].handle,
71 list[i].type,
72 list[i].maxRange,
73 list[i].resolution,
74 list[i].power);
75 }
76
Mathias Agopiancdefccd2010-07-15 18:29:03 -070077 sensors_event_t buffer[16];
Mathias Agopianb1e212e2010-07-08 16:44:54 -070078
79 err = sensors_open(&module->common, &device);
80 if (err != 0) {
81 printf("sensors_open() failed (%s)\n", strerror(-err));
82 return 0;
83 }
84
85 for (int i=0 ; i<count ; i++) {
Mathias Agopian1a2bf612010-07-20 18:01:19 -070086 err = device->activate(device, list[i].handle, 0);
87 if (err != 0) {
88 printf("deactivate() for '%s'failed (%s)\n",
89 list[i].name, strerror(-err));
90 return 0;
91 }
92 }
93
94 for (int i=0 ; i<count ; i++) {
Mathias Agopianb1e212e2010-07-08 16:44:54 -070095 err = device->activate(device, list[i].handle, 1);
96 if (err != 0) {
97 printf("activate() for '%s'failed (%s)\n",
98 list[i].name, strerror(-err));
99 return 0;
100 }
101 device->setDelay(device, list[i].handle, 10000000);
102 }
103
104 do {
105 int n = device->poll(device, buffer, 16);
106 if (n < 0) {
107 printf("poll() failed (%s)\n", strerror(-err));
108 break;
109 }
110
111 printf("read %d events:\n", n);
112 for (int i=0 ; i<n ; i++) {
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700113 const sensors_event_t& data = buffer[i];
114
115 if (data.version != sizeof(sensors_event_t)) {
116 printf("incorrect event version (version=%d, expected=%d",
117 data.version, sizeof(sensors_event_t));
118 break;
119 }
120
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700121 switch(data.type) {
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700122 case SENSOR_TYPE_ACCELEROMETER:
123 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700124 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700125 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700126 data.acceleration.x,
127 data.acceleration.y,
128 data.acceleration.z);
129 break;
130 case SENSOR_TYPE_MAGNETIC_FIELD:
131 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700132 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700133 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700134 data.magnetic.x,
135 data.magnetic.y,
136 data.magnetic.z);
137 break;
138 case SENSOR_TYPE_ORIENTATION:
139 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700140 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700141 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700142 data.orientation.azimuth,
143 data.orientation.pitch,
144 data.orientation.roll);
145 break;
146 case SENSOR_TYPE_PROXIMITY:
147 printf("sensor=%s, time=%lld, value=%f\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700148 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700149 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700150 data.distance);
151 break;
152 case SENSOR_TYPE_TEMPERATURE:
153 printf("sensor=%s, time=%lld, value=%f\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700154 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700155 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700156 data.temperature);
157 break;
158 case SENSOR_TYPE_LIGHT:
159 printf("sensor=%s, time=%lld, value=%f\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700160 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700161 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700162 data.light);
163 break;
164 default:
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700165 printf("sensor=%d, time=%lld, value=<%f,%f,%f>\n",
166 data.type,
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700167 data.timestamp,
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700168 data.acceleration.x,
169 data.acceleration.y,
170 data.acceleration.z);
171 break;
172 }
173 }
174
175
176 } while (1); // fix that
177
178
179 for (int i=0 ; i<count ; i++) {
180 err = device->activate(device, list[i].handle, 0);
181 if (err != 0) {
182 printf("deactivate() for '%s'failed (%s)\n",
183 list[i].name, strerror(-err));
184 return 0;
185 }
186 }
187
188 err = sensors_close(device);
189 if (err != 0) {
190 printf("sensors_close() failed (%s)\n", strerror(-err));
191 }
192 return 0;
193}