Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Read Numlock/Capslock/Scrolllock state #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions BleKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down
10 changes: 7 additions & 3 deletions BleKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -89,7 +91,8 @@ typedef struct
class BleKeyboard : public Print
{
private:
BleConnectionStatus* connectionStatus;
KeyboardOutputCallbacks* keyboardOutputCallBack;
BleConnectionStatus* connectionStatus;
BLEHIDDevice* hid;
BLECharacteristic* inputKeyboard;
BLECharacteristic* outputKeyboard;
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions KeyboardOutputCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

13 changes: 12 additions & 1 deletion KeyboardOutputCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions examples/SendKeyStrokes/ReadWriteNumLock.ino
Original file line number Diff line number Diff line change
@@ -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.h>

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);
}