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 (1 << 0)
28 #define WII_FLAG_NUNCHUCK_CONNECTED (1 << 1)
29 #define WII_FLAG_CALIBRATE_BALANCE_BOARD (1 << 2)
30 
31 #define wii_check_flag(flag) (wii_event_flag & (flag))
32 #define wii_set_flag(flag) (wii_event_flag |= (flag))
33 #define wii_clear_flag(flag) (wii_event_flag &= ~(flag))
34 
36 enum HatEnum {
38  HatX = 0,
40  HatY = 1,
41 };
42 
45  TopRight = 0,
46  BotRight = 1,
47  TopLeft = 2,
48  BotLeft = 3,
49 };
50 
56 class WII : public BluetoothService {
57 public:
64  WII(BTD *p, bool pair = false);
65 
68  void disconnect();
82  bool getButtonPress(ButtonEnum b);
83  bool getButtonClick(ButtonEnum b);
89  void pair(void) {
90  if(pBtd)
92  };
98  uint8_t getAnalogHat(HatEnum a);
104  uint16_t getAnalogHat(AnalogHatEnum a);
105 
110  float getPitch() {
112  return compPitch;
113  return getWiimotePitch();
114  };
115 
120  float getRoll() {
122  return compRoll;
123  return getWiimoteRoll();
124  };
125 
132  float 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  };
207  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
208 
212  float getWiimotePitch() {
213  return (atan2f(accYwiimote, accZwiimote) + PI) * RAD_TO_DEG;
214  };
215 
216  float getWiimoteRoll() {
217  return (atan2f(accXwiimote, accZwiimote) + PI) * RAD_TO_DEG;
218  };
225  return (atan2f(accYnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
226  };
227 
228  float getNunchuckRoll() {
229  return (atan2f(accXnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
230  };
239  /* Variables for the gyro inside the Motion Plus */
241  float gyroPitch;
243  float gyroRoll;
245  float gyroYaw;
246 
256  uint16_t pitchGyroScale;
257  uint16_t rollGyroScale;
258  uint16_t yawGyroScale;
263  int16_t gyroYawRaw;
264  int16_t gyroRollRaw;
265  int16_t gyroPitchRaw;
270  int16_t gyroYawZero;
271  int16_t gyroRollZero;
272  int16_t gyroPitchZero;
282  float getWeight(BalanceBoardEnum pos);
283 
288  float getTotalWeight();
289 
296  return wiiBalanceBoardRaw[pos];
297  };
300 #ifdef WIICAMERA
305  void IRinitialize();
306 
311  uint16_t getIRx1() {
312  return IR_object_x1;
313  };
314 
319  uint16_t getIRy1() {
320  return IR_object_y1;
321  };
322 
327  uint8_t getIRs1() {
328  return IR_object_s1;
329  };
330 
335  uint16_t getIRx2() {
336  return IR_object_x2;
337  };
338 
343  uint16_t getIRy2() {
344  return IR_object_y2;
345  };
346 
351  uint8_t getIRs2() {
352  return IR_object_s2;
353  };
354 
359  uint16_t getIRx3() {
360  return IR_object_x3;
361  };
362 
367  uint16_t getIRy3() {
368  return IR_object_y3;
369  };
370 
375  uint8_t getIRs3() {
376  return IR_object_s3;
377  };
378 
383  uint16_t getIRx4() {
384  return IR_object_x4;
385  };
386 
391  uint16_t getIRy4() {
392  return IR_object_y4;
393  };
394 
399  uint8_t getIRs4() {
400  return IR_object_s4;
401  };
402 
409  return (wiiState & 0x08);
410  };
412 #endif
413 
414 protected:
420  void ACLData(uint8_t* ACLData);
422  void Run();
424  void Reset();
430  void onInit();
433 private:
434  static int8_t getButtonIndexWii(ButtonEnum b);
435  static int8_t getButtonIndexWiiPro(ButtonEnum b);
436 
437  void L2CAP_task(); // L2CAP state machine
438 
439  /* Variables filled from HCI event management */
440  bool activeConnection; // Used to indicate if it's already has established a connection
441 
442  /* Variables used by high level L2CAP task */
443  uint8_t l2cap_state;
444  uint8_t wii_event_flag; // Used for Wii flags
445 
446  uint32_t ButtonState;
447  uint32_t OldButtonState;
448  uint32_t ButtonClickState;
449  uint16_t hatValues[4];
450 
451  uint8_t HIDBuffer[3]; // Used to store HID commands
452 
453  uint16_t stateCounter;
454  bool unknownExtensionConnected;
455  bool extensionConnected;
456  bool checkBatteryLevel; // Set to true when getBatteryLevel() is called otherwise if should be false
457  bool motionPlusInside; // True if it's a new Wiimote with the Motion Plus extension build into it
458 
459  /* L2CAP Channels */
460  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
461  uint8_t control_dcid[2]; // 0x0060
462  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
463  uint8_t interrupt_dcid[2]; // 0x0061
464 
465  /* HID Commands */
466  void HID_Command(uint8_t* data, uint8_t nbytes);
467  void setReportMode(bool continuous, uint8_t mode);
468 
469  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
470  void initExtension1();
471  void initExtension2();
472 
473  void statusRequest(); // Used to update the Wiimote state and battery level
474 
475  void readData(uint32_t offset, uint16_t size, bool EEPROM);
476  void readExtensionType();
477  void readCalData();
478  void readWiiBalanceBoardCalibration(); // Used by the library to read the Wii Balance Board calibration values
479 
480  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
481  void initMotionPlus();
482  void activateMotionPlus();
483 
484  uint16_t wiiBalanceBoardRaw[4]; // Wii Balance Board raw values
485  uint16_t wiiBalanceBoardCal[3][4]; // Wii Balance Board calibration values
486 
487  float compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
488  float compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
489 
490  bool activateNunchuck;
491  bool motionValuesReset; // This bool is true when the gyro values has been reset
492  uint32_t timer;
493 
494  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)
495  uint8_t batteryLevel;
496 
497 #ifdef WIICAMERA
498  /* Private function and variables for the readings from the IR Camera */
499  void enableIRCamera1(); // Sets bit 2 of output report 13
500  void enableIRCamera2(); // Sets bit 2 of output report 1A
501  void writeSensitivityBlock1();
502  void writeSensitivityBlock2();
503  void write0x08Value();
504  void setWiiModeNumber(uint8_t mode_number);
505 
506  uint16_t IR_object_x1; // IR x position 10 bits
507  uint16_t IR_object_y1; // IR y position 10 bits
508  uint8_t IR_object_s1; // IR size value
509  uint16_t IR_object_x2;
510  uint16_t IR_object_y2;
511  uint8_t IR_object_s2;
512  uint16_t IR_object_x3; // IR x position 10 bits
513  uint16_t IR_object_y3; // IR y position 10 bits
514  uint8_t IR_object_s3; // IR size value
515  uint16_t IR_object_x4;
516  uint16_t IR_object_y4;
517  uint8_t IR_object_s4;
518 #endif
519 };
520 #endif
BalanceBoardEnum
Definition: Wii.h:44
@ BotLeft
Definition: Wii.h:48
@ TopLeft
Definition: Wii.h:47
@ TopRight
Definition: Wii.h:45
@ BotRight
Definition: Wii.h:46
HatEnum
Definition: Wii.h:36
@ HatY
Definition: Wii.h:40
@ HatX
Definition: Wii.h:38
Definition: BTD.h:222
void pairWithWiimote()
Definition: BTD.h:498
BTD * pBtd
Definition: BTD.h:647
Definition: Wii.h:56
uint8_t getIRs3()
Definition: Wii.h:375
uint16_t yawGyroScale
Definition: Wii.h:258
int16_t gyroRollRaw
Definition: Wii.h:264
bool isIRCameraEnabled()
Definition: Wii.h:408
void setRumbleOn()
Definition: Wii.cpp:915
int16_t gyroRollZero
Definition: Wii.h:271
bool wiiBalanceBoardConnected
Definition: Wii.h:204
void setRumbleOff()
Definition: Wii.cpp:909
uint8_t getIRs1()
Definition: Wii.h:327
float getPitch()
Definition: Wii.h:110
float getWiimoteRoll()
Definition: Wii.h:216
uint16_t getIRy1()
Definition: Wii.h:319
void IRinitialize()
Definition: Wii.cpp:1193
void setLedOn(LEDEnum a)
Definition: Wii.cpp:939
void pair(void)
Definition: Wii.h:89
int16_t accXnunchuck
Definition: Wii.h:236
int16_t accZnunchuck
Definition: Wii.h:236
bool wiiUProControllerConnected
Definition: Wii.h:202
float yawGyroSpeed
Definition: Wii.h:251
uint16_t getIRx2()
Definition: Wii.h:335
uint16_t getIRx3()
Definition: Wii.h:359
void setLedStatus()
Definition: Wii.cpp:955
uint16_t getIRy3()
Definition: Wii.h:367
uint16_t getWeightRaw(BalanceBoardEnum pos)
Definition: Wii.h:295
WII(BTD *p, bool pair=false)
Definition: Wii.cpp:85
uint16_t rollGyroScale
Definition: Wii.h:257
void setAllOff()
Definition: Wii.cpp:903
bool motionPlusConnected
Definition: Wii.h:200
int16_t gyroPitchRaw
Definition: Wii.h:265
uint16_t getIRy4()
Definition: Wii.h:391
float getWiimotePitch()
Definition: Wii.h:212
void Reset()
Definition: Wii.cpp:101
uint16_t getIRx4()
Definition: Wii.h:383
bool getButtonPress(ButtonEnum b)
Definition: Wii.cpp:1109
bool nunchuckConnected
Definition: Wii.h:198
float rollGyroSpeed
Definition: Wii.h:250
void ACLData(uint8_t *ACLData)
Definition: Wii.cpp:133
void setRumbleToggle()
Definition: Wii.cpp:921
uint8_t getAnalogHat(HatEnum a)
Definition: Wii.cpp:1135
void setLedToggle(LEDEnum a)
Definition: Wii.cpp:949
uint8_t getWiiState()
Definition: Wii.h:189
float getRoll()
Definition: Wii.h:120
float getTotalWeight()
Definition: Wii.cpp:1183
int16_t accYwiimote
Definition: Wii.h:235
float pitchGyroSpeed
Definition: Wii.h:249
void disconnect()
Definition: Wii.cpp:116
float gyroPitch
Definition: Wii.h:241
int16_t gyroYawRaw
Definition: Wii.h:263
void onInit()
Definition: Wii.cpp:1159
int16_t gyroPitchZero
Definition: Wii.h:272
float getWeight(BalanceBoardEnum pos)
Definition: Wii.cpp:1170
float getNunchuckRoll()
Definition: Wii.h:228
float gyroYaw
Definition: Wii.h:245
void Run()
Definition: Wii.cpp:725
bool getButtonClick(ButtonEnum b)
Definition: Wii.cpp:1120
uint8_t getIRs2()
Definition: Wii.h:351
float gyroRoll
Definition: Wii.h:243
int16_t accZwiimote
Definition: Wii.h:235
float getNunchuckPitch()
Definition: Wii.h:224
int16_t gyroYawZero
Definition: Wii.h:270
float getYaw()
Definition: Wii.h:132
uint16_t getIRy2()
Definition: Wii.h:343
bool wiimoteConnected
Definition: Wii.h:191
void setLedOff()
Definition: Wii.h:152
int16_t accYnunchuck
Definition: Wii.h:236
void setLedRaw(uint8_t value)
Definition: Wii.cpp:927
uint8_t getBatteryLevel()
Definition: Wii.cpp:968
uint16_t pitchGyroScale
Definition: Wii.h:256
uint8_t getIRs4()
Definition: Wii.h:399
uint16_t getIRx1()
Definition: Wii.h:311
int16_t accXwiimote
Definition: Wii.h:230
AnalogHatEnum
LEDEnum
ButtonEnum