pleasedontcode

Bluetooth HID rev_01

Jul 29th, 2025
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Bluetooth HID
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-30 03:47:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Control pc cursor using the joystick */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <NimBLEDevice.h>    //https://github.com/h2zero/NimBLE-Arduino
  27. #include <NimBLEHIDDevice.h>
  28. #include <NimBLECharacteristic.h>
  29.  
  30. /****** PIN DEFINITIONS *****/
  31. #define VRx 34  // Joystick X axis pin
  32. #define VRy 35  // Joystick Y axis pin
  33. #define SW  32  // Joystick button pin
  34.  
  35. /****** CONSTANTS AND VARIABLES *****/
  36. const int DEAD_ZONE = 50; // Dead zone for joystick
  37. const int X_SENSITIVITY = 1; // Sensitivity multiplier for X
  38. const int Y_SENSITIVITY = 1; // Sensitivity multiplier for Y
  39. const int JOYSTICK_CENTER = 2048; // Center value for joystick (assuming 12-bit ADC)
  40. const unsigned long debounceDelay = 200; // Debounce delay in milliseconds
  41.  
  42. unsigned long lastButtonPressTime = 0;
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. NimBLEHIDDevice* hid;
  50. NimBLECharacteristic* input;
  51.  
  52. static const uint8_t _hidReportDescriptor[] = {
  53.     0x05, 0x01,
  54.     0x09, 0x02,
  55.     0xA1, 0x01,
  56.     0x09, 0x01,
  57.     0xA1, 0x00,
  58.     0x05, 0x09,
  59.     0x19, 0x01,
  60.     0x29, 0x03,
  61.     0x15, 0x00,
  62.     0x25, 0x01,
  63.     0x95, 0x03,
  64.     0x75, 0x01,
  65.     0x81, 0x02,
  66.     0x95, 0x01,
  67.     0x75, 0x05,
  68.     0x81, 0x03,
  69.     0x05, 0x01,
  70.     0x09, 0x30,
  71.     0x09, 0x31,
  72.     0x16, 0x01, 0xF8,
  73.     0x26, 0xFF, 0x07,
  74.     0x75, 0x10,
  75.     0x95, 0x02,
  76.     0x81, 0x06,
  77.     0xC0,
  78.     0xC0
  79. };
  80.  
  81. typedef struct {
  82.   uint8_t buttons;
  83.   int16_t x;
  84.   int16_t y;
  85. } __attribute__((packed)) mouse_report_t;
  86.  
  87. mouse_report_t mouseReport;
  88.  
  89. bool deviceConnected = false;
  90.  
  91. class MyServerCallbacks : public NimBLEServerCallbacks {
  92.     void onConnect(NimBLEServer* pServer) {
  93.         deviceConnected = true;
  94.     };
  95.  
  96.     void onDisconnect(NimBLEServer* pServer) {
  97.         deviceConnected = false;
  98.         NimBLEDevice::startAdvertising();
  99.     }
  100. };
  101.  
  102. void setup() {
  103.   Serial.begin(115200);
  104.   pinMode(SW, INPUT_PULLUP);
  105.   pinMode(VRx, INPUT);
  106.   pinMode(VRy, INPUT);
  107.  
  108.   NimBLEDevice::init("ESP32 Joystick Mouse");
  109.   NimBLEDevice::setPower(ESP_PWR_LVL_P9);
  110.  
  111.   NimBLEServer* pServer = NimBLEDevice::createServer();
  112.   pServer->setCallbacks(new MyServerCallbacks());
  113.  
  114.   hid = new NimBLEHIDDevice(pServer);
  115.   hid->setReportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
  116.  
  117.   input = hid->getInputReport(1);
  118.  
  119.   hid->startServices();
  120.  
  121.   NimBLEAdvertising* pAdvertising = pServer->getAdvertising();
  122.   pAdvertising->setAppearance(HID_MOUSE);
  123.   pAdvertising->addServiceUUID(hid->getHidService()->getUUID());
  124.   pAdvertising->start();
  125. }
  126.  
  127. void loop() {
  128.   if (!deviceConnected) {
  129.     delay(100);
  130.     return;
  131.   }
  132.  
  133.   int xValue = analogRead(VRx);
  134.   int yValue = analogRead(VRy);
  135.   int swState = digitalRead(SW);
  136.  
  137.   int16_t deltaX = 0;
  138.   int16_t deltaY = 0;
  139.  
  140.   if (xValue > JOYSTICK_CENTER + DEAD_ZONE) {
  141.     deltaX = (int16_t)((xValue - (JOYSTICK_CENTER + DEAD_ZONE)) * X_SENSITIVITY);
  142.   } else if (xValue < JOYSTICK_CENTER - DEAD_ZONE) {
  143.     deltaX = (int16_t)((xValue - (JOYSTICK_CENTER - DEAD_ZONE)) * X_SENSITIVITY);
  144.   }
  145.  
  146.   if (yValue < JOYSTICK_CENTER - DEAD_ZONE) {
  147.     deltaY = (int16_t)((yValue - (JOYSTICK_CENTER - DEAD_ZONE)) * Y_SENSITIVITY);
  148.   } else if (yValue > JOYSTICK_CENTER + DEAD_ZONE) {
  149.     deltaY = (int16_t)((yValue - (JOYSTICK_CENTER + DEAD_ZONE)) * Y_SENSITIVITY);
  150.   }
  151.  
  152.   mouseReport.x = deltaX;
  153.   mouseReport.y = deltaY;
  154.  
  155.   mouseReport.buttons = 0;
  156.  
  157.   if (swState == LOW) {
  158.     if (millis() - lastButtonPressTime > debounceDelay) {
  159.       mouseReport.buttons = (1 << 0);
  160.       input->setValue((uint8_t*)&mouseReport, sizeof(mouseReport));
  161.       input->notify();
  162.       delay(50);
  163.  
  164.       mouseReport.buttons = 0;
  165.       input->setValue((uint8_t*)&mouseReport, sizeof(mouseReport));
  166.       input->notify();
  167.  
  168.       lastButtonPressTime = millis();
  169.     }
  170.   }
  171.  
  172.   if (mouseReport.x != 0 || mouseReport.y != 0) {
  173.     input->setValue((uint8_t*)&mouseReport, sizeof(mouseReport));
  174.     input->notify();
  175.   }
  176.  
  177.   delay(10);
  178. }
  179.  
  180. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment