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 VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_ENVIRONMENT_H_
18 #define VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_ENVIRONMENT_H_
19 
20 #include "goog_sensor_wrapper.h"
21 
22 namespace android {
23 namespace camera_sensor_listener {
24 
25 // Supported environment sensor types:
26 // ::android::hardware::sensors::V1_0::SensorType::DEVICE_ORIENTATION
27 // ::android::hardware::sensors::V1_0::SensorType::LIGHT
28 // ::android::hardware::sensors::V1_0::SensorType::PROXIMITY
29 enum class EnvironmentSensorType {
30   DEVICE_ORIENTATION = 0,
31   LIGHT,
32   PROXIMITY,
33   TOTAL_NUM
34 };
35 
36 // Environement sensor listener class.
37 // It will create a environment sensor listener whose event data type is float.
38 // DEVICE_ORIENTATION sensor event data is 0, 1, 2, 3:
39 //   0: device is in default orientation (Y axis is vertical and points up).
40 //   1: device is rotated 90 degrees counter-clockwise from default
41 //      orientation (X axis is vertical and points up).
42 //   2: device is rotated 180 degrees from default orientation (Y axis is
43 //      vertical and points down).
44 //   3: device is rotated 90 degrees clockwise from default orientation
45 //      (X axis is vertical and points down).
46 // LIGHT sensor event data is in SI lux units.
47 // PROXIMITY sensor event data is 0 or 5:
48 //   0 means closest object is nearby, 5 means closest object is far.
49 // Current supported sensor types are listed in EnvironmentSensorType.
50 // Sample usage:
51 //   sp<GoogSensorEnvironment> sensor_ptr =
52 //       GoogSensorEnvironment::Create(EnvironmentSensorType::LIGHT,
53 //                                     /*event_queue_size=*/20);
54 //   std::function<void(const ExtendedSensorEvent& event)> callback =
55 //       [](const ExtendedSensorEvent& event) {
56 //         // customized operations.
57 //      };
58 //   sensor_ptr->SetEventProcessor(callback);
59 //   if (sensor_ptr->GetSensorenablingStatus()) {
60 //     std::vector<int64_t> event_timestamps;
61 //     std::vector<float> event_data;
62 //     std::vector<int64_t> arrival_timestamps;
63 //     sensor_ptr->GetLatestNSensorEvents(
64 //         /*num_sample=*/5, &event_timestamps, &event_data,
65 //         &arrival_timestamps);
66 //   }
67 class GoogSensorEnvironment : public GoogSensorWrapper {
68  public:
69   // Return a StrongPointer pointing to newly created GoogSensorEnvironment
70   // instance.
71   // Input:
72   //   environment_sensor_type: sensor type defined in enum class
73   //     EnvironmentSensorType.
74   //   event_queue_size: size of event queue to hold incoming sensor events.
75   static sp<GoogSensorEnvironment> Create(
76       EnvironmentSensorType environment_sensor_type,
77       size_t event_queue_size = kDefaultEventQueueSize);
78 
79   // Destructor.
80   // Destroy and free the resources of a GoogSensorEnvironment.
81   ~GoogSensorEnvironment();
82 
83   // Get whether sensor is enabled.
84   // Return true if sensor is enabled, false otherwise.
GetSensorEnablingStatus()85   bool GetSensorEnablingStatus() const {
86     return IsEnabled();
87   }
88 
89   // Get latest n sensor events' timestamps, event data and arrival times.
90   // If output vectors are not empty, latest_n_timestamps, event_data and
91   // latest_n_arrival_timestamps will be cleared first.
92   // If total samples in event_deque_ is smaller than num_sample,
93   // size of latest_n_timestamps, event_data and latest_n_arrival_timestamps
94   // will be equal to event_deque_.size().
95   // Input:
96   //   num_sample: number of latest samples to query.
97   // Outputs:
98   //   latest_n_timestamps: pointer of vector to hold timestamps.
99   //   event_data: pointer of vector to hold float type event data.
100   //   latest_n_arrival_timestamps: pointer of vector to hold arrival times.
101   // Event timestamps, data and arrival timestamps are listed in chronological
102   // order, i.e., latest_n_timestamps[0], event_data[0] and
103   // latest_n_arrival_timestamps[0] hold the earliest data.
104   void GetLatestNSensorEvents(
105       int num_sample, std::vector<int64_t>* latest_n_timestamps,
106       std::vector<float>* event_data,
107       std::vector<int64_t>* latest_n_arrival_timestamps) const;
108 
109   // Get sensor name.
GetSensorName()110   const char* GetSensorName() const {
111     return GetSensorName(environment_sensor_type_);
112   }
113 
114  protected:
115   // Get environment sensor handle.
116   virtual int32_t GetSensorHandle() final;
117 
118  private:
119   // Constructor.
120   // Create and initialize a GoogSensorEnvironment.
121   // Inputs:
122   //   environment_sensor_type: sensor type defined in enum class
123   //     EnvironmentSensorType.
124   //   event_queue_size: size of event queue to hold incoming sensor events.
125   GoogSensorEnvironment(EnvironmentSensorType environment_sensor_type,
126                         size_t event_queue_size);
127 
128   static const char* GetSensorName(EnvironmentSensorType motion_sensor_type);
129 
130   EnvironmentSensorType environment_sensor_type_;
131 
132   // Default sensor event queue size is set to 20.
133   static constexpr size_t kDefaultEventQueueSize = 20;
134 };
135 
136 }  // namespace camera_sensor_listener
137 }  // namespace android
138 
139 #endif  // VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_ENVIRONMENT_H_
140