12#include "tools/SO3Util.h"
14extern int SENSOR_DATA_CB;
16#if defined(OSX) || defined(APPLE_IOS)
18#include <boost/chrono/chrono.hpp>
20int64_t GetTickCountNano()
22 return boost::chrono::duration_cast<boost::chrono::nanoseconds>(boost::chrono::steady_clock::now().time_since_epoch()).count();
25int64_t GetTickCountNano()
27 int msec = GetTickCount();
28 return (int64_t)msec * (int64_t)1e-6;
31int64_t GetTickCountNano()
34 clock_gettime(CLOCK_MONOTONIC, &now);
35 return (int64_t)now.tv_sec * 1000000000LL + (int64_t)now.tv_nsec;
46 if (!mSingletonInstance)
49 mSingletonInstance =
new SASensorManager();
51 mSingletonInstance =
new SWSensorManager();
56 return mSingletonInstance;
59void SSensorManager::Kill(){
60 SAFE_DELETE(mSingletonInstance);
65SSensorCb::SSensorCb(SSensorType type): mType(type)
69SSensorCb::~SSensorCb()
74SSensorType SSensorCb::GetType()
83 mType = SSENSOR_TYPE_UNKNOWN;
89SSensor::SSensor(SSensorType type)
102SSensorType SSensor::GetType()
107bool SSensor::IsEnabled()
112void SSensor::SetEnabled(
bool enabled)
114 if (mEnabled != enabled)
121bool SSensor::IsPaused()
126void SSensor::SetPaused(
bool paused)
128 if (mPaused != paused)
138 mAxisCallbacks.push_back(sensorCb);
143void SSensor::RemoveCallBack(
SSensorCb* sensorCb)
148 mAxisCallbacks.remove(sensorCb);
149 SAFE_DELETE(sensorCb);
152double SSensor::GetElapsedTime(int64_t timeStamp)
155 if (mLastTimeStamp != 0)
157 delta = (timeStamp - mLastTimeStamp) / 1e9;
159 mLastTimeStamp = timeStamp;
165 for (
SSensorCb* sensorCb : mAxisCallbacks)
168 ndata->fval = data.fval;
170 ndata->delta = data.delta;
172 OBJpostEvent(SENSOR_DATA_CB, SCOL_PTR sensorCb, SCOL_PTR ndata);
179SSensorManager::SSensorManager()
184SSensorManager::~SSensorManager()
186 SAFE_DELETE(mOrientationTracker);
189bool SSensorManager::IsAvailable(SSensorType sensorType)
191 if (sensorType == SSENSOR_TYPE_ORIENTATION)
192 return IsAvailable(SSENSOR_TYPE_ACCELEROMETER) && IsAvailable(SSENSOR_TYPE_GYROSCOPE);
194 return (GetSensorByType(sensorType) != 0);
197bool SSensorManager::IsEnabled(SSensorType sensorType)
199 if (sensorType == SSENSOR_TYPE_ORIENTATION)
201 return IsEnabled(SSENSOR_TYPE_ACCELEROMETER) && IsEnabled(SSENSOR_TYPE_GYROSCOPE);
205 SSensor* sensor = GetSensorByType(sensorType);
206 return (sensor)? sensor->IsEnabled() :
false;
210SSensor* SSensorManager::GetSensorByType(SSensorType sensorType)
212 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
214 if ((*it) && (*it)->GetType() == sensorType)
222void SSensorManager::PauseSensorManager()
225 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
228 if (sensor && sensor->IsEnabled())
230 SetSensorEnable(sensor->GetType(),
false);
231 sensor->SetPaused(
true);
236void SSensorManager::ResumeSensorManager()
239 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
242 if (sensor && sensor->IsPaused())
244 SetSensorEnable(sensor->GetType(),
true);
245 sensor->SetPaused(
false);
250std::set<SSensor*> SSensorManager::GetSensorList()
257 return mOrientationTracker;
260Quaternion SSensorManager::GetDeviceOrientation(
double timeOffsetInSeconds)
262 if (mOrientationTracker && IsEnabled(SSENSOR_TYPE_ORIENTATION))
264 boost::shared_lock< boost::shared_mutex > lock(mGlobalMutex);
265 return mOrientationTracker->lastHeadView(timeOffsetInSeconds);
270 return Quaternion::IDENTITY;
274SSensorCb* SSensorManager::AddSensorCallBack(SSensorType sensorType)
276 SSensor* sensor = GetSensorByType(sensorType);
280 return sensor->AddCallBack();
283void SSensorManager::RemoveSensorCallBack(
SSensorCb* sensorCb)
288 SSensor* sensor = GetSensorByType(sensorCb->GetType());
292 sensor->RemoveCallBack(sensorCb);