EloiStree / HelloInput

Collect of information about old project on Input in aim to do a workshop on the topic
0 stars 0 forks source link

Hardware: Simulate Joystick from Arduino Leonardo #279

Open EloiStree opened 1 week ago

EloiStree commented 1 week ago

Create random input for testing

Tutorial FR: image https://youtu.be/CtSbidusOyc

image https://github.com/MHeironimus/ArduinoJoystickLibrary

Download this Zip:

Code:

#include <Joystick.h>
#include <Arduino.h>

// Create a Joystick object with 24 buttons and 2 analog triggers
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
                   24,  // Number of buttons
                   0,   // Number of hat switches
                   true, true, false,   // X, Y, Z for joystick 1
                   true, true, false,   // X, Y, Z for joystick 2
                   false, false,        // Rudder, Throttle
                   false, false, false  // Accelerator, Brake, Steering
);

void setup() {
  // Initialize the joystick
  Joystick.begin();
  randomSeed(analogRead(0));  // Seed random number generator with noise from analog pin
}

void loop() {
  // Generate random values for joystick 1 (X and Y axes)
  int joystick1X = random(-127, 128);  // Random value between -127 and 127
  int joystick1Y = random(-127, 128);  // Random value between -127 and 127
  Joystick.setXAxis(joystick1X);
  Joystick.setYAxis(joystick1Y);

  // Generate random values for joystick 2 (X and Y axes)
  int joystick2X = random(-127, 128);  // Random value between -127 and 127
  int joystick2Y = random(-127, 128);  // Random value between -127 and 127
  Joystick.setRxAxis(joystick2X);
  Joystick.setRyAxis(joystick2Y);

  // Generate random values for two analog triggers (0 to 255 range)
  int trigger1 = random(0, 256);  // Random value between 0 and 255
  int trigger2 = random(0, 256);  // Random value between 0 and 255
  Joystick.setZAxis(trigger1);
  Joystick.setRzAxis(trigger2);

  // Set random states for 24 buttons
  for (int i = 0; i < 24; i++) {
    bool buttonState = random(0, 2);  // Randomly 0 (not pressed) or 1 (pressed)
    Joystick.setButton(i, buttonState);
  }

  delay(100);  // Update every 100 ms
}

Multiple Joysticks from on Arduino Leonardo

Apparently the library allows to create several devices from only one 🧙‍♂️

image image


#include <Joystick.h>

#define JOYSTICK_COUNT 4

Joystick_ Joystick[JOYSTICK_COUNT] = {
  Joystick_(0x03, JOYSTICK_TYPE_JOYSTICK,  4, 2,  true, true, false, false, false, false, false, false, false, false, false),
  Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK,  8, 1,  true, true,  true,  true, false, false, false, false, false, false, false),
  Joystick_(0x05, JOYSTICK_TYPE_JOYSTICK, 16, 0, false, true, false,  true, false, false,  true,  true, false, false, false),
  Joystick_(0x06, JOYSTICK_TYPE_JOYSTICK, 32, 1,  true, true, false,  true,  true, false, false, false, false, false, false)
};

As this library authorized that, it means that it is the perfect code with an HC06 to simulate fake joystick on a computer for QA testing purpose.

EloiStree commented 1 week ago

image https://www.youtube.com/watch?v=ho4FeXY-Zgc https://github.com/EloiStree/2024_11_31_ReadHardwareToIndexInteger/blob/main/ArduinoLeonardo/ElevenPotentiometerJoystick/JoystickEleve/JoystickEleve.ino