Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2010 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 <android/sensor.h>
     18 #include <gui/Sensor.h>
     19 #include <gui/SensorManager.h>
     20 #include <gui/SensorEventQueue.h>
     21 #include <utils/Looper.h>
     22 
     23 using namespace android;
     24 
     25 int receiver(int fd, int events, void* data)
     26 {
     27     sp<SensorEventQueue> q((SensorEventQueue*)data);
     28     ssize_t n;
     29     ASensorEvent buffer[8];
     30 
     31     static nsecs_t oldTimeStamp = 0;
     32 
     33     while ((n = q->read(buffer, 8)) > 0) {
     34         for (int i=0 ; i<n ; i++) {
     35             if (buffer[i].type == Sensor::TYPE_GYROSCOPE) {
     36                 printf("time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
     37                         buffer[i].timestamp,
     38                         buffer[i].acceleration.x,
     39                         buffer[i].acceleration.y,
     40                         buffer[i].acceleration.z);
     41             }
     42 
     43             if (oldTimeStamp) {
     44                 float t = float(buffer[i].timestamp - oldTimeStamp) / s2ns(1);
     45                 printf("%f ms (%f Hz)\n", t*1000, 1.0/t);
     46             }
     47             oldTimeStamp = buffer[i].timestamp;
     48 
     49         }
     50     }
     51     if (n<0 && n != -EAGAIN) {
     52         printf("error reading events (%s)\n", strerror(-n));
     53     }
     54     return 1;
     55 }
     56 
     57 
     58 int main(int argc, char** argv)
     59 {
     60     SensorManager& mgr(SensorManager::getInstance());
     61 
     62     Sensor const* const* list;
     63     ssize_t count = mgr.getSensorList(&list);
     64     printf("numSensors=%d\n", int(count));
     65 
     66     sp<SensorEventQueue> q = mgr.createEventQueue();
     67     printf("queue=%p\n", q.get());
     68 
     69     Sensor const* accelerometer = mgr.getDefaultSensor(Sensor::TYPE_GYROSCOPE);
     70     printf("accelerometer=%p (%s)\n",
     71             accelerometer, accelerometer->getName().string());
     72     q->enableSensor(accelerometer);
     73 
     74     q->setEventRate(accelerometer, ms2ns(10));
     75 
     76     sp<Looper> loop = new Looper(false);
     77     loop->addFd(q->getFd(), 0, ALOOPER_EVENT_INPUT, receiver, q.get());
     78 
     79     do {
     80         //printf("about to poll...\n");
     81         int32_t ret = loop->pollOnce(-1);
     82         switch (ret) {
     83             case ALOOPER_POLL_WAKE:
     84                 //("ALOOPER_POLL_WAKE\n");
     85                 break;
     86             case ALOOPER_POLL_CALLBACK:
     87                 //("ALOOPER_POLL_CALLBACK\n");
     88                 break;
     89             case ALOOPER_POLL_TIMEOUT:
     90                 printf("ALOOPER_POLL_TIMEOUT\n");
     91                 break;
     92             case ALOOPER_POLL_ERROR:
     93                 printf("ALOOPER_POLL_TIMEOUT\n");
     94                 break;
     95             default:
     96                 printf("ugh? poll returned %d\n", ret);
     97                 break;
     98         }
     99     } while (1);
    100 
    101 
    102     return 0;
    103 }
    104