From 2010bee87882a42b02b24400b00a2532d42e1629 Mon Sep 17 00:00:00 2001 From: Cemu0 Date: Tue, 26 Jan 2021 16:59:30 +0700 Subject: [PATCH 1/2] Add Read Numlock/Capslock/Scrolllock state Also add KEY_SCROLL_LOCK and KEY_NUM_LOCK --- BleKeyboard.cpp | 10 +++++-- BleKeyboard.h | 16 +++++++---- KeyboardOutputCallbacks.cpp | 9 +++--- KeyboardOutputCallbacks.h | 13 ++++++++- examples/SendKeyStrokes/ReadWriteNumLock.ino | 29 ++++++++++++++++++++ 5 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 examples/SendKeyStrokes/ReadWriteNumLock.ino diff --git a/BleKeyboard.cpp b/BleKeyboard.cpp index e98af09..1c1f7d9 100644 --- a/BleKeyboard.cpp +++ b/BleKeyboard.cpp @@ -106,6 +106,11 @@ void BleKeyboard::end(void) { } +void BleKeyboard::setLedChangeCallBack(void (*func)(KbdLeds*)) +{ + keyboardOutputCallBack->func = func; +} + bool BleKeyboard::isConnected(void) { return this->connectionStatus->connected; } @@ -128,9 +133,10 @@ void BleKeyboard::taskServer(void* pvParameter) { bleKeyboardInstance->inputMediaKeys = bleKeyboardInstance->hid->inputReport(MEDIA_KEYS_ID); bleKeyboardInstance->connectionStatus->inputKeyboard = bleKeyboardInstance->inputKeyboard; bleKeyboardInstance->connectionStatus->outputKeyboard = bleKeyboardInstance->outputKeyboard; - bleKeyboardInstance->connectionStatus->inputMediaKeys = bleKeyboardInstance->inputMediaKeys; + bleKeyboardInstance->connectionStatus->inputMediaKeys = bleKeyboardInstance->inputMediaKeys; - bleKeyboardInstance->outputKeyboard->setCallbacks(new KeyboardOutputCallbacks()); + bleKeyboardInstance->keyboardOutputCallBack = new KeyboardOutputCallbacks(); + bleKeyboardInstance->outputKeyboard->setCallbacks(bleKeyboardInstance->keyboardOutputCallBack); bleKeyboardInstance->hid->manufacturer()->setValue(bleKeyboardInstance->deviceManufacturer); diff --git a/BleKeyboard.h b/BleKeyboard.h index b9dff8b..1a40259 100644 --- a/BleKeyboard.h +++ b/BleKeyboard.h @@ -3,12 +3,13 @@ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) +#include +#include "KeyboardOutputCallbacks.h" #include "BleConnectionStatus.h" #include "BLEHIDDevice.h" #include "BLECharacteristic.h" #include "Print.h" - const uint8_t KEY_LEFT_CTRL = 0x80; const uint8_t KEY_LEFT_SHIFT = 0x81; const uint8_t KEY_LEFT_ALT = 0x82; @@ -33,6 +34,8 @@ const uint8_t KEY_PAGE_DOWN = 0xD6; const uint8_t KEY_HOME = 0xD2; const uint8_t KEY_END = 0xD5; const uint8_t KEY_CAPS_LOCK = 0xC1; +const uint8_t KEY_NUM_LOCK = 0xDB; +const uint8_t KEY_SCROLL_LOCK = 0xCF; const uint8_t KEY_F1 = 0xC2; const uint8_t KEY_F2 = 0xC3; const uint8_t KEY_F3 = 0xC4; @@ -78,7 +81,7 @@ const MediaKeyReport KEY_MEDIA_CONSUMER_CONTROL_CONFIGURATION = {0, 64}; // Medi const MediaKeyReport KEY_MEDIA_EMAIL_READER = {0, 128}; -// Low level key report: up to 6 keys and shift, ctrl etc at once +// Low level key report: up to 6 keys and shift, ctrl etc at once // struct clone form USB_Host_Shield_2.0-master :D typedef struct { uint8_t modifiers; @@ -89,7 +92,8 @@ typedef struct class BleKeyboard : public Print { private: - BleConnectionStatus* connectionStatus; + KeyboardOutputCallbacks* keyboardOutputCallBack; + BleConnectionStatus* connectionStatus; BLEHIDDevice* hid; BLECharacteristic* inputKeyboard; BLECharacteristic* outputKeyboard; @@ -98,9 +102,10 @@ class BleKeyboard : public Print MediaKeyReport _mediaKeyReport; static void taskServer(void* pvParameter); public: - BleKeyboard(std::string deviceName = "ESP32 BLE Keyboard", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); + BleKeyboard(std::string deviceName = "ESP32_BLE_Keyboard_Test", std::string deviceManufacturer = "DIY", uint8_t batteryLevel = 100); void begin(void); void end(void); + void setLedChangeCallBack(void (*func)(KbdLeds*)); void sendReport(KeyReport* keys); void sendReport(MediaKeyReport* keys); size_t press(uint8_t k); @@ -117,7 +122,8 @@ class BleKeyboard : public Print std::string deviceManufacturer; std::string deviceName; protected: - virtual void onStarted(BLEServer *pServer) { }; + virtual void onStarted(BLEServer *pServer) { + }; }; #endif // CONFIG_BT_ENABLED diff --git a/KeyboardOutputCallbacks.cpp b/KeyboardOutputCallbacks.cpp index 286ce41..8abb42f 100644 --- a/KeyboardOutputCallbacks.cpp +++ b/KeyboardOutputCallbacks.cpp @@ -12,7 +12,8 @@ KeyboardOutputCallbacks::KeyboardOutputCallbacks(void) { } void KeyboardOutputCallbacks::onWrite(BLECharacteristic* me) { - uint8_t* value = (uint8_t*)(me->getValue().c_str()); - ESP_LOGI(LOG_TAG, "special keys: %d", *value); -} - + KbdLeds* kbled = (KbdLeds*)(me->getValue().c_str()); + ESP_LOGI(LOG_TAG, "special keys: %d", *kbled); + // if(func!=NULL) + func(kbled); +} \ No newline at end of file diff --git a/KeyboardOutputCallbacks.h b/KeyboardOutputCallbacks.h index bce158e..cddee1b 100644 --- a/KeyboardOutputCallbacks.h +++ b/KeyboardOutputCallbacks.h @@ -7,12 +7,23 @@ #include "BLE2902.h" #include "BLECharacteristic.h" +// key report back +typedef struct{ + uint8_t bmNumLock : 1; + uint8_t bmCapsLock : 1; + uint8_t bmScrollLock : 1; + uint8_t bmCompose : 1; + uint8_t bmKana : 1; + uint8_t bmReserved : 3; +} KbdLeds; +using callBackFunc = void (*)(KbdLeds*); + class KeyboardOutputCallbacks : public BLECharacteristicCallbacks { public: + callBackFunc func; //= [](KbdLeds*){ } KeyboardOutputCallbacks(void); void onWrite(BLECharacteristic* me); }; - #endif // CONFIG_BT_ENABLED #endif // ESP32_BLE_KEYBOARD_OUTPUT_CALLBACKS_H diff --git a/examples/SendKeyStrokes/ReadWriteNumLock.ino b/examples/SendKeyStrokes/ReadWriteNumLock.ino new file mode 100644 index 0000000..fce5e75 --- /dev/null +++ b/examples/SendKeyStrokes/ReadWriteNumLock.ino @@ -0,0 +1,29 @@ +/** + * This example turns the ESP32 into a Bluetooth LE keyboard that you can use num/caps/scroll lock led for some reason ex: turn your room light by scroll lock :))) + */ +#include + +BleKeyboard bleKeyboard; + +//note that this function should run "fast" or esp will crash +void KbdLedCb(KbdLeds *kbls) +{ + digitalWrite(2,kbls->bmNumLock); + // digitalWrite(2,kbls->bmCapsLock); + // digitalWrite(2,kbls->bmScrollLock); + // ... +} + +void setup() { + pinMode(2,OUTPUT); + Serial.begin(115200); + Serial.println("Starting BLE work!"); + bleKeyboard.begin(); + delay(1000);//must have delay for the BLE finish inital + bleKeyboard.setLedChangeCallBack(KbdLedCb); +} + +void loop() { + bleKeyboard.write(KEY_NUM_LOCK); + delay(10000); +} From 447608dfb488af2bbd75aeb8794a0bbd8ffae00d Mon Sep 17 00:00:00 2001 From: Cemu0 Date: Tue, 26 Jan 2021 17:14:40 +0700 Subject: [PATCH 2/2] remove unnessesery change --- BleKeyboard.h | 6 ++---- KeyboardOutputCallbacks.cpp | 2 +- KeyboardOutputCallbacks.h | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/BleKeyboard.h b/BleKeyboard.h index 1a40259..7c14d26 100644 --- a/BleKeyboard.h +++ b/BleKeyboard.h @@ -3,7 +3,6 @@ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) -#include #include "KeyboardOutputCallbacks.h" #include "BleConnectionStatus.h" #include "BLEHIDDevice.h" @@ -102,7 +101,7 @@ class BleKeyboard : public Print MediaKeyReport _mediaKeyReport; static void taskServer(void* pvParameter); public: - BleKeyboard(std::string deviceName = "ESP32_BLE_Keyboard_Test", std::string deviceManufacturer = "DIY", uint8_t batteryLevel = 100); + BleKeyboard(std::string deviceName = "ESP32 BLE Keyboard", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); void begin(void); void end(void); void setLedChangeCallBack(void (*func)(KbdLeds*)); @@ -122,8 +121,7 @@ class BleKeyboard : public Print std::string deviceManufacturer; std::string deviceName; protected: - virtual void onStarted(BLEServer *pServer) { - }; + virtual void onStarted(BLEServer *pServer) { }; }; #endif // CONFIG_BT_ENABLED diff --git a/KeyboardOutputCallbacks.cpp b/KeyboardOutputCallbacks.cpp index 8abb42f..189eb26 100644 --- a/KeyboardOutputCallbacks.cpp +++ b/KeyboardOutputCallbacks.cpp @@ -16,4 +16,4 @@ void KeyboardOutputCallbacks::onWrite(BLECharacteristic* me) { ESP_LOGI(LOG_TAG, "special keys: %d", *kbled); // if(func!=NULL) func(kbled); -} \ No newline at end of file +} diff --git a/KeyboardOutputCallbacks.h b/KeyboardOutputCallbacks.h index cddee1b..224c634 100644 --- a/KeyboardOutputCallbacks.h +++ b/KeyboardOutputCallbacks.h @@ -21,7 +21,7 @@ using callBackFunc = void (*)(KbdLeds*); class KeyboardOutputCallbacks : public BLECharacteristicCallbacks { public: - callBackFunc func; //= [](KbdLeds*){ } + callBackFunc func = [](KbdLeds*){ }; KeyboardOutputCallbacks(void); void onWrite(BLECharacteristic* me); };