1/*
2 The zlib/libpng License
3
4 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
5
6 This software is provided 'as-is', without any express or implied warranty. In no event will
7 the authors be held liable for any damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose, including commercial
10 applications, and to alter it and redistribute it freely, subject to the following
11 restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that
14 you wrote the original software. If you use this software in a product,
15 an acknowledgment in the product documentation would be appreciated but is
16 not required.
17
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source distribution.
22 */
23#include "iphone/iPhoneAccelerometer.h"
24#include "iphone/iPhoneInputManager.h"
25
26using namespace OIS;
27
28//-------------------------------------------------------------------//
29iPhoneAccelerometer::iPhoneAccelerometer( InputManager* creator, bool buffered )
30	: JoyStick(creator->inputSystemName(), buffered, 0, creator)
31{
32    iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator);
33
34    man->_setAccelerometerUsed(true);
35    [man->_getDelegate() setAccelerometerObject:this];
36    [[UIAccelerometer sharedAccelerometer] setDelegate:man->_getDelegate()];
37    mUpdateInterval = 60.0f;
38}
39
40iPhoneAccelerometer::~iPhoneAccelerometer()
41{
42    iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator);
43
44    man->_setAccelerometerUsed(false);
45    [man->_getDelegate() setAccelerometerObject:nil];
46}
47
48void iPhoneAccelerometer::_initialize()
49{
50	// Clear old joy state
51    mState.mVectors.resize(1);
52	mState.clear();
53	mTempState.clear();
54
55    // Set the update interval
56    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / mUpdateInterval)];
57}
58
59void iPhoneAccelerometer::setBuffered( bool buffered )
60{
61	mBuffered = buffered;
62}
63
64void iPhoneAccelerometer::didAccelerate(UIAcceleration *acceleration)
65{
66    mTempState.clear();
67
68    mTempState.x = acceleration.x;
69    mTempState.y = acceleration.y;
70    mTempState.z = acceleration.z;
71}
72
73void iPhoneAccelerometer::capture()
74{
75    mState.clear();
76    mState.mVectors[0] = mTempState;
77
78    if(mListener && mBuffered)
79        mListener->axisMoved(JoyStickEvent(this, mState), 0);
80}
81