1 /*
2  * Copyright (C) 2021 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 <unistd.h>
18 #include <iostream>
19 
20 #include <android/sensor.h>
21 #include <hardware/sensors.h>
22 #include <utils/SystemClock.h>
23 
24 #include <media/SensorPoseProvider.h>
25 #include <sensor/Sensor.h>
26 #include <sensor/SensorManager.h>
27 
28 using android::elapsedRealtimeNano;
29 using android::Sensor;
30 using android::SensorManager;
31 using android::String16;
32 using android::media::Pose3f;
33 using android::media::SensorPoseProvider;
34 using android::media::Twist3f;
35 
36 using namespace std::chrono_literals;
37 
38 const char kPackageName[] = "SensorPoseProvider-example";
39 
40 class Listener : public SensorPoseProvider::Listener {
41   public:
onPose(int64_t timestamp,int32_t handle,const Pose3f & pose,const std::optional<Twist3f> & twist,bool isNewReference)42     void onPose(int64_t timestamp, int32_t handle, const Pose3f& pose,
43                 const std::optional<Twist3f>& twist, bool isNewReference) override {
44         int64_t now = elapsedRealtimeNano();
45 
46         std::cout << "onPose t=" << timestamp
47                   << " lag=" << ((now - timestamp) / 1e6) << "[ms]"
48                   << " sensor=" << handle
49                   << " pose=" << pose
50                   << " twist=";
51         if (twist.has_value()) {
52             std::cout << twist.value();
53         } else {
54             std::cout << "<none>";
55         }
56         std::cout << " isNewReference=" << isNewReference << std::endl;
57     }
58 };
59 
main()60 int main() {
61     SensorManager& sensorManager = SensorManager::getInstanceForPackage(String16(kPackageName));
62 
63     const Sensor* headSensor = sensorManager.getDefaultSensor(SENSOR_TYPE_GAME_ROTATION_VECTOR);
64     const Sensor* screenSensor = sensorManager.getDefaultSensor(SENSOR_TYPE_ROTATION_VECTOR);
65 
66     Listener listener;
67 
68     std::unique_ptr<SensorPoseProvider> provider =
69             SensorPoseProvider::create(kPackageName, &listener);
70     if (!provider->startSensor(headSensor->getHandle(), 500ms)) {
71         std::cout << "Failed to start head sensor" << std::endl;
72     }
73     sleep(2);
74     if (!provider->startSensor(screenSensor->getHandle(), 500ms)) {
75         std::cout << "Failed to start screenSensor sensor" << std::endl;
76     }
77     sleep(2);
78     provider->stopSensor(headSensor->getHandle());
79     sleep(2);
80     return 0;
81 }
82