diff --git a/platformio.ini b/platformio.ini index 48f2ae8..7e8abfb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,3 +1,13 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + [env:esp32-s3-supermini] platform = espressif32 board = esp32-s3-devkitc-1 @@ -5,11 +15,13 @@ framework = arduino monitor_speed = 115200 upload_speed = 460800 - board_upload.flash_size = 4MB board_build.psram = enabled board_build.partitions = default.csv -build_flags = - -D ARDUINO_USB_CDC_ON_BOOT=1 - -D ARDUINO_USB_MODE=1 \ No newline at end of file +build_flags = + -D ARDUINO_USB_CDC_ON_BOOT=1 + -D ARDUINO_USB_MODE=1 + +upload_port = COM3 +monitor_port = COM3 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5474ec4..cdd8f31 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,75 @@ #include +#include +#include +#include + +USBHIDMouse Mouse; #define LED_BUILTIN 48 +const int buttonPin = 0; +int previousButtonState = HIGH; + +void smoothMouseMove(int xOffset, int yOffset, unsigned long durationMs, float steps) { + if (steps <= 0) { + // Avoid division by zero or infinite loop if steps is non-positive + Mouse.move(xOffset, yOffset); // Fallback to instant move + return; + } + + // Calculate the movement per step for X and Y + float xStep = (float)xOffset / steps; + float yStep = (float)yOffset / steps; + + // Calculate the delay between each step + unsigned long delayPerStep = durationMs / steps; + + // Perform the movement in small steps + for (int i = 0; i < steps; i++) { + // Move the mouse by the calculated step amount. + // We cast to int because Mouse.move expects integers, + // but float calculations ensure more accurate distribution over steps. + Mouse.move((int)xStep, (int)yStep); + delay(delayPerStep); // Pause for a short duration + } + + // Handle any remaining fractional movement due to integer casting + // This ensures the mouse ends up exactly at the target offset + int remainingX = xOffset - (int)(xStep * steps); + int remainingY = yOffset - (int)(yStep * steps); + if (remainingX != 0 || remainingY != 0) { + Mouse.move(remainingX, remainingY); + } +} + void setup() { delay(1000); Serial.begin(115200); - Serial.println("ESP32-S3 Blink Starting..."); + Serial.println("Starting ESP32-S3 USB Mouse"); + pinMode(LED_BUILTIN, OUTPUT); + + USB.begin(); + Mouse.begin(); + + delay(2000); } void loop() { - Serial.println("Beep Beep..."); + int buttonState = digitalRead(buttonPin); + const int purepixels = 8182; + const int kiripurepixels = 27300; + + const int input = purepixels; + + if ((buttonState != previousButtonState) && (buttonState == LOW)) { + digitalWrite(LED_BUILTIN, LOW); + smoothMouseMove(input, 0, 1000, (input / 10)); + delay(500); + Serial.println("Button Pressed - Mouse should have sent event."); + } + previousButtonState = buttonState; + digitalWrite(LED_BUILTIN, HIGH); - delay(500); - digitalWrite(LED_BUILTIN, LOW); - delay(1000); } \ No newline at end of file