diff --git a/README.md b/README.md index cc26b65..e26afec 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,13 @@ Sketch for Arduino based taiko game controller circuit ## Software Setup +### Arduino IDE + Install the latest version of Arduino IDE from the official website: [https://www.arduino.cc/en/Main/Software](https://www.arduino.cc/en/Main/Software) -To enable nintendo switch functionality, replace the following files with the ones provided in "setup" folder: +### Modifications to Arduino IDE + +Before starting Arduino IDE, to enable nintendo switch functionality, replace the following files with the ones provided in "setup" folder: - /hardware/arduino/avr/libraries/HID/src/HID.h - /hardware/arduino/avr/libraries/HID/src/HID.cpp @@ -15,15 +19,40 @@ Then copy the text in board.txt in "setup" folder and append it to the following - /hardware/arduino/avr/boards.txt +If you've successfully done all the modifications above, you should be able to see the board called "Nintendo Switch Controller" next time you start Arduino IDE: + +![](https://i.loli.net/2019/03/17/5c8e542c92603.png) + +Please select this board before uploading the code as this is essential for your Arduino Leonardo to be recognized by Nintendo Switch. + +### Keyboard or Nintendo Switch Controller + To enable or disable keyboard and Nintendo Switch controller functionality, remove or add two charactors "//" before these two lines in taiko_controller.ino: +- To enable Switch controller only ``` //#define ENABLE_KEYBOARD #define ENABLE_NS_JOYSTICK ``` +- To enable keyboard only +``` +#define ENABLE_KEYBOARD +//#define ENABLE_NS_JOYSTICK +``` +- To enable both (not tested) +``` +#define ENABLE_KEYBOARD +#define ENABLE_NS_JOYSTICK +``` ## Circuit Setup +### Materials + +To setup the circuit, you need an Arduino Leonardo, a set of four piezo sensors, and four 1MΩ resistors for some special cases. + +### Connect the Circuit + Connect the sensors to the 3.3v pin and the analog pins according to the diagram below: ![](https://i.loli.net/2019/03/07/5c812d28e0978.png) @@ -42,7 +71,9 @@ If the controller seems to be generating random inputs, you can fix this by plug ![](https://i.loli.net/2019/03/07/5c812d28e101d.png) -For best performance, the sensors must be piezo sensors (a.k.a. peizo speakers, contact microphones). No guarantee if other types of sensors will simply work, but if analog signals with voltage ranged 0-5V are fed into analog pins, this setup should be good to go. +### Notes + +For best performance, the sensors must be piezo sensors (a.k.a. piezo speakers, contact microphones). No guarantee if other types of sensors will simply work, but if analog signals with voltage ranged 0-5V are fed into analog pins, this setup should be good to go. For further improvements, you can use some diodes to limit the voltage of the piezo sensors, or use a 2.5v power supply, but this won't matter in most cases, at least on my side. @@ -60,6 +91,8 @@ To deal with four analog inputs, we read the sensor levels one at a time, and on To deal with Nintendo Switch, I used the HID descriptor for Hori's Pokken fightstick to let Switch trust Arduino as a valid controller device (see the [credits](#credits) section). The default buttons from the four sensors are the analog stick buttons (press the sticks down) and the trigger buttons (ZL and ZR). +As VID and PID of the controller have to be the specific value, the setup to boards.txt is essential. Also, Switch seems also to be judging the device strictly by the first-come HID descriptor of the device, so Arduino's default HID behavior have to be altered to have our customized HID descriptor to work. + ## Parameters (with suggested values) #### min_threshold = 15 diff --git a/setup/HID.cpp b/setup/HID.cpp index 21ede26..a9f99df 100644 --- a/setup/HID.cpp +++ b/setup/HID.cpp @@ -86,6 +86,13 @@ void HID_::AppendDescriptor(HIDSubDescriptor *node) descriptorSize += node->length; } +void HID_::PrependDescriptor(HIDSubDescriptor *node) +{ + node->next = rootNode; + rootNode = node; + descriptorSize += node->length; +} + int HID_::SendReport(uint8_t id, const void* data, int len) { auto ret = USB_Send(pluggedEndpoint, &id, 1); @@ -95,6 +102,11 @@ int HID_::SendReport(uint8_t id, const void* data, int len) return ret + ret2; } +int HID_::SendRaw(const void* data, int len) +{ + return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, len); +} + bool HID_::setup(USBSetup& setup) { if (pluggedInterface != setup.wIndex) { diff --git a/setup/boards.txt b/setup/boards.txt index 69293fc..9ce72b0 100644 --- a/setup/boards.txt +++ b/setup/boards.txt @@ -3,7 +3,7 @@ # Nintendo Switch Controller for Micro/Leonardo # ############################################################## -ns_con.name=Nintendo Switch Controller (Micro) +ns_con.name=Nintendo Switch Controller (Micro/Leo) ns_con.vid.1=0x0F0D ns_con.pid.1=0x0092 diff --git a/AnalogReadNow.cpp b/taiko_controller/AnalogReadNow.cpp similarity index 100% rename from AnalogReadNow.cpp rename to taiko_controller/AnalogReadNow.cpp diff --git a/AnalogReadNow.h b/taiko_controller/AnalogReadNow.h similarity index 100% rename from AnalogReadNow.h rename to taiko_controller/AnalogReadNow.h diff --git a/HIDReportData.h b/taiko_controller/HIDReportData.h similarity index 100% rename from HIDReportData.h rename to taiko_controller/HIDReportData.h diff --git a/Joystick.cpp b/taiko_controller/Joystick.cpp similarity index 87% rename from Joystick.cpp rename to taiko_controller/Joystick.cpp index a02b7cf..1c31738 100644 --- a/Joystick.cpp +++ b/taiko_controller/Joystick.cpp @@ -8,22 +8,6 @@ * https://github.com/progmem/Switch-Fightstick/blob/master/Joystick.h ***/ -// Functions added to HID_ class -// Don't forget to add definitions in HID.h, which is located at: -// \hardware\arduino\avr\libraries\HID\src\ - -void HID_::PrependDescriptor(HIDSubDescriptor *node) -{ - node->next = rootNode; - rootNode = node; - descriptorSize += node->length; -} - -int HID_::SendRaw(const void* data, int len) -{ - return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, len); -} - /*** * Descriptor modified from * progmem/Switch-Fightstick/Joystick.c @@ -100,4 +84,3 @@ void Joystick_::sendState() } Joystick_ Joystick; - diff --git a/Joystick.h b/taiko_controller/Joystick.h similarity index 100% rename from Joystick.h rename to taiko_controller/Joystick.h diff --git a/taiko_controller.ino b/taiko_controller/taiko_controller.ino similarity index 100% rename from taiko_controller.ino rename to taiko_controller/taiko_controller.ino