wiimote.h 6.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "OISConfig.h"
#ifdef OIS_WIN32_WIIMOTE_SUPPORT
//cWiimote 0.2 by Kevin Forbes (http://simulatedcomicproduct.com)
//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.

#ifndef WIIMOTE_H
#define WIIMOTE_H

#include "hiddevice.h"



class cWiiMote
{
public:
	cWiiMote();
	~cWiiMote();
Ben Hymers's avatar
Ben Hymers committed
18

19
20
	//connection management
	bool ConnectToDevice(int index = 0);
Ben Hymers's avatar
Ben Hymers committed
21
	bool Disconnect();
22
	bool IsConnected() const {return mHIDDevice.IsConnected();}
Ben Hymers's avatar
Ben Hymers committed
23
24


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	bool StartDataStream();
	bool StopDataStream();

	bool IsNunChuckAttached() { return mNunchuckAttached; }


	//this is the wiimote message pump. It should probably be called in loop from a thread
	bool HeartBeat(int timeout = 1);
	bool SetVibration(bool vib_on);
	bool SetLEDs(bool led1, bool led2, bool led3, bool led4);



	//Querying functions and structures:
	void GetCalibratedAcceleration(float & x, float & y, float &z) const;
	void GetCalibratedChuckAcceleration(float & x, float & y, float &z) const;
	void GetCalibratedChuckStick(float & x, float & y) const;
	bool GetIRP1(float &x, float &y) const;
	bool GetIRP2(float &x, float &y) const;


	struct tExpansionReport
	{
		bool mAttachmentPluggedIn;
		bool mIREnabled;
		bool mSpeakerEnabled;
		bool mLED1On;
		bool mLED2On;
		bool mLED3On;
		bool mLED4On;
		unsigned char mBatteryLevel;

		void Init()
		{
			mAttachmentPluggedIn = false;
			mIREnabled = false;
			mSpeakerEnabled = false;
			mLED1On = false;
			mLED2On = false;
			mLED3On = false;
			mLED4On = false;
			mBatteryLevel = 0;
		}
	};
	struct tButtonStatus
	{
		bool mA;
		bool mB;
		bool m1;
		bool m2;
		bool mPlus;
		bool mMinus;
		bool mHome;
		bool mUp;
		bool mDown;
		bool mLeft;
		bool mRight;
Ben Hymers's avatar
Ben Hymers committed
82

83
84
85
86
87
88
89
90
91
92
		void Init()
		{
			mA = mB = m1 = m2 = mPlus = mMinus = mHome = mUp = mDown = mLeft = mRight = false;
		}
	};
	struct tMotionReport
	{
		unsigned char mX;
		unsigned char mY;
		unsigned char mZ;
Ben Hymers's avatar
Ben Hymers committed
93

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
		void Init()
		{
			mX = mY = mZ = 0;
		}
	};

	struct tChuckReport
	{
		unsigned char mStickX;
		unsigned char mStickY;
		unsigned char mAccelX;
		unsigned char mAccelY;
		unsigned char mAccelZ;
		bool	mButtonC;
		bool	mButtonZ;
		void Init()
		{
			mStickX=mStickY=mAccelX=mAccelY=mAccelZ=0;
			mButtonC = mButtonZ = false;
		};
	};

	struct tIRReport
	{
		unsigned short mP1X;
		unsigned short mP1Y;
Ben Hymers's avatar
Ben Hymers committed
120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
		unsigned short mP2X;
		unsigned short mP2Y;

		unsigned char mP1Size;
		unsigned char mP2Size;

		bool mP1Found;
		bool mP2Found;

		void Init()
		{
			mP1X = mP1Y = mP2X = mP2Y = mP1Size = mP2Size = 0;
			mP1Found = mP2Found = false;
		}


	};
	const tButtonStatus & GetLastButtonStatus() const {return mLastButtonStatus;}
	const tChuckReport & GetLastChuckReport() const {return mLastChuckReport;}
	const tMotionReport & GetLastMotionReport() const { return mLastMotionReport;}
	const tExpansionReport & GetLastExpansionReport() const { return mLastExpansionReport;}
	const tIRReport & GetLastIRReport() const { return mLastIRReport;}
Ben Hymers's avatar
Ben Hymers committed
143
144


145
146
147
148
	//debugging functions:
	void PrintStatus() const;

private:
Ben Hymers's avatar
Ben Hymers committed
149

150
151
152
153
154
155
156
157
158
159
160
161
162
	//parsing functions for input reports
	void ParseExpansionReport(const unsigned char * data);
	void ParseButtonReport(const unsigned char * data);
	void ParseMotionReport(const unsigned char * data);
	void ParseReadData(const unsigned char * data);
	void ParseChuckReport(const unsigned char * data);
	void ParseIRReport(const unsigned char * data);


	//tell the wiimote how to send data
	enum eReportMode
	{
		REPORT_MODE_EVENT_BUTTONS,
Ben Hymers's avatar
Ben Hymers committed
163
		REPORT_MODE_MOTION,
164
165
166
167
168
169
170
171
172
173
174
175
176
177
		REPORT_MODE_MOTION_CHUCK,
		REPORT_MODE_MOTION_IR,
		REPORT_MODE_MOTION_CHUCK_IR
	};
	bool SetReportMode(eReportMode mode);


	//housekeeping functions
	void Init();
	void ClearBuffer();

	//low level tasks
	bool SelectInputChannel(bool continuous, unsigned char channel);
	bool UpdateOutput();
Ben Hymers's avatar
Ben Hymers committed
178
	bool ReadMemory(unsigned int address, unsigned short size, unsigned char * buffer) const;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	bool WriteMemory(unsigned int address, unsigned char size, const unsigned char * buffer);

	bool ReadData(unsigned int address, unsigned short size, unsigned char * buffer);
	bool IssueReadRequest(unsigned int address, unsigned short size, unsigned char * buffer);
	bool ReadCalibrationData();
	bool SendReportMode();

	bool InitNunchuck();
	bool EnableIR();
	bool DisableIR();

	static inline unsigned char NunChuckByte(unsigned char in) {return (in ^ 0x17)+0x17;}
	//flash reading vars
	struct tMemReadInfo
	{
		enum eReadStatus
		{
			READ_PENDING,
			READ_NONE,
			READ_COMPLETE,
			READ_ERROR
		} mReadStatus;

		unsigned char * mReadBuffer;
		unsigned short mTotalBytesToRead;
		unsigned short mBytesRead;
		unsigned short mBaseAddress;
		void Init()
		{
			mReadStatus = READ_NONE;
			mReadBuffer = NULL;
			mTotalBytesToRead = 0;
			mBytesRead = 0;
			mBaseAddress = 0;
		}
	} mReadInfo;

	//calibration data for the wiimote
	struct tAccelCalibrationData
	{
		unsigned char mXZero;
		unsigned char mYZero;
		unsigned char mZZero;
		unsigned char mXG;
		unsigned char mYG;
		unsigned char mZG;
		void Init()
		{
			mXZero = mYZero = mZZero = mXG = mYG = mZG= 0;
		}
	} ;

	struct tStickCalibrationData
	{
		unsigned char mXmin;
		unsigned char mXmid;
		unsigned char mXmax;
		unsigned char mYmin;
		unsigned char mYmid;
		unsigned char mYmax;

		void Init()
		{
			mXmax = mYmax = mXmin = mYmin = mXmid = mYmid =0;
		}
	};
Ben Hymers's avatar
Ben Hymers committed
245

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	tAccelCalibrationData mAccelCalibrationData;
	tAccelCalibrationData mNunchuckAccelCalibrationData;
	tStickCalibrationData mNunchuckStickCalibrationData;

	//output requests
	struct tOutputControls
	{
		bool mVibration;
		bool mLED1;
		bool mLED2;
		bool mLED3;
		bool mLED4;

		void Init()
		{
			mVibration = mLED1 = mLED2= mLED3= mLED4 = false;
		}
	};
Ben Hymers's avatar
Ben Hymers committed
264
265


266
267
268
269
270
271
272
273
274
275
276
277
278
	//input states
	tExpansionReport mLastExpansionReport;
	tButtonStatus mLastButtonStatus;
	tMotionReport mLastMotionReport;
	tChuckReport mLastChuckReport;
	tIRReport mLastIRReport;

	//output states
	tOutputControls mOutputControls;
	eReportMode	mReportMode;

	//our communications device
	cHIDDevice mHIDDevice;
Ben Hymers's avatar
Ben Hymers committed
279

280
281
282
283
284
285
286
287
288
289
290
291
	bool mNunchuckAttached;
	bool mIRRunning;
	bool mDataStreamRunning;

	//buffers for input/output
	static const int mOutputBufferSize = 22;
	unsigned char mOutputBuffer[mOutputBufferSize];
	static const int mInputBufferSize = 22;
	unsigned char mInputBuffer[mInputBufferSize];
};
#endif
#endif