diff --git a/BleConnectionStatus.cpp b/BleConnectionStatus.cpp index f01183b..3ff1791 100644 --- a/BleConnectionStatus.cpp +++ b/BleConnectionStatus.cpp @@ -15,4 +15,4 @@ void BleConnectionStatus::onDisconnect(BLEServer* pServer) this->connected = false; BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); desc->setNotifications(false); -} +} \ No newline at end of file diff --git a/BleConnectionStatus.h b/BleConnectionStatus.h index b703150..7267e97 100644 --- a/BleConnectionStatus.h +++ b/BleConnectionStatus.h @@ -18,4 +18,4 @@ class BleConnectionStatus : public BLEServerCallbacks }; #endif // CONFIG_BT_ENABLED -#endif // ESP32_BLE_CONNECTION_STATUS_H +#endif // ESP32_BLE_CONNECTION_STATUS_H \ No newline at end of file diff --git a/BleMouse.cpp b/BleMouse.cpp index 2bcdd4c..96603ca 100644 --- a/BleMouse.cpp +++ b/BleMouse.cpp @@ -1,15 +1,28 @@ + +#if defined(USE_NIMBLE) +#include "BleMouse.h" +#include +#include +#include +#include + +#else + #include #include #include #include "BLE2902.h" #include "BLEHIDDevice.h" -#include "HIDTypes.h" #include "HIDKeyboardTypes.h" +#include "BleConnectionStatus.h" +#include "BleMouse.h" + +#endif + +#include "HIDTypes.h" #include #include "sdkconfig.h" -#include "BleConnectionStatus.h" -#include "BleMouse.h" #if defined(CONFIG_ARDUHAL_ESP_LOG) #include "esp32-hal-log.h" @@ -19,6 +32,9 @@ static const char* LOG_TAG = "BLEDevice"; #endif +// Report IDs: +#define MOUSE_ID 0x00 + static const uint8_t _hidReportDescriptor[] = { USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) USAGE(1), 0x02, // USAGE (Mouse) @@ -59,7 +75,13 @@ static const uint8_t _hidReportDescriptor[] = { END_COLLECTION(0), // END_COLLECTION END_COLLECTION(0) // END_COLLECTION }; - +#if defined(USE_NIMBLE) +BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) + : hid(0) + , deviceName(std::string(deviceName).substr(0, 15)) + , deviceManufacturer(std::string(deviceManufacturer).substr(0,15)) + , batteryLevel(batteryLevel) {} +#else BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : _buttons(0), hid(0) @@ -69,10 +91,43 @@ BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8 this->batteryLevel = batteryLevel; this->connectionStatus = new BleConnectionStatus(); } - +#endif void BleMouse::begin(void) { - xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); + #if defined(USE_NIMBLE) + + BLEDevice::init(deviceName); + + BLEServer* pServer = BLEDevice::createServer(); + pServer->setCallbacks(this); + + hid = new BLEHIDDevice(pServer); + inputMouse = hid->inputReport(MOUSE_ID); // <-- input REPORTID from report map + + hid->manufacturer()->setValue(deviceManufacturer); + + hid->pnp(0x02, vid, pid, version); + hid->hidInfo(0x00,0x02); + + BLEDevice::setSecurityAuth(true, true, true); + + hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor)); + hid->startServices(); + + onStarted(pServer); + + advertising = pServer->getAdvertising(); + advertising->setAppearance(HID_MOUSE); + advertising->addServiceUUID(hid->hidService()->getUUID()); + advertising->setScanResponse(false); + advertising->start(); + hid->setBatteryLevel(batteryLevel); + + ESP_LOGD(LOG_TAG, "Advertising started!"); + + #else + xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); + #endif } void BleMouse::end(void) @@ -97,8 +152,12 @@ void BleMouse::move(signed char x, signed char y, signed char wheel, signed char m[2] = y; m[3] = wheel; m[4] = hWheel; - this->inputMouse->setValue(m, 5); + this->inputMouse->setValue((uint8_t*)m, sizeof(m)); this->inputMouse->notify(); + #if defined(USE_NIMBLE) + // vTaskDelay(delayTicks); + this->delay_ms(_delay_ms); + #endif // USE_NIMBLE } } @@ -129,7 +188,11 @@ bool BleMouse::isPressed(uint8_t b) } bool BleMouse::isConnected(void) { - return this->connectionStatus->connected; + #if defined(USE_NIMBLE) + return this->connected; + #else + return this->connectionStatus->connected; + #endif } void BleMouse::setBatteryLevel(uint8_t level) { @@ -138,6 +201,35 @@ void BleMouse::setBatteryLevel(uint8_t level) { this->hid->setBatteryLevel(this->batteryLevel); } +#if defined(USE_NIMBLE) +void BleMouse::onConnect(BLEServer* pServer) { + this->connected = true; +} + +void BleMouse::onDisconnect(BLEServer* pServer) { + this->connected = false; +} + +// this shouldn't +void BleMouse::onWrite(BLECharacteristic* me) { + uint8_t* value = (uint8_t*)(me->getValue().c_str()); + // is this can be use for future ? + ESP_LOGI(LOG_TAG, "special keys: %d", *value); +} + +void BleMouse::delay_ms(uint64_t ms) { + uint64_t m = esp_timer_get_time(); + if(ms){ + uint64_t e = (m + (ms * 1000)); + if(m > e){ //overflow + while(esp_timer_get_time() > e) { } + } + while(esp_timer_get_time() < e) {} + } +} +#endif // !USE_NIMBLE + +#if ! defined(USE_NIMBLE) void BleMouse::taskServer(void* pvParameter) { BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast(pvParameter); BLEDevice::init(bleMouseInstance->deviceName); @@ -171,3 +263,5 @@ void BleMouse::taskServer(void* pvParameter) { ESP_LOGD(LOG_TAG, "Advertising started!"); vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY); } +#endif + diff --git a/BleMouse.h b/BleMouse.h index 8b4ab9b..d871f6b 100644 --- a/BleMouse.h +++ b/BleMouse.h @@ -1,12 +1,34 @@ +// uncomment the following line to use NimBLE library +//#define USE_NIMBLE + #ifndef ESP32_BLE_MOUSE_H #define ESP32_BLE_MOUSE_H #include "sdkconfig.h" + + #if defined(CONFIG_BT_ENABLED) +#if defined(USE_NIMBLE) + +#include "NimBLECharacteristic.h" +#include "NimBLEHIDDevice.h" + +#define BLEDevice NimBLEDevice +#define BLEServerCallbacks NimBLEServerCallbacks +#define BLECharacteristicCallbacks NimBLECharacteristicCallbacks +#define BLEHIDDevice NimBLEHIDDevice +#define BLECharacteristic NimBLECharacteristic +#define BLEAdvertising NimBLEAdvertising +#define BLEServer NimBLEServer + +#else + #include "BleConnectionStatus.h" #include "BLEHIDDevice.h" #include "BLECharacteristic.h" +#endif // USE_NIMBLE + #define MOUSE_LEFT 1 #define MOUSE_RIGHT 2 #define MOUSE_MIDDLE 4 @@ -14,6 +36,48 @@ #define MOUSE_FORWARD 16 #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) # For compatibility with the Mouse library +#if defined(USE_NIMBLE) +class BleMouse : public BLEServerCallbacks, public BLECharacteristicCallbacks +{ +private: + uint8_t _buttons; + BLEHIDDevice* hid; + BLECharacteristic* inputMouse; + BLEAdvertising* advertising; + + uint8_t batteryLevel; + bool connected = false; + uint32_t _delay_ms = 7; + void buttons(uint8_t b); + void rawAction(uint8_t msg[], char msgSize); + void delay_ms(uint64_t ms); + + uint16_t vid = 0x05ac; + uint16_t pid = 0x820a; + uint16_t version = 0x0210; + +public: + BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); + void begin(void); + void end(void); + void click(uint8_t b = MOUSE_LEFT); + void move(signed char x, signed char y, signed char wheel = 0, signed char hWheel = 0); + void press(uint8_t b = MOUSE_LEFT); // press LEFT by default + void release(uint8_t b = MOUSE_LEFT); // release LEFT by default + bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default + bool isConnected(void); + void setBatteryLevel(uint8_t level); + + std::string deviceManufacturer; + std::string deviceName; +protected: + virtual void onStarted(BLEServer *pServer) { }; + virtual void onConnect(BLEServer* pServer) override; + virtual void onDisconnect(BLEServer* pServer) override; + virtual void onWrite(BLECharacteristic* me) override; +}; + +#else class BleMouse { private: uint8_t _buttons; @@ -41,5 +105,7 @@ class BleMouse { virtual void onStarted(BLEServer *pServer) { }; }; +#endif + #endif // CONFIG_BT_ENABLED #endif // ESP32_BLE_MOUSE_H