blob: 215c9728d4a207e11f6b2ecd789998c6e0f6bd4c [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
76 sensors_data_t buffer[16];
77
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++) {
85 err = device->activate(device, list[i].handle, 1);
86 if (err != 0) {
87 printf("activate() for '%s'failed (%s)\n",
88 list[i].name, strerror(-err));
89 return 0;
90 }
91 device->setDelay(device, list[i].handle, 10000000);
92 }
93
94 do {
95 int n = device->poll(device, buffer, 16);
96 if (n < 0) {
97 printf("poll() failed (%s)\n", strerror(-err));
98 break;
99 }
100
101 printf("read %d events:\n", n);
102 for (int i=0 ; i<n ; i++) {
103 const sensors_data_t& data = buffer[i];
104 switch(data.sensor) {
105 case SENSOR_TYPE_ACCELEROMETER:
106 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
107 getSensorName(data.sensor),
108 data.time,
109 data.acceleration.x,
110 data.acceleration.y,
111 data.acceleration.z);
112 break;
113 case SENSOR_TYPE_MAGNETIC_FIELD:
114 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
115 getSensorName(data.sensor),
116 data.time,
117 data.magnetic.x,
118 data.magnetic.y,
119 data.magnetic.z);
120 break;
121 case SENSOR_TYPE_ORIENTATION:
122 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
123 getSensorName(data.sensor),
124 data.time,
125 data.orientation.azimuth,
126 data.orientation.pitch,
127 data.orientation.roll);
128 break;
129 case SENSOR_TYPE_PROXIMITY:
130 printf("sensor=%s, time=%lld, value=%f\n",
131 getSensorName(data.sensor),
132 data.time,
133 data.distance);
134 break;
135 case SENSOR_TYPE_TEMPERATURE:
136 printf("sensor=%s, time=%lld, value=%f\n",
137 getSensorName(data.sensor),
138 data.time,
139 data.temperature);
140 break;
141 case SENSOR_TYPE_LIGHT:
142 printf("sensor=%s, time=%lld, value=%f\n",
143 getSensorName(data.sensor),
144 data.time,
145 data.light);
146 break;
147 default:
148 printf("sensor=%s, time=%lld, value=<%f,%f,%f>\n",
149 getSensorName(data.sensor),
150 data.time,
151 data.acceleration.x,
152 data.acceleration.y,
153 data.acceleration.z);
154 break;
155 }
156 }
157
158
159 } while (1); // fix that
160
161
162 for (int i=0 ; i<count ; i++) {
163 err = device->activate(device, list[i].handle, 0);
164 if (err != 0) {
165 printf("deactivate() for '%s'failed (%s)\n",
166 list[i].name, strerror(-err));
167 return 0;
168 }
169 }
170
171 err = sensors_close(device);
172 if (err != 0) {
173 printf("sensors_close() failed (%s)\n", strerror(-err));
174 }
175 return 0;
176}