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..7c14d26 100644 --- a/BleKeyboard.h +++ b/BleKeyboard.h @@ -3,12 +3,12 @@ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) +#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 +33,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 +80,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 +91,8 @@ typedef struct class BleKeyboard : public Print { private: - BleConnectionStatus* connectionStatus; + KeyboardOutputCallbacks* keyboardOutputCallBack; + BleConnectionStatus* connectionStatus; BLEHIDDevice* hid; BLECharacteristic* inputKeyboard; BLECharacteristic* outputKeyboard; @@ -101,6 +104,7 @@ class BleKeyboard : public Print 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*)); void sendReport(KeyReport* keys); void sendReport(MediaKeyReport* keys); size_t press(uint8_t k); diff --git a/KeyboardOutputCallbacks.cpp b/KeyboardOutputCallbacks.cpp index 286ce41..189eb26 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); } - diff --git a/KeyboardOutputCallbacks.h b/KeyboardOutputCallbacks.h index bce158e..224c634 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); +}