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.
19 #include <sys/cdefs.h>
20 #include <sys/types.h>
22 #include <cutils/log.h>
24 #include <hardware/sensors.h>
26 char const* getSensorName(int type) {
28 case SENSOR_TYPE_ACCELEROMETER:
30 case SENSOR_TYPE_MAGNETIC_FIELD:
32 case SENSOR_TYPE_ORIENTATION:
34 case SENSOR_TYPE_PROXIMITY:
36 case SENSOR_TYPE_TEMPERATURE:
38 case SENSOR_TYPE_LIGHT:
44 int main(int argc, char** argv)
47 struct sensors_poll_device_t* device;
48 struct sensors_module_t* module;
50 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
52 printf("hw_get_module() failed (%s)\n", strerror(-err));
56 struct sensor_t const* list;
57 int count = module->get_sensors_list(module, &list);
58 for (int i=0 ; i<count ; i++) {
77 sensors_event_t buffer[16];
79 err = sensors_open(&module->common, &device);
81 printf("sensors_open() failed (%s)\n", strerror(-err));
85 for (int i=0 ; i<count ; i++) {
86 err = device->activate(device, list[i].handle, 0);
88 printf("deactivate() for '%s'failed (%s)\n",
89 list[i].name, strerror(-err));
94 for (int i=0 ; i<count ; i++) {
95 err = device->activate(device, list[i].handle, 1);
97 printf("activate() for '%s'failed (%s)\n",
98 list[i].name, strerror(-err));
101 device->setDelay(device, list[i].handle, 10000000);
105 int n = device->poll(device, buffer, 16);
107 printf("poll() failed (%s)\n", strerror(-err));
111 printf("read %d events:\n", n);
112 for (int i=0 ; i<n ; i++) {
113 const sensors_event_t& data = buffer[i];
115 if (data.version != sizeof(sensors_event_t)) {
116 printf("incorrect event version (version=%d, expected=%d",
117 data.version, sizeof(sensors_event_t));
122 case SENSOR_TYPE_ACCELEROMETER:
123 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
124 getSensorName(data.type),
128 data.acceleration.z);
130 case SENSOR_TYPE_MAGNETIC_FIELD:
131 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
132 getSensorName(data.type),
138 case SENSOR_TYPE_ORIENTATION:
139 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
140 getSensorName(data.type),
142 data.orientation.azimuth,
143 data.orientation.pitch,
144 data.orientation.roll);
146 case SENSOR_TYPE_PROXIMITY:
147 printf("sensor=%s, time=%lld, value=%f\n",
148 getSensorName(data.type),
152 case SENSOR_TYPE_TEMPERATURE:
153 printf("sensor=%s, time=%lld, value=%f\n",
154 getSensorName(data.type),
158 case SENSOR_TYPE_LIGHT:
159 printf("sensor=%s, time=%lld, value=%f\n",
160 getSensorName(data.type),
165 printf("sensor=%d, time=%lld, value=<%f,%f,%f>\n",
170 data.acceleration.z);
176 } while (1); // fix that
179 for (int i=0 ; i<count ; i++) {
180 err = device->activate(device, list[i].handle, 0);
182 printf("deactivate() for '%s'failed (%s)\n",
183 list[i].name, strerror(-err));
188 err = sensors_close(device);
190 printf("sensors_close() failed (%s)\n", strerror(-err));