blob: a246e8bd018da53dfd5e5d6d83cd0605dc9d0c92 [file] [log] [blame]
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -07001/*
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>
Ytai Ben-Tsvi879f0912021-09-13 16:46:39 -070022#include <utils/SystemClock.h>
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070023
24#include <media/SensorPoseProvider.h>
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070025#include <sensor/Sensor.h>
26#include <sensor/SensorManager.h>
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070027
Ytai Ben-Tsvi879f0912021-09-13 16:46:39 -070028using android::elapsedRealtimeNano;
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070029using android::Sensor;
30using android::SensorManager;
31using android::String16;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070032using android::media::Pose3f;
33using android::media::SensorPoseProvider;
34using android::media::Twist3f;
35
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070036using namespace std::chrono_literals;
37
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070038const char kPackageName[] = "SensorPoseProvider-example";
39
40class Listener : public SensorPoseProvider::Listener {
41 public:
42 void onPose(int64_t timestamp, int32_t handle, const Pose3f& pose,
43 const std::optional<Twist3f>& twist) override {
Ytai Ben-Tsvi879f0912021-09-13 16:46:39 -070044 int64_t now = elapsedRealtimeNano();
45
46 std::cout << "onPose t=" << timestamp
47 << " lag=" << ((now - timestamp) / 1e6) << "[ms]"
48 << " sensor=" << handle
49 << " pose=" << pose
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070050 << " twist=";
51 if (twist.has_value()) {
52 std::cout << twist.value();
53 } else {
54 std::cout << "<none>";
55 }
56 std::cout << std::endl;
57 }
58};
59
60int main() {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070061 SensorManager& sensorManager = SensorManager::getInstanceForPackage(String16(kPackageName));
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070062
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070063 const Sensor* headSensor = sensorManager.getDefaultSensor(SENSOR_TYPE_GAME_ROTATION_VECTOR);
64 const Sensor* screenSensor = sensorManager.getDefaultSensor(SENSOR_TYPE_ROTATION_VECTOR);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070065
66 Listener listener;
67
68 std::unique_ptr<SensorPoseProvider> provider =
69 SensorPoseProvider::create(kPackageName, &listener);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070070 int32_t headHandle = provider->startSensor(headSensor->getHandle(), 500ms);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070071 sleep(2);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070072 provider->startSensor(screenSensor->getHandle(), 500ms);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070073 sleep(2);
74 provider->stopSensor(headHandle);
75 sleep(2);
76 return 0;
77}