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() {
152  return pitch;
153  };
154 
159  double getRoll() {
160  return roll;
161  };
162 
169  double getYaw() {
170  return gyroYaw;
171  };
172 
174  void setAllOff();
176  void setRumbleOff();
178  void setRumbleOn();
180  void setRumbleToggle();
181 
186  void setLedRaw(uint8_t value);
191  void setLedOff(LED a);
196  void setLedOn(LED a);
201  void setLedToggle(LED a);
211  void setLedStatus();
212 
216  void statusRequest();
221  uint8_t getBatteryLevel() {
222  return batteryLevel;
223  };
228  uint8_t getWiiState() {
229  return wiiState;
230  };
235  bool wiimoteConnected;
244  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
245 
248  double wiimotePitch;
249  double wiimoteRoll;
255  double nunchuckRoll;
260  int16_t accX;
261  int16_t accY;
262  int16_t accZ;
265  /* Variables for the gyro inside the Motion Plus */
267  double gyroPitch;
269  double gyroRoll;
271  double gyroYaw;
272 
277  double yawGyroSpeed;
282  uint16_t pitchGyroScale;
283  uint16_t rollGyroScale;
284  uint16_t yawGyroScale;
289  int16_t gyroYawRaw;
290  int16_t gyroRollRaw;
291  int16_t gyroPitchRaw;
296  int16_t gyroYawZero;
297  int16_t gyroRollZero;
298  int16_t gyroPitchZero;
301 #ifdef WIICAMERA
302 
306  void IRinitialize();
307 
312  uint16_t getIRx1() {
313  return IR_object_x1;
314  };
315 
320  uint16_t getIRy1() {
321  return IR_object_y1;
322  };
323 
328  uint8_t getIRs1() {
329  return IR_object_s1;
330  };
331 
336  uint16_t getIRx2() {
337  return IR_object_x2;
338  };
339 
344  uint16_t getIRy2() {
345  return IR_object_y2;
346  };
347 
352  uint8_t getIRs2() {
353  return IR_object_s2;
354  };
355 
360  uint16_t getIRx3() {
361  return IR_object_x3;
362  };
363 
368  uint16_t getIRy3() {
369  return IR_object_y3;
370  };
371 
376  uint8_t getIRs3() {
377  return IR_object_s3;
378  };
379 
384  uint16_t getIRx4() {
385  return IR_object_x4;
386  };
387 
392  uint16_t getIRy4() {
393  return IR_object_y4;
394  };
395 
400  uint8_t getIRs4() {
401  return IR_object_s4;
402  };
403 
410  return(wiiState & 0x08);
411  };
413 #endif
414 
415 private:
416  /* Mandatory members */
417  BTD *pBtd;
418 
419  void L2CAP_task(); // L2CAP state machine
420 
421  /* Variables filled from HCI event management */
422  uint16_t hci_handle;
423  bool activeConnection; // Used to indicate if it's already has established a connection
424 
425  /* variables used by high level L2CAP task */
426  uint8_t l2cap_state;
427  uint16_t l2cap_event_flag; // l2cap flags of received bluetooth events
428 
429  uint32_t ButtonState;
430  uint32_t OldButtonState;
431  uint32_t ButtonClickState;
432  uint16_t hatValues[4];
433 
434  uint8_t HIDBuffer[3]; // Used to store HID commands
435 
436  uint16_t stateCounter;
437  bool unknownExtensionConnected;
438  bool extensionConnected;
439 
440  /* L2CAP Channels */
441  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
442  uint8_t control_dcid[2]; // 0x0060
443  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
444  uint8_t interrupt_dcid[2]; // 0x0061
445  uint8_t identifier; // Identifier for connection
446 
447  /* HID Commands */
448  void HID_Command(uint8_t* data, uint8_t nbytes);
449  void setReportMode(bool continuous, uint8_t mode);
450 
451  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
452  void initExtension1();
453  void initExtension2();
454 
455  void readData(uint32_t offset, uint16_t size, bool EEPROM);
456  void readExtensionType();
457  void readCalData();
458 
459  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
460  void initMotionPlus();
461  void activateMotionPlus();
462 
463  double pitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
464  double roll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
465 
466  bool activateNunchuck;
467  bool motionValuesReset; // This bool is true when the gyro values has been reset
468  unsigned long timer;
469 
470  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)
471  uint8_t batteryLevel;
472 
473 #ifdef WIICAMERA
474  /* Private function and variables for the readings from teh IR Camera */
475  void enableIRCamera1(); // Sets bit 2 of output report 13
476  void enableIRCamera2(); // Sets bit 2 of output report 1A
477  void writeSensitivityBlock1();
478  void writeSensitivityBlock2();
479  void write0x08Value();
480  void setWiiModeNumber(uint8_t mode_number);
481 
482  uint16_t IR_object_x1; // IR x position 10 bits
483  uint16_t IR_object_y1; // IR y position 10 bits
484  uint8_t IR_object_s1; // IR size value
485  uint16_t IR_object_x2;
486  uint16_t IR_object_y2;
487  uint8_t IR_object_s2;
488  uint16_t IR_object_x3; // IR x position 10 bits
489  uint16_t IR_object_y3; // IR y position 10 bits
490  uint8_t IR_object_s3; // IR size value
491  uint16_t IR_object_x4;
492  uint16_t IR_object_y4;
493  uint8_t IR_object_s4;
494 #endif
495 };
496 #endif