f53c9106914749d03b554a164fe2121e26a26fc3
[platform/core/uifw/dali-adaptor.git] / adaptors / public-api / adaptor-framework / tilt-sensor.h
1 #ifndef __DALI_TILT_SENSOR_H__
2 #define __DALI_TILT_SENSOR_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <boost/function.hpp>
23
24 #include <dali/public-api/object/base-handle.h>
25 #include <dali/public-api/signals/dali-signal-v2.h>
26
27 namespace Dali
28 {
29
30 namespace Internal DALI_INTERNAL
31 {
32 namespace Adaptor
33 {
34 class TiltSensor;
35 }
36 }
37
38 /**
39  * TiltSensor provides pitch & roll values when the device is tilted.
40  * The basic usage is shown below:
41  *
42  * @code
43  *
44  *  void Example()
45  *  {
46  *    TiltSensor sensor = TiltSensor::Get();
47  *
48  *    // Try to enable the sensor
49  *    if ( sensor.Enable() )
50  *    {
51  *      // Query the current values
52  *      std::cout << "Roll = " << sensor.GetRoll() << ", Pitch = " << sensor.GetPitch() << std::endl;
53  *
54  *      // Get notifications when the device is tilted
55  *      sensor.SignalTilted().Connect( &OnTilted );
56  *    }
57  *  }
58  *
59  *  void OnTilted()
60  *  {
61  *    // Query the new values
62  *    std::cout << "Roll = " << sensor.GetRoll() << ", Pitch = " << sensor.GetPitch() << std::endl;
63  *  }
64  *
65  * @endcode
66  *
67  * While the tilt sensor is enabled, it will periodically poll for the latest pitch & roll values.
68  * For performance & power-saving, applications should disable this polling when no longer needed:
69  *
70  * @code
71  *
72  *  void EndExample()
73  *  {
74  *    // Disable the sensor when no longer needed
75  *    TiltSensor::Get().Disable();
76  *  }
77  *
78  * @endcode
79  */
80 class DALI_IMPORT_API TiltSensor : public BaseHandle
81 {
82 public:
83
84   typedef SignalV2< void (const TiltSensor&) > TiltedSignalV2;
85
86   static const float DEFAULT_UPDATE_FREQUENCY; // 60 hertz
87
88   /**
89    * Create an uninitialized handle.
90    * This can be initialized by calling TiltSensor::Get().
91    */
92   TiltSensor();
93
94   /**
95    * Create an initialized handle to the TiltSensor.
96    * @return A handle to a newly allocated Dali resource.
97    */
98   static TiltSensor Get();
99
100   /**
101    * @brief Destructor
102    *
103    * This is non-virtual since derived Handle types must not contain data or virtual methods.
104    */
105   ~TiltSensor();
106
107   /**
108    * Attempt to enable the tilt-sensor. This will fail if the underlying sensor hardware is powered-down,
109    * typically this occurs when the device is set to "sleep" mode.
110    * @return True if the tilt-sensor is enabled.
111    */
112   bool Enable();
113
114   /**
115    * Disable the tilt-sensor.
116    */
117   void Disable();
118
119   /**
120    * Query whether the tilt-sensor is disabled.
121    * The sensor may be disabled automatically; typically this occurs when the device is set to "sleep" mode.
122    * @return True if the tilt-sensor is enabled.
123    */
124   bool IsEnabled() const;
125
126   /**
127    * Query the roll value. This is in the range -1 to 1.
128    * When the device is lying face-up on a flat surface, this method will return a value close to zero.
129    * A value close to 1 indicates that the right-side of the device is pointing upwards.
130    * A value close to -1 indicates that the right-side of the device is pointing downwards.
131    * @pre The tilt-sensor is enabled.
132    * @return The roll value.
133    */
134   float GetRoll() const;
135
136   /**
137    * Query the pitch value. This is in the range -1 to 1.
138    * When the device is lying face-up on a flat surface, this method will return a value close to zero.
139    * A value close to 1 indicates that the top of the device is pointing upwards.
140    * A value close to -1 indicates that the top of the device is pointing downwards.
141    * @pre The tilt-sensor is enabled.
142    * @return The pitch value.
143    */
144   float GetPitch() const;
145
146   /**
147    * Retrieve the rotation of the device.
148    * When the device is lying face-up on a flat surface, the rotation angle will be approximately zero.
149    * The roll & pitch of the device is considered to be a rotation around the Y and X axes respectively.
150    * @pre The tilt-sensor is enabled.
151    * @return The rotation in quaternion format.
152    */
153   Quaternion GetRotation() const;
154
155   /**
156    * This signal will be emitted when the device is tilted, if the tilt-sensor is enabled.
157    * The frequency of the signals can be controlled using SetUpdateFrequency().
158    * @return The signal to connect to.
159    *
160    * @note The signal name is "tilted" if using BaseHandle::ConnectSignal()
161    */
162   TiltedSignalV2& TiltedSignal();
163
164   /**
165    * Set the sensor update frequency.
166    * The default is TiltSensor::DEFAULT_UPDATE_FREQUENCY.
167    * @param[in] frequencyHertz The frequency in hertz.
168    */
169   void SetUpdateFrequency( float frequencyHertz );
170
171   /**
172    * Query the sensor update frequency.
173    * @return The frequency in hertz.
174    */
175   float GetUpdateFrequency() const;
176
177   /**
178    * Set the threshold value for rotation in Radians, above which TiltedSignal should be emitted.
179    * The default is 0.0f in Radians (i.e) it will be emitted always at the frequency set.
180    * Example tiltSensor.SetRotationThreshold( Radian(Degree(10) ) // A rotation threshold of 10 degrees
181    * @param[in] rotationThreshold The threshold value for rotation.
182    */
183   void SetRotationThreshold( Radian rotationThreshold );
184
185   /**
186    * Query the rotation threshold above which TiltedSignal will be emitted.
187    * @return The rotation degree threshold in Radians.
188    */
189   Radian GetRotationThreshold() const;
190
191 public: // Not intended for application developers
192
193   /**
194    * This constructor is used by TiltSensor::Get().
195    * @param[in] sensor A pointer to the tilt sensor.
196    */
197   explicit DALI_INTERNAL TiltSensor( Internal::Adaptor::TiltSensor* sensor );
198 };
199
200 } // namespace Dali
201
202 #endif // __DALI_TILT_SENSOR_H__