2013-04-26 17:48:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
Copyright (c) 2013 Lee, Jae-Hong
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-06-20 18:20:27 +08:00
|
|
|
#include "CCStdC.h"
|
2013-04-26 17:48:26 +08:00
|
|
|
#include "CCAccelerometer.h"
|
2013-06-19 08:31:34 +08:00
|
|
|
|
|
|
|
using namespace Tizen::Uix::Sensor;
|
2013-04-26 17:48:26 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Accelerometer::Accelerometer()
|
2013-06-19 16:10:53 +08:00
|
|
|
: _function(nullptr)
|
|
|
|
, __sensorMgr(nullptr)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Accelerometer::~Accelerometer()
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Accelerometer::setDelegate(std::function<void(Acceleration*)> function)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-19 16:10:53 +08:00
|
|
|
_function = function;
|
2013-04-26 17:48:26 +08:00
|
|
|
|
2013-06-19 16:10:53 +08:00
|
|
|
if (_function)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-19 08:31:34 +08:00
|
|
|
startSensor();
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-19 08:31:34 +08:00
|
|
|
stopSensor();
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Accelerometer::setAccelerometerInterval(float interval)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-19 08:31:34 +08:00
|
|
|
if (__sensorMgr)
|
|
|
|
{
|
|
|
|
__sensorMgr->SetInterval(SENSOR_TYPE_ACCELERATION, interval * 1000);
|
|
|
|
}
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Accelerometer::startSensor()
|
2013-06-19 08:31:34 +08:00
|
|
|
{
|
|
|
|
long interval = 0L;
|
|
|
|
|
|
|
|
if (__sensorMgr)
|
|
|
|
{
|
|
|
|
__sensorMgr->RemoveSensorListener(*this);
|
|
|
|
delete __sensorMgr;
|
|
|
|
__sensorMgr = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
__sensorMgr = new SensorManager();
|
|
|
|
__sensorMgr->Construct();
|
|
|
|
__sensorMgr->GetMinInterval(SENSOR_TYPE_ACCELERATION, interval);
|
|
|
|
|
|
|
|
if (interval < 50)
|
|
|
|
{
|
|
|
|
interval = 50;
|
|
|
|
}
|
|
|
|
__sensorMgr->AddSensorListener(*this, SENSOR_TYPE_ACCELERATION, interval, true);
|
2013-04-26 17:48:26 +08:00
|
|
|
|
2013-06-19 08:31:34 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Accelerometer::stopSensor()
|
2013-06-19 08:31:34 +08:00
|
|
|
{
|
|
|
|
if (__sensorMgr)
|
|
|
|
{
|
|
|
|
__sensorMgr->RemoveSensorListener(*this);
|
|
|
|
delete __sensorMgr;
|
|
|
|
__sensorMgr = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Accelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorData , result r)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-19 16:10:53 +08:00
|
|
|
if (_function)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-19 08:31:34 +08:00
|
|
|
AccelerationSensorData& data = static_cast<AccelerationSensorData&>(sensorData);
|
|
|
|
AppLog("AccelerationSensorData x = %5.4f , y = %5.4f, z = %5.4f ", data.x,data.y,data.z);
|
|
|
|
|
|
|
|
_accelerationValue.x = -data.x;
|
|
|
|
_accelerationValue.y = -data.y;
|
|
|
|
_accelerationValue.z = -data.z;
|
|
|
|
_accelerationValue.timestamp = data.timestamp;
|
|
|
|
|
2013-06-19 16:10:53 +08:00
|
|
|
_function(&_accelerationValue);
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 08:31:34 +08:00
|
|
|
|
2013-04-26 17:48:26 +08:00
|
|
|
NS_CC_END
|
|
|
|
|