]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/PS3BT.h
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / PS3BT.h
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
18 #ifndef _ps3bt_h_
19 #define _ps3bt_h_
20
21 #include "BTD.h"
22 #include "PS3Enums.h"
23
24 #define HID_BUFFERSIZE 50 // Size of the buffer for the Playstation Motion Controller
25
26 /**
27  * This BluetoothService class implements support for all the official PS3 Controllers:
28  * Dualshock 3, Navigation or a Motion controller via Bluetooth.
29  *
30  * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
31  */
32 class PS3BT : public BluetoothService {
33 public:
34         /**
35          * Constructor for the PS3BT class.
36          * @param  pBtd   Pointer to BTD class instance.
37          * @param  btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
38          * Pass your dongles Bluetooth address into the constructor,
39          * This will set BTD#my_bdaddr, so you don't have to plug in the dongle before pairing with your controller.
40          */
41         PS3BT(BTD *pBtd, uint8_t btadr5 = 0, uint8_t btadr4 = 0, uint8_t btadr3 = 0, uint8_t btadr2 = 0, uint8_t btadr1 = 0, uint8_t btadr0 = 0);
42
43         /** @name BluetoothService implementation */
44         /** Used this to disconnect any of the controllers. */
45         void disconnect();
46         /**@}*/
47
48         /** @name PS3 Controller functions */
49         /**
50          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
51          *
52          * While getButtonClick(ButtonEnum b) will only return it once.
53          *
54          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
55          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
56          * @param  b          ::ButtonEnum to read.
57          * @return            getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
58          */
59         bool getButtonPress(ButtonEnum b);
60         bool getButtonClick(ButtonEnum b);
61         /**@}*/
62         /** @name PS3 Controller functions */
63         /**
64          * Used to get the analog value from button presses.
65          * @param  a The ::ButtonEnum to read.
66          * The supported buttons are:
67          * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
68          * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
69          * @return   Analog value in the range of 0-255.
70          */
71         uint8_t getAnalogButton(ButtonEnum a);
72         /**
73          * Used to read the analog joystick.
74          * @param  a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
75          * @return   Return the analog value in the range of 0-255.
76          */
77         uint8_t getAnalogHat(AnalogHatEnum a);
78         /**
79          * Used to read the sensors inside the Dualshock 3 and Move controller.
80          * @param  a
81          * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
82          * The Move controller has a 3-axis accelerometer, a 3-axis gyro, a 3-axis magnetometer
83          * and a temperature sensor inside.
84          * @return   Return the raw sensor value.
85          */
86         int16_t getSensor(SensorEnum a);
87         /**
88          * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
89          * @param  a Either ::Pitch or ::Roll.
90          * @return   Return the angle in the range of 0-360.
91          */
92         double getAngle(AngleEnum a);
93         /**
94          * Read the sensors inside the Move controller.
95          * @param  a ::aXmove, ::aYmove, ::aZmove, ::gXmove, ::gYmove, ::gZmove, ::mXmove, ::mYmove, and ::mXmove.
96          * @return   The value in SI units.
97          */
98         double get9DOFValues(SensorEnum a);
99         /**
100          * Get the status from the controller.
101          * @param  c The ::StatusEnum you want to read.
102          * @return   True if correct and false if not.
103          */
104         bool getStatus(StatusEnum c);
105         /** Read all the available statuses from the controller and prints it as a nice formated string. */
106         void printStatusString();
107         /**
108          * Read the temperature from the Move controller.
109          * @return The temperature in degrees Celsius.
110          */
111         String getTemperature();
112
113         /** Used to set all LEDs and rumble off. */
114         void setAllOff();
115         /** Turn off rumble. */
116         void setRumbleOff();
117         /**
118          * Turn on rumble.
119          * @param mode Either ::RumbleHigh or ::RumbleLow.
120          */
121         void setRumbleOn(RumbleEnum mode);
122         /**
123          * Turn on rumble using custom duration and power.
124          * @param rightDuration The duration of the right/low rumble effect.
125          * @param rightPower The intensity of the right/low rumble effect.
126          * @param leftDuration The duration of the left/high rumble effect.
127          * @param leftPower The intensity of the left/high rumble effect.
128          */
129         void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
130
131         /**
132          * Set LED value without using ::LEDEnum.
133          * @param value See: ::LEDEnum.
134          */
135         void setLedRaw(uint8_t value);
136
137         /** Turn all LEDs off. */
138         void setLedOff() {
139                 setLedRaw(0);
140         };
141         /**
142          * Turn the specific LED off.
143          * @param a The ::LEDEnum to turn off.
144          */
145         void setLedOff(LEDEnum a);
146         /**
147          * Turn the specific LED on.
148          * @param a The ::LEDEnum to turn on.
149          */
150         void setLedOn(LEDEnum a);
151         /**
152          * Toggle the specific LED.
153          * @param a The ::LEDEnum to toggle.
154          */
155         void setLedToggle(LEDEnum a);
156
157         /**
158          * Use this to set the Color using RGB values.
159          * @param r,g,b RGB value.
160          */
161         void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
162         /**
163          * Use this to set the color using the predefined colors in ::ColorsEnum.
164          * @param color The desired color.
165          */
166         void moveSetBulb(ColorsEnum color);
167         /**
168          * Set the rumble value inside the Move controller.
169          * @param rumble The desired value in the range from 64-255.
170          */
171         void moveSetRumble(uint8_t rumble);
172
173         /** Used to get the millis() of the last message */
174         uint32_t getLastMessageTime() {
175                 return lastMessageTime;
176         };
177         /**@}*/
178
179         /** Variable used to indicate if the normal Playstation controller is successfully connected. */
180         bool PS3Connected;
181         /** Variable used to indicate if the Move controller is successfully connected. */
182         bool PS3MoveConnected;
183         /** Variable used to indicate if the Navigation controller is successfully connected. */
184         bool PS3NavigationConnected;
185
186 protected:
187         /** @name BluetoothService implementation */
188         /**
189          * Used to pass acldata to the services.
190          * @param ACLData Incoming acldata.
191          */
192         void ACLData(uint8_t* ACLData);
193         /** Used to run part of the state machine. */
194         void Run();
195         /** Use this to reset the service. */
196         void Reset();
197         /**
198          * Called when the controller is successfully initialized.
199          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
200          * This is useful for instance if you want to set the LEDs in a specific way.
201          */
202         void onInit();
203         /**@}*/
204
205 private:
206
207         void L2CAP_task(); // L2CAP state machine
208
209         /* Variables filled from HCI event management */
210         char remote_name_first; // First letter in remote name
211         bool activeConnection; // Used to indicate if it's already has established a connection
212
213         /* Variables used by high level L2CAP task */
214         uint8_t l2cap_state;
215
216         uint32_t lastMessageTime; // Variable used to store the millis value of the last message.
217
218         uint32_t ButtonState;
219         uint32_t OldButtonState;
220         uint32_t ButtonClickState;
221
222         uint32_t timer; // Timer used to limit time between messages and also used to continuously set PS3 Move controller Bulb and rumble values
223         uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
224
225         uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
226         uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
227         uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
228
229         /* L2CAP Channels */
230         uint8_t control_scid[2]; // L2CAP source CID for HID_Control
231         uint8_t control_dcid[2]; // 0x0040
232         uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
233         uint8_t interrupt_dcid[2]; // 0x0041
234
235         /* HID Commands */
236         void HID_Command(uint8_t* data, uint8_t nbytes);
237         void HIDMove_Command(uint8_t* data, uint8_t nbytes);
238         void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
239 };
240 #endif