OSDN Git Service

Fix build (missing #include).
[android-x86/hardware-libhardware.git] / tests / nusensors / nusensors.cpp
1 /*
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 <string.h>
19 #include <sys/cdefs.h>
20 #include <sys/types.h>
21
22 #include <cutils/log.h>
23
24 #include <hardware/sensors.h>
25
26 char const* getSensorName(int type) {
27     switch(type) {
28         case SENSOR_TYPE_ACCELEROMETER:
29             return "Acc";
30         case SENSOR_TYPE_MAGNETIC_FIELD:
31             return "Mag";
32         case SENSOR_TYPE_ORIENTATION:
33             return "Ori";
34         case SENSOR_TYPE_PROXIMITY:
35             return "Prx";
36         case SENSOR_TYPE_TEMPERATURE:
37             return "Tmp";
38         case SENSOR_TYPE_LIGHT:
39             return "Lux";
40     }
41     return "ukn";
42 }
43
44 int main(int argc, char** argv)
45 {
46     int err;
47     struct sensors_poll_device_t* device;
48     struct sensors_module_t* module;
49
50     err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
51     if (err != 0) {
52         printf("hw_get_module() failed (%s)\n", strerror(-err));
53         return 0;
54     }
55
56     struct sensor_t const* list;
57     int count = module->get_sensors_list(module, &list);
58     for (int i=0 ; i<count ; i++) {
59         printf("%s\n"
60                 "\tvendor: %s\n"
61                 "\tversion: %d\n"
62                 "\thandle: %d\n"
63                 "\ttype: %d\n"
64                 "\tmaxRange: %f\n"
65                 "\tresolution: %f\n"
66                 "\tpower: %f mA\n",
67                 list[i].name,
68                 list[i].vendor,
69                 list[i].version,
70                 list[i].handle,
71                 list[i].type,
72                 list[i].maxRange,
73                 list[i].resolution,
74                 list[i].power);
75     }
76
77     sensors_event_t buffer[16];
78
79     err = sensors_open(&module->common, &device);
80     if (err != 0) {
81         printf("sensors_open() failed (%s)\n", strerror(-err));
82         return 0;
83     }
84
85     for (int i=0 ; i<count ; i++) {
86         err = device->activate(device, list[i].handle, 0);
87         if (err != 0) {
88             printf("deactivate() for '%s'failed (%s)\n",
89                     list[i].name, strerror(-err));
90             return 0;
91         }
92     }
93
94     for (int i=0 ; i<count ; i++) {
95         err = device->activate(device, list[i].handle, 1);
96         if (err != 0) {
97             printf("activate() for '%s'failed (%s)\n",
98                     list[i].name, strerror(-err));
99             return 0;
100         }
101         device->setDelay(device, list[i].handle, 10000000);
102     }
103
104     do {
105         int n = device->poll(device, buffer, 16);
106         if (n < 0) {
107             printf("poll() failed (%s)\n", strerror(-err));
108             break;
109         }
110
111         printf("read %d events:\n", n);
112         for (int i=0 ; i<n ; i++) {
113             const sensors_event_t& data = buffer[i];
114
115             if (data.version != sizeof(sensors_event_t)) {
116                 printf("incorrect event version (version=%d, expected=%d",
117                         data.version, sizeof(sensors_event_t));
118                 break;
119             }
120
121             switch(data.type) {
122                 case SENSOR_TYPE_ACCELEROMETER:
123                     printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
124                             getSensorName(data.type),
125                             data.timestamp,
126                             data.acceleration.x,
127                             data.acceleration.y,
128                             data.acceleration.z);
129                     break;
130                 case SENSOR_TYPE_MAGNETIC_FIELD:
131                     printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
132                             getSensorName(data.type),
133                             data.timestamp,
134                             data.magnetic.x,
135                             data.magnetic.y,
136                             data.magnetic.z);
137                     break;
138                 case SENSOR_TYPE_ORIENTATION:
139                     printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
140                             getSensorName(data.type),
141                             data.timestamp,
142                             data.orientation.azimuth,
143                             data.orientation.pitch,
144                             data.orientation.roll);
145                     break;
146                 case SENSOR_TYPE_PROXIMITY:
147                     printf("sensor=%s, time=%lld, value=%f\n",
148                             getSensorName(data.type),
149                             data.timestamp,
150                             data.distance);
151                     break;
152                 case SENSOR_TYPE_TEMPERATURE:
153                     printf("sensor=%s, time=%lld, value=%f\n",
154                             getSensorName(data.type),
155                             data.timestamp,
156                             data.temperature);
157                     break;
158                 case SENSOR_TYPE_LIGHT:
159                     printf("sensor=%s, time=%lld, value=%f\n",
160                             getSensorName(data.type),
161                             data.timestamp,
162                             data.light);
163                     break;
164                 default:
165                     printf("sensor=%d, time=%lld, value=<%f,%f,%f>\n",
166                             data.type,
167                             data.timestamp,
168                             data.acceleration.x,
169                             data.acceleration.y,
170                             data.acceleration.z);
171                     break;
172             }
173         }
174
175
176     } while (1); // fix that
177
178
179     for (int i=0 ; i<count ; i++) {
180         err = device->activate(device, list[i].handle, 0);
181         if (err != 0) {
182             printf("deactivate() for '%s'failed (%s)\n",
183                     list[i].name, strerror(-err));
184             return 0;
185         }
186     }
187
188     err = sensors_close(device);
189     if (err != 0) {
190         printf("sensors_close() failed (%s)\n", strerror(-err));
191     }
192     return 0;
193 }