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 
27 #define WIICAMERA
28 
29 /* Bluetooth L2CAP states for L2CAP_task() */
30 #define L2CAP_WAIT 0
31 
32 // These states are used if the Wiimote is the host
33 #define L2CAP_CONTROL_SUCCESS 1
34 #define L2CAP_INTERRUPT_SETUP 2
35 
36 // These states are used if the Arduino is the host
37 #define L2CAP_CONTROL_CONNECT_REQUEST 3
38 #define L2CAP_CONTROL_CONFIG_REQUEST 4
39 #define L2CAP_INTERRUPT_CONNECT_REQUEST 5
40 
41 #define L2CAP_INTERRUPT_CONFIG_REQUEST 6
42 
43 #define L2CAP_CHECK_MOTION_PLUS_STATE 7
44 #define L2CAP_CHECK_EXTENSION_STATE 8
45 #define L2CAP_INIT_MOTION_PLUS_STATE 9
46 #define L2CAP_LED_STATE 10
47 #define L2CAP_DONE 11
48 
49 #define L2CAP_INTERRUPT_DISCONNECT 12
50 #define L2CAP_CONTROL_DISCONNECT 13
51 
52 /* L2CAP event flags */
53 #define L2CAP_FLAG_CONTROL_CONNECTED 0x001
54 #define L2CAP_FLAG_INTERRUPT_CONNECTED 0x002
55 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x004
56 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x008
57 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x040
58 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x080
59 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x100
60 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x200
61 
62 /* Macros for L2CAP event flag tests */
63 #define l2cap_connected_control_flag (l2cap_event_flag & L2CAP_FLAG_CONTROL_CONNECTED)
64 #define l2cap_connected_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_INTERRUPT_CONNECTED)
65 #define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS)
66 #define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)
67 #define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE)
68 #define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE)
69 #define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST)
70 #define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST)
71 
72 /* Wii event flags */
73 #define WII_FLAG_MOTION_PLUS_CONNECTED 0x400
74 #define WII_FLAG_NUNCHUCK_CONNECTED 0x800
75 
76 #define motion_plus_connected_flag (l2cap_event_flag & WII_FLAG_MOTION_PLUS_CONNECTED)
77 #define nunchuck_connected_flag (l2cap_event_flag & WII_FLAG_NUNCHUCK_CONNECTED)
78 
79 #define PAIR 1
80 
82 enum Hat {
84  HatX = 0,
86  HatY = 1,
87 };
88 
94 class WII : public BluetoothService {
95 public:
102  WII(BTD *p, bool pair = false);
103 
109  virtual void ACLData(uint8_t* ACLData);
111  virtual void Run();
113  virtual void Reset();
115  virtual void disconnect();
127  bool getButtonPress(Button b);
128  bool getButtonClick(Button b);
133  void pair(void) {
134  if(pBtd)
135  pBtd->pairWithWiimote();
136  }
142  uint8_t getAnalogHat(Hat a);
148  uint16_t getAnalogHat(AnalogHat a);
149 
154  double getPitch() {
156  return compPitch;
157  return getWiimotePitch();
158  };
159 
164  double getRoll() {
166  return compRoll;
167  return getWiimoteRoll();
168  };
169 
176  double getYaw() {
177  return gyroYaw;
178  };
179 
181  void setAllOff();
183  void setRumbleOff();
185  void setRumbleOn();
187  void setRumbleToggle();
188 
193  void setLedRaw(uint8_t value);
198  void setLedOff(LED a);
203  void setLedOn(LED a);
208  void setLedToggle(LED a);
218  void setLedStatus();
219 
224  uint8_t getBatteryLevel();
229  uint8_t getWiiState() {
230  return wiiState;
231  };
232 
237  void attachOnInit(void (*funcOnInit)(void)) {
238  pFuncOnInit = funcOnInit;
239  };
244  bool wiimoteConnected;
253  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
254 
257  double getWiimotePitch() {
258  return (atan2(accYwiimote, accZwiimote) + PI) * RAD_TO_DEG;
259  };
260  double getWiimoteRoll() {
261  return (atan2(accXwiimote, accZwiimote) + PI) * RAD_TO_DEG;
262  };
267  double getNunchuckPitch() {
268  return (atan2(accYnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
269  };
270  double getNunchuckRoll() {
271  return (atan2(accXnunchuck, accZnunchuck) + PI) * RAD_TO_DEG;
272  };
281  /* Variables for the gyro inside the Motion Plus */
283  double gyroPitch;
285  double gyroRoll;
287  double gyroYaw;
288 
293  double yawGyroSpeed;
298  uint16_t pitchGyroScale;
299  uint16_t rollGyroScale;
300  uint16_t yawGyroScale;
305  int16_t gyroYawRaw;
306  int16_t gyroRollRaw;
307  int16_t gyroPitchRaw;
312  int16_t gyroYawZero;
313  int16_t gyroRollZero;
314  int16_t gyroPitchZero;
317 #ifdef WIICAMERA
318 
322  void IRinitialize();
323 
328  uint16_t getIRx1() {
329  return IR_object_x1;
330  };
331 
336  uint16_t getIRy1() {
337  return IR_object_y1;
338  };
339 
344  uint8_t getIRs1() {
345  return IR_object_s1;
346  };
347 
352  uint16_t getIRx2() {
353  return IR_object_x2;
354  };
355 
360  uint16_t getIRy2() {
361  return IR_object_y2;
362  };
363 
368  uint8_t getIRs2() {
369  return IR_object_s2;
370  };
371 
376  uint16_t getIRx3() {
377  return IR_object_x3;
378  };
379 
384  uint16_t getIRy3() {
385  return IR_object_y3;
386  };
387 
392  uint8_t getIRs3() {
393  return IR_object_s3;
394  };
395 
400  uint16_t getIRx4() {
401  return IR_object_x4;
402  };
403 
408  uint16_t getIRy4() {
409  return IR_object_y4;
410  };
411 
416  uint8_t getIRs4() {
417  return IR_object_s4;
418  };
419 
426  return(wiiState & 0x08);
427  };
429 #endif
430 
431 private:
432  BTD *pBtd; // Pointer to BTD instance
433 
439  void onInit();
440  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
441 
442  void L2CAP_task(); // L2CAP state machine
443 
444  /* Variables filled from HCI event management */
445  uint16_t hci_handle;
446  bool activeConnection; // Used to indicate if it's already has established a connection
447 
448  /* Variables used by high level L2CAP task */
449  uint8_t l2cap_state;
450  uint16_t l2cap_event_flag; // l2cap flags of received Bluetooth events
451 
452  uint32_t ButtonState;
453  uint32_t OldButtonState;
454  uint32_t ButtonClickState;
455  uint16_t hatValues[4];
456 
457  uint8_t HIDBuffer[3]; // Used to store HID commands
458 
459  uint16_t stateCounter;
460  bool unknownExtensionConnected;
461  bool extensionConnected;
462  bool checkExtension; // Set to false when getBatteryLevel() is called otherwise if should be true
463  bool motionPlusInside; // True if it's a new Wiimote with the Motion Plus extension build into it
464 
465  /* L2CAP Channels */
466  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
467  uint8_t control_dcid[2]; // 0x0060
468  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
469  uint8_t interrupt_dcid[2]; // 0x0061
470  uint8_t identifier; // Identifier for connection
471 
472  /* HID Commands */
473  void HID_Command(uint8_t* data, uint8_t nbytes);
474  void setReportMode(bool continuous, uint8_t mode);
475 
476  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
477  void initExtension1();
478  void initExtension2();
479 
480  void statusRequest(); // Used to update the Wiimote state and battery level
481 
482  void readData(uint32_t offset, uint16_t size, bool EEPROM);
483  void readExtensionType();
484  void readCalData();
485 
486  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
487  void initMotionPlus();
488  void activateMotionPlus();
489 
490  double compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
491  double compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
492 
493  bool activateNunchuck;
494  bool motionValuesReset; // This bool is true when the gyro values has been reset
495  unsigned long timer;
496 
497  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)
498  uint8_t batteryLevel;
499 
500 #ifdef WIICAMERA
501  /* Private function and variables for the readings from the IR Camera */
502  void enableIRCamera1(); // Sets bit 2 of output report 13
503  void enableIRCamera2(); // Sets bit 2 of output report 1A
504  void writeSensitivityBlock1();
505  void writeSensitivityBlock2();
506  void write0x08Value();
507  void setWiiModeNumber(uint8_t mode_number);
508 
509  uint16_t IR_object_x1; // IR x position 10 bits
510  uint16_t IR_object_y1; // IR y position 10 bits
511  uint8_t IR_object_s1; // IR size value
512  uint16_t IR_object_x2;
513  uint16_t IR_object_y2;
514  uint8_t IR_object_s2;
515  uint16_t IR_object_x3; // IR x position 10 bits
516  uint16_t IR_object_y3; // IR y position 10 bits
517  uint8_t IR_object_s3; // IR size value
518  uint16_t IR_object_x4;
519  uint16_t IR_object_y4;
520  uint8_t IR_object_s4;
521 #endif
522 };
523 #endif
bool wiimoteConnected
Definition: Wii.h:239
int16_t gyroPitchRaw
Definition: Wii.h:307
uint16_t rollGyroScale
Definition: Wii.h:299
double getNunchuckPitch()
Definition: Wii.h:267
void setLedRaw(uint8_t value)
Definition: Wii.cpp:876
uint16_t yawGyroScale
Definition: Wii.h:300
Definition: BTD.h:156
uint8_t getIRs1()
Definition: Wii.h:344
Definition: Wii.h:84
uint8_t getIRs3()
Definition: Wii.h:392
double getNunchuckRoll()
Definition: Wii.h:270
virtual void ACLData(uint8_t *ACLData)
Definition: Wii.cpp:134
double gyroRoll
Definition: Wii.h:285
uint16_t getIRx4()
Definition: Wii.h:400
void setLedToggle(LED a)
Definition: Wii.cpp:893
void setRumbleOn()
Definition: Wii.cpp:864
virtual void Reset()
Definition: Wii.cpp:103
double getRoll()
Definition: Wii.h:164
int16_t accZnunchuck
Definition: Wii.h:278
uint8_t getBatteryLevel()
Definition: Wii.cpp:912
virtual void disconnect()
Definition: Wii.cpp:117
void setLedOff(LED a)
Definition: Wii.cpp:881
double getPitch()
Definition: Wii.h:154
int16_t accXnunchuck
Definition: Wii.h:278
bool getButtonClick(Button b)
Definition: Wii.cpp:1041
uint8_t getWiiState()
Definition: Wii.h:229
uint16_t getIRy2()
Definition: Wii.h:360
int16_t gyroRollRaw
Definition: Wii.h:306
void IRinitialize()
Definition: Wii.cpp:1089
Definition: Wii.h:86
uint16_t getIRx2()
Definition: Wii.h:352
uint16_t getIRx1()
Definition: Wii.h:328
void setRumbleToggle()
Definition: Wii.cpp:870
int16_t gyroPitchZero
Definition: Wii.h:314
uint16_t pitchGyroScale
Definition: Wii.h:298
double getWiimoteRoll()
Definition: Wii.h:260
WII(BTD *p, bool pair=false)
Definition: Wii.cpp:84
double rollGyroSpeed
Definition: Wii.h:292
uint8_t getIRs4()
Definition: Wii.h:416
uint16_t getIRy3()
Definition: Wii.h:384
void setLedStatus()
Definition: Wii.cpp:899
void setLedOn(LED a)
Definition: Wii.cpp:887
int16_t accYwiimote
Definition: Wii.h:272
bool wiiUProControllerConnected
Definition: Wii.h:250
void setRumbleOff()
Definition: Wii.cpp:858
void setAllOff()
Definition: Wii.cpp:852
uint16_t getIRy1()
Definition: Wii.h:336
uint16_t getIRx3()
Definition: Wii.h:376
double gyroYaw
Definition: Wii.h:287
bool nunchuckConnected
Definition: Wii.h:246
uint16_t getIRy4()
Definition: Wii.h:408
int16_t gyroYawRaw
Definition: Wii.h:305
bool getButtonPress(Button b)
Definition: Wii.cpp:1034
bool isIRCameraEnabled()
Definition: Wii.h:425
Hat
Definition: Wii.h:82
void attachOnInit(void(*funcOnInit)(void))
Definition: Wii.h:237
double getYaw()
Definition: Wii.h:176
double getWiimotePitch()
Definition: Wii.h:257
void pair(void)
Definition: Wii.h:133
Button
double yawGyroSpeed
Definition: Wii.h:293
int16_t accYnunchuck
Definition: Wii.h:278
int16_t gyroYawZero
Definition: Wii.h:312
double pitchGyroSpeed
Definition: Wii.h:291
void pairWithWiimote()
Definition: BTD.h:412
uint8_t getIRs2()
Definition: Wii.h:368
double gyroPitch
Definition: Wii.h:283
Definition: Wii.h:94
bool motionPlusConnected
Definition: Wii.h:248
int16_t gyroRollZero
Definition: Wii.h:313
AnalogHat
virtual void Run()
Definition: Wii.cpp:682
int16_t accXwiimote
Definition: Wii.h:272
uint8_t getAnalogHat(Hat a)
Definition: Wii.cpp:1052
int16_t accZwiimote
Definition: Wii.h:272