efe12e74c8aaf475f9eea273f7fa0b8a1d38ec75
[platform/core/uifw/dali-adaptor.git] / dali / internal / sensor / ubuntu / tilt-sensor-impl-ubuntu.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 <dali/internal/sensor/ubuntu/tilt-sensor-impl-ubuntu.h>
20 #include <dali/internal/sensor/common/tilt-sensor-factory.h>
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/system/common/singleton-service-impl.h>
28
29 namespace // unnamed namespace
30 {
31
32 const char* const SIGNAL_TILTED = "tilted";
33
34 const int NUMBER_OF_SAMPLES = 10;
35
36 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
37
38 // Type Registration
39 Dali::BaseHandle GetInstance()
40 {
41   return Dali::Internal::Adaptor::TiltSensorFactory::Get();
42 }
43
44 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
45
46 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
47
48 } // unnamed namespace
49
50 namespace Dali
51 {
52
53 namespace Internal
54 {
55
56 namespace Adaptor
57 {
58
59 TiltSensorUbuntu* TiltSensorUbuntu::New()
60 {
61   return new TiltSensorUbuntu();
62 }
63
64 TiltSensorUbuntu::~TiltSensorUbuntu()
65 {
66   Stop();
67 }
68
69 bool TiltSensorUbuntu::Start()
70 {
71   // Make sure sensor API is responding
72   bool success = Update();
73
74   if ( success )
75   {
76     if ( !mTimer )
77     {
78       mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
79       mTimer.TickSignal().Connect( mTimerSlot, &TiltSensorUbuntu::Update );
80     }
81
82     if ( mTimer &&
83          !mTimer.IsRunning() )
84     {
85       mTimer.Start();
86     }
87   }
88
89   return success;
90 }
91
92 void TiltSensorUbuntu::Stop()
93 {
94   if ( mTimer )
95   {
96     mTimer.Stop();
97     mTimer.Reset();
98   }
99 }
100
101 bool TiltSensorUbuntu::IsStarted() const
102 {
103   return ( mTimer && mTimer.IsRunning() );
104 }
105
106 float TiltSensorUbuntu::GetRoll() const
107 {
108   return mRoll;
109 }
110
111 float TiltSensorUbuntu::GetPitch() const
112 {
113   return mPitch;
114 }
115
116 Quaternion TiltSensorUbuntu::GetRotation() const
117 {
118   return mRotation;
119 }
120
121 TiltSensor::TiltedSignalType& TiltSensorUbuntu::TiltedSignal()
122 {
123   return mTiltedSignal;
124 }
125
126 void TiltSensorUbuntu::SetUpdateFrequency( float frequencyHertz )
127 {
128   DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
129
130   if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
131   {
132     mFrequencyHertz = frequencyHertz;
133
134     if ( mTimer )
135     {
136       mTimer.SetInterval( 1000.0f / mFrequencyHertz );
137     }
138   }
139 }
140
141 float TiltSensorUbuntu::GetUpdateFrequency() const
142 {
143   return mFrequencyHertz;
144 }
145
146 void TiltSensorUbuntu::SetRotationThreshold(Radian rotationThreshold)
147 {
148   mRotationThreshold = rotationThreshold;
149 }
150
151 Radian TiltSensorUbuntu::GetRotationThreshold() const
152 {
153   return mRotationThreshold;
154 }
155
156 bool TiltSensorUbuntu::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
157 {
158   bool connected( true );
159   TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
160
161   if( sensor && ( SIGNAL_TILTED == signalName ) )
162   {
163     sensor->TiltedSignal().Connect( tracker, functor );
164   }
165   else
166   {
167     // signalName does not match any signal
168     connected = false;
169   }
170
171   return connected;
172 }
173
174 TiltSensorUbuntu::TiltSensorUbuntu()
175 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
176   mTimerSlot( this ),
177   mSensorFrameworkHandle( -1 ),
178   mRoll( 0.0f ),
179   mPitch( 0.0f ),
180   mRotation( Dali::ANGLE_0, Vector3::YAXIS ),
181   mRotationThreshold( 0.0f )
182 {
183   mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
184   mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
185 }
186
187 bool TiltSensorUbuntu::Update()
188 {
189   float newRoll = 0.0f;
190   float newPitch = 0.0f;
191   Quaternion newRotation;
192
193   Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
194   // If the change in value is more than the threshold then emit tilted signal.
195   if( angle > mRotationThreshold )
196   {
197     mRoll = newRoll;
198     mPitch = newPitch;
199     mRotation = newRotation;
200
201     if ( !mTiltedSignal.Empty() )
202     {
203       Dali::TiltSensor handle( this );
204       mTiltedSignal.Emit( handle );
205     }
206   }
207
208   return true;
209 }
210
211 } // namespace Adaptor
212
213 } // namespace Internal
214
215 } // namespace Dali