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"
|
2011-11-07 18:18:10 +08:00
|
|
|
#include "CCEGLView_bada.h"
|
2011-09-19 18:05:40 +08:00
|
|
|
|
2011-11-07 18:18:10 +08:00
|
|
|
using namespace Osp::Ui;
|
2011-09-19 18:05:40 +08:00
|
|
|
using namespace Osp::Uix;
|
|
|
|
|
2012-04-19 11:56:22 +08:00
|
|
|
NS_CC_BEGIN
|
2011-09-19 18:05:40 +08:00
|
|
|
|
|
|
|
|
2011-11-24 18:04:25 +08:00
|
|
|
CCAccelerometer::CCAccelerometer()
|
|
|
|
: m_pAccelDelegate(NULL)
|
2011-09-30 17:19:16 +08:00
|
|
|
, m_pSensor(NULL)
|
2011-11-24 18:04:25 +08:00
|
|
|
, m_bEnabled(false)
|
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()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_DELETE(m_pSensor);
|
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
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pAccelDelegate = pDelegate;
|
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
|
|
|
setEnable(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setEnable(false);
|
|
|
|
}
|
2011-09-19 18:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAccelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorData, result r)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because the axes are not swapped when the device's screen orientation changes.
|
|
|
|
* So we should swap it here.
|
|
|
|
*/
|
|
|
|
Orientation orientation = CCEGLView::sharedOpenGLView().GetOrientation();
|
|
|
|
if (orientation == ORIENTATION_LANDSCAPE || orientation == ORIENTATION_LANDSCAPE_REVERSE)
|
|
|
|
{
|
|
|
|
float tmp = x;
|
|
|
|
x = -y;
|
|
|
|
y = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAcceleration AccValue;
|
|
|
|
AccValue.x = -x;
|
|
|
|
AccValue.y = -y;
|
|
|
|
AccValue.z = -z;
|
|
|
|
AccValue.timestamp = timeStamp;
|
|
|
|
|
|
|
|
if (m_pAccelDelegate != NULL)
|
|
|
|
{
|
|
|
|
m_pAccelDelegate->didAccelerate(&AccValue);
|
|
|
|
}
|
|
|
|
//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)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
result r = E_INVALID_STATE;
|
|
|
|
if (m_bEnabled == bEnable)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bEnabled = bEnable;
|
|
|
|
|
|
|
|
if (m_bEnabled)
|
|
|
|
{
|
|
|
|
bool available = false;
|
|
|
|
long interval = 50;
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(m_pSensor);
|
|
|
|
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, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Accelerometer Sensor unavailable!");
|
|
|
|
delete m_pSensor;
|
|
|
|
m_pSensor = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_pSensor != NULL)
|
|
|
|
{
|
|
|
|
r = m_pSensor->RemoveSensorListener(*this);
|
|
|
|
delete m_pSensor;
|
|
|
|
m_pSensor = NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-09-19 18:05:40 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 11:56:22 +08:00
|
|
|
NS_CC_END
|