DALi signals refactor to remove V2 naming
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / tilt-sensor-impl-tizen.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include "tilt-sensor-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cmath>
23 #ifdef DALI_PROFILE_LITE
24 #include <sensor_internal.h>
25 #else
26 #include <sensor.h>
27 #endif
28
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL INCLUDES
33 #include <singleton-service-impl.h>
34
35 #ifdef __arm__
36 #define SENSOR_ENABLED
37 #endif
38
39 namespace // unnamed namespace
40 {
41
42 const char* const SIGNAL_TILTED = "tilted";
43
44 const int NUMBER_OF_SAMPLES = 10;
45
46 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
47
48 // Type Registration
49 Dali::BaseHandle GetInstance()
50 {
51   return Dali::Internal::Adaptor::TiltSensor::Get();
52 }
53
54 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
55
56 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
57
58 } // unnamed namespace
59
60 namespace Dali
61 {
62
63 namespace Internal
64 {
65
66 namespace Adaptor
67 {
68
69 Dali::TiltSensor TiltSensor::New()
70 {
71   Dali::TiltSensor sensor = Dali::TiltSensor(new TiltSensor());
72
73   return sensor;
74 }
75
76 Dali::TiltSensor TiltSensor::Get()
77 {
78   Dali::TiltSensor sensor;
79
80   Dali::SingletonService service( SingletonService::Get() );
81   if ( service )
82   {
83     // Check whether the keyboard focus manager is already created
84     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TiltSensor ) );
85     if(handle)
86     {
87       // If so, downcast the handle of singleton to keyboard focus manager
88       sensor = Dali::TiltSensor( dynamic_cast< TiltSensor* >( handle.GetObjectPtr() ) );
89     }
90     else
91     {
92       // Create a singleton instance
93       sensor = TiltSensor::New();
94       service.Register( typeid( sensor ), sensor );
95       handle = sensor;
96     }
97   }
98
99   return sensor;
100 }
101
102 TiltSensor::~TiltSensor()
103 {
104   Disable();
105 }
106
107 bool TiltSensor::Enable()
108 {
109   // Make sure sensor API is responding
110   bool success = Update();
111
112   if ( success )
113   {
114     if ( !mTimer )
115     {
116       mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
117       mTimer.TickSignal().Connect( mTimerSlot, &TiltSensor::Update );
118     }
119
120     if ( mTimer &&
121          !mTimer.IsRunning() )
122     {
123       mTimer.Start();
124     }
125   }
126
127   return success;
128 }
129
130 void TiltSensor::Disable()
131 {
132   if ( mTimer )
133   {
134     mTimer.Stop();
135     mTimer.Reset();
136   }
137 }
138
139 bool TiltSensor::IsEnabled() const
140 {
141   return ( mTimer && mTimer.IsRunning() );
142 }
143
144 float TiltSensor::GetRoll() const
145 {
146   return mRoll;
147 }
148
149 float TiltSensor::GetPitch() const
150 {
151   return mPitch;
152 }
153
154 Quaternion TiltSensor::GetRotation() const
155 {
156   return mRotation;
157 }
158
159 TiltSensor::TiltedSignalType& TiltSensor::TiltedSignal()
160 {
161   return mTiltedSignal;
162 }
163
164 void TiltSensor::SetUpdateFrequency( float frequencyHertz )
165 {
166   DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
167
168   if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
169   {
170     mFrequencyHertz = frequencyHertz;
171
172     if ( mTimer )
173     {
174       mTimer.SetInterval( 1000.0f / mFrequencyHertz );
175     }
176   }
177 }
178
179 float TiltSensor::GetUpdateFrequency() const
180 {
181   return mFrequencyHertz;
182 }
183
184 void TiltSensor::SetRotationThreshold(Radian rotationThreshold)
185 {
186   mRotationThreshold = rotationThreshold;
187 }
188
189 Radian TiltSensor::GetRotationThreshold() const
190 {
191   return mRotationThreshold;
192 }
193
194 bool TiltSensor::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
195 {
196   bool connected( true );
197   TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
198
199   if( sensor && ( SIGNAL_TILTED == signalName ) )
200   {
201     sensor->TiltedSignal().Connect( tracker, functor );
202   }
203   else
204   {
205     // signalName does not match any signal
206     connected = false;
207   }
208
209   return connected;
210 }
211
212 TiltSensor::TiltSensor()
213 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
214   mTimerSlot( this ),
215   mSensorFrameworkHandle( -1 ),
216   mRoll( 0.0f ),
217   mPitch( 0.0f ),
218   mRotation( 0.0f, Vector3::YAXIS ),
219   mRotationThreshold( 0.0f )
220 {
221   mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
222   mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
223 }
224
225 bool TiltSensor::Update()
226 {
227   float newRoll = 0.0f;
228   float newPitch = 0.0f;
229   Quaternion newRotation;
230 #ifdef SENSOR_ENABLED
231
232   // Read accelerometer data
233
234   mSensorFrameworkHandle = sf_connect( ACCELEROMETER_SENSOR );
235   if ( mSensorFrameworkHandle < 0 )
236   {
237     DALI_LOG_ERROR( "Failed to connect to sensor framework" );
238     return false;
239   }
240
241   if ( sf_start(mSensorFrameworkHandle, 0) < 0 )
242   {
243     DALI_LOG_ERROR( "Failed to start sensor" );
244     sf_disconnect(mSensorFrameworkHandle);
245     return false;
246   }
247
248   sensor_data_t* base_data_values = (sensor_data_t*)malloc(sizeof(sensor_data_t));
249
250   int dataErr = sf_get_data(mSensorFrameworkHandle, ACCELEROMETER_BASE_DATA_SET, base_data_values);
251   if ( dataErr < 0 )
252   {
253     DALI_LOG_ERROR( "Failed to retrieve sensor data" );
254     free(base_data_values);
255     sf_stop(mSensorFrameworkHandle);
256     sf_disconnect(mSensorFrameworkHandle);
257     return false;
258   }
259
260   sf_stop(mSensorFrameworkHandle);
261   sf_disconnect(mSensorFrameworkHandle);
262
263   mRollValues.push_back( base_data_values->values[0] );
264   mRollValues.pop_front();
265
266   mPitchValues.push_back( base_data_values->values[1] );
267   mPitchValues.pop_front();
268
269   free(base_data_values);
270   base_data_values = NULL;
271
272   float averageRoll( 0.0f );
273   for ( std::deque<float>::const_iterator iter = mRollValues.begin(); mRollValues.end() != iter; ++iter )
274   {
275     averageRoll += *iter;
276   }
277   averageRoll /= mRollValues.size();
278
279   float averagePitch( 0.0f );
280   for ( std::deque<float>::const_iterator iter = mPitchValues.begin(); mPitchValues.end() != iter; ++iter )
281   {
282     averagePitch += *iter;
283   }
284   averagePitch /= mPitchValues.size();
285
286   newRoll  = Clamp( float(averageRoll  / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
287   newPitch = Clamp( float(averagePitch / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
288
289   newRotation = Quaternion( newRoll  * Math::PI * -0.5f, Vector3::YAXIS ) *
290               Quaternion( newPitch * Math::PI * -0.5f, Vector3::XAXIS );
291 #endif // SENSOR_ENABLED
292
293   Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
294   // If the change in value is more than the threshold then emit tilted signal.
295   if( angle > mRotationThreshold )
296   {
297     mRoll = newRoll;
298     mPitch = newPitch;
299     mRotation = newRotation;
300
301     if ( !mTiltedSignal.Empty() )
302     {
303       Dali::TiltSensor handle( this );
304       mTiltedSignal.Emit( handle );
305     }
306   }
307
308   return true;
309 }
310
311 } // namespace Adaptor
312
313 } // namespace Internal
314
315 } // namespace Dali