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 #ifdef UNDER_TIZEN_2_3_CAPI
27 #include <sensor_internal.h>
31 #include <dali/public-api/object/type-registry.h>
32 #include <dali/integration-api/debug.h>
35 #include <singleton-service-impl.h>
38 #define SENSOR_ENABLED
41 namespace // unnamed namespace
44 const int NUMBER_OF_SAMPLES = 10;
46 const float MAX_ACCELEROMETER_XY_VALUE = 9.8f;
49 Dali::BaseHandle Create()
51 return Dali::Internal::Adaptor::TiltSensor::Get();
54 Dali::TypeRegistration typeRegistration( typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), Create );
56 Dali::SignalConnectorType signalConnector1( typeRegistration, Dali::TiltSensor::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 );
200 Dali::TiltSensor::SIGNAL_TILTED == signalName )
202 sensor->TiltedSignal().Connect( tracker, functor );
206 // signalName does not match any signal
213 TiltSensor::TiltSensor()
214 : mFrequencyHertz( Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY ),
216 mSensorFrameworkHandle( -1 ),
219 mRotation( 0.0f, Vector3::YAXIS ),
220 mRotationThreshold( 0.0f )
222 mRollValues.resize( NUMBER_OF_SAMPLES, 0.0f );
223 mPitchValues.resize( NUMBER_OF_SAMPLES, 0.0f );
226 bool TiltSensor::Update()
228 float newRoll = 0.0f;
229 float newPitch = 0.0f;
230 Quaternion newRotation;
231 #ifdef SENSOR_ENABLED
233 // Read accelerometer data
235 mSensorFrameworkHandle = sf_connect( ACCELEROMETER_SENSOR );
236 if ( mSensorFrameworkHandle < 0 )
238 DALI_LOG_ERROR( "Failed to connect to sensor framework" );
242 if ( sf_start(mSensorFrameworkHandle, 0) < 0 )
244 DALI_LOG_ERROR( "Failed to start sensor" );
245 sf_disconnect(mSensorFrameworkHandle);
249 sensor_data_t* base_data_values = (sensor_data_t*)malloc(sizeof(sensor_data_t));
251 int dataErr = sf_get_data(mSensorFrameworkHandle, ACCELEROMETER_BASE_DATA_SET, base_data_values);
254 DALI_LOG_ERROR( "Failed to retrieve sensor data" );
255 free(base_data_values);
256 sf_stop(mSensorFrameworkHandle);
257 sf_disconnect(mSensorFrameworkHandle);
261 sf_stop(mSensorFrameworkHandle);
262 sf_disconnect(mSensorFrameworkHandle);
264 mRollValues.push_back( base_data_values->values[0] );
265 mRollValues.pop_front();
267 mPitchValues.push_back( base_data_values->values[1] );
268 mPitchValues.pop_front();
270 free(base_data_values);
271 base_data_values = NULL;
273 float averageRoll( 0.0f );
274 for ( std::deque<float>::const_iterator iter = mRollValues.begin(); mRollValues.end() != iter; ++iter )
276 averageRoll += *iter;
278 averageRoll /= mRollValues.size();
280 float averagePitch( 0.0f );
281 for ( std::deque<float>::const_iterator iter = mPitchValues.begin(); mPitchValues.end() != iter; ++iter )
283 averagePitch += *iter;
285 averagePitch /= mPitchValues.size();
287 newRoll = Clamp( float(averageRoll / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
288 newPitch = Clamp( float(averagePitch / MAX_ACCELEROMETER_XY_VALUE), -1.0f/*min*/, 1.0f/*max*/ );
290 newRotation = Quaternion( newRoll * Math::PI * -0.5f, Vector3::YAXIS ) *
291 Quaternion( newPitch * Math::PI * -0.5f, Vector3::XAXIS );
292 #endif // SENSOR_ENABLED
294 Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
295 // If the change in value is more than the threshold then emit tilted signal.
296 if( angle > mRotationThreshold )
300 mRotation = newRotation;
302 if ( !mTiltedSignalV2.Empty() )
304 Dali::TiltSensor handle( this );
305 mTiltedSignalV2.Emit( handle );
312 } // namespace Adaptor
314 } // namespace Internal