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