aboutsummaryrefslogtreecommitdiff
path: root/src/libs/BleMouse.cpp
diff options
context:
space:
mode:
authorkrolyxon <me@krolyxon.com>2026-05-11 14:01:44 +0530
committerkrolyxon <me@krolyxon.com>2026-05-11 14:01:44 +0530
commitf34b1e2fbd94bcc8d0ee2e2e2e43e214d0b329a1 (patch)
tree235d29da4668906d1e11d651980cea8b0c69dec7 /src/libs/BleMouse.cpp
parent360d2d2ad3bd584014683f3579fc2e2348c16852 (diff)
Migrate to PlatformIO
Now since i have been able to make PSRAM working with platformio, i don't have to use this shitty Arduino-IDE anymore which does not even allow me to use subfolders like a normal programmer would do
Diffstat (limited to 'src/libs/BleMouse.cpp')
-rw-r--r--src/libs/BleMouse.cpp173
1 files changed, 173 insertions, 0 deletions
diff --git a/src/libs/BleMouse.cpp b/src/libs/BleMouse.cpp
new file mode 100644
index 0000000..665121e
--- /dev/null
+++ b/src/libs/BleMouse.cpp
@@ -0,0 +1,173 @@
+#include <BLEDevice.h>
+#include <BLEUtils.h>
+#include <BLEServer.h>
+#include "BLE2902.h"
+#include "BLEHIDDevice.h"
+#include "HIDTypes.h"
+#include "HIDKeyboardTypes.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"
+ #define LOG_TAG ""
+#else
+ #include "esp_log.h"
+ static const char* LOG_TAG = "BLEDevice";
+#endif
+
+static const uint8_t _hidReportDescriptor[] = {
+ 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)
+ // ------------------------------------------------- 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(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) :
+ _buttons(0),
+ hid(0)
+{
+ this->deviceName = deviceName;
+ this->deviceManufacturer = deviceManufacturer;
+ this->batteryLevel = batteryLevel;
+ this->connectionStatus = new BleConnectionStatus();
+}
+
+void BleMouse::begin(void)
+{
+ xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
+}
+
+void BleMouse::end(void)
+{
+}
+
+void BleMouse::click(uint8_t b)
+{
+ _buttons = b;
+ move(0,0,0,0);
+ _buttons = 0;
+ move(0,0,0,0);
+}
+
+void BleMouse::move(signed char x, signed char y, signed char wheel, signed char hWheel)
+{
+ if (this->isConnected())
+ {
+ uint8_t m[5];
+ m[0] = _buttons;
+ m[1] = x;
+ m[2] = y;
+ m[3] = wheel;
+ m[4] = hWheel;
+ this->inputMouse->setValue(m, 5);
+ this->inputMouse->notify();
+ }
+}
+
+void BleMouse::buttons(uint8_t b)
+{
+ if (b != _buttons)
+ {
+ _buttons = b;
+ move(0,0,0,0);
+ }
+}
+
+void BleMouse::press(uint8_t b)
+{
+ buttons(_buttons | b);
+}
+
+void BleMouse::release(uint8_t b)
+{
+ buttons(_buttons & ~b);
+}
+
+bool BleMouse::isPressed(uint8_t b)
+{
+ if ((b & _buttons) > 0)
+ return true;
+ return false;
+}
+
+bool BleMouse::isConnected(void) {
+ return this->connectionStatus->connected;
+}
+
+void BleMouse::setBatteryLevel(uint8_t level) {
+ this->batteryLevel = level;
+ if (hid != 0)
+ this->hid->setBatteryLevel(this->batteryLevel);
+}
+
+void BleMouse::taskServer(void* pvParameter) {
+ BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast<BleMouse *>(pvParameter);
+ BLEDevice::init(std::string(bleMouseInstance->deviceName.c_str()));
+ BLEServer *pServer = BLEDevice::createServer();
+ pServer->setCallbacks(bleMouseInstance->connectionStatus);
+
+ bleMouseInstance->hid = new BLEHIDDevice(pServer);
+ bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(0); // <-- input REPORTID from report map
+ bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse;
+
+ bleMouseInstance->hid->manufacturer()->setValue(std::string(bleMouseInstance->deviceManufacturer.c_str()));
+
+ bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
+ bleMouseInstance->hid->hidInfo(0x00,0x02);
+
+ BLESecurity *pSecurity = new BLESecurity();
+
+ pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
+
+ bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
+ bleMouseInstance->hid->startServices();
+
+ bleMouseInstance->onStarted(pServer);
+
+ BLEAdvertising *pAdvertising = pServer->getAdvertising();
+ pAdvertising->setAppearance(HID_MOUSE);
+ pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID());
+ pAdvertising->start();
+ bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel);
+
+ ESP_LOGD(LOG_TAG, "Advertising started!");
+ vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
+}