Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Bluetooth HID
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-30 03:47:44
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Control pc cursor using the joystick */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <NimBLEDevice.h> //https://github.com/h2zero/NimBLE-Arduino
- #include <NimBLEHIDDevice.h>
- #include <NimBLECharacteristic.h>
- /****** PIN DEFINITIONS *****/
- #define VRx 34 // Joystick X axis pin
- #define VRy 35 // Joystick Y axis pin
- #define SW 32 // Joystick button pin
- /****** CONSTANTS AND VARIABLES *****/
- const int DEAD_ZONE = 50; // Dead zone for joystick
- const int X_SENSITIVITY = 1; // Sensitivity multiplier for X
- const int Y_SENSITIVITY = 1; // Sensitivity multiplier for Y
- const int JOYSTICK_CENTER = 2048; // Center value for joystick (assuming 12-bit ADC)
- const unsigned long debounceDelay = 200; // Debounce delay in milliseconds
- unsigned long lastButtonPressTime = 0;
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- NimBLEHIDDevice* hid;
- NimBLECharacteristic* input;
- static const uint8_t _hidReportDescriptor[] = {
- 0x05, 0x01,
- 0x09, 0x02,
- 0xA1, 0x01,
- 0x09, 0x01,
- 0xA1, 0x00,
- 0x05, 0x09,
- 0x19, 0x01,
- 0x29, 0x03,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x95, 0x03,
- 0x75, 0x01,
- 0x81, 0x02,
- 0x95, 0x01,
- 0x75, 0x05,
- 0x81, 0x03,
- 0x05, 0x01,
- 0x09, 0x30,
- 0x09, 0x31,
- 0x16, 0x01, 0xF8,
- 0x26, 0xFF, 0x07,
- 0x75, 0x10,
- 0x95, 0x02,
- 0x81, 0x06,
- 0xC0,
- 0xC0
- };
- typedef struct {
- uint8_t buttons;
- int16_t x;
- int16_t y;
- } __attribute__((packed)) mouse_report_t;
- mouse_report_t mouseReport;
- bool deviceConnected = false;
- class MyServerCallbacks : public NimBLEServerCallbacks {
- void onConnect(NimBLEServer* pServer) {
- deviceConnected = true;
- };
- void onDisconnect(NimBLEServer* pServer) {
- deviceConnected = false;
- NimBLEDevice::startAdvertising();
- }
- };
- void setup() {
- Serial.begin(115200);
- pinMode(SW, INPUT_PULLUP);
- pinMode(VRx, INPUT);
- pinMode(VRy, INPUT);
- NimBLEDevice::init("ESP32 Joystick Mouse");
- NimBLEDevice::setPower(ESP_PWR_LVL_P9);
- NimBLEServer* pServer = NimBLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- hid = new NimBLEHIDDevice(pServer);
- hid->setReportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
- input = hid->getInputReport(1);
- hid->startServices();
- NimBLEAdvertising* pAdvertising = pServer->getAdvertising();
- pAdvertising->setAppearance(HID_MOUSE);
- pAdvertising->addServiceUUID(hid->getHidService()->getUUID());
- pAdvertising->start();
- }
- void loop() {
- if (!deviceConnected) {
- delay(100);
- return;
- }
- int xValue = analogRead(VRx);
- int yValue = analogRead(VRy);
- int swState = digitalRead(SW);
- int16_t deltaX = 0;
- int16_t deltaY = 0;
- if (xValue > JOYSTICK_CENTER + DEAD_ZONE) {
- deltaX = (int16_t)((xValue - (JOYSTICK_CENTER + DEAD_ZONE)) * X_SENSITIVITY);
- } else if (xValue < JOYSTICK_CENTER - DEAD_ZONE) {
- deltaX = (int16_t)((xValue - (JOYSTICK_CENTER - DEAD_ZONE)) * X_SENSITIVITY);
- }
- if (yValue < JOYSTICK_CENTER - DEAD_ZONE) {
- deltaY = (int16_t)((yValue - (JOYSTICK_CENTER - DEAD_ZONE)) * Y_SENSITIVITY);
- } else if (yValue > JOYSTICK_CENTER + DEAD_ZONE) {
- deltaY = (int16_t)((yValue - (JOYSTICK_CENTER + DEAD_ZONE)) * Y_SENSITIVITY);
- }
- mouseReport.x = deltaX;
- mouseReport.y = deltaY;
- mouseReport.buttons = 0;
- if (swState == LOW) {
- if (millis() - lastButtonPressTime > debounceDelay) {
- mouseReport.buttons = (1 << 0);
- input->setValue((uint8_t*)&mouseReport, sizeof(mouseReport));
- input->notify();
- delay(50);
- mouseReport.buttons = 0;
- input->setValue((uint8_t*)&mouseReport, sizeof(mouseReport));
- input->notify();
- lastButtonPressTime = millis();
- }
- }
- if (mouseReport.x != 0 || mouseReport.y != 0) {
- input->setValue((uint8_t*)&mouseReport, sizeof(mouseReport));
- input->notify();
- }
- delay(10);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment