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 <dali/internal/sensor/ubuntu/tilt-sensor-impl-ubuntu.h>
20 #include <dali/internal/sensor/common/tilt-sensor-factory.h>
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>
27 namespace // unnamed namespace
30 const char* const SIGNAL_TILTED = "tilted";
32 const int NUMBER_OF_SAMPLES = 10;
34 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
37 Dali::BaseHandle GetInstance()
39 return Dali::Internal::Adaptor::TiltSensorFactory::Get();
42 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
44 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
46 } // unnamed namespace
57 TiltSensorUbuntu* TiltSensorUbuntu::New()
59 return new TiltSensorUbuntu();
62 TiltSensorUbuntu::~TiltSensorUbuntu()
67 bool TiltSensorUbuntu::Start()
69 // Make sure sensor API is responding
70 bool success = Update();
76 mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
77 mTimer.TickSignal().Connect( mTimerSlot, &TiltSensorUbuntu::Update );
90 void TiltSensorUbuntu::Stop()
99 bool TiltSensorUbuntu::IsStarted() const
101 return ( mTimer && mTimer.IsRunning() );
104 float TiltSensorUbuntu::GetRoll() const
109 float TiltSensorUbuntu::GetPitch() const
114 Quaternion TiltSensorUbuntu::GetRotation() const
119 TiltSensor::TiltedSignalType& TiltSensorUbuntu::TiltedSignal()
121 return mTiltedSignal;
124 void TiltSensorUbuntu::SetUpdateFrequency( float frequencyHertz )
126 DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
128 if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
130 mFrequencyHertz = frequencyHertz;
134 mTimer.SetInterval( 1000.0f / mFrequencyHertz );
139 float TiltSensorUbuntu::GetUpdateFrequency() const
141 return mFrequencyHertz;
144 void TiltSensorUbuntu::SetRotationThreshold(Radian rotationThreshold)
146 mRotationThreshold = rotationThreshold;
149 Radian TiltSensorUbuntu::GetRotationThreshold() const
151 return mRotationThreshold;
154 bool TiltSensorUbuntu::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
156 bool connected( true );
157 TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
159 if( sensor && ( SIGNAL_TILTED == signalName ) )
161 sensor->TiltedSignal().Connect( tracker, functor );
165 // signalName does not match any signal
172 TiltSensorUbuntu::TiltSensorUbuntu()
173 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
175 mSensorFrameworkHandle( -1 ),
178 mRotation( Dali::ANGLE_0, Vector3::YAXIS ),
179 mRotationThreshold( 0.0f )
181 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
182 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
185 bool TiltSensorUbuntu::Update()
187 float newRoll = 0.0f;
188 float newPitch = 0.0f;
189 Quaternion newRotation;
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 )
197 mRotation = newRotation;
199 if ( !mTiltedSignal.Empty() )
201 Dali::TiltSensor handle( this );
202 mTiltedSignal.Emit( handle );
209 } // namespace Adaptor
211 } // namespace Internal