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"
23 #ifdef DALI_PROFILE_LITE
24 #include <sensor_internal.h>
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/integration-api/debug.h>
33 #include <singleton-service-impl.h>
36 #define SENSOR_ENABLED
39 namespace // unnamed namespace
42 const char* const SIGNAL_TILTED = "tilted";
44 const int NUMBER_OF_SAMPLES = 10;
46 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
49 Dali::BaseHandle GetInstance()
51 return Dali::Internal::Adaptor::TiltSensor::Get();
54 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), GetInstance );
56 Dali::SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal );
58 } // unnamed namespace
69 Dali::TiltSensor TiltSensor::New()
71 Dali::TiltSensor sensor = Dali::TiltSensor(new TiltSensor());
76 Dali::TiltSensor TiltSensor::Get()
78 Dali::TiltSensor sensor;
80 Dali::SingletonService service( SingletonService::Get() );
83 // Check whether the keyboard focus manager is already created
84 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TiltSensor ) );
87 // If so, downcast the handle of singleton to keyboard focus manager
88 sensor = Dali::TiltSensor( dynamic_cast< TiltSensor* >( handle.GetObjectPtr() ) );
92 // Create a singleton instance
93 sensor = TiltSensor::New();
94 service.Register( typeid( sensor ), sensor );
102 TiltSensor::~TiltSensor()
107 bool TiltSensor::Enable()
109 // Make sure sensor API is responding
110 bool success = Update();
116 mTimer = Dali::Timer::New( 1000.0f / mFrequencyHertz );
117 mTimer.TickSignal().Connect( mTimerSlot, &TiltSensor::Update );
121 !mTimer.IsRunning() )
130 void TiltSensor::Disable()
139 bool TiltSensor::IsEnabled() const
141 return ( mTimer && mTimer.IsRunning() );
144 float TiltSensor::GetRoll() const
149 float TiltSensor::GetPitch() const
154 Quaternion TiltSensor::GetRotation() const
159 TiltSensor::TiltedSignalV2& TiltSensor::TiltedSignal()
161 return mTiltedSignalV2;
164 void TiltSensor::SetUpdateFrequency( float frequencyHertz )
166 DALI_ASSERT_ALWAYS( frequencyHertz > 0.0f && "Frequency must have a positive value" );
168 if ( fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz) )
170 mFrequencyHertz = frequencyHertz;
174 mTimer.SetInterval( 1000.0f / mFrequencyHertz );
179 float TiltSensor::GetUpdateFrequency() const
181 return mFrequencyHertz;
184 void TiltSensor::SetRotationThreshold(Radian rotationThreshold)
186 mRotationThreshold = rotationThreshold;
189 Radian TiltSensor::GetRotationThreshold() const
191 return mRotationThreshold;
194 bool TiltSensor::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
196 bool connected( true );
197 TiltSensor* sensor = dynamic_cast<TiltSensor*>( object );
199 if( sensor && ( SIGNAL_TILTED == signalName ) )
201 sensor->TiltedSignal().Connect( tracker, functor );
205 // signalName does not match any signal
212 TiltSensor::TiltSensor()
213 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
215 mSensorFrameworkHandle( -1 ),
218 mRotation( 0.0f, Vector3::YAXIS ),
219 mRotationThreshold( 0.0f )
221 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
222 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
225 bool TiltSensor::Update()
227 float newRoll = 0.0f;
228 float newPitch = 0.0f;
229 Quaternion newRotation;
230 #ifdef SENSOR_ENABLED
232 // Read accelerometer data
234 mSensorFrameworkHandle = sf_connect( ACCELEROMETER_SENSOR );
235 if ( mSensorFrameworkHandle < 0 )
237 DALI_LOG_ERROR( "Failed to connect to sensor framework" );
241 if ( sf_start(mSensorFrameworkHandle, 0) < 0 )
243 DALI_LOG_ERROR( "Failed to start sensor" );
244 sf_disconnect(mSensorFrameworkHandle);
248 sensor_data_t* base_data_values = (sensor_data_t*)malloc(sizeof(sensor_data_t));
250 int dataErr = sf_get_data(mSensorFrameworkHandle, ACCELEROMETER_BASE_DATA_SET, base_data_values);
253 DALI_LOG_ERROR( "Failed to retrieve sensor data" );
254 free(base_data_values);
255 sf_stop(mSensorFrameworkHandle);
256 sf_disconnect(mSensorFrameworkHandle);
260 sf_stop(mSensorFrameworkHandle);
261 sf_disconnect(mSensorFrameworkHandle);
263 mRollValues.push_back( base_data_values->values[0] );
264 mRollValues.pop_front();
266 mPitchValues.push_back( base_data_values->values[1] );
267 mPitchValues.pop_front();
269 free(base_data_values);
270 base_data_values = NULL;
272 float averageRoll( 0.0f );
273 for ( std::deque<float>::const_iterator iter = mRollValues.begin(); mRollValues.end() != iter; ++iter )
275 averageRoll += *iter;
277 averageRoll /= mRollValues.size();
279 float averagePitch( 0.0f );
280 for ( std::deque<float>::const_iterator iter = mPitchValues.begin(); mPitchValues.end() != iter; ++iter )
282 averagePitch += *iter;
284 averagePitch /= mPitchValues.size();
286 newRoll = Clamp( float(averageRoll / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
287 newPitch = Clamp( float(averagePitch / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
289 newRotation = Quaternion( newRoll * Math::PI * -0.5f, Vector3::YAXIS ) *
290 Quaternion( newPitch * Math::PI * -0.5f, Vector3::XAXIS );
291 #endif // SENSOR_ENABLED
293 Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
294 // If the change in value is more than the threshold then emit tilted signal.
295 if( angle > mRotationThreshold )
299 mRotation = newRotation;
301 if ( !mTiltedSignalV2.Empty() )
303 Dali::TiltSensor handle( this );
304 mTiltedSignalV2.Emit( handle );
311 } // namespace Adaptor
313 } // namespace Internal