Skip to content

Commit

Permalink
Added Switch Joycon sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
slav-at-attachix committed May 2, 2022
1 parent a37d476 commit edd76fa
Show file tree
Hide file tree
Showing 13 changed files with 813 additions and 0 deletions.
Empty file.
76 changes: 76 additions & 0 deletions Sming/Libraries/SwitchJoycon/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Switch Joycon
=============

.. highlight:: c++

Introduction
------------
This library allows you to make the ESP32 act as a Nintendo Switch Joycon and control what it does.
The library uses :library:`NimBLE` for faster and lighter communication.

Disclaimer
----------
We are not affiliated, associated, authorized, endorsed by, or in any way officially connected with Nintendo,
or any of its subsidiaries or its affiliates.
The names Nintendo, Nintendo Switch and Joycon as well as related names, marks, emblems and images are
registered trademarks of their respective owners.

Features
--------

Using this library you can do the following:

- Button press and release (16 buttons)
- Switch Hat (1 hat )
- Rotate 4 Axis

Using
-----

1. Add ``COMPONENT_DEPENDS += SwitchJoycon`` to your application componenent.mk file.
2. Add these lines to your application::

#include <SwitchJoycon.h>
namespace
{
SwitchJoycon joycon;
// ...
} // namespace
void init()
{
// ...
joycon.begin();
}


Notes
-----
By default, reports are sent on every button press/release or axis/hat movement, however this can be disabled::

joycon.setAutoReport(false);
and then you should manually call sendReport on the joycon instance as shown below::

joycon.sendReport();


HID Debugging
-------------

On Linux you can install `hid-tools <https://gitlab.freedesktop.org/libevdev/hid-tools>`__ using the command below::

sudo pip3 install .

Once installed hid-recorder can be used to check the device HID report description and sniff the different reports::

sudo hid-recorder

Useful Links
------------
- `Tutorial about USB HID Report Descriptors <https://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/>`__
- `HID constants <https://github.com/katyo/hid_def/blob/master/include/hid_def.h>`__
5 changes: 5 additions & 0 deletions Sming/Libraries/SwitchJoycon/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
COMPONENT_SOC := esp32*
COMPONENT_DEPENDS := NimBLE

COMPONENT_SRCDIRS := src
COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#####################################################################
#### Please don't change this file. Use component.mk instead ####
#####################################################################

ifndef SMING_HOME
$(error SMING_HOME is not set: please configure it as an environment variable)
endif

include $(SMING_HOME)/project.mk
23 changes: 23 additions & 0 deletions Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Switch Joycon
=============

Introduction
------------
This sample turns the ESP32 into a Switch Joycon (Bluetooth LE gamepad) that presses buttons and moves axis

Possible buttons are 0 through to 15.

Possible HAT switch position values are:
Centered, Up, UpRight, Right, DownRight, Down, DownLeft, Left, UpLeft.


Testing
-------

You can use one of the following applications on your PC to test and see all buttons that were clicked.

On Linux install ``jstest-gtk`` to test the ESP32 gamepad. Under Ubuntu this can be done by typing the following command::

sudo apt install jstest-gtk
On Windows use this `Windows test application <http://www.planetpointy.co.uk/joystick-test-application/>`__.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <SmingCore.h>
#include <SwitchJoycon.h>

namespace
{
Timer procTimer;

void onConnect(NimBLEServer& server);
void onDisconnect(NimBLEServer& server);

SwitchJoycon joycon(SwitchJoycon::Type::Left, 100, onConnect, onDisconnect);

void loop()
{
if(!joycon.isConnected()) {
return;
}

uint8_t button = random(0, 15);

joycon.press(button);
joycon.setHat(SwitchJoycon::JoystickPosition::UpLeft);
delay(5000);

joycon.release(button);
joycon.setHat(SwitchJoycon::JoystickPosition::Center);
delay(5000);
}

void onConnect(NimBLEServer& server)
{
Serial.println("Connected :) !");

procTimer.initializeMs(500, loop).start();
}

void onDisconnect(NimBLEServer& server)
{
procTimer.stop();
Serial.println("Disconnected :(!");
}

} // namespace

void init()
{
Serial.begin(COM_SPEED_SERIAL);
Serial.systemDebugOutput(true);

Serial.println("Starting Joycon Gamepad sample!");
joycon.begin();
// Auto reporting is enabled by default.
// Use joycon.setAutoReport(false); to disable auto reporting, and then use joycon.sendReport(); as needed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPONENT_DEPENDS := SwitchJoycon
Empty file.
Loading

0 comments on commit edd76fa

Please sign in to comment.