1 #ifndef DALI_TILT_SENSOR_H
2 #define DALI_TILT_SENSOR_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/object/base-handle.h>
23 #include <dali/public-api/signals/dali-signal.h>
26 #include <dali/public-api/dali-adaptor-common.h>
30 namespace Internal DALI_INTERNAL
36 } // namespace DALI_INTERNAL
39 * TiltSensor provides pitch & roll values when the device is tilted.
40 * The basic usage is shown below:
46 * TiltSensor sensor = TiltSensor::Get();
48 * // Try to start the tilt sensor
49 * if ( sensor.Start() )
51 * // Query the current values
52 * std::cout << "Roll = " << sensor.GetRoll() << ", Pitch = " << sensor.GetPitch() << std::endl;
54 * // Get notifications when the device is tilted
55 * sensor.TiltedSignal().Connect( &OnTilted );
59 * void OnTilted( const TiltSensor& sensor )
61 * // Query the new values
62 * std::cout << "Roll = " << sensor.GetRoll() << ", Pitch = " << sensor.GetPitch() << std::endl;
67 * While the tilt sensor is started, it will periodically poll for the latest pitch & roll values.
68 * For performance & power-saving, applications should disable this polling when no longer needed:
74 * // Stop the sensor when no longer needed
75 * TiltSensor::Get().Stop();
80 class DALI_ADAPTOR_API TiltSensor : public BaseHandle
83 typedef Signal<void(const TiltSensor&)> TiltedSignalType;
85 static const float DEFAULT_UPDATE_FREQUENCY; // 60 hertz
88 * Create an uninitialized handle.
89 * This can be initialized by calling TiltSensor::Get().
94 * Create an initialized handle to the TiltSensor.
95 * @return A handle to a newly allocated Dali resource.
97 static TiltSensor Get();
102 * This is non-virtual since derived Handle types must not contain data or virtual methods.
107 * Attempt to start the tilt-sensor. This will fail if the underlying sensor hardware is powered-down,
108 * typically this occurs when the device is set to "sleep" mode.
109 * @return True if the tilt-sensor is started.
114 * Stop the tilt-sensor.
119 * Query whether the tilt-sensor is started.
120 * The sensor may be disabled automatically; typically this occurs when the device is set to "sleep" mode.
121 * @return True if the tilt-sensor is started.
123 bool IsStarted() const;
126 * Query the roll value. This is in the range -1 to 1.
127 * When the device is lying face-up on a flat surface, this method will return a value close to zero.
128 * A value close to 1 indicates that the right-side of the device is pointing upwards.
129 * A value close to -1 indicates that the right-side of the device is pointing downwards.
130 * @pre The tilt-sensor is started.
131 * @return The roll value.
133 float GetRoll() const;
136 * Query the pitch value. This is in the range -1 to 1.
137 * When the device is lying face-up on a flat surface, this method will return a value close to zero.
138 * A value close to 1 indicates that the top of the device is pointing upwards.
139 * A value close to -1 indicates that the top of the device is pointing downwards.
140 * @pre The tilt-sensor is started.
141 * @return The pitch value.
143 float GetPitch() const;
146 * Retrieve the rotation of the device.
147 * When the device is lying face-up on a flat surface, the rotation angle will be approximately zero.
148 * The roll & pitch of the device is considered to be a rotation around the Y and X axes respectively.
149 * @pre The tilt-sensor is started.
150 * @return The rotation in quaternion format.
152 Quaternion GetRotation() const;
155 * This signal will be emitted when the device is tilted, if the tilt-sensor is started.
156 * The frequency of the signals can be controlled using SetUpdateFrequency().
157 * @return The signal to connect to.
159 * @note The signal name is "tilted" if using BaseHandle::ConnectSignal()
161 TiltedSignalType& TiltedSignal();
164 * Set the sensor update frequency.
165 * The default is TiltSensor::DEFAULT_UPDATE_FREQUENCY.
166 * @param[in] frequencyHertz The frequency in hertz.
168 void SetUpdateFrequency(float frequencyHertz);
171 * Query the sensor update frequency.
172 * @return The frequency in hertz.
174 float GetUpdateFrequency() const;
177 * Set the threshold value for rotation in Radians, above which TiltedSignal should be emitted.
178 * The default is 0.0f in Radians (i.e) it will be emitted always at the frequency set.
179 * Example tiltSensor.SetRotationThreshold( Radian(Degree(10) ) // A rotation threshold of 10 degrees
180 * @param[in] rotationThreshold The threshold value for rotation.
182 void SetRotationThreshold(Radian rotationThreshold);
185 * Query the rotation threshold above which TiltedSignal will be emitted.
186 * @return The rotation degree threshold in Radians.
188 Radian GetRotationThreshold() const;
190 public: // Not intended for application developers
192 * This constructor is used by TiltSensor::Get().
193 * @param[in] sensor A pointer to the tilt sensor.
195 explicit DALI_INTERNAL TiltSensor(Internal::Adaptor::TiltSensor* sensor);
200 #endif // DALI_TILT_SENSOR_H