Last active
August 3, 2024 21:52
-
-
Save mrcodetastic/3669355c3c7f29e6d306d821f4d22d66 to your computer and use it in GitHub Desktop.
USB Mouse Jiggler (Work From Home (WFH) emulator)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Compile and flash this firmware onto a ESP32-S2 and then use a seperate micro USB connector | |
* to connect the S2's Pin 19 to USB D- and Pin 20 to USB D+ (and of course +5V to +5V and GND to GND) | |
* | |
* IMPORTANT: On an ESP32-S2 Mini using Arduino IDE, make sure you have 'USB CDC On Boot' set to 'False' | |
* otherwise the 'USB Device Name' that shows up on Windows will not be correct as per the code below. | |
* | |
* It will appear as a 'USB Mouse' in Windows/Linux/Mac, and move the mouse every 30 seconds or so | |
* that you are always online/green. The ESP's LED will flash briefly when this occurs. | |
* | |
*/ | |
//#define USE_SIMPLE_MOVEMENT | |
#include <Arduino.h> | |
#include "USB.h" | |
#include "USBHIDMouse.h" | |
#include <iostream> | |
#include <cmath> | |
#include <vector> | |
// We also need to make sure the VID and PID match that of a Dell Laser Mouse, or | |
// the VID and PID will be show up as associated as an ESP32-S3 / Espressif device.. | |
//usbdev.VID(0x413C); | |
//usbdev.PID(0x250E); | |
ESPUSB usbdev; | |
USBHIDMouse Mouse; | |
// Constants | |
const int moveRange = 100; // max pixel to move x or y | |
const int ledPin = LED_BUILTIN; | |
const int jiggleHours = 8; | |
// Runtime | |
unsigned long start_ms = 0; | |
// Distributor | |
std::vector<double> distributeValue(double targetValue, int timePoints) { | |
std::vector<double> values(timePoints); | |
double sum = 0.0; | |
// Calculate the distribution values | |
for (int i = 0; i < timePoints; ++i) { | |
double angle = (static_cast<double>(i) / (timePoints - 1)) * M_PI; | |
values[i] = 0.5 * (1 - cos(angle)); // Sine wave distribution between 0 and 1 | |
sum += values[i]; | |
} | |
// Normalize the values so that their sum equals the target value | |
for (int i = 0; i < timePoints; ++i) { | |
values[i] = (values[i] / sum) * targetValue; | |
} | |
return values; | |
} | |
// | |
// Arduino Sketch Start | |
// | |
void setup() { | |
usbdev.VID(0x413C); // Doesn't seem to work! | |
usbdev.PID(0x250E); // Doesn't seem to work! | |
// Editing of the file was required to hard-code the VID AND PID. | |
// C:\Users\Anonymous\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.3\cores\esp32\USB.cpp | |
/* | |
ESPUSB::ESPUSB(size_t task_stack_size, uint8_t event_task_priority) | |
: vid(0x413C), pid(0x250E), product_name(USB_PRODUCT), manufacturer_name(USB_MANUFACTURER), serial_number(USB_SERIAL), fw_version(0x0100), | |
*/ | |
usbdev.productName("Dell Laser Mouse MS3220"); | |
usbdev.manufacturerName("Dell"); | |
usbdev.begin(); | |
Mouse.begin(); | |
pinMode(ledPin, OUTPUT); | |
start_ms = millis(); | |
// Seed the random number generator | |
// randomSeed(analogRead(0)); | |
} | |
void loop() { | |
//digitalWrite(LED_PIN, HIGH); | |
if ( (millis() - start_ms) > (jiggleHours*60*60*1000) ) | |
{ | |
// Keep LED lit when jiggling is over. | |
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(5000); | |
return; | |
} | |
// If NOT defined | |
#ifndef USE_SIMPLE_MOVEMENT | |
// Generate random x and y movements within the specified range | |
int movementx = random(-moveRange, moveRange); | |
int movementy = random(-moveRange, moveRange); | |
int timePoints = random(20,30); // The number of time points | |
// Distribute the value | |
std::vector<double> distributedValues_x = distributeValue(movementx, timePoints); | |
std::vector<double> distributedValues_y = distributeValue(movementy, timePoints); | |
for (int i = 0; i < timePoints; i++) { | |
Mouse.move( distributedValues_x[i], distributedValues_y[i] ); | |
delay(5); | |
} | |
/* | |
for (double value : distributedValues) { | |
Mouse.move( (int)value, 0); | |
delay(10); | |
} | |
*/ | |
#else | |
// Old Code, just hard move a few px | |
int random_x = random(1,4); | |
int random_y = random(1,4); | |
Mouse.move(random_x,random_y,0,0); | |
#endif | |
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(1000); | |
digitalWrite(ledPin, LOW); // turn the LED on (LOW is the voltage level) | |
unsigned long delay_ms = random(22000, 42000); // delay between 12 and 29 seconds | |
//int delay_ms = 5000; | |
delay(delay_ms); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment