2 * Copyright (c) 2021 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/common/tilt-sensor-factory.h>
20 #include <dali/internal/sensor/tizen/tilt-sensor-impl-tizen.h>
23 #include <dali/devel-api/common/singleton-service.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/object/type-registry.h>
28 namespace // unnamed namespace
30 const char* const SIGNAL_TILTED = "tilted";
32 const float MAX_ORIENTATION_ROLL_VALUE = 90.f;
33 const float MAX_ORIENTATION_PITCH_VALUE = 180.f;
34 const float MAX_ACCELEROMETER_VALUE = 9.8f;
37 Dali::BaseHandle Create()
39 return Dali::Internal::Adaptor::TiltSensorFactory::Get();
42 Dali::TypeRegistration typeRegistration(typeid(Dali::TiltSensor), typeid(Dali::BaseHandle), Create);
44 Dali::SignalConnectorType signalConnector1(typeRegistration, SIGNAL_TILTED, Dali::Internal::Adaptor::TiltSensor::DoConnectSignal);
46 } // unnamed namespace
55 static void sensor_changed_cb(sensor_h sensor, sensor_event_s* event, void* user_data)
57 TiltSensorTizen* tiltSensor = reinterpret_cast<TiltSensorTizen*>(user_data);
61 tiltSensor->Update(event);
67 static std::string get_sensor_error_string(int errorValue)
73 case SENSOR_ERROR_IO_ERROR:
74 ret = "SENSOR_ERROR_IO_ERROR";
76 case SENSOR_ERROR_INVALID_PARAMETER:
77 ret = "SENSOR_ERROR_INVALID_PARAMETER";
79 case SENSOR_ERROR_NOT_SUPPORTED:
80 ret = "SENSOR_ERROR_NOT_SUPPORTED";
82 case SENSOR_ERROR_PERMISSION_DENIED:
83 ret = "SENSOR_ERROR_PERMISSION_DENIED";
85 case SENSOR_ERROR_OUT_OF_MEMORY:
86 ret = "SENSOR_ERROR_OUT_OF_MEMORY";
88 case SENSOR_ERROR_NOT_NEED_CALIBRATION:
89 ret = "SENSOR_ERROR_NOT_NEED_CALIBRATION";
91 case SENSOR_ERROR_OPERATION_FAILED:
92 ret = "SENSOR_ERROR_OPERATION_FAILED";
100 TiltSensorTizen* TiltSensorTizen::New()
102 return new TiltSensorTizen();
105 TiltSensorTizen::~TiltSensorTizen()
110 bool TiltSensorTizen::Connect()
112 #ifdef SENSOR_ENABLED
113 if(mState != DISCONNECTED)
119 const int interval = 1000 / mFrequencyHertz;
122 bool isSupported = false;
124 // try to use Orientation sensor at first for less power consumption.
125 ret = sensor_is_supported(SENSOR_ORIENTATION, &isSupported);
129 DALI_LOG_ERROR("sensor_is_supported() failed : %s\n", get_sensor_error_string(ret).c_str());
133 if(isSupported == true)
135 mSensorType = SENSOR_ORIENTATION;
139 DALI_LOG_ERROR("sensor does not support SENSOR_ORIENTATION\n");
141 ret = sensor_is_supported(SENSOR_ACCELEROMETER, &isSupported);
145 DALI_LOG_ERROR("sensor_is_supported() failed : %s\n", get_sensor_error_string(ret).c_str());
149 if(isSupported == false)
151 DALI_LOG_ERROR("sensor does not support both SENSOR_ORIENTATION and SENSOR_ACCELEROMETER\n");
155 mSensorType = SENSOR_ACCELEROMETER;
158 ret = sensor_get_default_sensor(mSensorType, &mSensor); /* mSensor should not be deleted */
162 DALI_LOG_ERROR("sensor_get_default_sensor() failed : %s\n", get_sensor_error_string(ret).c_str());
166 ret = sensor_create_listener(mSensor, &mSensorListener);
170 DALI_LOG_ERROR("sensor_create_listener() failed : %s\n", get_sensor_error_string(ret).c_str());
174 sensor_listener_set_event_cb(mSensorListener, interval, sensor_changed_cb, this);
175 sensor_listener_set_interval(mSensorListener, interval);
177 sensor_listener_set_option(mSensorListener, SENSOR_OPTION_DEFAULT /* Not receive data when LCD is off and in power save mode */);
187 void TiltSensorTizen::Disconnect()
191 if(mState == STARTED)
196 if(mState == STOPPED || mState == CONNECTED)
198 #ifdef SENSOR_ENABLED
199 sensor_listener_unset_event_cb(mSensorListener);
200 sensor_listener_stop(mSensorListener);
201 sensor_destroy_listener(mSensorListener);
204 mSensorListener = NULL;
205 mState = DISCONNECTED;
210 bool TiltSensorTizen::Start()
212 if(mSensorListener && (mState == CONNECTED || mState == STOPPED))
214 #ifdef SENSOR_ENABLED
216 ret = sensor_listener_start(mSensorListener);
217 if(ret != SENSOR_ERROR_NONE)
219 DALI_LOG_ERROR("sensor_listener_start() failed : %s\n", get_sensor_error_string(ret).c_str());
230 if(mState == STARTED)
232 DALI_LOG_ERROR("TiltSensor is already started. Current state [%d]\n", mState);
236 // mState is DISCONNECTED
237 DALI_LOG_ERROR("TiltSensor is disconnected. Current state [%d]\n", mState);
244 void TiltSensorTizen::Stop()
246 #ifdef SENSOR_ENABLED
247 if(mSensorListener && mState == STARTED)
249 sensor_listener_stop(mSensorListener);
255 bool TiltSensorTizen::IsStarted() const
257 return (mSensorListener && mState == STARTED);
260 float TiltSensorTizen::GetRoll() const
265 float TiltSensorTizen::GetPitch() const
270 Quaternion TiltSensorTizen::GetRotation() const
275 TiltSensor::TiltedSignalType& TiltSensorTizen::TiltedSignal()
277 return mTiltedSignal;
280 void TiltSensorTizen::SetUpdateFrequency(float frequencyHertz)
282 DALI_ASSERT_ALWAYS(frequencyHertz > 0.0f && "Frequency must have a positive value");
284 if(fabsf(mFrequencyHertz - frequencyHertz) >= GetRangedEpsilon(mFrequencyHertz, frequencyHertz))
286 mFrequencyHertz = frequencyHertz;
288 #ifdef SENSOR_ENABLED
291 const int interval = 1000 / mFrequencyHertz;
292 sensor_listener_set_interval(mSensorListener, interval);
298 float TiltSensorTizen::GetUpdateFrequency() const
300 return mFrequencyHertz;
303 void TiltSensorTizen::SetRotationThreshold(Radian rotationThreshold)
305 mRotationThreshold = rotationThreshold;
308 Radian TiltSensorTizen::GetRotationThreshold() const
310 return mRotationThreshold;
313 bool TiltSensorTizen::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
315 bool connected(true);
316 TiltSensor* sensor = dynamic_cast<TiltSensor*>(object);
318 if(sensor && SIGNAL_TILTED == signalName)
320 sensor->TiltedSignal().Connect(tracker, functor);
324 // signalName does not match any signal
331 TiltSensorTizen::TiltSensorTizen()
332 : mState(DISCONNECTED),
333 mFrequencyHertz(Dali::TiltSensor::DEFAULT_UPDATE_FREQUENCY),
335 mSensorListener(NULL),
338 mRotation(Radian(0.0f), Vector3::YAXIS),
339 mRotationThreshold(0.0f)
345 #ifdef SENSOR_ENABLED
346 void TiltSensorTizen::Update(sensor_event_s* event)
348 Radian newRoll(0.0f);
349 Radian newPitch(0.0f);
350 Quaternion newRotation;
352 if(mSensorType == SENSOR_ORIENTATION)
354 newRoll = Clamp(float(event->values[2] / MAX_ORIENTATION_ROLL_VALUE) /* -90 < roll < 90 */, -1.0f /*min*/, 1.0f /*max*/);
355 newPitch = Clamp(float(event->values[1] / MAX_ORIENTATION_PITCH_VALUE) /* -180 < pitch < 180 */, -1.0f /*min*/, 1.0f /*max*/);
357 else if(mSensorType == SENSOR_ACCELEROMETER)
359 newRoll = Clamp(float(event->values[0] / MAX_ACCELEROMETER_VALUE), -1.0f /*min*/, 1.0f /*max*/);
360 newPitch = Clamp(float(event->values[1] / MAX_ACCELEROMETER_VALUE), -1.0f /*min*/, 1.0f /*max*/);
364 DALI_LOG_ERROR("Invalid sensor type\n");
368 newRotation = Quaternion(Radian(newRoll * Math::PI * -0.5f), Vector3::YAXIS) *
369 Quaternion(Radian(newPitch * Math::PI * -0.5f), Vector3::XAXIS);
371 Radian angle(Quaternion::AngleBetween(newRotation, mRotation));
373 // If the change in value is more than the threshold then emit tilted signal.
374 if(angle >= mRotationThreshold)
378 mRotation = newRotation;
381 if(!mTiltedSignal.Empty())
383 Dali::TiltSensor handle(this);
384 mTiltedSignal.Emit(handle);
388 #endif // SENSOR_ENABLED
390 } // namespace Adaptor
392 } // namespace Internal