2011-09-19 18:05:40 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCAccelerometer_bada.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
|
|
|
|
using namespace Osp::Uix;
|
|
|
|
|
|
|
|
NS_CC_BEGIN;
|
|
|
|
|
|
|
|
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
|
|
|
|
|
2011-09-30 17:19:16 +08:00
|
|
|
CCAccelerometer::CCAccelerometer() :
|
|
|
|
m_pAccelDelegate(NULL)
|
|
|
|
, m_pSensor(NULL)
|
2011-09-19 18:05:40 +08:00
|
|
|
{
|
2011-09-30 17:19:16 +08:00
|
|
|
|
2011-09-19 18:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCAccelerometer::~CCAccelerometer()
|
|
|
|
{
|
|
|
|
m_spCCAccelerometer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
|
|
|
|
{
|
2011-09-29 18:12:08 +08:00
|
|
|
if (m_spCCAccelerometer == NULL)
|
|
|
|
{
|
|
|
|
m_spCCAccelerometer = new CCAccelerometer();
|
|
|
|
}
|
|
|
|
return m_spCCAccelerometer;
|
2011-09-19 18:05:40 +08:00
|
|
|
}
|
|
|
|
|
2011-09-29 18:12:08 +08:00
|
|
|
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
|
2011-09-19 18:05:40 +08:00
|
|
|
{
|
|
|
|
m_pAccelDelegate = pDelegate;
|
2011-10-10 10:24:52 +08:00
|
|
|
if (pDelegate != NULL)
|
2011-09-30 17:19:16 +08:00
|
|
|
{
|
|
|
|
setEnable(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setEnable(false);
|
|
|
|
}
|
2011-09-19 18:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAccelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorData, result r)
|
|
|
|
{
|
|
|
|
long timeStamp = 0;
|
|
|
|
float x = 0.0, y = 0.0, z = 0.0;
|
|
|
|
|
|
|
|
sensorData.GetValue((SensorDataKey)ACCELERATION_DATA_KEY_TIMESTAMP, timeStamp);
|
|
|
|
sensorData.GetValue((SensorDataKey)ACCELERATION_DATA_KEY_X, x);
|
|
|
|
sensorData.GetValue((SensorDataKey)ACCELERATION_DATA_KEY_Y, y);
|
|
|
|
sensorData.GetValue((SensorDataKey)ACCELERATION_DATA_KEY_Z, z);
|
|
|
|
|
2011-09-30 17:19:16 +08:00
|
|
|
// only consider land postion, to be continued.
|
2011-09-19 18:05:40 +08:00
|
|
|
CCAcceleration AccValue;
|
2011-10-09 18:15:31 +08:00
|
|
|
AccValue.x = -x;
|
|
|
|
AccValue.y = -y;
|
|
|
|
AccValue.z = -z;
|
2011-09-19 18:05:40 +08:00
|
|
|
AccValue.timestamp = timeStamp;
|
|
|
|
|
2011-10-10 10:24:52 +08:00
|
|
|
if (m_pAccelDelegate != NULL)
|
|
|
|
{
|
|
|
|
m_pAccelDelegate->didAccelerate(&AccValue);
|
|
|
|
}
|
2011-11-07 00:28:09 +08:00
|
|
|
//AppLog("##TimeStamp:[%d], Accel.x,y,z:[%f,%f,%f]", timeStamp, x, y, z);
|
2011-09-30 17:19:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAccelerometer::setEnable(bool bEnable)
|
|
|
|
{
|
|
|
|
if (m_pSensor != NULL)
|
|
|
|
{
|
|
|
|
m_pSensor->RemoveSensorListener(*this);
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(m_pSensor);
|
|
|
|
if (bEnable)
|
|
|
|
{
|
|
|
|
long interval = 10;
|
|
|
|
bool available = false;
|
|
|
|
result r = E_INVALID_STATE;
|
|
|
|
m_pSensor = new SensorManager();
|
|
|
|
m_pSensor->Construct();
|
|
|
|
|
|
|
|
available = m_pSensor->IsAvailable(SENSOR_TYPE_ACCELERATION);
|
|
|
|
|
|
|
|
if (available)
|
|
|
|
{
|
|
|
|
long intervalTemp = 0;
|
|
|
|
m_pSensor->GetMaxInterval(SENSOR_TYPE_ACCELERATION, intervalTemp);
|
|
|
|
if (interval > intervalTemp)
|
|
|
|
interval = intervalTemp;
|
|
|
|
m_pSensor->GetMinInterval(SENSOR_TYPE_ACCELERATION, intervalTemp);
|
|
|
|
if (interval < intervalTemp)
|
|
|
|
interval = intervalTemp;
|
|
|
|
|
|
|
|
r = m_pSensor->AddSensorListener(*this, SENSOR_TYPE_ACCELERATION, interval, false);
|
|
|
|
}
|
|
|
|
}
|
2011-09-19 18:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END;
|