Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 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 <sys/cdefs.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <cutils/log.h> |
| 22 | |
| 23 | #include <hardware/sensors.h> |
| 24 | |
| 25 | char 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 | |
| 43 | int 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 | |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 76 | sensors_event_t buffer[16]; |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 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++) { |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 103 | const sensors_event_t& data = buffer[i]; |
| 104 | |
| 105 | if (data.version != sizeof(sensors_event_t)) { |
| 106 | printf("incorrect event version (version=%d, expected=%d", |
| 107 | data.version, sizeof(sensors_event_t)); |
| 108 | break; |
| 109 | } |
| 110 | |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 111 | switch(data.sensor) { |
| 112 | case SENSOR_TYPE_ACCELEROMETER: |
| 113 | printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n", |
| 114 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 115 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 116 | data.acceleration.x, |
| 117 | data.acceleration.y, |
| 118 | data.acceleration.z); |
| 119 | break; |
| 120 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 121 | printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n", |
| 122 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 123 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 124 | data.magnetic.x, |
| 125 | data.magnetic.y, |
| 126 | data.magnetic.z); |
| 127 | break; |
| 128 | case SENSOR_TYPE_ORIENTATION: |
| 129 | printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n", |
| 130 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 131 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 132 | data.orientation.azimuth, |
| 133 | data.orientation.pitch, |
| 134 | data.orientation.roll); |
| 135 | break; |
| 136 | case SENSOR_TYPE_PROXIMITY: |
| 137 | printf("sensor=%s, time=%lld, value=%f\n", |
| 138 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 139 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 140 | data.distance); |
| 141 | break; |
| 142 | case SENSOR_TYPE_TEMPERATURE: |
| 143 | printf("sensor=%s, time=%lld, value=%f\n", |
| 144 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 145 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 146 | data.temperature); |
| 147 | break; |
| 148 | case SENSOR_TYPE_LIGHT: |
| 149 | printf("sensor=%s, time=%lld, value=%f\n", |
| 150 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 151 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 152 | data.light); |
| 153 | break; |
| 154 | default: |
| 155 | printf("sensor=%s, time=%lld, value=<%f,%f,%f>\n", |
| 156 | getSensorName(data.sensor), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame^] | 157 | data.timestamp, |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 158 | data.acceleration.x, |
| 159 | data.acceleration.y, |
| 160 | data.acceleration.z); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | } while (1); // fix that |
| 167 | |
| 168 | |
| 169 | for (int i=0 ; i<count ; i++) { |
| 170 | err = device->activate(device, list[i].handle, 0); |
| 171 | if (err != 0) { |
| 172 | printf("deactivate() for '%s'failed (%s)\n", |
| 173 | list[i].name, strerror(-err)); |
| 174 | return 0; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | err = sensors_close(device); |
| 179 | if (err != 0) { |
| 180 | printf("sensors_close() failed (%s)\n", strerror(-err)); |
| 181 | } |
| 182 | return 0; |
| 183 | } |