blob: 30c6537b9c3808f4bd6ddb445d8b91381ba8c0ce [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>
22
23#include <media/SensorPoseProvider.h>
24
25using android::media::Pose3f;
26using android::media::SensorPoseProvider;
27using android::media::Twist3f;
28
29const char kPackageName[] = "SensorPoseProvider-example";
30
31class Listener : public SensorPoseProvider::Listener {
32 public:
33 void onPose(int64_t timestamp, int32_t handle, const Pose3f& pose,
34 const std::optional<Twist3f>& twist) override {
35 std::cout << "onPose t=" << timestamp << " sensor=" << handle << " pose=" << pose
36 << " twist=";
37 if (twist.has_value()) {
38 std::cout << twist.value();
39 } else {
40 std::cout << "<none>";
41 }
42 std::cout << std::endl;
43 }
44};
45
46int main() {
47 ASensorManager* sensor_manager = ASensorManager_getInstanceForPackage(kPackageName);
48 if (!sensor_manager) {
49 std::cerr << "Failed to get a sensor manager" << std::endl;
50 return 1;
51 }
52
53 const ASensor* headSensor =
54 ASensorManager_getDefaultSensor(sensor_manager, SENSOR_TYPE_GAME_ROTATION_VECTOR);
55 const ASensor* screenSensor =
56 ASensorManager_getDefaultSensor(sensor_manager, SENSOR_TYPE_ROTATION_VECTOR);
57
58 Listener listener;
59
60 std::unique_ptr<SensorPoseProvider> provider =
61 SensorPoseProvider::create(kPackageName, &listener);
62 int32_t headHandle = provider->startSensor(headSensor, std::chrono::milliseconds(500));
63 sleep(2);
64 provider->startSensor(screenSensor, std::chrono::milliseconds(500));
65 sleep(2);
66 provider->stopSensor(headHandle);
67 sleep(2);
68 return 0;
69}