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