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