diff --git a/lib/Bad_Usb_Lib/USBHIDMouse.cpp b/lib/Bad_Usb_Lib/USBHIDMouse.cpp new file mode 100644 index 00000000..b6f9fe26 --- /dev/null +++ b/lib/Bad_Usb_Lib/USBHIDMouse.cpp @@ -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 */ diff --git a/lib/Bad_Usb_Lib/USBHIDMouse.h b/lib/Bad_Usb_Lib/USBHIDMouse.h new file mode 100644 index 00000000..9b9e348b --- /dev/null +++ b/lib/Bad_Usb_Lib/USBHIDMouse.h @@ -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 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 */ diff --git a/platformio.ini b/platformio.ini index c95b13a5..14c1197d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/core/menu_items/OthersMenu.cpp b/src/core/menu_items/OthersMenu.cpp index aaf3e38b..3c61695c 100644 --- a/src/core/menu_items/OthersMenu.cpp +++ b/src/core/menu_items/OthersMenu.cpp @@ -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" @@ -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 @@ -78,4 +80,4 @@ void OthersMenu::drawIcon(float scale) { 240, 360, bruceConfig.priColor, bruceConfig.bgColor ); -} \ No newline at end of file +} diff --git a/src/modules/others/clicker.cpp b/src/modules/others/clicker.cpp new file mode 100644 index 00000000..26aebb02 --- /dev/null +++ b/src/modules/others/clicker.cpp @@ -0,0 +1,77 @@ +#include "globals.h" +#include +#include "core/display.h" +#include "clicker.h" +#include +#include "core/mykeyboard.h" + +#ifdef USB_as_HID +#include +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); + } +} diff --git a/src/modules/others/clicker.h b/src/modules/others/clicker.h new file mode 100644 index 00000000..f70c5fb9 --- /dev/null +++ b/src/modules/others/clicker.h @@ -0,0 +1,3 @@ +void clicker_setup(); +void usbClickerSetup(); +void bleClickerSetup();