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();
127  bool getButtonPress(Button b);
128  bool getButtonClick(Button b);
137  uint8_t getAnalogHat(Hat a);
143  uint16_t getAnalogHat(AnalogHat a);
144 
149  double getPitch() { return pitch; };
154  double getRoll() { return roll; };
161  double getYaw() { return gyroYaw; };
162 
164  void setAllOff();
166  void setRumbleOff();
168  void setRumbleOn();
170  void setRumbleToggle();
175  void setLedOff(LED a);
180  void setLedOn(LED a);
185  void setLedToggle(LED a);
195  void setLedStatus();
196 
201  uint8_t getBatteryLevel() { return batteryLevel; };
206  uint8_t getWiiState() { return wiiState; };
211  bool wiimoteConnected;
220  /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
221 
224  double wiimotePitch;
225  double wiimoteRoll;
231  double nunchuckRoll;
236  int16_t accX;
237  int16_t accY;
238  int16_t accZ;
241  /* Variables for the gyro inside the Motion Plus */
243  double gyroPitch;
245  double gyroRoll;
247  double gyroYaw;
248 
253  double yawGyroSpeed;
258  uint16_t pitchGyroScale;
259  uint16_t rollGyroScale;
260  uint16_t yawGyroScale;
265  int16_t gyroYawRaw;
266  int16_t gyroRollRaw;
267  int16_t gyroPitchRaw;
272  int16_t gyroYawZero;
273  int16_t gyroRollZero;
274  int16_t gyroPitchZero;
277 #ifdef WIICAMERA
278 
282  void IRinitialize();
283 
285  uint16_t getIRx1() { return IR_object_x1; };
287  uint16_t getIRy1() { return IR_object_y1; };
289  uint8_t getIRs1() { return IR_object_s1; };
290 
292  uint16_t getIRx2() { return IR_object_x2; };
294  uint16_t getIRy2() { return IR_object_y2; };
296  uint8_t getIRs2() { return IR_object_s2; };
297 
299  uint16_t getIRx3() { return IR_object_x3; };
301  uint16_t getIRy3() { return IR_object_y3; };
303  uint8_t getIRs3() { return IR_object_s3; };
304 
306  uint16_t getIRx4() { return IR_object_x4; };
308  uint16_t getIRy4() { return IR_object_y4; };
310  uint8_t getIRs4() { return IR_object_s4; };
311 
317  bool isIRCameraEnabled() { return (wiiState & 0x08); };
319 #endif
320 
321 private:
322  /* Mandatory members */
323  BTD *pBtd;
324 
325  void L2CAP_task(); // L2CAP state machine
326 
327  /* Variables filled from HCI event management */
328  uint16_t hci_handle;
329  bool activeConnection; // Used to indicate if it's already has established a connection
330 
331  /* variables used by high level L2CAP task */
332  uint8_t l2cap_state;
333  uint16_t l2cap_event_flag;// l2cap flags of received bluetooth events
334 
335  uint32_t ButtonState;
336  uint32_t OldButtonState;
337  uint32_t ButtonClickState;
338  uint16_t hatValues[4];
339 
340  uint8_t HIDBuffer[3];// Used to store HID commands
341 
342  uint16_t stateCounter;
343  bool unknownExtensionConnected;
344  bool extensionConnected;
345 
346  /* L2CAP Channels */
347  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
348  uint8_t control_dcid[2]; // 0x0060
349  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
350  uint8_t interrupt_dcid[2]; // 0x0061
351  uint8_t identifier; // Identifier for connection
352 
353  /* HID Commands */
354  void HID_Command(uint8_t* data, uint8_t nbytes);
355  void setReportMode(bool continuous, uint8_t mode);
356  void statusRequest();
357 
358  void writeData(uint32_t offset, uint8_t size, uint8_t* data);
359  void initExtension1();
360  void initExtension2();
361 
362  void readData(uint32_t offset, uint16_t size, bool EEPROM);
363  void readExtensionType();
364  void readCalData();
365 
366  void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
367  void initMotionPlus();
368  void activateMotionPlus();
369 
370  double pitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
371  double roll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
372 
373  bool activateNunchuck;
374  bool motionValuesReset; // This bool is true when the gyro values has been reset
375  unsigned long timer;
376 
377  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)
378  uint8_t batteryLevel;
379 
380 #ifdef WIICAMERA
381  /* Private function and variables for the readings from teh IR Camera */
382  void enableIRCamera1(); // Sets bit 2 of output report 13
383  void enableIRCamera2(); // Sets bit 2 of output report 1A
384  void writeSensitivityBlock1();
385  void writeSensitivityBlock2();
386  void write0x08Value();
387  void setWiiModeNumber(uint8_t mode_number);
388 
389  uint16_t IR_object_x1; // IR x position 10 bits
390  uint16_t IR_object_y1; // IR y position 10 bits
391  uint8_t IR_object_s1; // IR size value
392  uint16_t IR_object_x2;
393  uint16_t IR_object_y2;
394  uint8_t IR_object_s2;
395  uint16_t IR_object_x3; // IR x position 10 bits
396  uint16_t IR_object_y3; // IR y position 10 bits
397  uint8_t IR_object_s3; // IR size value
398  uint16_t IR_object_x4;
399  uint16_t IR_object_y4;
400  uint8_t IR_object_s4;
401 #endif
402 };
403 #endif