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>
27 #include <dali/internal/system/common/singleton-service-impl.h>
29 namespace // unnamed namespace
32 const char* const SIGNAL_TILTED = "tilted";
34 const int NUMBER_OF_SAMPLES = 10;
36 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
39 Dali::BaseHandle GetInstance()
41 return Dali::Internal::Adaptor::TiltSensorFactory::Get();
44 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
46 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
48 } // unnamed namespace
59 TiltSensorUbuntu* TiltSensorUbuntu::New()
61 return new TiltSensorUbuntu();
64 TiltSensorUbuntu::~TiltSensorUbuntu()
69 bool TiltSensorUbuntu::Start()
71 // Make sure sensor API is responding
72 bool success = Update();
78 mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
79 mTimer.TickSignal().Connect( mTimerSlot, &TiltSensorUbuntu::Update );
92 void TiltSensorUbuntu::Stop()
101 bool TiltSensorUbuntu::IsStarted() const
103 return ( mTimer && mTimer.IsRunning() );
106 float TiltSensorUbuntu::GetRoll() const
111 float TiltSensorUbuntu::GetPitch() const
116 Quaternion TiltSensorUbuntu::GetRotation() const
121 TiltSensor::TiltedSignalType& TiltSensorUbuntu::TiltedSignal()
123 return mTiltedSignal;
126 void TiltSensorUbuntu::SetUpdateFrequency( float frequencyHertz )
128 DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
130 if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
132 mFrequencyHertz = frequencyHertz;
136 mTimer.SetInterval( 1000.0f / mFrequencyHertz );
141 float TiltSensorUbuntu::GetUpdateFrequency() const
143 return mFrequencyHertz;
146 void TiltSensorUbuntu::SetRotationThreshold(Radian rotationThreshold)
148 mRotationThreshold = rotationThreshold;
151 Radian TiltSensorUbuntu::GetRotationThreshold() const
153 return mRotationThreshold;
156 bool TiltSensorUbuntu::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
158 bool connected( true );
159 TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
161 if( sensor && ( SIGNAL_TILTED == signalName ) )
163 sensor->TiltedSignal().Connect( tracker, functor );
167 // signalName does not match any signal
174 TiltSensorUbuntu::TiltSensorUbuntu()
175 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
177 mSensorFrameworkHandle( -1 ),
180 mRotation( Dali::ANGLE_0, Vector3::YAXIS ),
181 mRotationThreshold( 0.0f )
183 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
184 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
187 bool TiltSensorUbuntu::Update()
189 float newRoll = 0.0f;
190 float newPitch = 0.0f;
191 Quaternion newRotation;
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 )
199 mRotation = newRotation;
201 if ( !mTiltedSignal.Empty() )
203 Dali::TiltSensor handle( this );
204 mTiltedSignal.Emit( handle );
211 } // namespace Adaptor
213 } // namespace Internal