From 834621d0e5c902c090d55e208dc702877821d872 Mon Sep 17 00:00:00 2001 From: T-vK Date: Wed, 7 Aug 2019 20:00:00 +0000 Subject: [PATCH] Fix the HID report map --- BleMouse.cpp | 137 ++++++++++++++++++--------------------------- BleMouse.h | 7 ++- README.md | 5 ++ library.properties | 2 +- 4 files changed, 66 insertions(+), 85 deletions(-) diff --git a/BleMouse.cpp b/BleMouse.cpp index e6bcaa0..2f07703 100644 --- a/BleMouse.cpp +++ b/BleMouse.cpp @@ -12,91 +12,60 @@ #include "BleMouse.h" #if defined(CONFIG_ARDUHAL_ESP_LOG) -#include "esp32-hal-log.h" -#define LOG_TAG "" + #include "esp32-hal-log.h" + #define LOG_TAG "" #else -#include "esp_log.h" -static const char* LOG_TAG = "BLEDevice"; + #include "esp_log.h" + static const char* LOG_TAG = "BLEDevice"; #endif static const uint8_t _hidReportDescriptor[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x02, // COLLECTION (Logical) - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - // ------------------------------ Buttons - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x05, // USAGE_MAXIMUM (Button 5) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x05, // REPORT_COUNT (5) - 0x81, 0x02, // INPUT (Data,Var,Abs) - // ------------------------------ Padding - 0x75, 0x03, // REPORT_SIZE (3) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x03, // INPUT (Cnst,Var,Abs) - // ------------------------------ X,Y position - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x02, // REPORT_COUNT (2) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0xa1, 0x02, // COLLECTION (Logical) - // ------------------------------ Vertical wheel res multiplier - 0x09, 0x48, // USAGE (Resolution Multiplier) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x35, 0x01, // PHYSICAL_MINIMUM (1) - 0x45, 0x04, // PHYSICAL_MAXIMUM (4) - 0x75, 0x02, // REPORT_SIZE (2) - 0x95, 0x01, // REPORT_COUNT (1) - 0xa4, // PUSH - 0xb1, 0x02, // FEATURE (Data,Var,Abs) - // ------------------------------ Vertical wheel - 0x09, 0x38, // USAGE (Wheel) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical - 0x45, 0x00, // PHYSICAL_MAXIMUM (0) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0xc0, // END_COLLECTION - 0xa1, 0x02, // COLLECTION (Logical) - // ------------------------------ Horizontal wheel res multiplier - 0x09, 0x48, // USAGE (Resolution Multiplier) - 0xb4, // POP - 0xb1, 0x02, // FEATURE (Data,Var,Abs) - // ------------------------------ Padding for Feature report - 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical - 0x45, 0x00, // PHYSICAL_MAXIMUM (0) - 0x75, 0x04, // REPORT_SIZE (4) - 0x95, 0x01, // REPORT_COUNT (1) - 0xb1, 0x03, // FEATURE (Cnst,Var,Abs) - // ------------------------------ Horizontal wheel - 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) - 0x0a, 0x38, 0x02, // USAGE (AC Pan) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0xc0, // END_COLLECTION - 0xc0, // END_COLLECTION - 0xc0, // END_COLLECTION - 0xc0 // END_COLLECTION + USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) + USAGE(1), 0x02, // USAGE (Mouse) + COLLECTION(1), 0x01, // COLLECTION (Application) + USAGE(1), 0x01, // USAGE (Pointer) + COLLECTION(1), 0x00, // COLLECTION (Physical) + REPORT_ID(1), 0x01, // REPORT_ID (1) + // ------------------------------------------------- Buttons (Left, Right, Middle, Back, Forward) + USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) + USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) + USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) + LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) + LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) + REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) + REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) + HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits + // ------------------------------------------------- Padding + REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) + REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) + HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding + // ------------------------------------------------- X/Y position, Wheel + USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) + USAGE(1), 0x30, // USAGE (X) + USAGE(1), 0x31, // USAGE (Y) + USAGE(1), 0x38, // USAGE (Wheel) + LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) + LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) + REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) + REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) + HIDINPUT(1), 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) + // ------------------------------------------------- Horizontal wheel + USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) + USAGE(2), 0x38, 0x02, // USAGE (AC Pan) + LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) + LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) + REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) + REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) + HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) + END_COLLECTION(0), // END_COLLECTION + END_COLLECTION(0) // END_COLLECTION }; -BleMouse::BleMouse(void) : _buttons(0) +BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : _buttons(0) { + this->deviceName = deviceName; + this->deviceManufacturer = deviceManufacturer; + this->batteryLevel = batteryLevel; this->connectionStatus = new BleConnectionStatus(); } @@ -162,9 +131,14 @@ bool BleMouse::isConnected(void) { return this->connectionStatus->connected; } +void BleMouse::setBatteryLevel(uint8_t level) { + this->batteryLevel = level; + this->hid->setBatteryLevel(level); +} + void BleMouse::taskServer(void* pvParameter) { BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast(pvParameter); - BLEDevice::init("ESP32-BLE-Mouse"); + BLEDevice::init(bleMouseInstance->deviceName); BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(bleMouseInstance->connectionStatus); @@ -172,8 +146,7 @@ void BleMouse::taskServer(void* pvParameter) { bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(1); // <-- input REPORTID from report map bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse; - std::string name = "chegewara"; - bleMouseInstance->hid->manufacturer()->setValue(name); + bleMouseInstance->hid->manufacturer()->setValue(bleMouseInstance->deviceManufacturer); bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); bleMouseInstance->hid->hidInfo(0x00,0x02); @@ -189,7 +162,7 @@ void BleMouse::taskServer(void* pvParameter) { pAdvertising->setAppearance(HID_MOUSE); pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID()); pAdvertising->start(); - bleMouseInstance->hid->setBatteryLevel(7); + bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); ESP_LOGD(LOG_TAG, "Advertising started!"); vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY); diff --git a/BleMouse.h b/BleMouse.h index 5bab37e..63e7cc4 100644 --- a/BleMouse.h +++ b/BleMouse.h @@ -24,7 +24,7 @@ class BleMouse { void rawAction(uint8_t msg[], char msgSize); static void taskServer(void* pvParameter); public: - BleMouse(void); + BleMouse(std::string deviceName = "Espressif", std::string deviceManufacturer = "ESP32 Bluetooth Mouse", uint8_t batteryLevel = 100); void begin(void); void end(void); void click(uint8_t b = MOUSE_LEFT); @@ -32,8 +32,11 @@ class BleMouse { 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); + uint8_t batteryLevel; + std::string deviceManufacturer; + std::string deviceName; }; #endif // CONFIG_BT_ENABLED diff --git a/README.md b/README.md index 770d5dc..185c718 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,11 @@ This library allows you to make the ESP32 act as a Bluetooth Mouse and control w - [x] Move mouse pointer up/down - [x] Scroll up/down - [x] Scroll left/right + - [x] Compatible with Android + - [x] Compatible with Windows + - [x] Compatible with Linux + - [ ] Compatible with MacOS X (Untested) + - [ ] Compatible with iOS (Untested) ## Installation - (Make sure you can use the ESP32 with the Arduino IDE. [Instructions can be found here.](https://github.com/espressif/arduino-esp32#installation-instructions)) diff --git a/library.properties b/library.properties index 6407676..766e772 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP32 BLE Mouse -version=0.2 +version=0.2.0 author=T-vK maintainer=T-vK sentence=Bluetooth LE Mouse library for the ESP32.