mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
Now onInit() works with the PS4 library as well
This commit is contained in:
parent
c163228063
commit
eea5c44b96
3 changed files with 53 additions and 16 deletions
|
@ -46,6 +46,11 @@ void BTHID::Reset() {
|
|||
activeConnection = false;
|
||||
l2cap_event_flag = 0; // Reset flags
|
||||
l2cap_state = L2CAP_WAIT;
|
||||
|
||||
for(uint8_t i = 0; i < BTHID_NUM_SERVICES; i++) {
|
||||
if(bthidService[i])
|
||||
bthidService[i]->Reset();
|
||||
}
|
||||
}
|
||||
|
||||
void BTHID::disconnect() { // Use this void to disconnect the device
|
||||
|
|
30
BTHID.h
30
BTHID.h
|
@ -25,6 +25,14 @@
|
|||
#define MOUSE_PARSER_ID 1
|
||||
#define NUM_PARSERS 2
|
||||
|
||||
#define BTHID_NUM_SERVICES 4 // Max number of Bluetooth HID services - if you need more than 4 simply increase this number
|
||||
|
||||
class BTHIDService {
|
||||
public:
|
||||
virtual void onInit();
|
||||
virtual void Reset();
|
||||
};
|
||||
|
||||
/** This BluetoothService class implements support for the HID keyboard and mice. */
|
||||
class BTHID : public BluetoothService {
|
||||
public:
|
||||
|
@ -106,10 +114,25 @@ public:
|
|||
pFuncOnInit = funcOnInit;
|
||||
};
|
||||
|
||||
/**
|
||||
* Register Bluetooth HID services.
|
||||
* @param pService Pointer to BTHIDService class instance.
|
||||
* @return The service ID on success or -1 on fail.
|
||||
*/
|
||||
int8_t registerServiceClass(BTHIDService *pService) {
|
||||
for(uint8_t i = 0; i < BTHID_NUM_SERVICES; i++) {
|
||||
if(!bthidService[i]) {
|
||||
bthidService[i] = pService;
|
||||
return i; // Return ID
|
||||
}
|
||||
}
|
||||
return -1; // ErrorregisterServiceClass
|
||||
};
|
||||
|
||||
private:
|
||||
BTD *pBtd; // Pointer to BTD instance
|
||||
|
||||
HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
|
||||
BTHIDService *bthidService[BTHID_NUM_SERVICES];
|
||||
|
||||
/** Set report protocol. */
|
||||
void setProtocol();
|
||||
|
@ -123,6 +146,11 @@ private:
|
|||
void onInit() {
|
||||
if(pFuncOnInit)
|
||||
pFuncOnInit(); // Call the user function
|
||||
|
||||
for(uint8_t i = 0; i < BTHID_NUM_SERVICES; i++) {
|
||||
if(bthidService[i])
|
||||
bthidService[i]->onInit();
|
||||
}
|
||||
};
|
||||
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
|
||||
|
||||
|
|
34
PS4BT.h
34
PS4BT.h
|
@ -67,15 +67,16 @@ struct PS4Data {
|
|||
* This class implements support for the PS4 controller via Bluetooth.
|
||||
* It uses the BTHID class for all the Bluetooth communication.
|
||||
*/
|
||||
class PS4BT : public HIDReportParser {
|
||||
class PS4BT : public HIDReportParser, public BTHIDService {
|
||||
public:
|
||||
/**
|
||||
* Constructor for the PS4BT class.
|
||||
* @param p Pointer to the BTD class instance.
|
||||
* @param p Pointer to the BTHID class instance.
|
||||
*/
|
||||
PS4BT(BTHID *p) :
|
||||
pBthid(p) {
|
||||
pBthid->SetReportParser(KEYBOARD_PARSER_ID, this);
|
||||
pBthid->registerServiceClass(this); // Register it as a Bluetooth HID service
|
||||
Reset();
|
||||
};
|
||||
|
||||
|
@ -133,7 +134,17 @@ public:
|
|||
pBthid->pair();
|
||||
};
|
||||
|
||||
void Reset() {
|
||||
/**
|
||||
* Used to call your own function when the device is successfully initialized.
|
||||
* @param funcOnInit Function to call.
|
||||
*/
|
||||
void attachOnInit(void (*funcOnInit)(void)) {
|
||||
pFuncOnInit = funcOnInit;
|
||||
};
|
||||
|
||||
/** @name BTHIDService implementation */
|
||||
/** Used to reset the different buffers to there default values */
|
||||
virtual void Reset() {
|
||||
uint8_t i;
|
||||
for (0; i < sizeof(ps4Data.hatValue); i++)
|
||||
ps4Data.hatValue[i] = 127;
|
||||
|
@ -149,25 +160,18 @@ public:
|
|||
buttonClickState.dpad = DPAD_OFF;
|
||||
};
|
||||
|
||||
/**
|
||||
* Used to call your own function when the device is successfully initialized.
|
||||
* @param funcOnInit Function to call.
|
||||
*/
|
||||
void attachOnInit(void (*funcOnInit)(void)) {
|
||||
pFuncOnInit = funcOnInit;
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
* Called when a device is successfully initialized.
|
||||
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
|
||||
* This is useful for instance if you want to set the LEDs in a specific way.
|
||||
*/
|
||||
void onInit() {
|
||||
Reset();
|
||||
if(pFuncOnInit)
|
||||
virtual void onInit() {
|
||||
if (pFuncOnInit)
|
||||
pFuncOnInit(); // Call the user function
|
||||
};
|
||||
/**@}*/
|
||||
|
||||
private:
|
||||
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
|
||||
|
||||
bool checkDpad(PS4Buttons ps4Buttons, DPADEnum b); // Used to check PS4 DPAD buttons
|
||||
|
|
Loading…
Reference in a new issue