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

Adding NimBLE #45

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion BleConnectionStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion BleConnectionStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class BleConnectionStatus : public BLEServerCallbacks
};

#endif // CONFIG_BT_ENABLED
#endif // ESP32_BLE_CONNECTION_STATUS_H
#endif // ESP32_BLE_CONNECTION_STATUS_H
110 changes: 102 additions & 8 deletions BleMouse.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@

#if defined(USE_NIMBLE)
#include "BleMouse.h"
#include <NimBLEDevice.h>
#include <NimBLEServer.h>
#include <NimBLEUtils.h>
#include <NimBLEHIDDevice.h>

#else

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "BLE2902.h"
#include "BLEHIDDevice.h"
#include "HIDTypes.h"
#include "HIDKeyboardTypes.h"
#include "BleConnectionStatus.h"
#include "BleMouse.h"

#endif

#include "HIDTypes.h"
#include <driver/adc.h>
#include "sdkconfig.h"

#include "BleConnectionStatus.h"
#include "BleMouse.h"

#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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<BleMouse *>(pvParameter);
BLEDevice::init(bleMouseInstance->deviceName);
Expand Down Expand Up @@ -171,3 +263,5 @@ void BleMouse::taskServer(void* pvParameter) {
ESP_LOGD(LOG_TAG, "Advertising started!");
vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
}
#endif

66 changes: 66 additions & 0 deletions BleMouse.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,83 @@
// 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
#define MOUSE_BACK 8
#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;
Expand Down Expand Up @@ -41,5 +105,7 @@ class BleMouse {
virtual void onStarted(BLEServer *pServer) { };
};

#endif

#endif // CONFIG_BT_ENABLED
#endif // ESP32_BLE_MOUSE_H