Skip to content

Commit

Permalink
Mouse clicker feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pr3y committed Jan 4, 2025
1 parent 3e99279 commit bd6f0b2
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 1 deletion.
121 changes: 121 additions & 0 deletions lib/Bad_Usb_Lib/USBHIDMouse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Mouse.cpp
Copyright (c) 2015, Arduino LLC
Original code (pre-library): Copyright (c) 2011, Peter Barrett
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "USBHID.h"
#if SOC_USB_OTG_SUPPORTED

#if CONFIG_TINYUSB_HID_ENABLED

#include "USBHIDMouse.h"

USBHIDMouseBase::USBHIDMouseBase(HIDMouseType_t *type) : hid(), _buttons(0), _type(type) {
static bool initialized = false;
if (!initialized) {
initialized = true;
hid.addDevice(this, _type->descriptor_size);
}
};

uint16_t USBHIDMouseBase::_onGetDescriptor(uint8_t *dst) {
memcpy(dst, _type->report_descriptor, _type->descriptor_size);
return _type->descriptor_size;
}

void USBHIDMouseBase::buttons(uint8_t b) {
if (b != _buttons) {
_buttons = b;
}
}

void USBHIDMouseBase::begin() {
hid.begin();
}

void USBHIDMouseBase::end() {}

void USBHIDMouseBase::press(uint8_t b) {
this->buttons(_buttons | b);
}

void USBHIDMouseBase::release(uint8_t b) {
this->buttons(_buttons & ~b);
}

bool USBHIDMouseBase::isPressed(uint8_t b) {
if ((b & _buttons) > 0) {
return true;
}
return false;
}

static const uint8_t abs_mouse_report_descriptor[] = {TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(HID_REPORT_ID_MOUSE))};

HIDMouseType_t HIDMouseAbs = {HID_MOUSE_ABSOLUTE, abs_mouse_report_descriptor, sizeof(abs_mouse_report_descriptor), sizeof(hid_mouse_report_t)};

void USBHIDAbsoluteMouse::move(int16_t x, int16_t y, int8_t wheel, int8_t pan) {
//hid_abs_mouse_report_t report;
hid_mouse_report_t report;
report.buttons = _buttons;
report.x = _lastx = x;
report.y = _lasty = y;
report.wheel = wheel;
report.pan = pan;
sendReport(report);
}

void USBHIDAbsoluteMouse::click(uint8_t b) {
_buttons = b;
move(_lastx, _lasty);
_buttons = 0;
move(_lastx, _lasty);
}

void USBHIDAbsoluteMouse::buttons(uint8_t b) {
if (b != _buttons) {
_buttons = b;
move(_lastx, _lasty);
}
}

static const uint8_t rel_mouse_report_descriptor[] = {TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(HID_REPORT_ID_MOUSE))};

HIDMouseType_t HIDMouseRel = {HID_MOUSE_RELATIVE, rel_mouse_report_descriptor, sizeof(rel_mouse_report_descriptor), sizeof(hid_mouse_report_t)};

void USBHIDRelativeMouse::move(int8_t x, int8_t y, int8_t wheel, int8_t pan) {
hid_mouse_report_t report = {.buttons = _buttons, .x = x, .y = y, .wheel = wheel, .pan = pan};
sendReport(report);
}

void USBHIDRelativeMouse::click(uint8_t b) {
_buttons = b;
move(0, 0);
_buttons = 0;
move(0, 0);
}

void USBHIDRelativeMouse::buttons(uint8_t b) {
if (b != _buttons) {
_buttons = b;
move(0, 0);
}
}

#endif /* CONFIG_TINYUSB_HID_ENABLED */
#endif /* SOC_USB_OTG_SUPPORTED */
98 changes: 98 additions & 0 deletions lib/Bad_Usb_Lib/USBHIDMouse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Mouse.h
Copyright (c) 2015, Arduino LLC
Original code (pre-library): Copyright (c) 2011, Peter Barrett
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

#include "soc/soc_caps.h"
#if SOC_USB_OTG_SUPPORTED

#include "USBHID.h"
#if CONFIG_TINYUSB_HID_ENABLED

#define MOUSE_LEFT 0x01
#define MOUSE_RIGHT 0x02
#define MOUSE_MIDDLE 0x04
#define MOUSE_BACKWARD 0x08
#define MOUSE_FORWARD 0x10
#define MOUSE_ALL 0x1F

enum MousePositioning_t {
HID_MOUSE_RELATIVE,
HID_MOUSE_ABSOLUTE
};

struct HIDMouseType_t {
MousePositioning_t positioning;
const uint8_t *report_descriptor;
size_t descriptor_size;
size_t report_size;
};

extern HIDMouseType_t HIDMouseRel;
extern HIDMouseType_t HIDMouseAbs;

class USBHIDMouseBase : public USBHIDDevice {
public:
USBHIDMouseBase(HIDMouseType_t *type);
void begin(void);
void end(void);
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
template<typename T> bool sendReport(T report) {
return hid.SendReport(HID_REPORT_ID_MOUSE, &report, _type->report_size);
};
// internal use
uint16_t _onGetDescriptor(uint8_t *buffer);
virtual void click(uint8_t b) = 0;
virtual void buttons(uint8_t b) = 0;

protected:
USBHID hid;
uint8_t _buttons;
HIDMouseType_t *_type;
};

class USBHIDRelativeMouse : public USBHIDMouseBase {
public:
USBHIDRelativeMouse(void) : USBHIDMouseBase(&HIDMouseRel) {}
void move(int8_t x, int8_t y, int8_t wheel = 0, int8_t pan = 0);
void click(uint8_t b = MOUSE_LEFT) override;
void buttons(uint8_t b) override;
};

class USBHIDAbsoluteMouse : public USBHIDMouseBase {
public:
USBHIDAbsoluteMouse(void) : USBHIDMouseBase(&HIDMouseAbs) {}
void move(int16_t x, int16_t y, int8_t wheel = 0, int8_t pan = 0);
void click(uint8_t b = MOUSE_LEFT) override;
void buttons(uint8_t b) override;

private:
int16_t _lastx = 0;
int16_t _lasty = 0;
};

// don't break examples and old sketches
typedef USBHIDRelativeMouse USBHIDMouse;

#endif /* CONFIG_TINYUSB_HID_ENABLED */
#endif /* SOC_USB_OTG_SUPPORTED */
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ lib_deps =
https://github.com/rennancockles/ESP-ChameleonUltra
https://github.com/rennancockles/ESP-Amiibolink
https://github.com/whywilson/ESP-PN532BLE
https://github.com/T-vK/ESP32-BLE-Mouse
NTPClient
Timezone
ESP32Time
Expand Down
4 changes: 3 additions & 1 deletion src/core/menu_items/OthersMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "modules/others/timer.h"
#include "core/utils.h"

#include "modules/others/clicker.h"
#include "modules/others/bad_usb.h"
#ifdef HAS_RGB_LED
#include "modules/others/led_control.h"
Expand All @@ -26,6 +27,7 @@ void OthersMenu::optionsMenu() {
{"Mic Spectrum", [=]() { mic_test(); }},
#endif
{"BadUSB", [=]() { usb_setup(); }},
{"Clicker", [=]() { clicker_setup(); }},
#ifdef HAS_KEYBOARD_HID
{"USB Keyboard", [=]() { usb_keyboard(); }},
#endif
Expand Down Expand Up @@ -78,4 +80,4 @@ void OthersMenu::drawIcon(float scale) {
240, 360,
bruceConfig.priColor, bruceConfig.bgColor
);
}
}
77 changes: 77 additions & 0 deletions src/modules/others/clicker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "globals.h"
#include <USB.h>
#include "core/display.h"
#include "clicker.h"
#include <BleMouse.h>
#include "core/mykeyboard.h"

#ifdef USB_as_HID
#include <USBHIDMouse.h>
USBHIDMouse Mouse;
#endif
BleMouse bleMouse;

unsigned long prevMillisec = 0;
unsigned long currMillisec = 0;
int clickCount = 0,delayValueInt;

void clicker_setup(){
String delayValue = keyboard("100",4,"Delay between click(ms)");
delayValueInt = atoi(delayValue.c_str());

options = {
#ifdef USB_as_HID
{"USB", [=]() { usbClickerSetup();}},
#endif
{"BLE", [=]() { bleClickerSetup();}},
};
delay(200);
loopOptions(options, true);
delay(200);
}

#ifdef USB_as_HID
void usbClickerSetup(){
USB.begin();
Mouse.begin();

tft.fillScreen(TFT_BLACK);
tft.setCursor(0,0);
tft.print("CPS");

for(;;){
Mouse.click(MOUSE_LEFT);
clickCount++;
currMillisec = millis();
if (currMillisec - prevMillisec >= 1000) {
Serial.println(clickCount);
displaySomething(String(clickCount));
clickCount = 0;
prevMillisec = currMillisec;
}
delay(delayValueInt);
}
}
#endif

void bleClickerSetup(){
bleMouse.begin();
tft.fillScreen(TFT_BLACK);
tft.setCursor(0,0);
displaySomething("Waiting to pair");

for(;;){
if(bleMouse.isConnected()) {
bleMouse.click(MOUSE_LEFT);
clickCount++;
currMillisec = millis();
if (currMillisec - prevMillisec >= 1000) {
Serial.println(clickCount);
displaySomething(String(clickCount));
clickCount = 0;
prevMillisec = currMillisec;
}
}
delay(delayValueInt);
}
}
3 changes: 3 additions & 0 deletions src/modules/others/clicker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void clicker_setup();
void usbClickerSetup();
void bleClickerSetup();

0 comments on commit bd6f0b2

Please sign in to comment.