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