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