Use reinterpret_cast to cast from BTHID to HID class, so it can be used in the parser

This commit is contained in:
Kristian Lauszus 2013-11-25 17:39:59 +01:00
parent 5f49b321cb
commit a31cc42585
3 changed files with 10 additions and 12 deletions

View file

@ -24,7 +24,7 @@ BTHID::BTHID(BTD *p, bool pair, const char *pin) :
pBtd(p), // pointer to USB class instance - mandatory
protocolMode(HID_BOOT_PROTOCOL)
{
for(uint8_t i = 0; i < epMUL; i++)
for (uint8_t i = 0; i < epMUL; i++)
pRptParser[i] = NULL;
if (pBtd)
@ -194,14 +194,14 @@ void BTHID::ACLData(uint8_t* l2capinbuf) {
case 0x01: // Keyboard events
if (pRptParser[KEYBOARD_PARSER_ID]) {
uint16_t length = ((uint16_t)l2capinbuf[5] << 8 | l2capinbuf[4]);
pRptParser[KEYBOARD_PARSER_ID]->Parse((HID*)this, 0, (uint8_t) length, &l2capinbuf[10]);
pRptParser[KEYBOARD_PARSER_ID]->Parse(reinterpret_cast<HID *> (this), 0, (uint8_t) length, &l2capinbuf[10]); // Use reinterpret_cast again to extract the instance
}
break;
case 0x02: // Mouse events
if (pRptParser[MOUSE_PARSER_ID]) {
uint16_t length = ((uint16_t)l2capinbuf[5] << 8 | l2capinbuf[4]);
pRptParser[MOUSE_PARSER_ID]->Parse((HID*)this, 0, (uint8_t) length, &l2capinbuf[10]);
pRptParser[MOUSE_PARSER_ID]->Parse(reinterpret_cast<HID *> (this), 0, (uint8_t) length, &l2capinbuf[10]); // Use reinterpret_cast again to extract the instance
}
break;
case 0x03:

View file

@ -21,7 +21,7 @@ BTHID hid(&Btd, PAIR, "0000");
// After that you can simply create the instance like so and then press any button on the device
//BTHID hid(&Btd);
KbdRptParser keyboardPrs(&hid);
KbdRptParser keyboardPrs;
MouseRptParser mousePrs;
void setup() {

View file

@ -2,11 +2,8 @@
#define __kbdrptparser_h_
class KbdRptParser : public KeyboardReportParser {
public:
KbdRptParser(BTHID *p) : pBTHID(p) {};
protected:
virtual uint8_t HandleLockingKeys(HID* hid, uint8_t key);
virtual uint8_t HandleLockingKeys(HID *hid, uint8_t key);
virtual void OnControlKeysChanged(uint8_t before, uint8_t after);
virtual void OnKeyDown(uint8_t mod, uint8_t key);
virtual void OnKeyUp(uint8_t mod, uint8_t key);
@ -14,10 +11,9 @@ class KbdRptParser : public KeyboardReportParser {
private:
void PrintKey(uint8_t mod, uint8_t key);
BTHID *pBTHID;
};
uint8_t KbdRptParser::HandleLockingKeys(HID* hid, uint8_t key) {
uint8_t KbdRptParser::HandleLockingKeys(HID *hid, uint8_t key) {
uint8_t old_keys = kbdLockingKeys.bLeds;
switch (key) {
@ -35,8 +31,10 @@ uint8_t KbdRptParser::HandleLockingKeys(HID* hid, uint8_t key) {
break;
}
if (old_keys != kbdLockingKeys.bLeds && pBTHID)
pBTHID->setLeds(kbdLockingKeys.bLeds);
if (old_keys != kbdLockingKeys.bLeds && hid) {
BTHID *pBTHID = reinterpret_cast<BTHID *> (hid); // A cast the other way around is done in BTHID.cpp
pBTHID->setLeds(kbdLockingKeys.bLeds); // Update the LEDs on the keyboard
}
return 0;
}