2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "tilt-sensor-impl.h"
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/integration-api/debug.h>
28 #include <singleton-service-impl.h>
30 namespace // unnamed namespace
33 const int NUMBER_OF_SAMPLES = 10;
35 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
38 Dali::BaseHandle Create()
40 return Dali::Internal::Adaptor::TiltSensor::Get();
43 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), Create );
45 Dali::SignalConnectorType signalConnector1( typeRegistration, Dali::TiltSensor::SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
47 } // unnamed namespace
58 Dali::TiltSensor TiltSensor::New()
60 Dali::TiltSensor sensor = Dali::TiltSensor(new TiltSensor());
65 Dali::TiltSensor TiltSensor::Get()
67 Dali::TiltSensor sensor;
69 Dali::SingletonService service( SingletonService::Get() );
72 // Check whether the keyboard focus manager is already created
73 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TiltSensor ) );
76 // If so, downcast the handle of singleton to keyboard focus manager
77 sensor = Dali::TiltSensor( dynamic_cast< TiltSensor* >( handle.GetObjectPtr() ) );
81 // Create a singleton instance
82 sensor = TiltSensor::New();
83 service.Register( typeid( sensor ), sensor );
91 TiltSensor::~TiltSensor()
96 bool TiltSensor::Enable()
98 // Make sure sensor API is responding
99 bool success = Update();
105 mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
106 mTimer.TickSignal().Connect( mTimerSlot, &TiltSensor::Update );
110 !mTimer.IsRunning() )
119 void TiltSensor::Disable()
128 bool TiltSensor::IsEnabled() const
130 return ( mTimer && mTimer.IsRunning() );
133 float TiltSensor::GetRoll() const
138 float TiltSensor::GetPitch() const
143 Quaternion TiltSensor::GetRotation() const
148 TiltSensor::TiltedSignalV2& TiltSensor::TiltedSignal()
150 return mTiltedSignalV2;
153 void TiltSensor::SetUpdateFrequency( float frequencyHertz )
155 DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
157 if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
159 mFrequencyHertz = frequencyHertz;
163 mTimer.SetInterval( 1000.0f / mFrequencyHertz );
168 float TiltSensor::GetUpdateFrequency() const
170 return mFrequencyHertz;
173 void TiltSensor::SetRotationThreshold(Radian rotationThreshold)
175 mRotationThreshold = rotationThreshold;
178 Radian TiltSensor::GetRotationThreshold() const
180 return mRotationThreshold;
183 bool TiltSensor::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
185 bool connected( true );
186 TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
189 Dali::TiltSensor::SIGNAL_TILTED == signalName )
191 sensor->TiltedSignal().Connect( tracker, functor );
195 // signalName does not match any signal
202 TiltSensor::TiltSensor()
203 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
205 mSensorFrameworkHandle( -1 ),
208 mRotation( 0.0f, Vector3::YAXIS ),
209 mRotationThreshold( 0.0f )
211 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
212 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
215 bool TiltSensor::Update()
217 float newRoll = 0.0f;
218 float newPitch = 0.0f;
219 Quaternion newRotation;
221 Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
222 // If the change in value is more than the threshold then emit tilted signal.
223 if( angle > mRotationThreshold )
227 mRotation = newRotation;
229 if ( !mTiltedSignalV2.Empty() )
231 Dali::TiltSensor handle( this );
232 mTiltedSignalV2.Emit( handle );
239 } // namespace Adaptor
241 } // namespace Internal