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 <sensor_internal.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/integration-api/debug.h>
31 #include <singleton-service-impl.h>
33 namespace // unnamed namespace
36 const char* const SIGNAL_TILTED = "tilted";
38 const int NUMBER_OF_SAMPLES = 10;
40 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
43 Dali::BaseHandle GetInstance()
45 return Dali::Internal::Adaptor::TiltSensor::Get();
48 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
50 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
52 } // unnamed namespace
63 Dali::TiltSensor TiltSensor::New()
65 Dali::TiltSensor sensor = Dali::TiltSensor(new TiltSensor());
70 Dali::TiltSensor TiltSensor::Get()
72 Dali::TiltSensor sensor;
74 Dali::SingletonService service( SingletonService::Get() );
77 // Check whether the keyboard focus manager is already created
78 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TiltSensor ) );
81 // If so, downcast the handle of singleton to keyboard focus manager
82 sensor = Dali::TiltSensor( dynamic_cast< TiltSensor* >( handle.GetObjectPtr() ) );
86 // Create a singleton instance
87 sensor = TiltSensor::New();
88 service.Register( typeid( sensor ), sensor );
96 TiltSensor::~TiltSensor()
101 bool TiltSensor::Enable()
103 // Make sure sensor API is responding
104 bool success = Update();
110 mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
111 mTimer.TickSignal().Connect( mTimerSlot, &TiltSensor::Update );
115 !mTimer.IsRunning() )
124 void TiltSensor::Disable()
133 bool TiltSensor::IsEnabled() const
135 return ( mTimer && mTimer.IsRunning() );
138 float TiltSensor::GetRoll() const
143 float TiltSensor::GetPitch() const
148 Quaternion TiltSensor::GetRotation() const
153 TiltSensor::TiltedSignalType& TiltSensor::TiltedSignal()
155 return mTiltedSignal;
158 void TiltSensor::SetUpdateFrequency( float frequencyHertz )
160 DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
162 if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
164 mFrequencyHertz = frequencyHertz;
168 mTimer.SetInterval( 1000.0f / mFrequencyHertz );
173 float TiltSensor::GetUpdateFrequency() const
175 return mFrequencyHertz;
178 void TiltSensor::SetRotationThreshold(Radian rotationThreshold)
180 mRotationThreshold = rotationThreshold;
183 Radian TiltSensor::GetRotationThreshold() const
185 return mRotationThreshold;
188 bool TiltSensor::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
190 bool connected( true );
191 TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
193 if( sensor && ( SIGNAL_TILTED == signalName ) )
195 sensor->TiltedSignal().Connect( tracker, functor );
199 // signalName does not match any signal
206 TiltSensor::TiltSensor()
207 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
209 mSensorFrameworkHandle( -1 ),
212 mRotation( 0.0f, Vector3::YAXIS ),
213 mRotationThreshold( 0.0f )
215 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
216 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
219 bool TiltSensor::Update()
221 float newRoll = 0.0f;
222 float newPitch = 0.0f;
223 Quaternion newRotation;
224 #ifdef SENSOR_ENABLED
226 // Read accelerometer data
228 mSensorFrameworkHandle = sf_connect( ACCELEROMETER_SENSOR );
229 if ( mSensorFrameworkHandle < 0 )
231 DALI_LOG_ERROR( "Failed to connect to sensor framework" );
235 if ( sf_start(mSensorFrameworkHandle, 0) < 0 )
237 DALI_LOG_ERROR( "Failed to start sensor" );
238 sf_disconnect(mSensorFrameworkHandle);
242 sensor_data_t* base_data_values = (sensor_data_t*)malloc(sizeof(sensor_data_t));
244 int dataErr = sf_get_data(mSensorFrameworkHandle, ACCELEROMETER_BASE_DATA_SET, base_data_values);
247 DALI_LOG_ERROR( "Failed to retrieve sensor data" );
248 free(base_data_values);
249 sf_stop(mSensorFrameworkHandle);
250 sf_disconnect(mSensorFrameworkHandle);
254 sf_stop(mSensorFrameworkHandle);
255 sf_disconnect(mSensorFrameworkHandle);
257 mRollValues.push_back( base_data_values->values[0] );
258 mRollValues.pop_front();
260 mPitchValues.push_back( base_data_values->values[1] );
261 mPitchValues.pop_front();
263 free(base_data_values);
264 base_data_values = NULL;
266 float averageRoll( 0.0f );
267 for ( std::deque<float>::const_iterator iter = mRollValues.begin(); mRollValues.end() != iter; ++iter )
269 averageRoll += *iter;
271 averageRoll /= mRollValues.size();
273 float averagePitch( 0.0f );
274 for ( std::deque<float>::const_iterator iter = mPitchValues.begin(); mPitchValues.end() != iter; ++iter )
276 averagePitch += *iter;
278 averagePitch /= mPitchValues.size();
280 newRoll = Clamp( float(averageRoll / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
281 newPitch = Clamp( float(averagePitch / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
283 newRotation = Quaternion( newRoll * Math::PI * -0.5f, Vector3::YAXIS ) *
284 Quaternion( newPitch * Math::PI * -0.5f, Vector3::XAXIS );
285 #endif // SENSOR_ENABLED
287 Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
288 // If the change in value is more than the threshold then emit tilted signal.
289 if( angle > mRotationThreshold )
293 mRotation = newRotation;
295 if ( !mTiltedSignal.Empty() )
297 Dali::TiltSensor handle( this );
298 mTiltedSignal.Emit( handle );
305 } // namespace Adaptor
307 } // namespace Internal