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