USB Host Shield 2.0
Wii.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16 
17  IR camera support added by Allan Glover (adglover9.81@gmail.com) and Kristian Lauszus
18  */
19 
20 #ifndef _wii_h_
21 #define _wii_h_
22 
23 #include "BTD.h"
24 #include "controllerEnums.h"
25 
26 /* Wii event flags */
27 #define WII_FLAG_MOTION_PLUS_CONNECTED 0x01
28 #define WII_FLAG_NUNCHUCK_CONNECTED 0x02
29 
30 #define wii_check_flag(flag) (wii_event_flag & (flag))
31 #define wii_set_flag(flag) (wii_event_flag |= (flag))
32 #define wii_clear_flag(flag) (wii_event_flag &= ~(flag))
33 
35 enum HatEnum {
37  HatX = 0,
39  HatY = 1,
40 };
41 
47 class WII : public BluetoothService {
48 public:
55  WII(BTD *p, bool pair = false);
56 
59  void disconnect();
73  bool getButtonPress(ButtonEnum b);
74  bool getButtonClick(ButtonEnum b);
80  void pair(void) {
81  if(pBtd)
83  };
89  uint8_t getAnalogHat(HatEnum a);
95  uint16_t getAnalogHat(AnalogHatEnum a);
96 
101  double getPitch() {
103  return compPitch;
104  return getWiimotePitch();
105  };
106 
111  double getRoll() {
113  return compRoll;
114  return getWiimoteRoll();
115  };
116 
123  double getYaw() {
124  return gyroYaw;
125  };
126 
128  void setAllOff();
130  void setRumbleOff();
132  void setRumbleOn();
134  void setRumbleToggle();
135 
140  void setLedRaw(uint8_t value);
141 
143  void setLedOff() {
144  setLedRaw(0);
145  };
150  void setLedOff(LEDEnum a);
155  void setLedOn(LEDEnum a);
160  void setLedToggle(LEDEnum a);
168  void setLedStatus();
169 
174  uint8_t getBatteryLevel();
175 
180  uint8_t getWiiState() {
181  return wiiState;
182  };
187  bool wiimoteConnected;
196  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
197 
201  double getWiimotePitch() {
202  return (atan2(accYwiimote, accZwiimote) + PI) * RAD_TO_DEG;
203  };
204 
205  double getWiimoteRoll() {
206  return (atan2(accXwiimote, accZwiimote) + PI) * RAD_TO_DEG;
207  };
213  double getNunchuckPitch() {
214  return (atan2(accYnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
215  };
216 
217  double getNunchuckRoll() {
218  return (atan2(accXnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
219  };
228  /* Variables for the gyro inside the Motion Plus */
230  double gyroPitch;
232  double gyroRoll;
234  double gyroYaw;
235 
240  double yawGyroSpeed;
245  uint16_t pitchGyroScale;
246  uint16_t rollGyroScale;
247  uint16_t yawGyroScale;
252  int16_t gyroYawRaw;
253  int16_t gyroRollRaw;
254  int16_t gyroPitchRaw;
259  int16_t gyroYawZero;
260  int16_t gyroRollZero;
261  int16_t gyroPitchZero;
264 #ifdef WIICAMERA
265 
269  void IRinitialize();
270 
275  uint16_t getIRx1() {
276  return IR_object_x1;
277  };
278 
283  uint16_t getIRy1() {
284  return IR_object_y1;
285  };
286 
291  uint8_t getIRs1() {
292  return IR_object_s1;
293  };
294 
299  uint16_t getIRx2() {
300  return IR_object_x2;
301  };
302 
307  uint16_t getIRy2() {
308  return IR_object_y2;
309  };
310 
315  uint8_t getIRs2() {
316  return IR_object_s2;
317  };
318 
323  uint16_t getIRx3() {
324  return IR_object_x3;
325  };
326 
331  uint16_t getIRy3() {
332  return IR_object_y3;
333  };
334 
339  uint8_t getIRs3() {
340  return IR_object_s3;
341  };
342 
347  uint16_t getIRx4() {
348  return IR_object_x4;
349  };
350 
355  uint16_t getIRy4() {
356  return IR_object_y4;
357  };
358 
363  uint8_t getIRs4() {
364  return IR_object_s4;
365  };
366 
373  return (wiiState & 0x08);
374  };
376 #endif
377 
378 protected:
384  void ACLData(uint8_t* ACLData);
386  void Run();
388  void Reset();
394  void onInit();
397 private:
398 
399  void L2CAP_task(); // L2CAP state machine
400 
401  /* Variables filled from HCI event management */
402  bool activeConnection; // Used to indicate if it's already has established a connection
403 
404  /* Variables used by high level L2CAP task */
405  uint8_t l2cap_state;
406  uint8_t wii_event_flag; // Used for Wii flags
407 
408  uint32_t ButtonState;
409  uint32_t OldButtonState;
410  uint32_t ButtonClickState;
411  uint16_t hatValues[4];
412 
413  uint8_t HIDBuffer[3]; // Used to store HID commands
414 
415  uint16_t stateCounter;
416  bool unknownExtensionConnected;
417  bool extensionConnected;
418  bool checkExtension; // Set to false when getBatteryLevel() is called otherwise if should be true
419  bool motionPlusInside; // True if it's a new Wiimote with the Motion Plus extension build into it
420 
421  /* L2CAP Channels */
422  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
423  uint8_t control_dcid[2]; // 0x0060
424  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
425  uint8_t interrupt_dcid[2]; // 0x0061
426 
427  /* HID Commands */
428  void HID_Command(uint8_t* data, uint8_t nbytes);
429  void setReportMode(bool continuous, uint8_t mode);
430 
431  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
432  void initExtension1();
433  void initExtension2();
434 
435  void statusRequest(); // Used to update the Wiimote state and battery level
436 
437  void readData(uint32_t offset, uint16_t size, bool EEPROM);
438  void readExtensionType();
439  void readCalData();
440 
441  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
442  void initMotionPlus();
443  void activateMotionPlus();
444 
445  double compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
446  double compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
447 
448  bool activateNunchuck;
449  bool motionValuesReset; // This bool is true when the gyro values has been reset
450  uint32_t timer;
451 
452  uint8_t wiiState; // Stores the value in l2capinbuf[12] - (0x01: Battery is nearly empty), (0x02: An Extension Controller is connected), (0x04: Speaker enabled), (0x08: IR enabled), (0x10: LED1, 0x20: LED2, 0x40: LED3, 0x80: LED4)
453  uint8_t batteryLevel;
454 
455 #ifdef WIICAMERA
456  /* Private function and variables for the readings from the IR Camera */
457  void enableIRCamera1(); // Sets bit 2 of output report 13
458  void enableIRCamera2(); // Sets bit 2 of output report 1A
459  void writeSensitivityBlock1();
460  void writeSensitivityBlock2();
461  void write0x08Value();
462  void setWiiModeNumber(uint8_t mode_number);
463 
464  uint16_t IR_object_x1; // IR x position 10 bits
465  uint16_t IR_object_y1; // IR y position 10 bits
466  uint8_t IR_object_s1; // IR size value
467  uint16_t IR_object_x2;
468  uint16_t IR_object_y2;
469  uint8_t IR_object_s2;
470  uint16_t IR_object_x3; // IR x position 10 bits
471  uint16_t IR_object_y3; // IR y position 10 bits
472  uint8_t IR_object_s3; // IR size value
473  uint16_t IR_object_x4;
474  uint16_t IR_object_y4;
475  uint8_t IR_object_s4;
476 #endif
477 };
478 #endif
bool wiimoteConnected
Definition: Wii.h:182
void onInit()
Definition: Wii.cpp:1080
int16_t gyroPitchRaw
Definition: Wii.h:254
uint16_t rollGyroScale
Definition: Wii.h:246
double getNunchuckPitch()
Definition: Wii.h:213
void setLedToggle(LEDEnum a)
Definition: Wii.cpp:897
void setLedRaw(uint8_t value)
Definition: Wii.cpp:875
uint8_t getAnalogHat(HatEnum a)
Definition: Wii.cpp:1056
uint16_t yawGyroScale
Definition: Wii.h:247
Definition: Wii.h:39
Definition: BTD.h:198
uint8_t getIRs1()
Definition: Wii.h:291
uint8_t getIRs3()
Definition: Wii.h:339
double getNunchuckRoll()
Definition: Wii.h:217
void ACLData(uint8_t *ACLData)
Definition: Wii.cpp:132
double gyroRoll
Definition: Wii.h:232
uint16_t getIRx4()
Definition: Wii.h:347
void setRumbleOn()
Definition: Wii.cpp:863
void Reset()
Definition: Wii.cpp:101
double getRoll()
Definition: Wii.h:111
AnalogHatEnum
int16_t accZnunchuck
Definition: Wii.h:225
uint8_t getBatteryLevel()
Definition: Wii.cpp:916
void disconnect()
Definition: Wii.cpp:115
double getPitch()
Definition: Wii.h:101
int16_t accXnunchuck
Definition: Wii.h:225
uint8_t getWiiState()
Definition: Wii.h:180
uint16_t getIRy2()
Definition: Wii.h:307
int16_t gyroRollRaw
Definition: Wii.h:253
LEDEnum
void IRinitialize()
Definition: Wii.cpp:1093
Definition: Wii.h:37
uint16_t getIRx2()
Definition: Wii.h:299
uint16_t getIRx1()
Definition: Wii.h:275
void setRumbleToggle()
Definition: Wii.cpp:869
int16_t gyroPitchZero
Definition: Wii.h:261
uint16_t pitchGyroScale
Definition: Wii.h:245
double getWiimoteRoll()
Definition: Wii.h:205
WII(BTD *p, bool pair=false)
Definition: Wii.cpp:85
double rollGyroSpeed
Definition: Wii.h:239
uint8_t getIRs4()
Definition: Wii.h:363
uint16_t getIRy3()
Definition: Wii.h:331
void setLedStatus()
Definition: Wii.cpp:903
ButtonEnum
bool getButtonClick(ButtonEnum b)
Definition: Wii.cpp:1045
int16_t accYwiimote
Definition: Wii.h:219
bool wiiUProControllerConnected
Definition: Wii.h:193
void setRumbleOff()
Definition: Wii.cpp:857
void setAllOff()
Definition: Wii.cpp:851
void setLedOff()
Definition: Wii.h:143
uint16_t getIRy1()
Definition: Wii.h:283
uint16_t getIRx3()
Definition: Wii.h:323
BTD * pBtd
Definition: BTD.h:606
double gyroYaw
Definition: Wii.h:234
bool nunchuckConnected
Definition: Wii.h:189
uint16_t getIRy4()
Definition: Wii.h:355
int16_t gyroYawRaw
Definition: Wii.h:252
bool isIRCameraEnabled()
Definition: Wii.h:372
double getYaw()
Definition: Wii.h:123
double getWiimotePitch()
Definition: Wii.h:201
void pair(void)
Definition: Wii.h:80
void setLedOn(LEDEnum a)
Definition: Wii.cpp:887
double yawGyroSpeed
Definition: Wii.h:240
HatEnum
Definition: Wii.h:35
int16_t accYnunchuck
Definition: Wii.h:225
int16_t gyroYawZero
Definition: Wii.h:259
double pitchGyroSpeed
Definition: Wii.h:238
void pairWithWiimote()
Definition: BTD.h:464
uint8_t getIRs2()
Definition: Wii.h:315
double gyroPitch
Definition: Wii.h:230
Definition: Wii.h:47
bool getButtonPress(ButtonEnum b)
Definition: Wii.cpp:1038
bool motionPlusConnected
Definition: Wii.h:191
int16_t gyroRollZero
Definition: Wii.h:260
void Run()
Definition: Wii.cpp:681
int16_t accXwiimote
Definition: Wii.h:219
int16_t accZwiimote
Definition: Wii.h:219