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

Compatibility with Arduino #329

Open
wants to merge 1 commit 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
58 changes: 9 additions & 49 deletions BleKeyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "BleKeyboard.h"

#if defined(USE_NIMBLE)
#include <NimBLEDevice.h>
#include <NimBLEServer.h>
Expand All @@ -16,6 +14,7 @@
#include <driver/adc.h>
#include "sdkconfig.h"

#include "BleKeyboard.h"

#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
Expand Down Expand Up @@ -95,10 +94,10 @@ static const uint8_t _hidReportDescriptor[] = {
END_COLLECTION(0) // END_COLLECTION
};

BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel)
BleKeyboard::BleKeyboard(String deviceName, String deviceManufacturer, uint8_t batteryLevel)
: hid(0)
, deviceName(std::string(deviceName).substr(0, 15))
, deviceManufacturer(std::string(deviceManufacturer).substr(0,15))
, deviceName(String(deviceName).substring(0, 15))
, deviceManufacturer(String(deviceManufacturer).substring(0,15))
, batteryLevel(batteryLevel) {}

void BleKeyboard::begin(void)
Expand All @@ -116,20 +115,12 @@ void BleKeyboard::begin(void)

hid->manufacturer()->setValue(deviceManufacturer);

hid->pnp(0x02, vid, pid, version);
hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
hid->hidInfo(0x00, 0x01);


#if defined(USE_NIMBLE)

BLEDevice::setSecurityAuth(true, true, true);

#else

BLESecurity* pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);

#endif // USE_NIMBLE
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);

hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
hid->startServices();
Expand Down Expand Up @@ -161,7 +152,7 @@ void BleKeyboard::setBatteryLevel(uint8_t level) {
}

//must be called before begin in order to set the name
void BleKeyboard::setName(std::string deviceName) {
void BleKeyboard::setName(String deviceName) {
this->deviceName = deviceName;
}

Expand All @@ -174,18 +165,6 @@ void BleKeyboard::setDelay(uint32_t ms) {
this->_delay_ms = ms;
}

void BleKeyboard::set_vendor_id(uint16_t vid) {
this->vid = vid;
}

void BleKeyboard::set_product_id(uint16_t pid) {
this->pid = pid;
}

void BleKeyboard::set_version(uint16_t version) {
this->version = version;
}

void BleKeyboard::sendReport(KeyReport* keys)
{
if (this->isConnected())
Expand Down Expand Up @@ -468,7 +447,6 @@ void BleKeyboard::releaseAll(void)
_mediaKeyReport[0] = 0;
_mediaKeyReport[1] = 0;
sendReport(&_keyReport);
sendReport(&_mediaKeyReport);
}

size_t BleKeyboard::write(uint8_t c)
Expand Down Expand Up @@ -502,31 +480,13 @@ size_t BleKeyboard::write(const uint8_t *buffer, size_t size) {

void BleKeyboard::onConnect(BLEServer* pServer) {
this->connected = true;

#if !defined(USE_NIMBLE)

BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(true);
desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(true);

#endif // !USE_NIMBLE

}

void BleKeyboard::onDisconnect(BLEServer* pServer) {
this->connected = false;

#if !defined(USE_NIMBLE)

BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(false);
desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(false);

advertising->start();

#endif // !USE_NIMBLE
#endif // !USE_NIMBLE
}

void BleKeyboard::onWrite(BLECharacteristic* me) {
Expand All @@ -544,4 +504,4 @@ void BleKeyboard::delay_ms(uint64_t ms) {
}
while(esp_timer_get_time() < e) {}
}
}
}
41 changes: 4 additions & 37 deletions BleKeyboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// uncomment the following line to use NimBLE library
//#define USE_NIMBLE

#ifndef ESP32_BLE_KEYBOARD_H
#define ESP32_BLE_KEYBOARD_H
#include "sdkconfig.h"
Expand Down Expand Up @@ -28,10 +25,6 @@

#include "Print.h"

#define BLE_KEYBOARD_VERSION "0.0.4"
#define BLE_KEYBOARD_VERSION_MAJOR 0
#define BLE_KEYBOARD_VERSION_MINOR 0
#define BLE_KEYBOARD_VERSION_REVISION 4

const uint8_t KEY_LEFT_CTRL = 0x80;
const uint8_t KEY_LEFT_SHIFT = 0x81;
Expand All @@ -51,7 +44,6 @@ const uint8_t KEY_TAB = 0xB3;
const uint8_t KEY_RETURN = 0xB0;
const uint8_t KEY_ESC = 0xB1;
const uint8_t KEY_INSERT = 0xD1;
const uint8_t KEY_PRTSC = 0xCE;
const uint8_t KEY_DELETE = 0xD4;
const uint8_t KEY_PAGE_UP = 0xD3;
const uint8_t KEY_PAGE_DOWN = 0xD6;
Expand Down Expand Up @@ -83,23 +75,6 @@ const uint8_t KEY_F22 = 0xF9;
const uint8_t KEY_F23 = 0xFA;
const uint8_t KEY_F24 = 0xFB;

const uint8_t KEY_NUM_0 = 0xEA;
const uint8_t KEY_NUM_1 = 0xE1;
const uint8_t KEY_NUM_2 = 0xE2;
const uint8_t KEY_NUM_3 = 0xE3;
const uint8_t KEY_NUM_4 = 0xE4;
const uint8_t KEY_NUM_5 = 0xE5;
const uint8_t KEY_NUM_6 = 0xE6;
const uint8_t KEY_NUM_7 = 0xE7;
const uint8_t KEY_NUM_8 = 0xE8;
const uint8_t KEY_NUM_9 = 0xE9;
const uint8_t KEY_NUM_SLASH = 0xDC;
const uint8_t KEY_NUM_ASTERISK = 0xDD;
const uint8_t KEY_NUM_MINUS = 0xDE;
const uint8_t KEY_NUM_PLUS = 0xDF;
const uint8_t KEY_NUM_ENTER = 0xE0;
const uint8_t KEY_NUM_PERIOD = 0xEB;

typedef uint8_t MediaKeyReport[2];

const MediaKeyReport KEY_MEDIA_NEXT_TRACK = {1, 0};
Expand Down Expand Up @@ -138,19 +113,15 @@ class BleKeyboard : public Print, public BLEServerCallbacks, public BLECharacter
BLEAdvertising* advertising;
KeyReport _keyReport;
MediaKeyReport _mediaKeyReport;
std::string deviceName;
std::string deviceManufacturer;
String deviceName;
String deviceManufacturer;
uint8_t batteryLevel;
bool connected = false;
uint32_t _delay_ms = 7;
void delay_ms(uint64_t ms);

uint16_t vid = 0x05ac;
uint16_t pid = 0x820a;
uint16_t version = 0x0210;

public:
BleKeyboard(std::string deviceName = "ESP32 Keyboard", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100);
BleKeyboard(String deviceName = "ESP32 Keyboard", String deviceManufacturer = "Espressif", uint8_t batteryLevel = 100);
void begin(void);
void end(void);
void sendReport(KeyReport* keys);
Expand All @@ -165,12 +136,8 @@ class BleKeyboard : public Print, public BLEServerCallbacks, public BLECharacter
void releaseAll(void);
bool isConnected(void);
void setBatteryLevel(uint8_t level);
void setName(std::string deviceName);
void setName(String deviceName);
void setDelay(uint32_t ms);

void set_vendor_id(uint16_t vid);
void set_product_id(uint16_t pid);
void set_version(uint16_t version);
protected:
virtual void onStarted(BLEServer *pServer) { };
virtual void onConnect(BLEServer* pServer) override;
Expand Down