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  };
196  bool wiimoteConnected;
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
301 
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 
435  void L2CAP_task(); // L2CAP state machine
436 
437  /* Variables filled from HCI event management */
438  bool activeConnection; // Used to indicate if it's already has established a connection
439 
440  /* Variables used by high level L2CAP task */
441  uint8_t l2cap_state;
442  uint8_t wii_event_flag; // Used for Wii flags
443 
444  uint32_t ButtonState;
445  uint32_t OldButtonState;
446  uint32_t ButtonClickState;
447  uint16_t hatValues[4];
448 
449  uint8_t HIDBuffer[3]; // Used to store HID commands
450 
451  uint16_t stateCounter;
452  bool unknownExtensionConnected;
453  bool extensionConnected;
454  bool checkBatteryLevel; // Set to true when getBatteryLevel() is called otherwise if should be false
455  bool motionPlusInside; // True if it's a new Wiimote with the Motion Plus extension build into it
456 
457  /* L2CAP Channels */
458  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
459  uint8_t control_dcid[2]; // 0x0060
460  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
461  uint8_t interrupt_dcid[2]; // 0x0061
462 
463  /* HID Commands */
464  void HID_Command(uint8_t* data, uint8_t nbytes);
465  void setReportMode(bool continuous, uint8_t mode);
466 
467  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
468  void initExtension1();
469  void initExtension2();
470 
471  void statusRequest(); // Used to update the Wiimote state and battery level
472 
473  void readData(uint32_t offset, uint16_t size, bool EEPROM);
474  void readExtensionType();
475  void readCalData();
476  void readWiiBalanceBoardCalibration(); // Used by the library to read the Wii Balance Board calibration values
477 
478  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
479  void initMotionPlus();
480  void activateMotionPlus();
481 
482  uint16_t wiiBalanceBoardRaw[4]; // Wii Balance Board raw values
483  uint16_t wiiBalanceBoardCal[3][4]; // Wii Balance Board calibration values
484 
485  float compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
486  float compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
487 
488  bool activateNunchuck;
489  bool motionValuesReset; // This bool is true when the gyro values has been reset
490  uint32_t timer;
491 
492  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)
493  uint8_t batteryLevel;
494 
495 #ifdef WIICAMERA
496  /* Private function and variables for the readings from the IR Camera */
497  void enableIRCamera1(); // Sets bit 2 of output report 13
498  void enableIRCamera2(); // Sets bit 2 of output report 1A
499  void writeSensitivityBlock1();
500  void writeSensitivityBlock2();
501  void write0x08Value();
502  void setWiiModeNumber(uint8_t mode_number);
503 
504  uint16_t IR_object_x1; // IR x position 10 bits
505  uint16_t IR_object_y1; // IR y position 10 bits
506  uint8_t IR_object_s1; // IR size value
507  uint16_t IR_object_x2;
508  uint16_t IR_object_y2;
509  uint8_t IR_object_s2;
510  uint16_t IR_object_x3; // IR x position 10 bits
511  uint16_t IR_object_y3; // IR y position 10 bits
512  uint8_t IR_object_s3; // IR size value
513  uint16_t IR_object_x4;
514  uint16_t IR_object_y4;
515  uint8_t IR_object_s4;
516 #endif
517 };
518 #endif
bool wiimoteConnected
Definition: Wii.h:191
void onInit()
Definition: Wii.cpp:1128
int16_t gyroPitchRaw
Definition: Wii.h:265
uint16_t rollGyroScale
Definition: Wii.h:257
void setLedToggle(LEDEnum a)
Definition: Wii.cpp:942
void setLedRaw(uint8_t value)
Definition: Wii.cpp:920
uint8_t getAnalogHat(HatEnum a)
Definition: Wii.cpp:1104
uint16_t yawGyroScale
Definition: Wii.h:258
Definition: Wii.h:40
Definition: BTD.h:198
uint8_t getIRs1()
Definition: Wii.h:327
uint8_t getIRs3()
Definition: Wii.h:375
void ACLData(uint8_t *ACLData)
Definition: Wii.cpp:133
uint16_t getIRx4()
Definition: Wii.h:383
float pitchGyroSpeed
Definition: Wii.h:249
void setRumbleOn()
Definition: Wii.cpp:908
void Reset()
Definition: Wii.cpp:101
AnalogHatEnum
int16_t accZnunchuck
Definition: Wii.h:236
Definition: Wii.h:48
uint8_t getBatteryLevel()
Definition: Wii.cpp:961
void disconnect()
Definition: Wii.cpp:116
Definition: Wii.h:47
int16_t accXnunchuck
Definition: Wii.h:236
uint8_t getWiiState()
Definition: Wii.h:189
uint16_t getIRy2()
Definition: Wii.h:343
int16_t gyroRollRaw
Definition: Wii.h:264
LEDEnum
void IRinitialize()
Definition: Wii.cpp:1162
Definition: Wii.h:38
uint16_t getIRx2()
Definition: Wii.h:335
uint16_t getIRx1()
Definition: Wii.h:311
void setRumbleToggle()
Definition: Wii.cpp:914
int16_t gyroPitchZero
Definition: Wii.h:272
uint16_t pitchGyroScale
Definition: Wii.h:256
float getPitch()
Definition: Wii.h:110
WII(BTD *p, bool pair=false)
Definition: Wii.cpp:85
float getNunchuckPitch()
Definition: Wii.h:224
uint8_t getIRs4()
Definition: Wii.h:399
float rollGyroSpeed
Definition: Wii.h:250
uint16_t getIRy3()
Definition: Wii.h:367
void setLedStatus()
Definition: Wii.cpp:948
BalanceBoardEnum
Definition: Wii.h:44
ButtonEnum
bool getButtonClick(ButtonEnum b)
Definition: Wii.cpp:1093
int16_t accYwiimote
Definition: Wii.h:230
bool wiiUProControllerConnected
Definition: Wii.h:202
float getYaw()
Definition: Wii.h:132
void setRumbleOff()
Definition: Wii.cpp:902
bool wiiBalanceBoardConnected
Definition: Wii.h:204
float gyroRoll
Definition: Wii.h:243
void setAllOff()
Definition: Wii.cpp:896
void setLedOff()
Definition: Wii.h:152
float getWiimoteRoll()
Definition: Wii.h:216
uint16_t getIRy1()
Definition: Wii.h:319
uint16_t getIRx3()
Definition: Wii.h:359
BTD * pBtd
Definition: BTD.h:608
bool nunchuckConnected
Definition: Wii.h:198
uint16_t getIRy4()
Definition: Wii.h:391
float gyroPitch
Definition: Wii.h:241
Definition: Wii.h:45
int16_t gyroYawRaw
Definition: Wii.h:263
float gyroYaw
Definition: Wii.h:245
bool isIRCameraEnabled()
Definition: Wii.h:408
float yawGyroSpeed
Definition: Wii.h:251
void pair(void)
Definition: Wii.h:89
void setLedOn(LEDEnum a)
Definition: Wii.cpp:932
uint16_t getWeightRaw(BalanceBoardEnum pos)
Definition: Wii.h:295
float getRoll()
Definition: Wii.h:120
HatEnum
Definition: Wii.h:36
int16_t accYnunchuck
Definition: Wii.h:236
int16_t gyroYawZero
Definition: Wii.h:270
Definition: Wii.h:46
void pairWithWiimote()
Definition: BTD.h:464
uint8_t getIRs2()
Definition: Wii.h:351
Definition: Wii.h:56
bool getButtonPress(ButtonEnum b)
Definition: Wii.cpp:1086
float getWiimotePitch()
Definition: Wii.h:212
bool motionPlusConnected
Definition: Wii.h:200
int16_t gyroRollZero
Definition: Wii.h:271
float getTotalWeight()
Definition: Wii.cpp:1152
float getNunchuckRoll()
Definition: Wii.h:228
void Run()
Definition: Wii.cpp:718
int16_t accXwiimote
Definition: Wii.h:230
float getWeight(BalanceBoardEnum pos)
Definition: Wii.cpp:1139
int16_t accZwiimote
Definition: Wii.h:230