2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/cdefs.h>
19 #include <sys/types.h>
21 #include <cutils/log.h>
23 #include <hardware/sensors.h>
25 char const* getSensorName(int type) {
27 case SENSOR_TYPE_ACCELEROMETER:
29 case SENSOR_TYPE_MAGNETIC_FIELD:
31 case SENSOR_TYPE_ORIENTATION:
33 case SENSOR_TYPE_PROXIMITY:
35 case SENSOR_TYPE_TEMPERATURE:
37 case SENSOR_TYPE_LIGHT:
43 int main(int argc, char** argv)
46 struct sensors_poll_device_t* device;
47 struct sensors_module_t* module;
49 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
51 printf("hw_get_module() failed (%s)\n", strerror(-err));
55 struct sensor_t const* list;
56 int count = module->get_sensors_list(module, &list);
57 for (int i=0 ; i<count ; i++) {
76 sensors_event_t buffer[16];
78 err = sensors_open(&module->common, &device);
80 printf("sensors_open() failed (%s)\n", strerror(-err));
84 for (int i=0 ; i<count ; i++) {
85 err = device->activate(device, list[i].handle, 0);
87 printf("deactivate() for '%s'failed (%s)\n",
88 list[i].name, strerror(-err));
93 for (int i=0 ; i<count ; i++) {
94 err = device->activate(device, list[i].handle, 1);
96 printf("activate() for '%s'failed (%s)\n",
97 list[i].name, strerror(-err));
100 device->setDelay(device, list[i].handle, 10000000);
104 int n = device->poll(device, buffer, 16);
106 printf("poll() failed (%s)\n", strerror(-err));
110 printf("read %d events:\n", n);
111 for (int i=0 ; i<n ; i++) {
112 const sensors_event_t& data = buffer[i];
114 if (data.version != sizeof(sensors_event_t)) {
115 printf("incorrect event version (version=%d, expected=%d",
116 data.version, sizeof(sensors_event_t));
121 case SENSOR_TYPE_ACCELEROMETER:
122 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
123 getSensorName(data.type),
127 data.acceleration.z);
129 case SENSOR_TYPE_MAGNETIC_FIELD:
130 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
131 getSensorName(data.type),
137 case SENSOR_TYPE_ORIENTATION:
138 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
139 getSensorName(data.type),
141 data.orientation.azimuth,
142 data.orientation.pitch,
143 data.orientation.roll);
145 case SENSOR_TYPE_PROXIMITY:
146 printf("sensor=%s, time=%lld, value=%f\n",
147 getSensorName(data.type),
151 case SENSOR_TYPE_TEMPERATURE:
152 printf("sensor=%s, time=%lld, value=%f\n",
153 getSensorName(data.type),
157 case SENSOR_TYPE_LIGHT:
158 printf("sensor=%s, time=%lld, value=%f\n",
159 getSensorName(data.type),
164 printf("sensor=%d, time=%lld, value=<%f,%f,%f>\n",
169 data.acceleration.z);
175 } while (1); // fix that
178 for (int i=0 ; i<count ; i++) {
179 err = device->activate(device, list[i].handle, 0);
181 printf("deactivate() for '%s'failed (%s)\n",
182 list[i].name, strerror(-err));
187 err = sensors_close(device);
189 printf("sensors_close() failed (%s)\n", strerror(-err));