Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PS4BT.h Source File

PS4BT.h

00001 /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
00002 
00003  This software may be distributed and modified under the terms of the GNU
00004  General Public License version 2 (GPL2) as published by the Free Software
00005  Foundation and appearing in the file GPL2.TXT included in the packaging of
00006  this file. Please note that GPL2 Section 2[b] requires that all works based
00007  on this software must also be made publicly available under the terms of
00008  the GPL2 ("Copyleft").
00009 
00010  Contact information
00011  -------------------
00012 
00013  Kristian Lauszus, TKJ Electronics
00014  Web      :  http://www.tkjelectronics.com
00015  e-mail   :  kristianl@tkjelectronics.com
00016  */
00017 
00018 #ifndef _ps4bt_h_
00019 #define _ps4bt_h_
00020 
00021 #include "BTHID.h"
00022 #include "PS4Parser.h"
00023 
00024 /**
00025  * This class implements support for the PS4 controller via Bluetooth.
00026  * It uses the BTHID class for all the Bluetooth communication.
00027  */
00028 class PS4BT : public BTHID, public PS4Parser {
00029 public:
00030         /**
00031          * Constructor for the PS4BT class.
00032          * @param  p     Pointer to the BTD class instance.
00033          * @param  pair  Set this to true in order to pair with the device. If the argument is omitted then it will not pair with it. One can use ::PAIR to set it to true.
00034          * @param  pin   Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
00035          */
00036         PS4BT(BTD *p, bool pair = false, const char *pin = "0000") :
00037         BTHID(p, pair, pin) {
00038                 PS4Parser::Reset();
00039         };
00040 
00041         /**
00042          * Used to check if a PS4 controller is connected.
00043          * @return Returns true if it is connected.
00044          */
00045         bool connected() {
00046                 return BTHID::connected;
00047         };
00048 
00049 protected:
00050         /** @name BTHID implementation */
00051         /**
00052          * Used to parse Bluetooth HID data.
00053          * @param len The length of the incoming data.
00054          * @param buf Pointer to the data buffer.
00055          */
00056         virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
00057                 PS4Parser::Parse(len, buf);
00058         };
00059 
00060         /**
00061          * Called when a device is successfully initialized.
00062          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
00063          * This is useful for instance if you want to set the LEDs in a specific way.
00064          */
00065         virtual void OnInitBTHID() {
00066                 PS4Parser::Reset();
00067                 enable_sixaxis(); // Make the controller send out the entire output report
00068                 if (pFuncOnInit)
00069                         pFuncOnInit(); // Call the user function
00070                 else
00071                         setLed(Blue);
00072         };
00073 
00074         /** Used to reset the different buffers to there default values */
00075         virtual void ResetBTHID() {
00076                 PS4Parser::Reset();
00077         };
00078         /**@}*/
00079 
00080         /** @name PS4Parser implementation */
00081         virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
00082                 uint8_t buf[79];
00083                 memset(buf, 0, sizeof(buf));
00084 
00085                 buf[0] = 0x52; // HID BT Set_report (0x50) | Report Type (Output 0x02)
00086                 buf[1] = 0x11; // Report ID
00087                 buf[2] = 0x80;
00088                 buf[4]= 0xFF;
00089 
00090                 buf[7] = output->smallRumble; // Small Rumble
00091                 buf[8] = output->bigRumble; // Big rumble
00092 
00093                 buf[9] = output->r; // Red
00094                 buf[10] = output->g; // Green
00095                 buf[11] = output->b; // Blue
00096 
00097                 buf[12] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
00098                 buf[13] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
00099 
00100                 output->reportChanged = false;
00101 
00102                 // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
00103 
00104                 HID_Command(buf, sizeof(buf));
00105         };
00106         /**@}*/
00107 
00108 private:
00109         void enable_sixaxis() { // Command used to make the PS4 controller send out the entire output report
00110                 uint8_t buf[2];
00111                 buf[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
00112                 buf[1] = 0x02; // Report ID
00113 
00114                 HID_Command(buf, 2);
00115         };
00116 
00117         void HID_Command(uint8_t *data, uint8_t nbytes) {
00118                 pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]);
00119         };
00120 };
00121 #endif