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  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
BTD.h
WII::setLedRaw
void setLedRaw(uint8_t value)
Definition: Wii.cpp:927
WII::setLedOff
void setLedOff()
Definition: Wii.h:152
WII::getButtonClick
bool getButtonClick(ButtonEnum b)
Definition: Wii.cpp:1120
WII::setRumbleOff
void setRumbleOff()
Definition: Wii.cpp:909
WII::gyroRoll
float gyroRoll
Definition: Wii.h:243
WII::setAllOff
void setAllOff()
Definition: Wii.cpp:903
WII::disconnect
void disconnect()
Definition: Wii.cpp:116
WII::gyroYawRaw
int16_t gyroYawRaw
Definition: Wii.h:263
WII::getIRy1
uint16_t getIRy1()
Definition: Wii.h:319
WII::pitchGyroSpeed
float pitchGyroSpeed
Definition: Wii.h:249
WII::getIRx3
uint16_t getIRx3()
Definition: Wii.h:359
WII::onInit
void onInit()
Definition: Wii.cpp:1159
WII::getWiimoteRoll
float getWiimoteRoll()
Definition: Wii.h:216
WII::getIRy4
uint16_t getIRy4()
Definition: Wii.h:391
WII::ACLData
void ACLData(uint8_t *ACLData)
Definition: Wii.cpp:133
WII::wiiBalanceBoardConnected
bool wiiBalanceBoardConnected
Definition: Wii.h:204
WII::getIRs1
uint8_t getIRs1()
Definition: Wii.h:327
WII::IRinitialize
void IRinitialize()
Definition: Wii.cpp:1193
HatY
@ HatY
Definition: Wii.h:40
WII::getIRx2
uint16_t getIRx2()
Definition: Wii.h:335
WII::yawGyroSpeed
float yawGyroSpeed
Definition: Wii.h:251
WII::Reset
void Reset()
Definition: Wii.cpp:101
BTD::pairWithWiimote
void pairWithWiimote()
Definition: BTD.h:497
BTD
Definition: BTD.h:221
WII::getWeightRaw
uint16_t getWeightRaw(BalanceBoardEnum pos)
Definition: Wii.h:295
WII::getWiiState
uint8_t getWiiState()
Definition: Wii.h:189
BotLeft
@ BotLeft
Definition: Wii.h:48
WII::getWiimotePitch
float getWiimotePitch()
Definition: Wii.h:212
WII::accXnunchuck
int16_t accXnunchuck
Definition: Wii.h:236
LEDEnum
LEDEnum
Definition: controllerEnums.h:31
TopLeft
@ TopLeft
Definition: Wii.h:47
WII::accZwiimote
int16_t accZwiimote
Definition: Wii.h:235
WII::getYaw
float getYaw()
Definition: Wii.h:132
WII::getButtonPress
bool getButtonPress(ButtonEnum b)
Definition: Wii.cpp:1109
WII::Run
void Run()
Definition: Wii.cpp:725
WII::accXwiimote
int16_t accXwiimote
Definition: Wii.h:230
WII::pitchGyroScale
uint16_t pitchGyroScale
Definition: Wii.h:256
WII::WII
WII(BTD *p, bool pair=false)
Definition: Wii.cpp:85
WII::wiimoteConnected
bool wiimoteConnected
Definition: Wii.h:191
BluetoothService::pBtd
BTD * pBtd
Definition: BTD.h:646
WII::getIRx1
uint16_t getIRx1()
Definition: Wii.h:311
WII::getIRs4
uint8_t getIRs4()
Definition: Wii.h:399
WII::setLedToggle
void setLedToggle(LEDEnum a)
Definition: Wii.cpp:949
WII::getTotalWeight
float getTotalWeight()
Definition: Wii.cpp:1183
HatX
@ HatX
Definition: Wii.h:38
WII::getPitch
float getPitch()
Definition: Wii.h:110
WII::rollGyroScale
uint16_t rollGyroScale
Definition: Wii.h:257
controllerEnums.h
WII::getNunchuckRoll
float getNunchuckRoll()
Definition: Wii.h:228
WII::gyroPitchRaw
int16_t gyroPitchRaw
Definition: Wii.h:265
HatEnum
HatEnum
Definition: Wii.h:36
WII::getWeight
float getWeight(BalanceBoardEnum pos)
Definition: Wii.cpp:1170
WII::nunchuckConnected
bool nunchuckConnected
Definition: Wii.h:198
WII::rollGyroSpeed
float rollGyroSpeed
Definition: Wii.h:250
WII::setLedStatus
void setLedStatus()
Definition: Wii.cpp:955
WII::getIRx4
uint16_t getIRx4()
Definition: Wii.h:383
WII
Definition: Wii.h:56
WII::getAnalogHat
uint8_t getAnalogHat(HatEnum a)
Definition: Wii.cpp:1135
WII::accYwiimote
int16_t accYwiimote
Definition: Wii.h:235
WII::yawGyroScale
uint16_t yawGyroScale
Definition: Wii.h:258
WII::setLedOn
void setLedOn(LEDEnum a)
Definition: Wii.cpp:939
WII::getIRs3
uint8_t getIRs3()
Definition: Wii.h:375
WII::setRumbleOn
void setRumbleOn()
Definition: Wii.cpp:915
BotRight
@ BotRight
Definition: Wii.h:46
BalanceBoardEnum
BalanceBoardEnum
Definition: Wii.h:44
WII::gyroPitch
float gyroPitch
Definition: Wii.h:241
BluetoothService
Definition: BTD.h:603
WII::accZnunchuck
int16_t accZnunchuck
Definition: Wii.h:236
WII::getBatteryLevel
uint8_t getBatteryLevel()
Definition: Wii.cpp:968
WII::motionPlusConnected
bool motionPlusConnected
Definition: Wii.h:200
WII::gyroRollRaw
int16_t gyroRollRaw
Definition: Wii.h:264
WII::gyroYaw
float gyroYaw
Definition: Wii.h:245
WII::pair
void pair(void)
Definition: Wii.h:89
WII::isIRCameraEnabled
bool isIRCameraEnabled()
Definition: Wii.h:408
TopRight
@ TopRight
Definition: Wii.h:45
WII::gyroRollZero
int16_t gyroRollZero
Definition: Wii.h:271
WII::setRumbleToggle
void setRumbleToggle()
Definition: Wii.cpp:921
WII::gyroPitchZero
int16_t gyroPitchZero
Definition: Wii.h:272
WII::getRoll
float getRoll()
Definition: Wii.h:120
WII::accYnunchuck
int16_t accYnunchuck
Definition: Wii.h:236
WII::getIRy2
uint16_t getIRy2()
Definition: Wii.h:343
WII::gyroYawZero
int16_t gyroYawZero
Definition: Wii.h:270
ButtonEnum
ButtonEnum
Definition: controllerEnums.h:78
WII::getIRs2
uint8_t getIRs2()
Definition: Wii.h:351
AnalogHatEnum
AnalogHatEnum
Definition: controllerEnums.h:213
WII::getIRy3
uint16_t getIRy3()
Definition: Wii.h:367
WII::wiiUProControllerConnected
bool wiiUProControllerConnected
Definition: Wii.h:202
WII::getNunchuckPitch
float getNunchuckPitch()
Definition: Wii.h:224