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 char* const SIGNAL_TILTED = "tilted";
35 const int NUMBER_OF_SAMPLES = 10;
37 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
40 Dali::BaseHandle GetInstance()
42 return Dali::Internal::Adaptor::TiltSensor::Get();
45 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
47 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
49 } // unnamed namespace
60 Dali::TiltSensor TiltSensor::New()
62 Dali::TiltSensor sensor = Dali::TiltSensor(new TiltSensor());
67 Dali::TiltSensor TiltSensor::Get()
69 Dali::TiltSensor sensor;
71 Dali::SingletonService service( SingletonService::Get() );
74 // Check whether the keyboard focus manager is already created
75 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TiltSensor ) );
78 // If so, downcast the handle of singleton to keyboard focus manager
79 sensor = Dali::TiltSensor( dynamic_cast< TiltSensor* >( handle.GetObjectPtr() ) );
83 // Create a singleton instance
84 sensor = TiltSensor::New();
85 service.Register( typeid( sensor ), sensor );
93 TiltSensor::~TiltSensor()
98 bool TiltSensor::Start()
100 // Make sure sensor API is responding
101 bool success = Update();
107 mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
108 mTimer.TickSignal().Connect( mTimerSlot, &TiltSensor::Update );
112 !mTimer.IsRunning() )
121 void TiltSensor::Stop()
130 bool TiltSensor::IsStarted() const
132 return ( mTimer && mTimer.IsRunning() );
135 float TiltSensor::GetRoll() const
140 float TiltSensor::GetPitch() const
145 Quaternion TiltSensor::GetRotation() const
150 TiltSensor::TiltedSignalType& TiltSensor::TiltedSignal()
152 return mTiltedSignal;
155 void TiltSensor::SetUpdateFrequency( float frequencyHertz )
157 DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
159 if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
161 mFrequencyHertz = frequencyHertz;
165 mTimer.SetInterval( 1000.0f / mFrequencyHertz );
170 float TiltSensor::GetUpdateFrequency() const
172 return mFrequencyHertz;
175 void TiltSensor::SetRotationThreshold(Radian rotationThreshold)
177 mRotationThreshold = rotationThreshold;
180 Radian TiltSensor::GetRotationThreshold() const
182 return mRotationThreshold;
185 bool TiltSensor::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
187 bool connected( true );
188 TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
190 if( sensor && ( SIGNAL_TILTED == signalName ) )
192 sensor->TiltedSignal().Connect( tracker, functor );
196 // signalName does not match any signal
203 TiltSensor::TiltSensor()
204 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
206 mSensorFrameworkHandle( -1 ),
209 mRotation( Dali::ANGLE_0, Vector3::YAXIS ),
210 mRotationThreshold( 0.0f )
212 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
213 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
216 bool TiltSensor::Update()
218 float newRoll = 0.0f;
219 float newPitch = 0.0f;
220 Quaternion newRotation;
222 Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
223 // If the change in value is more than the threshold then emit tilted signal.
224 if( angle > mRotationThreshold )
228 mRotation = newRotation;
230 if ( !mTiltedSignal.Empty() )
232 Dali::TiltSensor handle( this );
233 mTiltedSignal.Emit( handle );
240 } // namespace Adaptor
242 } // namespace Internal