PageRenderTime 43ms CodeModel.GetById 1ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ois/src/win32/extras/WiiMote/wiimote.h

https://bitbucket.org/cabalistic/ogredeps/
C++ Header | 291 lines | 222 code | 52 blank | 17 comment | 0 complexity | c684228ea4925cbd74ed29f06a33f6d9 MD5 | raw file
  1#include "OISConfig.h"
  2#ifdef OIS_WIN32_WIIMOTE_SUPPORT
  3//cWiimote 0.2 by Kevin Forbes (http://simulatedcomicproduct.com)
  4//This code is public domain, and comes with no warranty. The user takes full responsibility for anything that happens as a result from using this code.
  5
  6#ifndef WIIMOTE_H
  7#define WIIMOTE_H
  8
  9#include "hiddevice.h"
 10
 11
 12
 13class cWiiMote
 14{
 15public:
 16	cWiiMote();
 17	~cWiiMote();
 18	
 19	//connection management
 20	bool ConnectToDevice(int index = 0);
 21	bool Disconnect();	
 22	bool IsConnected() const {return mHIDDevice.IsConnected();}
 23	
 24	
 25	bool StartDataStream();
 26	bool StopDataStream();
 27
 28	bool IsNunChuckAttached() { return mNunchuckAttached; }
 29
 30
 31	//this is the wiimote message pump. It should probably be called in loop from a thread
 32	bool HeartBeat(int timeout = 1);
 33	bool SetVibration(bool vib_on);
 34	bool SetLEDs(bool led1, bool led2, bool led3, bool led4);
 35
 36
 37
 38	//Querying functions and structures:
 39	void GetCalibratedAcceleration(float & x, float & y, float &z) const;
 40	void GetCalibratedChuckAcceleration(float & x, float & y, float &z) const;
 41	void GetCalibratedChuckStick(float & x, float & y) const;
 42	bool GetIRP1(float &x, float &y) const;
 43	bool GetIRP2(float &x, float &y) const;
 44
 45
 46	struct tExpansionReport
 47	{
 48		bool mAttachmentPluggedIn;
 49		bool mIREnabled;
 50		bool mSpeakerEnabled;
 51		bool mLED1On;
 52		bool mLED2On;
 53		bool mLED3On;
 54		bool mLED4On;
 55		unsigned char mBatteryLevel;
 56
 57		void Init()
 58		{
 59			mAttachmentPluggedIn = false;
 60			mIREnabled = false;
 61			mSpeakerEnabled = false;
 62			mLED1On = false;
 63			mLED2On = false;
 64			mLED3On = false;
 65			mLED4On = false;
 66			mBatteryLevel = 0;
 67		}
 68	};
 69	struct tButtonStatus
 70	{
 71		bool mA;
 72		bool mB;
 73		bool m1;
 74		bool m2;
 75		bool mPlus;
 76		bool mMinus;
 77		bool mHome;
 78		bool mUp;
 79		bool mDown;
 80		bool mLeft;
 81		bool mRight;
 82	
 83		void Init()
 84		{
 85			mA = mB = m1 = m2 = mPlus = mMinus = mHome = mUp = mDown = mLeft = mRight = false;
 86		}
 87	};
 88	struct tMotionReport
 89	{
 90		unsigned char mX;
 91		unsigned char mY;
 92		unsigned char mZ;
 93		
 94		void Init()
 95		{
 96			mX = mY = mZ = 0;
 97		}
 98	};
 99
100	struct tChuckReport
101	{
102		unsigned char mStickX;
103		unsigned char mStickY;
104		unsigned char mAccelX;
105		unsigned char mAccelY;
106		unsigned char mAccelZ;
107		bool	mButtonC;
108		bool	mButtonZ;
109		void Init()
110		{
111			mStickX=mStickY=mAccelX=mAccelY=mAccelZ=0;
112			mButtonC = mButtonZ = false;
113		};
114	};
115
116	struct tIRReport
117	{
118		unsigned short mP1X;
119		unsigned short mP1Y;
120		
121		unsigned short mP2X;
122		unsigned short mP2Y;
123
124		unsigned char mP1Size;
125		unsigned char mP2Size;
126
127		bool mP1Found;
128		bool mP2Found;
129
130		void Init()
131		{
132			mP1X = mP1Y = mP2X = mP2Y = mP1Size = mP2Size = 0;
133			mP1Found = mP2Found = false;
134		}
135
136
137	};
138	const tButtonStatus & GetLastButtonStatus() const {return mLastButtonStatus;}
139	const tChuckReport & GetLastChuckReport() const {return mLastChuckReport;}
140	const tMotionReport & GetLastMotionReport() const { return mLastMotionReport;}
141	const tExpansionReport & GetLastExpansionReport() const { return mLastExpansionReport;}
142	const tIRReport & GetLastIRReport() const { return mLastIRReport;}
143	
144	
145	//debugging functions:
146	void PrintStatus() const;
147
148private:
149	
150	//parsing functions for input reports
151	void ParseExpansionReport(const unsigned char * data);
152	void ParseButtonReport(const unsigned char * data);
153	void ParseMotionReport(const unsigned char * data);
154	void ParseReadData(const unsigned char * data);
155	void ParseChuckReport(const unsigned char * data);
156	void ParseIRReport(const unsigned char * data);
157
158
159	//tell the wiimote how to send data
160	enum eReportMode
161	{
162		REPORT_MODE_EVENT_BUTTONS,
163		REPORT_MODE_MOTION, 
164		REPORT_MODE_MOTION_CHUCK,
165		REPORT_MODE_MOTION_IR,
166		REPORT_MODE_MOTION_CHUCK_IR
167	};
168	bool SetReportMode(eReportMode mode);
169
170
171	//housekeeping functions
172	void Init();
173	void ClearBuffer();
174
175	//low level tasks
176	bool SelectInputChannel(bool continuous, unsigned char channel);
177	bool UpdateOutput();
178	bool ReadMemory(unsigned int address, unsigned short size, unsigned char * buffer) const;	
179	bool WriteMemory(unsigned int address, unsigned char size, const unsigned char * buffer);
180
181	bool ReadData(unsigned int address, unsigned short size, unsigned char * buffer);
182	bool IssueReadRequest(unsigned int address, unsigned short size, unsigned char * buffer);
183	bool ReadCalibrationData();
184	bool SendReportMode();
185
186	bool InitNunchuck();
187	bool EnableIR();
188	bool DisableIR();
189
190	static inline unsigned char NunChuckByte(unsigned char in) {return (in ^ 0x17)+0x17;}
191	//flash reading vars
192	struct tMemReadInfo
193	{
194		enum eReadStatus
195		{
196			READ_PENDING,
197			READ_NONE,
198			READ_COMPLETE,
199			READ_ERROR
200		} mReadStatus;
201
202		unsigned char * mReadBuffer;
203		unsigned short mTotalBytesToRead;
204		unsigned short mBytesRead;
205		unsigned short mBaseAddress;
206		void Init()
207		{
208			mReadStatus = READ_NONE;
209			mReadBuffer = NULL;
210			mTotalBytesToRead = 0;
211			mBytesRead = 0;
212			mBaseAddress = 0;
213		}
214	} mReadInfo;
215
216	//calibration data for the wiimote
217	struct tAccelCalibrationData
218	{
219		unsigned char mXZero;
220		unsigned char mYZero;
221		unsigned char mZZero;
222		unsigned char mXG;
223		unsigned char mYG;
224		unsigned char mZG;
225		void Init()
226		{
227			mXZero = mYZero = mZZero = mXG = mYG = mZG= 0;
228		}
229	} ;
230
231	struct tStickCalibrationData
232	{
233		unsigned char mXmin;
234		unsigned char mXmid;
235		unsigned char mXmax;
236		unsigned char mYmin;
237		unsigned char mYmid;
238		unsigned char mYmax;
239
240		void Init()
241		{
242			mXmax = mYmax = mXmin = mYmin = mXmid = mYmid =0;
243		}
244	};
245	
246	tAccelCalibrationData mAccelCalibrationData;
247	tAccelCalibrationData mNunchuckAccelCalibrationData;
248	tStickCalibrationData mNunchuckStickCalibrationData;
249
250	//output requests
251	struct tOutputControls
252	{
253		bool mVibration;
254		bool mLED1;
255		bool mLED2;
256		bool mLED3;
257		bool mLED4;
258
259		void Init()
260		{
261			mVibration = mLED1 = mLED2= mLED3= mLED4 = false;
262		}
263	};
264	
265	
266	//input states
267	tExpansionReport mLastExpansionReport;
268	tButtonStatus mLastButtonStatus;
269	tMotionReport mLastMotionReport;
270	tChuckReport mLastChuckReport;
271	tIRReport mLastIRReport;
272
273	//output states
274	tOutputControls mOutputControls;
275	eReportMode	mReportMode;
276
277	//our communications device
278	cHIDDevice mHIDDevice;
279	
280	bool mNunchuckAttached;
281	bool mIRRunning;
282	bool mDataStreamRunning;
283
284	//buffers for input/output
285	static const int mOutputBufferSize = 22;
286	unsigned char mOutputBuffer[mOutputBufferSize];
287	static const int mInputBufferSize = 22;
288	unsigned char mInputBuffer[mInputBufferSize];
289};
290#endif
291#endif