Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |
| 18 | #define ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |
| 19 | |
| 20 | #include <android/hardware/sensors/1.0/types.h> |
| 21 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 22 | #include <condition_variable> |
| 23 | #include <memory> |
| 24 | #include <thread> |
| 25 | #include <vector> |
| 26 | |
| 27 | using ::android::hardware::sensors::V1_0::Event; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 28 | using ::android::hardware::sensors::V1_0::SensorInfo; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 29 | using ::android::hardware::sensors::V1_0::SensorType; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace sensors { |
| 34 | namespace V2_0 { |
| 35 | namespace implementation { |
| 36 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 37 | class ISensorsEventCallback { |
| 38 | public: |
| 39 | virtual ~ISensorsEventCallback(){}; |
| 40 | virtual void postEvents(const std::vector<Event>& events) = 0; |
| 41 | }; |
| 42 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 43 | class Sensor { |
| 44 | public: |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 45 | Sensor(ISensorsEventCallback* callback); |
| 46 | virtual ~Sensor(); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 47 | |
| 48 | const SensorInfo& getSensorInfo() const; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 49 | void batch(int32_t samplingPeriodNs); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 50 | void activate(bool enable); |
| 51 | |
| 52 | protected: |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 53 | void run(); |
| 54 | virtual std::vector<Event> readEvents(); |
| 55 | static void startThread(Sensor* sensor); |
| 56 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 57 | bool mIsEnabled; |
| 58 | int64_t mSamplingPeriodNs; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 59 | int64_t mLastSampleTimeNs; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 60 | SensorInfo mSensorInfo; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 61 | |
| 62 | std::atomic_bool mStopThread; |
| 63 | std::condition_variable mWaitCV; |
| 64 | std::thread mRunThread; |
| 65 | |
| 66 | ISensorsEventCallback* mCallback; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace implementation |
| 70 | } // namespace V2_0 |
| 71 | } // namespace sensors |
| 72 | } // namespace hardware |
| 73 | } // namespace android |
| 74 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 75 | #endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |