Coder Social home page Coder Social logo

schnoog / joystick_esp32s2 Goto Github PK

View Code? Open in Web Editor NEW
34.0 5.0 3.0 273 KB

Joystick library for ESP32 S2 & S3 devices (native USB) for the Arduino framework.

License: GNU Lesser General Public License v3.0

C++ 100.00%
arduino arduino-library esp32 esp32-s2 esp32-s3 usb-joystick

joystick_esp32s2's Introduction

ESP32-S2/S3 Joystick Library

based on Matthew Heironimus great Arduino Joystick lirary

Version 0.9 - forked from Version 2.1.1

This will not work with Arduino IDE 1.6.5 (or below) or with non ESP32-S2/S3 devices (e.g. Arduino UNO, Arduino MEGA, etc.).

To use this library you need to have Arduino core for the ESP32 installed (https://github.com/espressif/arduino-esp32)

Features

The joystick or gamepad can have the following features:

  • Buttons (default: 32)
  • Up to 2 Hat Switches
  • X, Y, and/or Z Axis (up to 16-bit precision)
  • X, Y, and/or Z Axis Rotation (up to 16-bit precision)
  • Rudder (up to 16-bit precision)
  • Throttle (up to 16-bit precision)
  • Accelerator (up to 16-bit precision)
  • Brake (up to 16-bit precision)
  • Steering (up to 16-bit precision)

Installation Instructions

Library manager

Just search for Joystick_ESP32S2 in the library manager of your installation of ArduinoIDE or PlatformIO and install it.

Manual installation

The following instructions can be used to install the latest version of the library in the Arduino IDE (thanks to @per1234 for this update):

  1. Download https://github.com/schnoog/Joystick_ESP32S2/archive/master.zip
  2. In the Arduino IDE, select Sketch > Include Library > Add .ZIP Library.... Browse to where the downloaded ZIP file is located and click Open. The Joystick library's examples will now appear under File > Examples > Joystick.

Please keep in mind that a manual installation is not updated by the library manager of your IDE.

USB Setup

If you only want to play arround with it, I propose to enable USB CDC on boot. This can be done quite easily

  • ArduinoIDE: There's an option in the tools menue to enable USB CDC on boot
  • PlatformIO: Set the option within the platformio.ini of your project
build_flags =
	....
    -DARDUINO_USB_CDC_ON_BOOT=1

If you want to change your joysticks name, USB product id or vendor id, please consult the USBOPTIONS.md or have a look into the Joystick_WithCustomPIDandVID example provided with this library

Examples

Simple example

#include <Joystick_ESP32S2.h>

// Create the Joystick
Joystick_ Joystick;

// Constant that maps the physical pin to the joystick button.
const int pinToButtonMap = 9;

void setup() {
	// Initialize Button Pins
	pinMode(pinToButtonMap, INPUT_PULLUP);

	// Initialize Joystick Library
	Joystick.begin();
}

// Last state of the button
int lastButtonState = 0;

void loop() {

	// Read pin values
	int currentButtonState = !digitalRead(pinToButtonMap);
	if (currentButtonState != lastButtonState)
	{
	Joystick.setButton(0, currentButtonState);
	lastButtonState = currentButtonState;
	}

	delay(50);
}

Included Examples

The following example Arduino sketch files are included in this library:

Simple Samples

  • JoystickButton - Creates a Joystick and maps pin 9 to button 0 of the joystick, pin 10 to button 1, pin 11 to button 2, and pin 12 to button 3.
  • JoystickKeyboard - Creates a Joystick and a Keyboard. Maps pin 9 to Joystick Button 0, pin 10 to Joystick Button 1, pin 11 to Keyboard key 1, and pin 12 to Keyboard key 2.
  • GamepadExample - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
  • FunduinoJoystickShield - Creates a simple Gamepad using a Funduino Joystick Shield (https://protosupplies.com/product/funduino-joystick-shield-v1-a/).
  • ArcadeStickExample - Simple arcade stick example that demonstrates how to read twelve Arduino Pro Micro digital pins and map them to the library (thanks to @nebhead for this example). NOTE: This sketch is for the Arduino Pro Micro only.

Used for Testing

  • JoystickTest - Simple test of the Joystick library. It exercises many of the Joystick library’s functions when pin A0 is grounded.
  • MultipleJoystickTest - Creates 4 Joysticks using the library (each with a slightly different configuration) and exercises the first 16 buttons (if present), the X axis, and the Y axis of each joystick when pin A0 is grounded.
  • FlightControllerTest - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
  • HatSwitchTest - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
  • DrivingControllerTest - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.

Joystick Library API

The following API is available if the Joystick library in included in a sketch file.

Joystick_(...)

Constructor used to initialize and setup the Joystick. The following optional parameters are available:

  • uint8_t hidReportId - Default: 0x03 - Indicates the joystick's HID report ID. This value must be unique if you are creating multiple instances of Joystick. Do not use 0x01 or 0x02 as they are used by the built-in Arduino Keyboard and Mouse libraries.
  • uint8_t joystickType - Default: JOYSTICK_TYPE_JOYSTICK or 0x04 - Indicates the HID input device type. Supported values:
    • JOYSTICK_TYPE_JOYSTICK or 0x04 - Joystick
    • JOYSTICK_TYPE_GAMEPAD or 0x05 - Gamepad
    • JOYSTICK_TYPE_MULTI_AXIS or 0x08 - Multi-axis Controller
  • uint8_t buttonCount - Default: 32 - Indicates how many buttons will be available on the joystick.
  • uint8_t hatSwitchCount - Default: 2 - Indicates how many hat switches will be available on the joystick. Range: 0 - 2
  • bool includeXAxis - Default: true - Indicates if the X Axis is available on the joystick.
  • bool includeYAxis - Default: true - Indicates if the Y Axis is available on the joystick.
  • bool includeZAxis - Default: true - Indicates if the Z Axis (in some situations this is the right X Axis) is available on the joystick.
  • bool includeRxAxis - Default: true - Indicates if the X Axis Rotation (in some situations this is the right Y Axis) is available on the joystick.
  • bool includeRyAxis - Default: true - Indicates if the Y Axis Rotation is available on the joystick.
  • bool includeRzAxis - Default: true - Indicates if the Z Axis Rotation is available on the joystick.
  • bool includeRudder - Default: true - Indicates if the Rudder is available on the joystick.
  • bool includeThrottle - Default: true - Indicates if the Throttle is available on the joystick.
  • bool includeAccelerator - Default: true - Indicates if the Accelerator is available on the joystick.
  • bool includeBrake - Default: true - Indicates if the Brake is available on the joystick.
  • bool includeSteering - Default: true - Indicates if the Steering is available on the joystick.

The following constants define the default values for the constructor parameters listed above:

  • JOYSTICK_DEFAULT_REPORT_ID is set to 0x03
  • JOYSTICK_DEFAULT_BUTTON_COUNT is set to 32
  • JOYSTICK_DEFAULT_HATSWITCH_COUNT is set to 2

Joystick.begin(bool initAutoSendState)

Starts emulating a game controller connected to a computer. By default, all methods update the game controller state immediately. If initAutoSendState is set to false, the Joystick.sendState method must be called to update the game controller state.

Joystick.end()

Stops the game controller emulation to a connected computer (Note: just like the Arduino Keyboard.h and Mouse.h libraries, the end() function does not actually do anything).

Joystick.setXAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the X axis. Default: 0 to 1023

Joystick.setXAxis(int32_t value)

Sets the X axis value. See setXAxisRange for the range.

Joystick.setYAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Y axis. Default: 0 to 1023

Joystick.setYAxis(int32_t value)

Sets the Y axis value. See setYAxisRange for the range.

Joystick.setZAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Z axis. Default: 0 to 1023

Joystick.setZAxis(int32_t value)

Sets the Z axis value. See setZAxisRange for the range.

Joystick.setRxAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the X axis rotation. Default: 0 to 1023

Joystick.setRxAxis(int32_t value)

Sets the X axis rotation value. See setRxAxisRange for the range.

Joystick.setRyAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Y axis rotation. Default: 0 to 1023

Joystick.setRyAxis(int32_t value)

Sets the Y axis rotation value. See setRyAxisRange for the range.

Joystick.setRzAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Z axis rotation. Default: 0 to 1023

Joystick.setRzAxis(int32_t value)

Sets the Z axis rotation value. See setRzAxisRange for the range.

Joystick.setRudderRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Rudder. Default: 0 to 1023

Joystick.setRudder(int32_t value)

Sets the Rudder value. See setRudderRange for the range.

Joystick.setThrottleRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Throttle. Default: 0 to 1023

Joystick.setThrottle(int32_t value)

Sets the Throttle value. See setThrottleRange for the range.

Joystick.setAcceleratorRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Accelerator. Default: 0 to 1023

Joystick.setAccelerator(int32_t value)

Sets the Accelerator value. See setAcceleratorRange for the range.

Joystick.setBrakeRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Brake. Default: 0 to 1023

Joystick.setBrake(int32_t value)

Sets the Brake value. See setBrakeRange for the range.

Joystick.setSteeringRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Steering. Default: 0 to 1023

Joystick.setSteering(int32_t value)

Sets the Steering value. See setSteeringRange for the range.

Joystick.setButton(uint8_t button, uint8_t value)

Sets the state (0 or 1) of the specified button (range: 0 - (buttonCount - 1)). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.). The value is 1 if the button is pressed and 0 if the button is released.

Joystick.pressButton(uint8_t button)

Press the indicated button (range: 0 - (buttonCount - 1)). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick.releaseButton(uint8_t button)

Release the indicated button (range: 0 - (buttonCount - 1)). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick.setHatSwitch(int8_t hatSwitch, int16_t value)

Sets the value of the specified hat switch. The hatSwitch is 0-based (i.e. hat switch #1 is 0 and hat switch #2 is 1). The value is from 0° to 360°, but in 45° increments. Any value less than 45° will be rounded down (i.e. 44° is rounded down to 0°, 89° is rounded down to 45°, etc.). Set the value to JOYSTICK_HATSWITCH_RELEASE or -1 to release the hat switch.

Joystick.sendState()

Sends the updated joystick state to the host computer. Only needs to be called if AutoSendState is false (see Joystick.begin for more details).

See the Wiki for more details on things like FAQ, supported boards, testing, etc.

joystick_esp32s2's People

Contributors

schnoog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

joystick_esp32s2's Issues

Syncronization issue.

HID.SendReport(_hidReportId, data, sizeof(data),0);

I just found an issue in the code. USBHID::SendReport method works asynchroniously, i.e. it sends a report and returns immediately, even before the report completes and the method is ready to be called again. If you call it again before it gets ready, there will be an error. The error is not critical, but the next report won't be sent. It doesn't affect absolute attributes like steering or joystick position, but it may affect one-time effects like button clicks and relative changes like mouse movements.

To overcome these issues, there's forth parameter uint32_t timeout_ms, which is used to wait for the report successful sending and returns from the method after that.

I'd recommend to set it to some default value >0 out of the box. And potentially make it configurable for better tuning.
I assume most of the use-cases for this library will be using a simpler blocking approach for these calls, which means a timeout is necessary. Also, there are other libs, including standard ones by espressif. They will fail if used together with the current implementation of Joystick_ESP32.

Joystick not recognized

Issue

Examples "FlightControllerTest.ino" and "JoystickTest.ino" both do not report HID device. Actually, nothing is reported.
Test with flashing the onboard LED works, so I expect the program is transferred correctly.

Hardware

S2_DevBoard-clone by "VCC GND", separate usb connectors for native usb and UART

PC

  • Linux Mint (feat. Ubuntu 20.3)
  • Platformio w/ Arduino framework

Examples checked with lsusb, dmesg and syslog, no USB from board reported

Allowed Values

Hey
It would be great to know possible maximum and minimum values. Defaults are great but to customise we need to know available ranges. For example, the default number of buttons is 32. Can I set it to 0 or 10000?
The original library was restricted to 6 axes, is this the same case in this library for ESP32? Or can I use let's say 11 axis?
Can I use all 16-bit analogues and use them all at once with 1024 buttons using a shift register?
Please provide a complete datasheet for this library with minimum and maximum values on top of pre-set defaults.
Kind Regards

Mux over i2c

Hello, first of all thanks for the work, I am trying to add an mcp23017 mutiplexer via i2c but I cannot get the communication between it and esp32s3. The information I have found does not take me anywhere. Would you know how to configure the gpio's for this purpose, thank you

Slow execution time

Hi @schnoog,
thank you for the port. Good job. It works as expected. I wrote a simple script to measure the runtime.
With Arduino Due and the original lib, 36µs have been timed. With an ESP32 S2, approx. 1000µs have been timed for the same script, see attachment.

Is it possible to achieve similar runtimes on ESP32?

BR

----EDIT-----
I've added an zero here, now it runs at 20µs on the ESP:
image

DrivingControllerTest_Profiling.zip

can i use a rotary encoder with the code??

im trying to make a steering wheel i have used apot but the 270 degreees isnt realy good tbh so i was wondering if i could use a rotary encoder with the code or using a hdd motor/stepper as a rotary encoder

Simultaneous serial and gamepad data

Thank you for this awesome project.

I'm using an ESP32 S3 (connected via USB port) and want to send/read additional serial data over the USB port to monitor/debug the ESP.

What seems to work:

  • USB gamepad data
  • ESP seems to receive serial data from PC.

What does not work:

  • ESP doesn't send serial data to PC.

Do you have an example script where the additional serial communication is working properly?

BR
Chris

Question regarding using esp32 s3 with analog read 12bit resolution

Hello there,
i am experiencing some strange behavior when using the 12 bit analog read of a esp32 s3 with this library to create a USB Gamecontroller (two joystick, each providing x/y axis which will be mapped to x,y,z and throttle.)

After calibration (which seems to work fine) for example the x/y axis will move only in two direction from the center. So to the left and up, but not to the right and down - although i can see on my transmitter serial monitor that values are sent correctly.

This issues does not occur if i map my readings to a 10 bit resolution 0 - 1024 with 512 beeing the center - but then i am loosing the resolution of the esp32, which is a pitty.

Any idea on why that might be?

down_and_right_stay_center
left
up
x_y_center

Not visible or working

Hi,
thanks for your library. I have made some tests with my Esp32-S3 which has two Usb-c connectors (flight controller test example). I have first used Arduino Studio and made all Usb Settings (cdc on boot) in Menu but the game controller was not shown in the game controller settings list. I have add the usb settings in the setup() as shown in your readme but with the same result.
I have installed Visual Studio Code and made the same settings and upload the code. The device is shown as TinyUsb i. the gamecontroller list but the valuex (axes) and buttons are not shown anything (changing values). Do you have any idea what i‘ve doing wrong? I do nat have any knowledge in plattforio.ini

Greetings from germany

How to flash onto an ESP32S3 Dev Kit C N8R2

Hello I'm having trouble getting this library to work with an esp 32 S3 dev kit C. My unit has two usb type c ports as depicted below.
ESP32 S3 Type C amazon
My problem is that when I make a sketch in arduino ide and attempt to upload it to the board, the esp32 is still not picked up by my computer as a USB HID device. The best I have managed is getting the be detected by window as something different than the default ESP32-S3 it would show a message saying that Windows does not recognize the USB device. I have tried flashing it with the OTG and the other port with different device settings and tools enabled but haven't had much success. I also performed the enable usb .How would one get this to work for esp32-S3 Dev Kit boards? Which settings and IDE tools would I need to enable. Here is a link to the original board and the protoype sketch. https://www.amazon.com/gp/product/B0BPP28ZFB/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1

`
#include <Joystick_ESP32S2.h>

using namespace std;

// Analog outputs
#define pitch 0 // Rx input
#define throttle 1 // throttle input

// Button and switches
#define button 4
#define pos_3_up 5
#define pos_3_down 6
#define mom_up 7
#define mom_down 15

// Joystick_(uint8_t hidReportId = JOYSTICK_DEFAULT_REPORT_ID, uint8_t joystickType = JOYSTICK_TYPE_JOYSTICK,
// uint8_t buttonCount = JOYSTICK_DEFAULT_BUTTON_COUNT, uint8_t hatSwitchCount = JOYSTICK_DEFAULT_HATSWITCH_COUNT,
// bool includeXAxis = true, bool includeYAxis = true, bool includeZAxis = true, bool includeRxAxis = true, bool
// includeRyAxis = true, bool includeRzAxis = true, bool includeRudder = true, bool includeThrottle = true,
//bool includeAccelerator = true, bool includeBrake = true, bool includeSteering = true)

// We are going to include the Rx and Ry inputs dfo

Joystick_ Joystick(0x11, JOYSTICK_TYPE_JOYSTICK, 5, 0, false, false, false, true, false, false, false, true, false, false, false ); // create joystick object

const bool initAutoSendState =true;

// set the axis on the joystick side
int RxAxis = 0;
int throttleAxis = 0;

//declare buttons
bool button1State = 0;
bool button2State = 0; // 3pos switch
bool button3State = 0; // 3 post down
bool button4State = 0; // mom down
bool button5State = 0; // mom up

void setup() {
// put your setup code here, to run once:
// First get the serial boot loader working
Serial.begin(115200); // begin serial monitor to see what is going out

// Setup the button pins
// pinMode(pitch, INPUT); // collective angle input
// pinMode(throttle, INPUT); // twist throttle
pinMode(button, INPUT_PULLUP); // button input
pinMode(pos_3_up, INPUT_PULLUP); // 3 position switch
pinMode(pos_3_down, INPUT_PULLUP); // 3 position switch
pinMode(mom_up, INPUT_PULLUP);// momentary up
pinMode(mom_down, INPUT_PULLUP);// momentary down

// begin the joystick
// duhhh shame to myself
Joystick.begin();

// Setup USB Names
USB.PID(0x8211);
USB.VID(0x303b);
USB.productName("ESP32 S2 Collective");
USB.manufacturerName("My company name");
USB.begin();
// build_flags =
// ....
// // -DARDUINO_USB_CDC_ON_BOOT=1
// // // -DUSB_VID=0xF011
// // // -DUSB_PID=0xF011
// -DUSB_PRODUCT="Helicopter Collective"
// -DUSB_MANUFACTURER="Espressif Systems"

}

void loop(){
// lets do the axis inputs

// Axis runtime for pitch and throttle
RxAxis = analogRead(pitch);
RxAxis = map(RxAxis, 0, 1023, 0, 255);
Joystick.setRxAxis(RxAxis);

throttleAxis = analogRead(throttle);
// throttleAxis = 1023 -throttleAxis;
throttleAxis = map(throttleAxis, 0, 1023, 0, 255); // I reversed the axis
Joystick.setThrottle(throttleAxis);
Serial.print("throttle setting: ");
Serial.print(analogRead(throttleAxis));

// now include the digital buttons
// NOTE: no pullup resistors used yet

// normal switch button code
bool currentButton1State = !digitalRead(button);
if( currentButton1State != button1State){
Joystick.setButton(0, currentButton1State); // update joystick button0
button1State = currentButton1State; // update the button variable
}
Serial.print(", ButtonState: ");
Serial.print(currentButton1State);

// 3position switch code
bool currentButton2State = !digitalRead(pos_3_up);
if( currentButton2State != button2State){
Joystick.setButton(1,currentButton2State);
button2State = currentButton2State;
}
Serial.print(", 3 position UP state:");
Serial.print(currentButton2State);

bool currentButton3State = !digitalRead(pos_3_down);
if (currentButton3State != button3State) {
Joystick.setButton(2, currentButton3State);
button3State = currentButton3State;
}
Serial.print(digitalRead(pos_3_down));

// momentary switch code
bool currentButton4State = !digitalRead(mom_up);
if (currentButton4State != button4State) {
Joystick.setButton(3, currentButton4State);
button4State = currentButton4State;
}
Serial.print(", Momentary UP state:");
Serial.print(currentButton4State);

bool currentButton5State = !digitalRead(mom_down);
if (currentButton5State != button5State) {
Joystick.setButton(4, currentButton5State);
button5State = currentButton5State;
}
Serial.print(", Momentary Down state:");
Serial.print(currentButton5State);
Serial.println();
// Debounce time to prevent flooding the USB input
delay(10); // in milliseconds

}`

Simulating as an XInput controller

Is it possible to simulate the controller as a standard XInput one? For some reasons my game doesn't recognize the buttons and keys input but only support XInput compliant ones. Is it possible to achieve this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.