Last active
April 26, 2023 10:46
-
-
Save squirelo/a980d4e866438e7b6ce60392b6a4c1be to your computer and use it in GitHub Desktop.
This file contains 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
#include <BleMouse.h> // Include the bleMouse library | |
#include "Adafruit_TinyUSB.h" // Include the Adafruit TinyUSB library | |
BleMouse bleMouse; | |
uint8_t const desc_hid_report[] = {TUD_HID_REPORT_DESC_MOUSE()}; | |
Adafruit_USBD_HID usbMouse(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_MOUSE, 2, false); | |
const int joystickXPin = 26; // Pin for the X-axis of the joystick | |
const int joystickYPin = 36; // Pin for the Y-axis of the joystick | |
const int joystickButtonPin = 2; // Pin for the joystick button | |
const int sensitivity = 5; // Sensitivity for mouse movements | |
int xValue = 0; // Value for the X-axis of the joystick | |
int yValue = 0; // Value for the Y-axis of the joystick | |
bool joystickButtonPressed = false; // State of the joystick button | |
void setup() { | |
bleMouse.begin(); // Initialize the bleMouse library | |
usbMouse.begin(); // Initialize the usbMouse library | |
Serial.begin(115200); // Initialize the serial communication | |
while (! Serial) delay(1); // Wait for the serial communication to be established | |
pinMode(joystickXPin, INPUT); // Set the X-axis pin of the joystick as an input | |
pinMode(joystickYPin, INPUT); // Set the Y-axis pin of the joystick as an input | |
pinMode(joystickButtonPin, INPUT_PULLUP); // Set the joystick button pin as an input with a pull-up resistor | |
} | |
void loop() { | |
xValue = analogRead(joystickXPin); // Read the analog value from the X-axis pin of the joystick | |
yValue = analogRead(joystickYPin); // Read the analog value from the Y-axis pin of the joystick | |
joystickButtonPressed = !digitalRead(joystickButtonPin); // Read the digital value from the joystick button pin | |
const int deadzone = 6; // Deadzone range, centered around the middle value | |
int xMouse = map(xValue, 0, 4095, -sensitivity, sensitivity); // Map the X-axis value to the range of mouse movement from -sensitivity to +sensitivity | |
int yMouse = map(yValue, 0, 4095, -sensitivity, sensitivity); // Map the Y-axis value to the range of mouse movement from -sensitivity to +sensitivity | |
xMouse = constrain(xMouse, -sensitivity, sensitivity); | |
yMouse = constrain(yMouse, -sensitivity, sensitivity); | |
// Ignore joystick input within the deadzone range | |
if (abs(xMouse) < deadzone / 2) xMouse = 0; | |
if (abs(yMouse) < deadzone / 2) yMouse = 0; | |
if (abs(xMouse) > 0 || abs(yMouse) > 0) { | |
bleMouse.move(xMouse, yMouse); // If the mapped values are outside the deadzone, move the BLE mouse by xMouse and yMouse | |
if (usbMouse.ready()) { | |
usbMouse.mouseMove(0, xMouse, yMouse); // If the mapped values are outside the deadzone and usbMouse is ready, move the USB mouse by xMouse and yMouse | |
} | |
} | |
if (joystickButtonPressed) { | |
bleMouse.press(MOUSE_LEFT); // If the joystick button is pressed, press the left button of the BLE mouse | |
usbMouse.buttonPress(0, 1); // If the joystick button is pressed, press the left button of the USB mouse (report ID 0, left button 1) | |
} else { | |
bleMouse.release(MOUSE_LEFT); // If the joystick button is not pressed, release the left button of the BLE mouse | |
usbMouse.buttonRelease(0, 1); // If the joystick button is not pressed, release the left button of the USB mouse (report ID 0, left button 1) | |
} | |
delay(10); // Add a small delay to avoid overwhelming the loop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment