USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
62  virtual void ACLData(uint8_t* ACLData);
64  virtual void Run();
66  virtual void Reset();
68  virtual void disconnect();
82  bool getButtonPress(ButtonEnum b);
83  bool getButtonClick(ButtonEnum b);
89  void pair(void) {
90  if(pBtd)
91  pBtd->pairWithWiimote();
92  };
98  uint8_t getAnalogHat(HatEnum a);
104  uint16_t getAnalogHat(AnalogHatEnum a);
105 
110  double getPitch() {
112  return compPitch;
113  return getWiimotePitch();
114  };
115 
120  double getRoll() {
122  return compRoll;
123  return getWiimoteRoll();
124  };
125 
132  double getYaw() {
133  return gyroYaw;
134  };
135 
137  void setAllOff();
139  void setRumbleOff();
141  void setRumbleOn();
143  void setRumbleToggle();
144 
149  void setLedRaw(uint8_t value);
150 
152  void setLedOff() {
153  setLedRaw(0);
154  };
159  void setLedOff(LEDEnum a);
164  void setLedOn(LEDEnum a);
169  void setLedToggle(LEDEnum a);
177  void setLedStatus();
178 
183  uint8_t getBatteryLevel();
184 
189  uint8_t getWiiState() {
190  return wiiState;
191  };
192 
197  void attachOnInit(void (*funcOnInit)(void)) {
198  pFuncOnInit = funcOnInit;
199  };
204  bool wiimoteConnected;
213  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
214 
218  double getWiimotePitch() {
219  return (atan2(accYwiimote, accZwiimote) + PI) * RAD_TO_DEG;
220  };
221 
222  double getWiimoteRoll() {
223  return (atan2(accXwiimote, accZwiimote) + PI) * RAD_TO_DEG;
224  };
230  double getNunchuckPitch() {
231  return (atan2(accYnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
232  };
233 
234  double getNunchuckRoll() {
235  return (atan2(accXnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
236  };
245  /* Variables for the gyro inside the Motion Plus */
247  double gyroPitch;
249  double gyroRoll;
251  double gyroYaw;
252 
257  double yawGyroSpeed;
262  uint16_t pitchGyroScale;
263  uint16_t rollGyroScale;
264  uint16_t yawGyroScale;
269  int16_t gyroYawRaw;
270  int16_t gyroRollRaw;
271  int16_t gyroPitchRaw;
276  int16_t gyroYawZero;
277  int16_t gyroRollZero;
278  int16_t gyroPitchZero;
281 #ifdef WIICAMERA
282 
286  void IRinitialize();
287 
292  uint16_t getIRx1() {
293  return IR_object_x1;
294  };
295 
300  uint16_t getIRy1() {
301  return IR_object_y1;
302  };
303 
308  uint8_t getIRs1() {
309  return IR_object_s1;
310  };
311 
316  uint16_t getIRx2() {
317  return IR_object_x2;
318  };
319 
324  uint16_t getIRy2() {
325  return IR_object_y2;
326  };
327 
332  uint8_t getIRs2() {
333  return IR_object_s2;
334  };
335 
340  uint16_t getIRx3() {
341  return IR_object_x3;
342  };
343 
348  uint16_t getIRy3() {
349  return IR_object_y3;
350  };
351 
356  uint8_t getIRs3() {
357  return IR_object_s3;
358  };
359 
364  uint16_t getIRx4() {
365  return IR_object_x4;
366  };
367 
372  uint16_t getIRy4() {
373  return IR_object_y4;
374  };
375 
380  uint8_t getIRs4() {
381  return IR_object_s4;
382  };
383 
390  return (wiiState & 0x08);
391  };
393 #endif
394 
395 private:
396  BTD *pBtd; // Pointer to BTD instance
397 
403  void onInit();
404  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
405 
406  void L2CAP_task(); // L2CAP state machine
407 
408  /* Variables filled from HCI event management */
409  uint16_t hci_handle;
410  bool activeConnection; // Used to indicate if it's already has established a connection
411 
412  /* Variables used by high level L2CAP task */
413  uint8_t l2cap_state;
414  uint32_t l2cap_event_flag; // L2CAP flags of received Bluetooth events
415  uint8_t wii_event_flag; // Used for Wii flags
416 
417  uint32_t ButtonState;
418  uint32_t OldButtonState;
419  uint32_t ButtonClickState;
420  uint16_t hatValues[4];
421 
422  uint8_t HIDBuffer[3]; // Used to store HID commands
423 
424  uint16_t stateCounter;
425  bool unknownExtensionConnected;
426  bool extensionConnected;
427  bool checkExtension; // Set to false when getBatteryLevel() is called otherwise if should be true
428  bool motionPlusInside; // True if it's a new Wiimote with the Motion Plus extension build into it
429 
430  /* L2CAP Channels */
431  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
432  uint8_t control_dcid[2]; // 0x0060
433  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
434  uint8_t interrupt_dcid[2]; // 0x0061
435  uint8_t identifier; // Identifier for connection
436 
437  /* HID Commands */
438  void HID_Command(uint8_t* data, uint8_t nbytes);
439  void setReportMode(bool continuous, uint8_t mode);
440 
441  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
442  void initExtension1();
443  void initExtension2();
444 
445  void statusRequest(); // Used to update the Wiimote state and battery level
446 
447  void readData(uint32_t offset, uint16_t size, bool EEPROM);
448  void readExtensionType();
449  void readCalData();
450 
451  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
452  void initMotionPlus();
453  void activateMotionPlus();
454 
455  double compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
456  double compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
457 
458  bool activateNunchuck;
459  bool motionValuesReset; // This bool is true when the gyro values has been reset
460  unsigned long timer;
461 
462  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)
463  uint8_t batteryLevel;
464 
465 #ifdef WIICAMERA
466  /* Private function and variables for the readings from the IR Camera */
467  void enableIRCamera1(); // Sets bit 2 of output report 13
468  void enableIRCamera2(); // Sets bit 2 of output report 1A
469  void writeSensitivityBlock1();
470  void writeSensitivityBlock2();
471  void write0x08Value();
472  void setWiiModeNumber(uint8_t mode_number);
473 
474  uint16_t IR_object_x1; // IR x position 10 bits
475  uint16_t IR_object_y1; // IR y position 10 bits
476  uint8_t IR_object_s1; // IR size value
477  uint16_t IR_object_x2;
478  uint16_t IR_object_y2;
479  uint8_t IR_object_s2;
480  uint16_t IR_object_x3; // IR x position 10 bits
481  uint16_t IR_object_y3; // IR y position 10 bits
482  uint8_t IR_object_s3; // IR size value
483  uint16_t IR_object_x4;
484  uint16_t IR_object_y4;
485  uint8_t IR_object_s4;
486 #endif
487 };
488 #endif
bool wiimoteConnected
Definition: Wii.h:199
int16_t gyroPitchRaw
Definition: Wii.h:271
uint16_t rollGyroScale
Definition: Wii.h:263
double getNunchuckPitch()
Definition: Wii.h:230
void setLedToggle(LEDEnum a)
Definition: Wii.cpp:900
void setLedRaw(uint8_t value)
Definition: Wii.cpp:878
uint8_t getAnalogHat(HatEnum a)
Definition: Wii.cpp:1059
uint16_t yawGyroScale
Definition: Wii.h:264
Definition: Wii.h:39
Definition: BTD.h:230
uint8_t getIRs1()
Definition: Wii.h:308
uint8_t getIRs3()
Definition: Wii.h:356
double getNunchuckRoll()
Definition: Wii.h:234
virtual void ACLData(uint8_t *ACLData)
Definition: Wii.cpp:135
double gyroRoll
Definition: Wii.h:249
uint16_t getIRx4()
Definition: Wii.h:364
void setRumbleOn()
Definition: Wii.cpp:866
virtual void Reset()
Definition: Wii.cpp:104
double getRoll()
Definition: Wii.h:120
AnalogHatEnum
int16_t accZnunchuck
Definition: Wii.h:242
uint8_t getBatteryLevel()
Definition: Wii.cpp:919
virtual void disconnect()
Definition: Wii.cpp:118
double getPitch()
Definition: Wii.h:110
int16_t accXnunchuck
Definition: Wii.h:242
uint8_t getWiiState()
Definition: Wii.h:189
uint16_t getIRy2()
Definition: Wii.h:324
int16_t gyroRollRaw
Definition: Wii.h:270
LEDEnum
void IRinitialize()
Definition: Wii.cpp:1096
Definition: Wii.h:37
uint16_t getIRx2()
Definition: Wii.h:316
uint16_t getIRx1()
Definition: Wii.h:292
void setRumbleToggle()
Definition: Wii.cpp:872
int16_t gyroPitchZero
Definition: Wii.h:278
uint16_t pitchGyroScale
Definition: Wii.h:262
double getWiimoteRoll()
Definition: Wii.h:222
WII(BTD *p, bool pair=false)
Definition: Wii.cpp:85
double rollGyroSpeed
Definition: Wii.h:256
uint8_t getIRs4()
Definition: Wii.h:380
uint16_t getIRy3()
Definition: Wii.h:348
void setLedStatus()
Definition: Wii.cpp:906
ButtonEnum
bool getButtonClick(ButtonEnum b)
Definition: Wii.cpp:1048
int16_t accYwiimote
Definition: Wii.h:236
bool wiiUProControllerConnected
Definition: Wii.h:210
void setRumbleOff()
Definition: Wii.cpp:860
void setAllOff()
Definition: Wii.cpp:854
void setLedOff()
Definition: Wii.h:152
uint16_t getIRy1()
Definition: Wii.h:300
uint16_t getIRx3()
Definition: Wii.h:340
double gyroYaw
Definition: Wii.h:251
bool nunchuckConnected
Definition: Wii.h:206
uint16_t getIRy4()
Definition: Wii.h:372
int16_t gyroYawRaw
Definition: Wii.h:269
bool isIRCameraEnabled()
Definition: Wii.h:389
void attachOnInit(void(*funcOnInit)(void))
Definition: Wii.h:197
double getYaw()
Definition: Wii.h:132
double getWiimotePitch()
Definition: Wii.h:218
void pair(void)
Definition: Wii.h:89
void setLedOn(LEDEnum a)
Definition: Wii.cpp:890
double yawGyroSpeed
Definition: Wii.h:257
HatEnum
Definition: Wii.h:35
int16_t accYnunchuck
Definition: Wii.h:242
int16_t gyroYawZero
Definition: Wii.h:276
double pitchGyroSpeed
Definition: Wii.h:255
void pairWithWiimote()
Definition: BTD.h:500
uint8_t getIRs2()
Definition: Wii.h:332
double gyroPitch
Definition: Wii.h:247
Definition: Wii.h:47
bool getButtonPress(ButtonEnum b)
Definition: Wii.cpp:1041
bool motionPlusConnected
Definition: Wii.h:208
int16_t gyroRollZero
Definition: Wii.h:277
virtual void Run()
Definition: Wii.cpp:684
int16_t accXwiimote
Definition: Wii.h:236
int16_t accZwiimote
Definition: Wii.h:236