2012-03-04 02:38:50 +01:00
|
|
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
|
|
|
|
2012-03-04 02:38:50 +01:00
|
|
|
Kristian Lauszus, TKJ Electronics
|
2012-03-02 08:34:29 +01:00
|
|
|
Web : http://www.tkjelectronics.com
|
2012-03-04 02:38:50 +01:00
|
|
|
e-mail : kristianl@tkjelectronics.com
|
2012-03-02 08:34:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ps3bt_h_
|
|
|
|
#define _ps3bt_h_
|
|
|
|
|
2012-08-04 12:20:47 +02:00
|
|
|
#include "BTD.h"
|
2012-09-09 22:13:52 +02:00
|
|
|
#include "PS3Enums.h"
|
2012-03-02 08:34:29 +01:00
|
|
|
|
2012-04-11 01:52:43 +02:00
|
|
|
#define HID_BUFFERSIZE 50 // size of the buffer for the Playstation Motion Controller
|
|
|
|
#define OUTPUT_REPORT_BUFFER_SIZE 48 //Size of the output report buffer for the controllers
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
/* Bluetooth L2CAP states for L2CAP_task() */
|
2012-08-17 14:18:55 +02:00
|
|
|
#define L2CAP_WAIT 0
|
|
|
|
#define L2CAP_CONTROL_REQUEST 1
|
|
|
|
#define L2CAP_CONTROL_SUCCESS 2
|
|
|
|
#define L2CAP_INTERRUPT_SETUP 3
|
|
|
|
#define L2CAP_INTERRUPT_REQUEST 4
|
|
|
|
#define L2CAP_INTERRUPT_SUCCESS 5
|
|
|
|
#define L2CAP_HID_ENABLE_SIXAXIS 6
|
|
|
|
#define L2CAP_HID_PS3_LED 7
|
|
|
|
#define L2CAP_DONE 8
|
|
|
|
#define L2CAP_INTERRUPT_DISCONNECT 9
|
|
|
|
#define L2CAP_CONTROL_DISCONNECT 10
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
/* L2CAP event flags */
|
2012-08-04 12:20:47 +02:00
|
|
|
#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x01
|
|
|
|
#define L2CAP_FLAG_CONFIG_CONTROL_REQUEST 0x02
|
|
|
|
#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x04
|
|
|
|
#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x08
|
|
|
|
#define L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST 0x10
|
|
|
|
#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x20
|
|
|
|
#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x40
|
|
|
|
#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x80
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
/*Macros for L2CAP event flag tests */
|
2012-08-04 12:20:47 +02:00
|
|
|
#define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST)
|
|
|
|
#define l2cap_config_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_REQUEST)
|
|
|
|
#define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS)
|
|
|
|
#define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST)
|
|
|
|
#define l2cap_config_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST)
|
|
|
|
#define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)
|
|
|
|
#define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE)
|
|
|
|
#define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE)
|
2012-03-02 08:34:29 +01:00
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* This BluetoothService class implements support for all the official PS3 Controllers:
|
|
|
|
* Dualshock 3, Navigation or a Motion controller via Bluetooth.
|
|
|
|
*
|
|
|
|
* Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
|
|
|
|
*/
|
2012-08-04 12:20:47 +02:00
|
|
|
class PS3BT : public BluetoothService {
|
2012-05-24 14:39:01 +02:00
|
|
|
public:
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Constructor for the PS3BT class.
|
|
|
|
* @param pBtd Pointer to BTD class instance.
|
|
|
|
* @param btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
|
|
|
|
* Pass your dongles Bluetooth address into the constructor,
|
|
|
|
* This will set BTD#my_bdaddr, so you don't have to plug in the dongle before pairing with your controller.
|
|
|
|
*/
|
2012-08-04 12:20:47 +02:00
|
|
|
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);
|
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/** @name BluetoothService implementation */
|
|
|
|
/**
|
|
|
|
* Used to pass acldata to the services.
|
|
|
|
* @param ACLData Incoming acldata.
|
|
|
|
*/
|
|
|
|
virtual void ACLData(uint8_t* ACLData);
|
|
|
|
/** Used to run part of the state maschine. */
|
|
|
|
virtual void Run();
|
|
|
|
/** Use this to reset the service. */
|
|
|
|
virtual void Reset();
|
|
|
|
/** Used this to disconnect any of the controllers. */
|
|
|
|
virtual void disconnect();
|
|
|
|
/**@}*/
|
2012-08-04 12:20:47 +02:00
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/** @name PS3 Controller functions */
|
|
|
|
/**
|
2013-02-17 01:25:49 +01:00
|
|
|
* getButtonPress(Button b) will return true as long as the button is held down.
|
|
|
|
*
|
|
|
|
* While getButtonClick(Button b) will only return it once.
|
|
|
|
*
|
2013-02-05 19:51:45 +01:00
|
|
|
* So you instance if you need to increase a variable once you would use getButtonClick(Button b),
|
|
|
|
* but if you need to drive a robot forward you would use getButtonPress(Button b).
|
|
|
|
*/
|
2012-09-09 22:13:52 +02:00
|
|
|
bool getButtonPress(Button b);
|
|
|
|
bool getButtonClick(Button b);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**@}*/
|
|
|
|
/** @name PS3 Controller functions */
|
|
|
|
/**
|
|
|
|
* Used to get the analog value from button presses.
|
|
|
|
* @param a The ::Button to read.
|
|
|
|
* The supported buttons are:
|
|
|
|
* ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
|
|
|
|
* ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
|
|
|
|
* @return Analog value in the range of 0-255.
|
|
|
|
*/
|
2013-01-27 22:02:33 +01:00
|
|
|
uint8_t getAnalogButton(Button a);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Used to read the analog joystick.
|
|
|
|
* @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
|
|
|
|
* @return Return the analog value in the range of 0-255.
|
|
|
|
*/
|
2012-03-02 08:34:29 +01:00
|
|
|
uint8_t getAnalogHat(AnalogHat a);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Used to read the sensors inside the Dualshock 3 and Move controller.
|
|
|
|
* @param a
|
|
|
|
* The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
|
|
|
|
* The Move controller has a 3-axis accelerometer, a 3-axis gyro, a 3-axis magnetometer
|
|
|
|
* and a temperature sensor inside.
|
|
|
|
* @return Return the raw sensor value.
|
|
|
|
*/
|
2012-06-01 17:27:21 +02:00
|
|
|
int16_t getSensor(Sensor a);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Use this to get ::Pitch and ::Roll calculated using the accelerometer.
|
|
|
|
* @param a Either ::Pitch or ::Roll.
|
|
|
|
* @return Return the angle in the range of 0-360.
|
|
|
|
*/
|
2012-04-24 01:35:43 +02:00
|
|
|
double getAngle(Angle a);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Read the sensors inside the Move controller.
|
|
|
|
* @param a ::aXmove, ::aYmove, ::aZmove, ::gXmove, ::gYmove, ::gZmove, ::mXmove, ::mYmove, and ::mXmove.
|
|
|
|
* @return The value in SI units.
|
|
|
|
*/
|
2012-10-02 02:05:07 +02:00
|
|
|
double get9DOFValues(Sensor a);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Get the ::Status from the controller.
|
|
|
|
* @param c The ::Status you want to read.
|
|
|
|
* @return True if correct and false if not.
|
|
|
|
*/
|
|
|
|
bool getStatus(Status c);
|
|
|
|
/**
|
|
|
|
* Read all the available ::Status from the controller.
|
|
|
|
* @return One large string with all the information.
|
|
|
|
*/
|
|
|
|
String getStatusString();
|
|
|
|
/**
|
|
|
|
* Read the temperature from the Move controller.
|
|
|
|
* @return The temperature in degrees celsius.
|
|
|
|
*/
|
2012-06-01 18:47:15 +02:00
|
|
|
String getTemperature();
|
2012-03-02 08:34:29 +01:00
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/** Used to set all LEDs and ::Rumble off. */
|
2012-03-02 08:34:29 +01:00
|
|
|
void setAllOff();
|
2013-02-05 19:51:45 +01:00
|
|
|
/** Turn off ::Rumble. */
|
2012-03-02 08:34:29 +01:00
|
|
|
void setRumbleOff();
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Turn on ::Rumble.
|
|
|
|
* @param mode Either ::RumbleHigh or ::RumbleLow.
|
|
|
|
*/
|
2012-03-02 08:34:29 +01:00
|
|
|
void setRumbleOn(Rumble mode);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Turn the specific ::LED off.
|
|
|
|
* @param a The ::LED to turn off.
|
|
|
|
*/
|
2012-03-02 08:34:29 +01:00
|
|
|
void setLedOff(LED a);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Turn the specific ::LED on.
|
|
|
|
* @param a The ::LED to turn on.
|
|
|
|
*/
|
|
|
|
void setLedOn(LED a);
|
|
|
|
/**
|
|
|
|
* Toggle the specific ::LED.
|
|
|
|
* @param a The ::LED to toggle.
|
|
|
|
*/
|
2012-05-09 18:38:38 +02:00
|
|
|
void setLedToggle(LED a);
|
2012-08-04 12:20:47 +02:00
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* Use this to set the Color using RGB values.
|
|
|
|
* @param r,g,b RGB value.
|
|
|
|
*/
|
|
|
|
void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
|
|
|
|
/**
|
|
|
|
* Use this to set the color using the predefined colors in ::Colors.
|
|
|
|
* @param color The desired color.
|
|
|
|
*/
|
|
|
|
void moveSetBulb(Colors color);
|
|
|
|
/**
|
|
|
|
* Set the rumble value inside the Move controller.
|
|
|
|
* @param rumble The desired value in the range from 64-255.
|
|
|
|
*/
|
2012-03-02 08:34:29 +01:00
|
|
|
void moveSetRumble(uint8_t rumble);
|
2013-02-05 19:51:45 +01:00
|
|
|
/**@}*/
|
2012-03-02 08:34:29 +01:00
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/** Variable used to indicate if the normal playstation controller is successfully connected. */
|
|
|
|
bool PS3Connected;
|
|
|
|
/** Variable used to indicate if the move controller is successfully connected. */
|
|
|
|
bool PS3MoveConnected;
|
|
|
|
/** Variable used to indicate if the navigation controller is successfully connected. */
|
|
|
|
bool PS3NavigationConnected;
|
2012-03-02 08:34:29 +01:00
|
|
|
|
2012-08-04 12:20:47 +02:00
|
|
|
private:
|
2012-03-02 08:34:29 +01:00
|
|
|
/* mandatory members */
|
2012-08-04 12:20:47 +02:00
|
|
|
BTD *pBtd;
|
2012-04-11 01:52:43 +02:00
|
|
|
|
2012-08-08 05:40:53 +02:00
|
|
|
void L2CAP_task(); // L2CAP state machine
|
|
|
|
|
2012-08-04 12:20:47 +02:00
|
|
|
/* Variables filled from HCI event management */
|
2012-04-24 01:35:43 +02:00
|
|
|
int16_t hci_handle;
|
2013-01-28 14:28:05 +01:00
|
|
|
uint8_t remote_name[30]; // First 30 chars of remote name
|
2013-01-20 22:33:54 +01:00
|
|
|
bool activeConnection; // Used to indicate if it's already has established a connection
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
/* variables used by high level L2CAP task */
|
|
|
|
uint8_t l2cap_state;
|
2013-01-28 14:28:05 +01:00
|
|
|
uint16_t l2cap_event_flag; // L2CAP flags of received bluetooth events
|
2012-03-02 08:34:29 +01:00
|
|
|
|
2012-04-15 01:42:16 +02:00
|
|
|
unsigned long timer;
|
|
|
|
|
2012-03-02 08:34:29 +01:00
|
|
|
uint32_t ButtonState;
|
|
|
|
uint32_t OldButtonState;
|
2012-09-09 22:13:52 +02:00
|
|
|
uint32_t ButtonClickState;
|
|
|
|
|
2013-01-28 14:28:05 +01:00
|
|
|
uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
|
2012-03-02 08:34:29 +01:00
|
|
|
uint32_t timerBulbRumble;// used to continuously set PS3 Move controller Bulb and rumble values
|
|
|
|
|
2013-01-28 14:28:05 +01:00
|
|
|
uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
|
|
|
|
uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
|
|
|
|
uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
/* L2CAP Channels */
|
2013-01-28 14:28:05 +01:00
|
|
|
uint8_t control_scid[2]; // L2CAP source CID for HID_Control
|
|
|
|
uint8_t control_dcid[2]; // 0x0040
|
|
|
|
uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
|
|
|
|
uint8_t interrupt_dcid[2]; // 0x0041
|
|
|
|
uint8_t identifier; // Identifier for connection
|
2012-03-02 08:34:29 +01:00
|
|
|
|
|
|
|
/* HID Commands */
|
2012-08-04 12:20:47 +02:00
|
|
|
void HID_Command(uint8_t* data, uint8_t nbytes);
|
|
|
|
void HIDMove_Command(uint8_t* data, uint8_t nbytes);
|
2013-01-28 14:28:05 +01:00
|
|
|
void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
|
2012-03-02 08:34:29 +01:00
|
|
|
};
|
|
|
|
#endif
|