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);
135  void pair(void) {
136  if(pBtd)
137  pBtd->pairWithWiimote();
138  }
144  uint8_t getAnalogHat(Hat a);
150  uint16_t getAnalogHat(AnalogHat a);
151 
156  double getPitch() {
157  return pitch;
158  };
159 
164  double getRoll() {
165  return roll;
166  };
167 
174  double getYaw() {
175  return gyroYaw;
176  };
177 
179  void setAllOff();
181  void setRumbleOff();
183  void setRumbleOn();
185  void setRumbleToggle();
186 
191  void setLedRaw(uint8_t value);
196  void setLedOff(LED a);
201  void setLedOn(LED a);
206  void setLedToggle(LED a);
216  void setLedStatus();
217 
221  void statusRequest();
226  uint8_t getBatteryLevel() {
227  return batteryLevel;
228  };
233  uint8_t getWiiState() {
234  return wiiState;
235  };
240  bool wiimoteConnected;
249  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
250 
253  double wiimotePitch;
254  double wiimoteRoll;
260  double nunchuckRoll;
265  int16_t accX;
266  int16_t accY;
267  int16_t accZ;
270  /* Variables for the gyro inside the Motion Plus */
272  double gyroPitch;
274  double gyroRoll;
276  double gyroYaw;
277 
282  double yawGyroSpeed;
287  uint16_t pitchGyroScale;
288  uint16_t rollGyroScale;
289  uint16_t yawGyroScale;
294  int16_t gyroYawRaw;
295  int16_t gyroRollRaw;
296  int16_t gyroPitchRaw;
301  int16_t gyroYawZero;
302  int16_t gyroRollZero;
303  int16_t gyroPitchZero;
306 #ifdef WIICAMERA
307 
311  void IRinitialize();
312 
317  uint16_t getIRx1() {
318  return IR_object_x1;
319  };
320 
325  uint16_t getIRy1() {
326  return IR_object_y1;
327  };
328 
333  uint8_t getIRs1() {
334  return IR_object_s1;
335  };
336 
341  uint16_t getIRx2() {
342  return IR_object_x2;
343  };
344 
349  uint16_t getIRy2() {
350  return IR_object_y2;
351  };
352 
357  uint8_t getIRs2() {
358  return IR_object_s2;
359  };
360 
365  uint16_t getIRx3() {
366  return IR_object_x3;
367  };
368 
373  uint16_t getIRy3() {
374  return IR_object_y3;
375  };
376 
381  uint8_t getIRs3() {
382  return IR_object_s3;
383  };
384 
389  uint16_t getIRx4() {
390  return IR_object_x4;
391  };
392 
397  uint16_t getIRy4() {
398  return IR_object_y4;
399  };
400 
405  uint8_t getIRs4() {
406  return IR_object_s4;
407  };
408 
415  return(wiiState & 0x08);
416  };
418 #endif
419 
420 private:
421  /* Mandatory members */
422  BTD *pBtd;
423 
424  void L2CAP_task(); // L2CAP state machine
425 
426  /* Variables filled from HCI event management */
427  uint16_t hci_handle;
428  bool activeConnection; // Used to indicate if it's already has established a connection
429 
430  /* variables used by high level L2CAP task */
431  uint8_t l2cap_state;
432  uint16_t l2cap_event_flag; // l2cap flags of received bluetooth events
433 
434  uint32_t ButtonState;
435  uint32_t OldButtonState;
436  uint32_t ButtonClickState;
437  uint16_t hatValues[4];
438 
439  uint8_t HIDBuffer[3]; // Used to store HID commands
440 
441  uint16_t stateCounter;
442  bool unknownExtensionConnected;
443  bool extensionConnected;
444 
445  /* L2CAP Channels */
446  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
447  uint8_t control_dcid[2]; // 0x0060
448  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
449  uint8_t interrupt_dcid[2]; // 0x0061
450  uint8_t identifier; // Identifier for connection
451 
452  /* HID Commands */
453  void HID_Command(uint8_t* data, uint8_t nbytes);
454  void setReportMode(bool continuous, uint8_t mode);
455 
456  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
457  void initExtension1();
458  void initExtension2();
459 
460  void readData(uint32_t offset, uint16_t size, bool EEPROM);
461  void readExtensionType();
462  void readCalData();
463 
464  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
465  void initMotionPlus();
466  void activateMotionPlus();
467 
468  double pitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
469  double roll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
470 
471  bool activateNunchuck;
472  bool motionValuesReset; // This bool is true when the gyro values has been reset
473  unsigned long timer;
474 
475  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)
476  uint8_t batteryLevel;
477 
478 #ifdef WIICAMERA
479  /* Private function and variables for the readings from teh IR Camera */
480  void enableIRCamera1(); // Sets bit 2 of output report 13
481  void enableIRCamera2(); // Sets bit 2 of output report 1A
482  void writeSensitivityBlock1();
483  void writeSensitivityBlock2();
484  void write0x08Value();
485  void setWiiModeNumber(uint8_t mode_number);
486 
487  uint16_t IR_object_x1; // IR x position 10 bits
488  uint16_t IR_object_y1; // IR y position 10 bits
489  uint8_t IR_object_s1; // IR size value
490  uint16_t IR_object_x2;
491  uint16_t IR_object_y2;
492  uint8_t IR_object_s2;
493  uint16_t IR_object_x3; // IR x position 10 bits
494  uint16_t IR_object_y3; // IR y position 10 bits
495  uint8_t IR_object_s3; // IR size value
496  uint16_t IR_object_x4;
497  uint16_t IR_object_y4;
498  uint8_t IR_object_s4;
499 #endif
500 };
501 #endif