From 40993141004b215fa1b2b8da10e52e1d44505873 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Mon, 25 Nov 2013 01:45:24 +0100 Subject: [PATCH] Can now set the LEDs on a keyboard --- BTHID.cpp | 8 ++++++ BTHID.h | 3 +++ examples/Bluetooth/BTHID/BTHID.ino | 2 +- examples/Bluetooth/BTHID/KeyboardParser.h | 33 +++++++++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/BTHID.cpp b/BTHID.cpp index de7b485d..2fe77163 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -380,4 +380,12 @@ void BTHID::Run() { void BTHID::setProtocol() { uint8_t command = 0x70 | protocolMode; // Set Protocol, see HID specs page 33 pBtd->L2CAP_Command(hci_handle, &command, 1, control_scid[0], control_scid[1]); +} + +void BTHID::setLeds(uint8_t data) { + uint8_t buf[3]; + buf[0] = 0xA2; // HID BT DATA_request (0xA0) | Report Type (Output 0x02) + buf[1] = 0x01; // Report ID + buf[2] = data; + pBtd->L2CAP_Command(hci_handle, buf, 3, interrupt_scid[0], interrupt_scid[1]); } \ No newline at end of file diff --git a/BTHID.h b/BTHID.h index cd6b58dc..c26de15b 100644 --- a/BTHID.h +++ b/BTHID.h @@ -102,6 +102,9 @@ public: protocolMode = mode; }; + /** Used to set the leds on a keyboard */ + void setLeds(uint8_t data); + /** True if a device is connected */ bool connected; diff --git a/examples/Bluetooth/BTHID/BTHID.ino b/examples/Bluetooth/BTHID/BTHID.ino index fecc4b50..444cc8d2 100644 --- a/examples/Bluetooth/BTHID/BTHID.ino +++ b/examples/Bluetooth/BTHID/BTHID.ino @@ -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; +KbdRptParser keyboardPrs(&hid); MouseRptParser mousePrs; void setup() { diff --git a/examples/Bluetooth/BTHID/KeyboardParser.h b/examples/Bluetooth/BTHID/KeyboardParser.h index 1054cac0..0d8c081d 100644 --- a/examples/Bluetooth/BTHID/KeyboardParser.h +++ b/examples/Bluetooth/BTHID/KeyboardParser.h @@ -2,16 +2,45 @@ #define __kbdrptparser_h_ class KbdRptParser : public KeyboardReportParser { - private: - void PrintKey(uint8_t mod, uint8_t key); + public: + KbdRptParser(BTHID *p) : pBTHID(p) {}; protected: + 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); virtual void OnKeyPressed(uint8_t key); + + private: + void PrintKey(uint8_t mod, uint8_t key); + BTHID *pBTHID; }; +uint8_t KbdRptParser::HandleLockingKeys(HID* hid, uint8_t key) { + uint8_t old_keys = kbdLockingKeys.bLeds; + + switch (key) { + case KEY_NUM_LOCK: + Serial.println(F("Num lock")); + kbdLockingKeys.kbdLeds.bmNumLock = ~kbdLockingKeys.kbdLeds.bmNumLock; + break; + case KEY_CAPS_LOCK: + Serial.println(F("Caps lock")); + kbdLockingKeys.kbdLeds.bmCapsLock = ~kbdLockingKeys.kbdLeds.bmCapsLock; + break; + case KEY_SCROLL_LOCK: + Serial.println(F("Scroll lock")); + kbdLockingKeys.kbdLeds.bmScrollLock = ~kbdLockingKeys.kbdLeds.bmScrollLock; + break; + } + + if (old_keys != kbdLockingKeys.bLeds && pBTHID) + pBTHID->setLeds(kbdLockingKeys.bLeds); + + return 0; +} + void KbdRptParser::PrintKey(uint8_t m, uint8_t key) { MODIFIERKEYS mod; *((uint8_t*)&mod) = m;