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:
18  Allan Glover
19  adglover9.81@gmail.com
20  */
21 
22 #ifndef _wii_h_
23 #define _wii_h_
24 
25 #include "BTD.h"
26 #include "controllerEnums.h"
27 
29 #define WIICAMERA
30 
31 /* Bluetooth L2CAP states for L2CAP_task() */
32 #define L2CAP_WAIT 0
33 
34 // These states are used if the Wiimote is the host
35 #define L2CAP_CONTROL_SUCCESS 1
36 #define L2CAP_INTERRUPT_SETUP 2
37 
38 // These states are used if the Arduino is the host
39 #define L2CAP_CONTROL_CONNECT_REQUEST 3
40 #define L2CAP_CONTROL_CONFIG_REQUEST 4
41 #define L2CAP_INTERRUPT_CONNECT_REQUEST 5
42 
43 #define L2CAP_INTERRUPT_CONFIG_REQUEST 6
44 
45 #define L2CAP_CHECK_MOTION_PLUS_STATE 7
46 #define L2CAP_CHECK_EXTENSION_STATE 8
47 #define L2CAP_INIT_MOTION_PLUS_STATE 9
48 #define L2CAP_LED_STATE 10
49 #define L2CAP_DONE 11
50 
51 #define L2CAP_INTERRUPT_DISCONNECT 12
52 #define L2CAP_CONTROL_DISCONNECT 13
53 
54 /* L2CAP event flags */
55 #define L2CAP_FLAG_CONTROL_CONNECTED 0x001
56 #define L2CAP_FLAG_INTERRUPT_CONNECTED 0x002
57 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x004
58 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x008
59 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x040
60 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x080
61 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x100
62 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x200
63 
64 /* Macros for L2CAP event flag tests */
65 #define l2cap_connected_control_flag (l2cap_event_flag & L2CAP_FLAG_CONTROL_CONNECTED)
66 #define l2cap_connected_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_INTERRUPT_CONNECTED)
67 #define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS)
68 #define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)
69 #define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE)
70 #define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE)
71 #define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST)
72 #define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST)
73 
74 /* Wii event flags */
75 #define WII_FLAG_MOTION_PLUS_CONNECTED 0x400
76 #define WII_FLAG_NUNCHUCK_CONNECTED 0x800
77 
78 #define motion_plus_connected_flag (l2cap_event_flag & WII_FLAG_MOTION_PLUS_CONNECTED)
79 #define nunchuck_connected_flag (l2cap_event_flag & WII_FLAG_NUNCHUCK_CONNECTED)
80 
81 #define PAIR 1
82 
84 enum Hat {
86  HatX = 0,
88  HatY = 1,
89 };
90 
96 class WII : public BluetoothService {
97 public:
104  WII(BTD *p, bool pair=false);
105 
111  virtual void ACLData(uint8_t* ACLData);
113  virtual void Run();
115  virtual void Reset();
117  virtual void disconnect();
129  bool getButtonPress(Button b);
130  bool getButtonClick(Button b);
139  uint8_t getAnalogHat(Hat a);
145  uint16_t getAnalogHat(AnalogHat a);
146 
151  double getPitch() { return pitch; };
156  double getRoll() { return roll; };
163  double getYaw() { return gyroYaw; };
164 
166  void setAllOff();
168  void setRumbleOff();
170  void setRumbleOn();
172  void setRumbleToggle();
177  void setLedOff(LED a);
182  void setLedOn(LED a);
187  void setLedToggle(LED a);
197  void setLedStatus();
198 
203  uint8_t getBatteryLevel() { return batteryLevel; };
208  uint8_t getWiiState() { return wiiState; };
213  bool wiimoteConnected;
222  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
223 
226  double wiimotePitch;
227  double wiimoteRoll;
233  double nunchuckRoll;
238  int16_t accX;
239  int16_t accY;
240  int16_t accZ;
243  /* Variables for the gyro inside the Motion Plus */
245  double gyroPitch;
247  double gyroRoll;
249  double gyroYaw;
250 
255  double yawGyroSpeed;
260  uint16_t pitchGyroScale;
261  uint16_t rollGyroScale;
262  uint16_t yawGyroScale;
267  int16_t gyroYawRaw;
268  int16_t gyroRollRaw;
269  int16_t gyroPitchRaw;
274  int16_t gyroYawZero;
275  int16_t gyroRollZero;
276  int16_t gyroPitchZero;
279 #ifdef WIICAMERA
280 
284  void IRinitialize();
285 
290  uint16_t getIRx1() { return IR_object_x1; };
295  uint16_t getIRy1() { return IR_object_y1; };
300  uint8_t getIRs1() { return IR_object_s1; };
301 
306  uint16_t getIRx2() { return IR_object_x2; };
311  uint16_t getIRy2() { return IR_object_y2; };
316  uint8_t getIRs2() { return IR_object_s2; };
317 
322  uint16_t getIRx3() { return IR_object_x3; };
327  uint16_t getIRy3() { return IR_object_y3; };
332  uint8_t getIRs3() { return IR_object_s3; };
333 
338  uint16_t getIRx4() { return IR_object_x4; };
343  uint16_t getIRy4() { return IR_object_y4; };
348  uint8_t getIRs4() { return IR_object_s4; };
349 
355  bool isIRCameraEnabled() { return (wiiState & 0x08); };
357 #endif
358 
359 private:
360  /* Mandatory members */
361  BTD *pBtd;
362 
363  void L2CAP_task(); // L2CAP state machine
364 
365  /* Variables filled from HCI event management */
366  uint16_t hci_handle;
367  bool activeConnection; // Used to indicate if it's already has established a connection
368 
369  /* variables used by high level L2CAP task */
370  uint8_t l2cap_state;
371  uint16_t l2cap_event_flag;// l2cap flags of received bluetooth events
372 
373  uint32_t ButtonState;
374  uint32_t OldButtonState;
375  uint32_t ButtonClickState;
376  uint16_t hatValues[4];
377 
378  uint8_t HIDBuffer[3];// Used to store HID commands
379 
380  uint16_t stateCounter;
381  bool unknownExtensionConnected;
382  bool extensionConnected;
383 
384  /* L2CAP Channels */
385  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
386  uint8_t control_dcid[2]; // 0x0060
387  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
388  uint8_t interrupt_dcid[2]; // 0x0061
389  uint8_t identifier; // Identifier for connection
390 
391  /* HID Commands */
392  void HID_Command(uint8_t* data, uint8_t nbytes);
393  void setReportMode(bool continuous, uint8_t mode);
394  void statusRequest();
395 
396  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
397  void initExtension1();
398  void initExtension2();
399 
400  void readData(uint32_t offset, uint16_t size, bool EEPROM);
401  void readExtensionType();
402  void readCalData();
403 
404  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
405  void initMotionPlus();
406  void activateMotionPlus();
407 
408  double pitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
409  double roll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
410 
411  bool activateNunchuck;
412  bool motionValuesReset; // This bool is true when the gyro values has been reset
413  unsigned long timer;
414 
415  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)
416  uint8_t batteryLevel;
417 
418 #ifdef WIICAMERA
419  /* Private function and variables for the readings from teh IR Camera */
420  void enableIRCamera1(); // Sets bit 2 of output report 13
421  void enableIRCamera2(); // Sets bit 2 of output report 1A
422  void writeSensitivityBlock1();
423  void writeSensitivityBlock2();
424  void write0x08Value();
425  void setWiiModeNumber(uint8_t mode_number);
426 
427  uint16_t IR_object_x1; // IR x position 10 bits
428  uint16_t IR_object_y1; // IR y position 10 bits
429  uint8_t IR_object_s1; // IR size value
430  uint16_t IR_object_x2;
431  uint16_t IR_object_y2;
432  uint8_t IR_object_s2;
433  uint16_t IR_object_x3; // IR x position 10 bits
434  uint16_t IR_object_y3; // IR y position 10 bits
435  uint8_t IR_object_s3; // IR size value
436  uint16_t IR_object_x4;
437  uint16_t IR_object_y4;
438  uint8_t IR_object_s4;
439 #endif
440 };
441 #endif