Now onInit() works with the PS4 library as well

This commit is contained in:
Kristian Lauszus 2014-01-12 17:20:18 +01:00
parent c163228063
commit eea5c44b96
3 changed files with 53 additions and 16 deletions

View file

@ -46,6 +46,11 @@ void BTHID::Reset() {
activeConnection = false; activeConnection = false;
l2cap_event_flag = 0; // Reset flags l2cap_event_flag = 0; // Reset flags
l2cap_state = L2CAP_WAIT; 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 void BTHID::disconnect() { // Use this void to disconnect the device

30
BTHID.h
View file

@ -25,6 +25,14 @@
#define MOUSE_PARSER_ID 1 #define MOUSE_PARSER_ID 1
#define NUM_PARSERS 2 #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. */ /** This BluetoothService class implements support for the HID keyboard and mice. */
class BTHID : public BluetoothService { class BTHID : public BluetoothService {
public: public:
@ -106,10 +114,25 @@ public:
pFuncOnInit = funcOnInit; 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: private:
BTD *pBtd; // Pointer to BTD instance BTD *pBtd; // Pointer to BTD instance
HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers. HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
BTHIDService *bthidService[BTHID_NUM_SERVICES];
/** Set report protocol. */ /** Set report protocol. */
void setProtocol(); void setProtocol();
@ -123,6 +146,11 @@ private:
void onInit() { void onInit() {
if(pFuncOnInit) if(pFuncOnInit)
pFuncOnInit(); // Call the user function 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() void (*pFuncOnInit)(void); // Pointer to function called in onInit()

34
PS4BT.h
View file

@ -67,15 +67,16 @@ struct PS4Data {
* This class implements support for the PS4 controller via Bluetooth. * This class implements support for the PS4 controller via Bluetooth.
* It uses the BTHID class for all the Bluetooth communication. * It uses the BTHID class for all the Bluetooth communication.
*/ */
class PS4BT : public HIDReportParser { class PS4BT : public HIDReportParser, public BTHIDService {
public: public:
/** /**
* Constructor for the PS4BT class. * Constructor for the PS4BT class.
* @param p Pointer to the BTD class instance. * @param p Pointer to the BTHID class instance.
*/ */
PS4BT(BTHID *p) : PS4BT(BTHID *p) :
pBthid(p) { pBthid(p) {
pBthid->SetReportParser(KEYBOARD_PARSER_ID, this); pBthid->SetReportParser(KEYBOARD_PARSER_ID, this);
pBthid->registerServiceClass(this); // Register it as a Bluetooth HID service
Reset(); Reset();
}; };
@ -133,7 +134,17 @@ public:
pBthid->pair(); 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; uint8_t i;
for (0; i < sizeof(ps4Data.hatValue); i++) for (0; i < sizeof(ps4Data.hatValue); i++)
ps4Data.hatValue[i] = 127; ps4Data.hatValue[i] = 127;
@ -149,25 +160,18 @@ public:
buttonClickState.dpad = DPAD_OFF; 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. * Called when a device is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function. * 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. * This is useful for instance if you want to set the LEDs in a specific way.
*/ */
void onInit() { virtual void onInit() {
Reset(); if (pFuncOnInit)
if(pFuncOnInit)
pFuncOnInit(); // Call the user function pFuncOnInit(); // Call the user function
}; };
/**@}*/
private:
void (*pFuncOnInit)(void); // Pointer to function called in onInit() void (*pFuncOnInit)(void); // Pointer to function called in onInit()
bool checkDpad(PS4Buttons ps4Buttons, DPADEnum b); // Used to check PS4 DPAD buttons bool checkDpad(PS4Buttons ps4Buttons, DPADEnum b); // Used to check PS4 DPAD buttons