2012-10-19 23:26:34 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
|
|
|
|
|
Copyright (c) 2011 Максим Аксенов
|
|
|
|
|
Copyright (c) 2011 Giovanni Zito, Francis Styck
|
|
|
|
|
|
|
|
|
|
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.h"
|
|
|
|
|
#include "ccMacros.h"
|
|
|
|
|
|
|
|
|
|
namespace cocos2d
|
|
|
|
|
{
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CCAccelerometer* CCAccelerometer::_spCCAccelerometer = NULL;
|
2012-10-19 23:26:34 +08:00
|
|
|
|
|
2013-06-19 16:10:53 +08:00
|
|
|
|
CCAccelerometer::CCAccelerometer() : _function(nullptr)
|
2012-10-19 23:26:34 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCAccelerometer::~CCAccelerometer()
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
// if( _spCCAccelerometer ) {
|
|
|
|
|
// delete _spCCAccelerometer ;
|
|
|
|
|
_spCCAccelerometer = NULL;
|
2012-10-19 23:26:34 +08:00
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
|
|
|
|
|
{
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_spCCAccelerometer == NULL)
|
2012-10-19 23:26:34 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_spCCAccelerometer = new CCAccelerometer();
|
2012-10-19 23:26:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return _spCCAccelerometer;
|
2012-10-19 23:26:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-19 16:10:53 +08:00
|
|
|
|
void CCAccelerometer::setDelegate(std::function<void(CCAcceleration*)> function)
|
2012-10-19 23:26:34 +08:00
|
|
|
|
{
|
2013-06-19 16:10:53 +08:00
|
|
|
|
_function = function;
|
2012-10-19 23:26:34 +08:00
|
|
|
|
|
2013-06-19 16:10:53 +08:00
|
|
|
|
if (_function)
|
2012-10-19 23:26:34 +08:00
|
|
|
|
{
|
|
|
|
|
if (s3eAccelerometerStart() != S3E_RESULT_SUCCESS)
|
|
|
|
|
{
|
2013-03-16 23:56:03 +08:00
|
|
|
|
CCLog("s3eAccelerometerStart() - ERROR");
|
2012-10-19 23:26:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s3eAccelerometerStop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CCAccelerometer::update(float x, float y, float z, uint64 sensorTimeStamp)
|
|
|
|
|
{
|
2013-06-19 16:10:53 +08:00
|
|
|
|
if (_function)
|
2012-10-19 23:26:34 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_accelerationValue.x = ((double)x)/S3E_ACCELEROMETER_1G ;
|
|
|
|
|
_accelerationValue.y = ((double)y)/S3E_ACCELEROMETER_1G ;
|
|
|
|
|
_accelerationValue.z = ((double)z)/S3E_ACCELEROMETER_1G ;
|
|
|
|
|
_accelerationValue.timestamp = (double)(sensorTimeStamp / 1000.0);
|
2012-10-19 23:26:34 +08:00
|
|
|
|
|
2013-06-19 16:10:53 +08:00
|
|
|
|
_function(&_accelerationValue);
|
2012-10-19 23:26:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // end of namespace cococs2d
|
|
|
|
|
|