├── .gitignore ├── ATmega32U4 ├── ATmega32U4.ino └── cache.h ├── ESP32-S3-Analog ├── ESP32-S3-Analog.ino ├── PCB │ ├── BOM_PCB_Taiko Controller_2_2024-01-26.csv │ └── Gerber_PCB_Taiko Controller_2.zip ├── adjustable.h ├── cache.h ├── extra │ └── bnusio.dll ├── joystick.cpp ├── joystick.h ├── params.h └── two_players.h ├── ESP32-S3 ├── ESP32-S3.ino └── cache.h ├── ESP32 ├── ESP32.ino ├── cache.h └── keyboard.h ├── LICENSE ├── README.md ├── README_ja-JP.md ├── README_zh-CN.md └── images ├── banner-taiko.png ├── bridge_signal.png ├── piezo_locations.png ├── scheme.png ├── scheme_bridge.png ├── taiko_test.webp ├── tune_hit_reset.png └── tune_sensitivities.png /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .vscode/ 3 | .DS_Store -------------------------------------------------------------------------------- /ATmega32U4/ATmega32U4.ino: -------------------------------------------------------------------------------- 1 | #define CHANNELS 4 2 | 3 | // SAMPLE_CACHE_LENGTH must be power of 2 (8, 16, 32, etc.) 4 | // See cache.h for implementation 5 | #define SAMPLE_CACHE_LENGTH 16 6 | 7 | // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you 8 | // changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds 9 | #define HIT_THRES 750 10 | #define RESET_THRES 200 11 | 12 | // Sensitivity multipliers for each channel, 1.0 as the baseline 13 | #define L_DON_SENS 1.0 14 | #define L_KAT_SENS 1.0 15 | #define R_DON_SENS 1.0 16 | #define R_KAT_SENS 1.0 17 | 18 | // Input pins for each channel 19 | #define L_DON_IN A0 20 | #define L_KAT_IN A1 21 | #define R_DON_IN A2 22 | #define R_KAT_IN A3 23 | 24 | // Output LED pins for each channel (just for visualization) 25 | #define L_DON_LED 5 26 | #define L_KAT_LED 6 27 | #define R_DON_LED 7 28 | #define R_KAT_LED 8 29 | 30 | // Keyboard output for each channel 31 | #define L_DON_KEY 'f' 32 | #define L_KAT_KEY 'd' 33 | #define R_DON_KEY 'j' 34 | #define R_KAT_KEY 'k' 35 | 36 | // Enable debug mode to view analog input values from the Serial 37 | // Enabling this also disables the keyboard simulation 38 | #define DEBUG 0 39 | 40 | #include 41 | #include 42 | 43 | #include "cache.h" 44 | 45 | Cache inputWindow[CHANNELS]; 46 | unsigned long power[CHANNELS]; 47 | unsigned long lastPower[CHANNELS]; 48 | 49 | bool triggered; 50 | unsigned long triggeredTime[CHANNELS]; 51 | 52 | const byte inPins[] = {L_DON_IN, L_KAT_IN, R_DON_IN, R_KAT_IN}; 53 | const byte outPins[] = {L_DON_LED, L_KAT_LED, R_DON_LED, R_KAT_LED}; 54 | const char outKeys[] = {L_DON_KEY, L_KAT_KEY, R_DON_KEY, R_KAT_KEY}; 55 | float sensitivities[] = {L_DON_SENS, L_KAT_SENS, R_DON_SENS, R_KAT_SENS}; 56 | 57 | short maxIndex; 58 | float maxPower; 59 | 60 | void setup() { 61 | Serial.begin(115200); 62 | Keyboard.begin(); 63 | analogReference(DEFAULT); 64 | for (byte i = 0; i < CHANNELS; i++) { 65 | power[i] = 0; 66 | lastPower[i] = 0; 67 | triggered = false; 68 | } 69 | maxIndex = -1; 70 | maxPower = 0; 71 | } 72 | 73 | void loop() { 74 | if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) { 75 | triggered = false; 76 | digitalWrite(outPins[maxIndex], LOW); 77 | maxIndex = -1; 78 | maxPower = 0; 79 | } 80 | 81 | for (byte i = 0; i < CHANNELS; i++) { 82 | inputWindow[i].put(analogRead(inPins[i])); 83 | power[i] = sensitivities[i] * 84 | (power[i] - inputWindow[i].get(1) + inputWindow[i].get()); 85 | 86 | if (lastPower[i] > maxPower && power[i] < lastPower[i]) { 87 | maxPower = lastPower[i]; 88 | maxIndex = i; 89 | } 90 | lastPower[i] = power[i]; 91 | #if DEBUG 92 | Serial.print(power[i]); 93 | Serial.print(" "); 94 | #endif 95 | } 96 | 97 | if (!triggered && maxPower >= HIT_THRES) { 98 | triggered = true; 99 | digitalWrite(outPins[maxIndex], HIGH); 100 | #if !DEBUG 101 | Keyboard.print(outKeys[maxIndex]); 102 | #endif 103 | } 104 | #if DEBUG 105 | Serial.print("\n"); 106 | #endif 107 | } 108 | -------------------------------------------------------------------------------- /ATmega32U4/cache.h: -------------------------------------------------------------------------------- 1 | /*************************************************************** 2 | * * 3 | * Taiko Sanro - Arduino * 4 | * Cache data structure * 5 | * * 6 | * Chris * 7 | * wisaly@gmail.com * 8 | * * 9 | ***************************************************************/ 10 | 11 | #ifndef CACHE_H 12 | #define CACHE_H 13 | 14 | template 15 | class Cache { 16 | public: 17 | Cache() { memset(data_, 0, sizeof(data_)); } 18 | inline void put(T value) { 19 | current_ = (current_ + 1) & (L - 1); 20 | data_[current_] = value; 21 | } 22 | inline T get(int offset = 0) const { 23 | return data_[(current_ + offset) & (L - 1)]; 24 | } 25 | 26 | private: 27 | T data_[L]; 28 | int current_ = 0; 29 | }; 30 | 31 | #endif // CACHE_H -------------------------------------------------------------------------------- /ESP32-S3-Analog/ESP32-S3-Analog.ino: -------------------------------------------------------------------------------- 1 | /******************************************************** 2 | 3 | ESP32 is not fast enough to process 16 channels of input 4 | simultaneously, so you can choose one of the 2 modes: 5 | 6 | - MODE_TWO_PLAYERS: Allows the controller to connect 2 7 | drums, but the sensitivity values are flashed to the 8 | board so you can't adjust them without re-flash the 9 | firmware. If you use the provided board, connect the 10 | 2 drums and ignore the adjustable resistors. 11 | 12 | - MODE_ADJUSTABLE: Allows you to adjust the sensitivity 13 | values with the potentiometer as you play, but only 14 | the P1 side of the board will be enabled (you can 15 | configurate it as 1P or 2P in Params.h file). 16 | 17 | **********************************************************/ 18 | 19 | #define MODE_TWO_PLAYERS 20 | 21 | #ifdef MODE_TWO_PLAYERS 22 | #include "two_players.h" 23 | #elif defined(MODE_ADJUSTABLE) 24 | #include "adjustable.h" 25 | #endif 26 | -------------------------------------------------------------------------------- /ESP32-S3-Analog/PCB/BOM_PCB_Taiko Controller_2_2024-01-26.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/ESP32-S3-Analog/PCB/BOM_PCB_Taiko Controller_2_2024-01-26.csv -------------------------------------------------------------------------------- /ESP32-S3-Analog/PCB/Gerber_PCB_Taiko Controller_2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/ESP32-S3-Analog/PCB/Gerber_PCB_Taiko Controller_2.zip -------------------------------------------------------------------------------- /ESP32-S3-Analog/adjustable.h: -------------------------------------------------------------------------------- 1 | #include "params.h" 2 | 3 | const byte inPins[CHANNELS] = { 4 | P1_L_DON_IN, P1_L_KAT_IN, P1_R_DON_IN, P1_R_KAT_IN 5 | }; 6 | const byte sensitivityPins[CHANNELS] = { 7 | P1_L_DON_SENS_IN, P1_L_KAT_SENS_IN, P1_R_DON_SENS_IN, P1_R_KAT_SENS_IN 8 | }; 9 | 10 | Cache inputWindow[CHANNELS]; 11 | unsigned long power[CHANNELS]; 12 | 13 | #ifndef RAW_ANALOG_MODE 14 | unsigned long lastPower[PLAYERS][CHANNELS]; 15 | bool triggered[PLAYERS]; 16 | unsigned long triggeredTime[PLAYERS][CHANNELS]; 17 | int outputValue[PLAYERS] = {0, 0}; 18 | uint resetTimer[PLAYERS] = {0, 0}; 19 | short maxIndex[PLAYERS] = {0, 0}; 20 | float maxPower[PLAYERS] = {0, 0}; 21 | #endif 22 | 23 | uint axisValues[CHANNELS] = {0, 0, 0, 0}; 24 | 25 | Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 10, 4, 26 | true, true, false, true, true, false, 27 | false, false, false, false, false); 28 | 29 | void setup() { 30 | for (byte i = 0; i < CHANNELS; i++) { 31 | power[i] = 0; 32 | #ifndef RAW_ANALOG_MODE 33 | lastPower[i] = 0; 34 | triggered = false; 35 | #endif 36 | pinMode(inPins[i], INPUT); 37 | pinMode(sensitivityPins[i], INPUT); 38 | } 39 | #ifndef RAW_ANALOG_MODE 40 | maxIndex = -1; 41 | maxPower = 0; 42 | #endif 43 | USB.PID(0x4869); 44 | USB.VID(0x4869); 45 | USB.productName("Taiko Controller"); 46 | USB.manufacturerName("GitHub Community"); 47 | USB.begin(); 48 | Joystick.begin(false); 49 | Joystick.setXAxisRange(-AXIS_RANGE, AXIS_RANGE); 50 | Joystick.setYAxisRange(-AXIS_RANGE, AXIS_RANGE); 51 | Joystick.setRxAxisRange(-AXIS_RANGE, AXIS_RANGE); 52 | Joystick.setRyAxisRange(-AXIS_RANGE, AXIS_RANGE); 53 | } 54 | 55 | void loop() { 56 | 57 | for (byte i = 0; i < CHANNELS; i++) { 58 | inputWindow[i].put(analogRead(inPins[i])); 59 | power[i] = power[i] - inputWindow[i].get(1) + inputWindow[i].get(); 60 | #ifndef RAW_ANALOG_MODE 61 | if (lastPower[i] > maxPower && power[i] < lastPower[i]) { 62 | maxPower = lastPower[i]; 63 | maxIndex = i; 64 | } 65 | lastPower[i] = power[i]; 66 | #else 67 | float x = analogRead(sensitivityPins[i]) / 2048.0 - 1; 68 | float x2 = x * x; 69 | float x3 = x2 * x; 70 | float x4 = x3 * x; 71 | float v = (1.0 + x + 0.5 * x2 + 0.166667 * x3) * power[i]; 72 | axisValues[i] = AXIS_RANGE * (v >= MAX_THRES ? 1 : (v / MAX_THRES)); 73 | #endif 74 | } 75 | #ifndef RAW_ANALOG_MODE 76 | if (!triggered && maxPower >= HIT_THRES) { 77 | triggered = true; 78 | digitalWrite(outPins[maxIndex], HIGH); 79 | outputValue = (int)(AXIS_RANGE * (maxPower >= MAX_THRES ? 1 : maxPower / MAX_THRES)); 80 | } 81 | 82 | if (triggered && resetTimer >= RESET_TIME) { 83 | triggered = false; 84 | resetTimer = 0; 85 | digitalWrite(outPins[maxIndex], LOW); 86 | maxPower = 0; 87 | maxIndex = -1; 88 | outputValue = 0; 89 | } 90 | 91 | for (byte i = 0; i < CHANNELS; i++) { 92 | if (triggered && i == maxIndex) { 93 | axisValues[i] = outputValue; 94 | } else { 95 | axisValues[i] = 0; 96 | } 97 | } 98 | 99 | if (triggered) { 100 | resetTimer++; 101 | } 102 | #endif 103 | 104 | #if PLAYER_SELECT == 1 105 | Joystick.setXAxis(axisValues[0] > axisValues[1] ? axisValues[0] : -axisValues[1]); 106 | Joystick.setYAxis(axisValues[2] > axisValues[3] ? axisValues[2] : -axisValues[3]); 107 | Joystick.setRxAxis(0); 108 | Joystick.setRyAxis(0); 109 | #elif PLAYER_SELECT == 2 110 | Joystick.setXAxis(0); 111 | Joystick.setYAxis(0); 112 | Joystick.setRxAxis(axisValues[0] > axisValues[1] ? axisValues[0] : -axisValues[1]); 113 | Joystick.setRyAxis(axisValues[2] > axisValues[3] ? axisValues[2] : -axisValues[3]); 114 | #else 115 | Joystick.setXAxis(0); 116 | Joystick.setYAxis(0); 117 | Joystick.setRxAxis(0); 118 | Joystick.setRyAxis(0); 119 | #endif 120 | 121 | Joystick.sendState(); 122 | } 123 | -------------------------------------------------------------------------------- /ESP32-S3-Analog/cache.h: -------------------------------------------------------------------------------- 1 | /*************************************************************** 2 | * * 3 | * Taiko Sanro - Arduino * 4 | * Cache data structure * 5 | * * 6 | * Chris * 7 | * wisaly@gmail.com * 8 | * * 9 | ***************************************************************/ 10 | 11 | #ifndef CACHE_H 12 | #define CACHE_H 13 | 14 | template 15 | class Cache { 16 | public: 17 | Cache() { memset(data_, 0, sizeof(data_)); } 18 | inline void put(T value) { 19 | current_ = (current_ + 1) % L; 20 | data_[current_] = value; 21 | } 22 | inline T get(int offset = 0) const { 23 | return data_[(current_ + offset) % L]; 24 | } 25 | 26 | private: 27 | T data_[L]; 28 | int current_ = 0; 29 | }; 30 | 31 | #endif // CACHE_H -------------------------------------------------------------------------------- /ESP32-S3-Analog/extra/bnusio.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/ESP32-S3-Analog/extra/bnusio.dll -------------------------------------------------------------------------------- /ESP32-S3-Analog/joystick.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Joystick_ESP32S2.cpp 3 | 4 | Copyright (c) 2015-2017, Matthew Heironimus 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | 20 | Edited by Schnoog to make it running on ESP32-S2/s3 devices 21 | */ 22 | 23 | #include "joystick.h" 24 | 25 | #define JOYSTICK_REPORT_ID_INDEX 7 26 | #define JOYSTICK_AXIS_MINIMUM 0 27 | #define JOYSTICK_AXIS_MAXIMUM 65535 28 | #define JOYSTICK_SIMULATOR_MINIMUM 0 29 | #define JOYSTICK_SIMULATOR_MAXIMUM 65535 30 | 31 | #define JOYSTICK_INCLUDE_X_AXIS B00000001 32 | #define JOYSTICK_INCLUDE_Y_AXIS B00000010 33 | #define JOYSTICK_INCLUDE_Z_AXIS B00000100 34 | #define JOYSTICK_INCLUDE_RX_AXIS B00001000 35 | #define JOYSTICK_INCLUDE_RY_AXIS B00010000 36 | #define JOYSTICK_INCLUDE_RZ_AXIS B00100000 37 | 38 | #define JOYSTICK_INCLUDE_RUDDER B00000001 39 | #define JOYSTICK_INCLUDE_THROTTLE B00000010 40 | #define JOYSTICK_INCLUDE_ACCELERATOR B00000100 41 | #define JOYSTICK_INCLUDE_BRAKE B00001000 42 | #define JOYSTICK_INCLUDE_STEERING B00010000 43 | 44 | 45 | 46 | Joystick_::Joystick_( 47 | uint8_t hidReportId, 48 | uint8_t joystickType, 49 | uint8_t buttonCount, 50 | uint8_t hatSwitchCount, 51 | bool includeXAxis, 52 | bool includeYAxis, 53 | bool includeZAxis, 54 | bool includeRxAxis, 55 | bool includeRyAxis, 56 | bool includeRzAxis, 57 | bool includeRudder, 58 | bool includeThrottle, 59 | bool includeAccelerator, 60 | bool includeBrake, 61 | bool includeSteering) 62 | { 63 | // Set the USB HID Report ID 64 | _hidReportId = hidReportId; 65 | 66 | // Save Joystick Settings 67 | _buttonCount = buttonCount; 68 | _hatSwitchCount = hatSwitchCount; 69 | _includeAxisFlags = 0; 70 | _includeAxisFlags |= (includeXAxis ? JOYSTICK_INCLUDE_X_AXIS : 0); 71 | _includeAxisFlags |= (includeYAxis ? JOYSTICK_INCLUDE_Y_AXIS : 0); 72 | _includeAxisFlags |= (includeZAxis ? JOYSTICK_INCLUDE_Z_AXIS : 0); 73 | _includeAxisFlags |= (includeRxAxis ? JOYSTICK_INCLUDE_RX_AXIS : 0); 74 | _includeAxisFlags |= (includeRyAxis ? JOYSTICK_INCLUDE_RY_AXIS : 0); 75 | _includeAxisFlags |= (includeRzAxis ? JOYSTICK_INCLUDE_RZ_AXIS : 0); 76 | _includeSimulatorFlags = 0; 77 | _includeSimulatorFlags |= (includeRudder ? JOYSTICK_INCLUDE_RUDDER : 0); 78 | _includeSimulatorFlags |= (includeThrottle ? JOYSTICK_INCLUDE_THROTTLE : 0); 79 | _includeSimulatorFlags |= (includeAccelerator ? JOYSTICK_INCLUDE_ACCELERATOR : 0); 80 | _includeSimulatorFlags |= (includeBrake ? JOYSTICK_INCLUDE_BRAKE : 0); 81 | _includeSimulatorFlags |= (includeSteering ? JOYSTICK_INCLUDE_STEERING : 0); 82 | 83 | // Build Joystick HID Report Description 84 | // Button Calculations 85 | uint8_t buttonsInLastByte = _buttonCount % 8; 86 | uint8_t buttonPaddingBits = 0; 87 | if (buttonsInLastByte > 0) 88 | { 89 | buttonPaddingBits = 8 - buttonsInLastByte; 90 | } 91 | 92 | // Axis Calculations 93 | uint8_t axisCount = (includeXAxis == true) 94 | + (includeYAxis == true) 95 | + (includeZAxis == true) 96 | + (includeRxAxis == true) 97 | + (includeRyAxis == true) 98 | + (includeRzAxis == true); 99 | 100 | uint8_t simulationCount = (includeRudder == true) 101 | + (includeThrottle == true) 102 | + (includeAccelerator == true) 103 | + (includeBrake == true) 104 | + (includeSteering == true); 105 | 106 | uint8_t tempHidReportDescriptor[150]; 107 | hidReportDescriptorSize = 0; 108 | 109 | // USAGE_PAGE (Generic Desktop) 110 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05; 111 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 112 | 113 | // USAGE (Joystick - 0x04; Gamepad - 0x05; Multi-axis Controller - 0x08) 114 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 115 | tempHidReportDescriptor[hidReportDescriptorSize++] = joystickType; 116 | 117 | // COLLECTION (Application) 118 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xa1; 119 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 120 | 121 | // REPORT_ID (Default: 3) 122 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x85; 123 | tempHidReportDescriptor[hidReportDescriptorSize++] = _hidReportId; 124 | 125 | if (_buttonCount > 0) { 126 | 127 | // USAGE_PAGE (Button) 128 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05; 129 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 130 | 131 | // USAGE_MINIMUM (Button 1) 132 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x19; 133 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 134 | 135 | // USAGE_MAXIMUM (Button 32) 136 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x29; 137 | tempHidReportDescriptor[hidReportDescriptorSize++] = _buttonCount; 138 | 139 | // LOGICAL_MINIMUM (0) 140 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15; 141 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 142 | 143 | // LOGICAL_MAXIMUM (1) 144 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25; 145 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 146 | 147 | // REPORT_SIZE (1) 148 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 149 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 150 | 151 | // REPORT_COUNT (# of buttons) 152 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 153 | tempHidReportDescriptor[hidReportDescriptorSize++] = _buttonCount; 154 | 155 | // UNIT_EXPONENT (0) 156 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x55; 157 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 158 | 159 | // UNIT (None) 160 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65; 161 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 162 | 163 | // INPUT (Data,Var,Abs) 164 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 165 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; 166 | 167 | if (buttonPaddingBits > 0) { 168 | 169 | // REPORT_SIZE (1) 170 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 171 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 172 | 173 | // REPORT_COUNT (# of padding bits) 174 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 175 | tempHidReportDescriptor[hidReportDescriptorSize++] = buttonPaddingBits; 176 | 177 | // INPUT (Const,Var,Abs) 178 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 179 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03; 180 | 181 | } // Padding Bits Needed 182 | 183 | } // Buttons 184 | 185 | if ((axisCount > 0) || (_hatSwitchCount > 0)) { 186 | 187 | // USAGE_PAGE (Generic Desktop) 188 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05; 189 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 190 | 191 | } 192 | 193 | if (_hatSwitchCount > 0) { 194 | 195 | // USAGE (Hat Switch) 196 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 197 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x39; 198 | 199 | // LOGICAL_MINIMUM (0) 200 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15; 201 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 202 | 203 | // LOGICAL_MAXIMUM (7) 204 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25; 205 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x07; 206 | 207 | // PHYSICAL_MINIMUM (0) 208 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35; 209 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 210 | 211 | // PHYSICAL_MAXIMUM (315) 212 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x46; 213 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3B; 214 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 215 | 216 | // UNIT (Eng Rot:Angular Pos) 217 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65; 218 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x14; 219 | 220 | // REPORT_SIZE (4) 221 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 222 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04; 223 | 224 | // REPORT_COUNT (1) 225 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 226 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 227 | 228 | // INPUT (Data,Var,Abs) 229 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 230 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; 231 | 232 | if (_hatSwitchCount > 1) { 233 | 234 | // USAGE (Hat Switch) 235 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 236 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x39; 237 | 238 | // LOGICAL_MINIMUM (0) 239 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15; 240 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 241 | 242 | // LOGICAL_MAXIMUM (7) 243 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25; 244 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x07; 245 | 246 | // PHYSICAL_MINIMUM (0) 247 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35; 248 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 249 | 250 | // PHYSICAL_MAXIMUM (315) 251 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x46; 252 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3B; 253 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 254 | 255 | // UNIT (Eng Rot:Angular Pos) 256 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65; 257 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x14; 258 | 259 | // REPORT_SIZE (4) 260 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 261 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04; 262 | 263 | // REPORT_COUNT (1) 264 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 265 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 266 | 267 | // INPUT (Data,Var,Abs) 268 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 269 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; 270 | 271 | } else { 272 | 273 | // Use Padding Bits 274 | 275 | // REPORT_SIZE (1) 276 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 277 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 278 | 279 | // REPORT_COUNT (4) 280 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 281 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04; 282 | 283 | // INPUT (Const,Var,Abs) 284 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 285 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03; 286 | 287 | } // One or Two Hat Switches? 288 | 289 | } // Hat Switches 290 | 291 | if (axisCount > 0) { 292 | 293 | // USAGE (Pointer) 294 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 295 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; 296 | 297 | // LOGICAL_MINIMUM (0) 298 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15; 299 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 300 | 301 | // LOGICAL_MAXIMUM (65535) 302 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x27; 303 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0XFF; 304 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0XFF; 305 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 306 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 307 | 308 | // REPORT_SIZE (16) 309 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 310 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10; 311 | 312 | // REPORT_COUNT (axisCount) 313 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 314 | tempHidReportDescriptor[hidReportDescriptorSize++] = axisCount; 315 | 316 | // COLLECTION (Physical) 317 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1; 318 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 319 | 320 | if (includeXAxis == true) { 321 | // USAGE (X) 322 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 323 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x30; 324 | } 325 | 326 | if (includeYAxis == true) { 327 | // USAGE (Y) 328 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 329 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x31; 330 | } 331 | 332 | if (includeZAxis == true) { 333 | // USAGE (Z) 334 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 335 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x32; 336 | } 337 | 338 | if (includeRxAxis == true) { 339 | // USAGE (Rx) 340 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 341 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x33; 342 | } 343 | 344 | if (includeRyAxis == true) { 345 | // USAGE (Ry) 346 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 347 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x34; 348 | } 349 | 350 | if (includeRzAxis == true) { 351 | // USAGE (Rz) 352 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 353 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35; 354 | } 355 | 356 | // INPUT (Data,Var,Abs) 357 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 358 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; 359 | 360 | // END_COLLECTION (Physical) 361 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0; 362 | 363 | } // X, Y, Z, Rx, Ry, and Rz Axis 364 | 365 | if (simulationCount > 0) { 366 | 367 | // USAGE_PAGE (Simulation Controls) 368 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05; 369 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; 370 | 371 | // LOGICAL_MINIMUM (0) 372 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15; 373 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 374 | 375 | // LOGICAL_MAXIMUM (65535) 376 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x27; 377 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0XFF; 378 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0XFF; 379 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 380 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 381 | 382 | // REPORT_SIZE (16) 383 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75; 384 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10; 385 | 386 | // REPORT_COUNT (simulationCount) 387 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95; 388 | tempHidReportDescriptor[hidReportDescriptorSize++] = simulationCount; 389 | 390 | // COLLECTION (Physical) 391 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1; 392 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; 393 | 394 | if (includeRudder == true) { 395 | // USAGE (Rudder) 396 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 397 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBA; 398 | } 399 | 400 | if (includeThrottle == true) { 401 | // USAGE (Throttle) 402 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 403 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBB; 404 | } 405 | 406 | if (includeAccelerator == true) { 407 | // USAGE (Accelerator) 408 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 409 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC4; 410 | } 411 | 412 | if (includeBrake == true) { 413 | // USAGE (Brake) 414 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 415 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC5; 416 | } 417 | 418 | if (includeSteering == true) { 419 | // USAGE (Steering) 420 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; 421 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC8; 422 | } 423 | 424 | // INPUT (Data,Var,Abs) 425 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81; 426 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; 427 | 428 | // END_COLLECTION (Physical) 429 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0; 430 | 431 | } // Simulation Controls 432 | 433 | // END_COLLECTION 434 | tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0; 435 | 436 | // Create a copy of the HID Report Descriptor template that is just the right size 437 | //org: uint8_t *customHidReportDescriptor = new uint8_t[hidReportDescriptorSize]; 438 | customHidReportDescriptor = new uint8_t[hidReportDescriptorSize]; 439 | memcpy(customHidReportDescriptor, tempHidReportDescriptor, hidReportDescriptorSize); 440 | 441 | // Register HID Report Description 442 | HID.addDevice(this, hidReportDescriptorSize); 443 | // Setup Joystick State 444 | if (buttonCount > 0) { 445 | _buttonValuesArraySize = _buttonCount / 8; 446 | if ((_buttonCount % 8) > 0) { 447 | _buttonValuesArraySize++; 448 | } 449 | _buttonValues = new uint8_t[_buttonValuesArraySize]; 450 | } 451 | 452 | // Calculate HID Report Size 453 | _hidReportSize = _buttonValuesArraySize; 454 | _hidReportSize += (_hatSwitchCount > 0); 455 | _hidReportSize += (axisCount * 2); 456 | _hidReportSize += (simulationCount * 2); 457 | 458 | // Initialize Joystick State 459 | _xAxis = 0; 460 | _yAxis = 0; 461 | _zAxis = 0; 462 | _xAxisRotation = 0; 463 | _yAxisRotation = 0; 464 | _zAxisRotation = 0; 465 | _throttle = 0; 466 | _rudder = 0; 467 | _accelerator = 0; 468 | _brake = 0; 469 | _steering = 0; 470 | for (int index = 0; index < JOYSTICK_HATSWITCH_COUNT_MAXIMUM; index++) 471 | { 472 | _hatSwitchValues[index] = JOYSTICK_HATSWITCH_RELEASE; 473 | } 474 | for (int index = 0; index < _buttonValuesArraySize; index++) 475 | { 476 | _buttonValues[index] = 0; 477 | } 478 | 479 | 480 | } 481 | 482 | 483 | uint16_t Joystick_::_onGetDescriptor(uint8_t* buffer){ 484 | memcpy(buffer, customHidReportDescriptor,hidReportDescriptorSize); 485 | return hidReportDescriptorSize; 486 | } 487 | 488 | 489 | void Joystick_::begin(bool initAutoSendState, uint8_t intervalMs) 490 | { 491 | HID.begin(); 492 | _autoSendState = initAutoSendState; 493 | sendState(); 494 | } 495 | 496 | void Joystick_::end() 497 | { 498 | } 499 | 500 | void Joystick_::setButton(uint8_t button, uint8_t value) 501 | { 502 | if (value == 0) 503 | { 504 | releaseButton(button); 505 | } 506 | else 507 | { 508 | pressButton(button); 509 | } 510 | } 511 | void Joystick_::pressButton(uint8_t button) 512 | { 513 | if (button >= _buttonCount) return; 514 | 515 | int index = button / 8; 516 | int bit = button % 8; 517 | 518 | bitSet(_buttonValues[index], bit); 519 | if (_autoSendState) sendState(); 520 | } 521 | void Joystick_::releaseButton(uint8_t button) 522 | { 523 | if (button >= _buttonCount) return; 524 | 525 | int index = button / 8; 526 | int bit = button % 8; 527 | 528 | bitClear(_buttonValues[index], bit); 529 | if (_autoSendState) sendState(); 530 | } 531 | 532 | void Joystick_::setXAxis(int32_t value) 533 | { 534 | _xAxis = value; 535 | if (_autoSendState) sendState(); 536 | } 537 | void Joystick_::setYAxis(int32_t value) 538 | { 539 | _yAxis = value; 540 | if (_autoSendState) sendState(); 541 | } 542 | void Joystick_::setZAxis(int32_t value) 543 | { 544 | _zAxis = value; 545 | if (_autoSendState) sendState(); 546 | } 547 | 548 | void Joystick_::setRxAxis(int32_t value) 549 | { 550 | _xAxisRotation = value; 551 | if (_autoSendState) sendState(); 552 | } 553 | void Joystick_::setRyAxis(int32_t value) 554 | { 555 | _yAxisRotation = value; 556 | if (_autoSendState) sendState(); 557 | } 558 | void Joystick_::setRzAxis(int32_t value) 559 | { 560 | _zAxisRotation = value; 561 | if (_autoSendState) sendState(); 562 | } 563 | 564 | void Joystick_::setRudder(int32_t value) 565 | { 566 | _rudder = value; 567 | if (_autoSendState) sendState(); 568 | } 569 | void Joystick_::setThrottle(int32_t value) 570 | { 571 | _throttle = value; 572 | if (_autoSendState) sendState(); 573 | } 574 | void Joystick_::setAccelerator(int32_t value) 575 | { 576 | _accelerator = value; 577 | if (_autoSendState) sendState(); 578 | } 579 | void Joystick_::setBrake(int32_t value) 580 | { 581 | _brake = value; 582 | if (_autoSendState) sendState(); 583 | } 584 | void Joystick_::setSteering(int32_t value) 585 | { 586 | _steering = value; 587 | if (_autoSendState) sendState(); 588 | } 589 | 590 | void Joystick_::setHatSwitch(int8_t hatSwitchIndex, int16_t value) 591 | { 592 | if (hatSwitchIndex >= _hatSwitchCount) return; 593 | 594 | _hatSwitchValues[hatSwitchIndex] = value; 595 | if (_autoSendState) sendState(); 596 | } 597 | 598 | int Joystick_::buildAndSet16BitValue(bool includeValue, int32_t value, int32_t valueMinimum, int32_t valueMaximum, int32_t actualMinimum, int32_t actualMaximum, uint8_t dataLocation[]) 599 | { 600 | int32_t convertedValue; 601 | uint8_t highByte; 602 | uint8_t lowByte; 603 | int32_t realMinimum = min(valueMinimum, valueMaximum); 604 | int32_t realMaximum = max(valueMinimum, valueMaximum); 605 | 606 | if (includeValue == false) return 0; 607 | 608 | if (value < realMinimum) { 609 | value = realMinimum; 610 | } 611 | if (value > realMaximum) { 612 | value = realMaximum; 613 | } 614 | 615 | if (valueMinimum > valueMaximum) { 616 | // Values go from a larger number to a smaller number (e.g. 1024 to 0) 617 | value = realMaximum - value + realMinimum; 618 | } 619 | 620 | convertedValue = map(value, realMinimum, realMaximum, actualMinimum, actualMaximum); 621 | 622 | highByte = (uint8_t)(convertedValue >> 8); 623 | lowByte = (uint8_t)(convertedValue & 0x00FF); 624 | 625 | dataLocation[0] = lowByte; 626 | dataLocation[1] = highByte; 627 | 628 | return 2; 629 | } 630 | 631 | int Joystick_::buildAndSetAxisValue(bool includeAxis, int32_t axisValue, int32_t axisMinimum, int32_t axisMaximum, uint8_t dataLocation[]) 632 | { 633 | return buildAndSet16BitValue(includeAxis, axisValue, axisMinimum, axisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM, dataLocation); 634 | } 635 | 636 | int Joystick_::buildAndSetSimulationValue(bool includeValue, int32_t value, int32_t valueMinimum, int32_t valueMaximum, uint8_t dataLocation[]) 637 | { 638 | return buildAndSet16BitValue(includeValue, value, valueMinimum, valueMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM, dataLocation); 639 | } 640 | 641 | void Joystick_::sendState() 642 | { 643 | uint8_t data[_hidReportSize]; 644 | int index = 0; 645 | 646 | // Load Button State 647 | for (; index < _buttonValuesArraySize; index++) 648 | { 649 | data[index] = _buttonValues[index]; 650 | } 651 | 652 | // Set Hat Switch Values 653 | if (_hatSwitchCount > 0) { 654 | 655 | // Calculate hat-switch values 656 | uint8_t convertedHatSwitch[JOYSTICK_HATSWITCH_COUNT_MAXIMUM]; 657 | for (int hatSwitchIndex = 0; hatSwitchIndex < JOYSTICK_HATSWITCH_COUNT_MAXIMUM; hatSwitchIndex++) 658 | { 659 | if (_hatSwitchValues[hatSwitchIndex] < 0) 660 | { 661 | convertedHatSwitch[hatSwitchIndex] = 8; 662 | } 663 | else 664 | { 665 | convertedHatSwitch[hatSwitchIndex] = (_hatSwitchValues[hatSwitchIndex] % 360) / 45; 666 | } 667 | } 668 | 669 | // Pack hat-switch states into a single byte 670 | data[index++] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]); 671 | 672 | } // Hat Switches 673 | 674 | // Set Axis Values 675 | index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_X_AXIS, _xAxis, _xAxisMinimum, _xAxisMaximum, &(data[index])); 676 | index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_Y_AXIS, _yAxis, _yAxisMinimum, _yAxisMaximum, &(data[index])); 677 | index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_Z_AXIS, _zAxis, _zAxisMinimum, _zAxisMaximum, &(data[index])); 678 | index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_RX_AXIS, _xAxisRotation, _rxAxisMinimum, _rxAxisMaximum, &(data[index])); 679 | index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_RY_AXIS, _yAxisRotation, _ryAxisMinimum, _ryAxisMaximum, &(data[index])); 680 | index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_RZ_AXIS, _zAxisRotation, _rzAxisMinimum, _rzAxisMaximum, &(data[index])); 681 | 682 | // Set Simulation Values 683 | index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_RUDDER, _rudder, _rudderMinimum, _rudderMaximum, &(data[index])); 684 | index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_THROTTLE, _throttle, _throttleMinimum, _throttleMaximum, &(data[index])); 685 | index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_ACCELERATOR, _accelerator, _acceleratorMinimum, _acceleratorMaximum, &(data[index])); 686 | index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_BRAKE, _brake, _brakeMinimum, _brakeMaximum, &(data[index])); 687 | index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_STEERING, _steering, _steeringMinimum, _steeringMaximum, &(data[index])); 688 | 689 | 690 | if (HID.ready()) { 691 | HID.SendReport(_hidReportId, data, sizeof(data),0); 692 | } 693 | 694 | } -------------------------------------------------------------------------------- /ESP32-S3-Analog/joystick.h: -------------------------------------------------------------------------------- 1 | /* 2 | Joystick.h 3 | 4 | Copyright (c) 2015-2017, Matthew Heironimus 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | 20 | Edited by Schnoog to make it running on ESP32-S2/s3 devices 21 | 22 | */ 23 | 24 | #ifndef JOYSTICK_h 25 | #define JOYSTICK_h 26 | 27 | #include 28 | #include 29 | #include "USB.h" 30 | #include "USBHID.h" 31 | 32 | 33 | 34 | //================================================================================ 35 | // Joystick (Gamepad) 36 | 37 | #define JOYSTICK_DEFAULT_REPORT_ID 0x03 38 | #define JOYSTICK_DEFAULT_BUTTON_COUNT 32 39 | #define JOYSTICK_DEFAULT_AXIS_MINIMUM 0 40 | #define JOYSTICK_DEFAULT_AXIS_MAXIMUM 1023 41 | #define JOYSTICK_DEFAULT_SIMULATOR_MINIMUM 0 42 | #define JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM 1023 43 | #define JOYSTICK_DEFAULT_HATSWITCH_COUNT 2 44 | #define JOYSTICK_HATSWITCH_COUNT_MAXIMUM 2 45 | #define JOYSTICK_HATSWITCH_RELEASE -1 46 | #define JOYSTICK_TYPE_JOYSTICK 0x04 47 | #define JOYSTICK_TYPE_GAMEPAD 0x05 48 | #define JOYSTICK_TYPE_MULTI_AXIS 0x08 49 | 50 | class Joystick_: public USBHIDDevice 51 | { 52 | private: 53 | 54 | // Joystick State 55 | int32_t _xAxis; 56 | int32_t _yAxis; 57 | int32_t _zAxis; 58 | int32_t _xAxisRotation; 59 | int32_t _yAxisRotation; 60 | int32_t _zAxisRotation; 61 | int32_t _throttle; 62 | int32_t _rudder; 63 | int32_t _accelerator; 64 | int32_t _brake; 65 | int32_t _steering; 66 | int16_t _hatSwitchValues[JOYSTICK_HATSWITCH_COUNT_MAXIMUM]; 67 | uint8_t *_buttonValues = NULL; 68 | 69 | // Joystick Settings 70 | bool _autoSendState; 71 | uint8_t _buttonCount; 72 | uint8_t _buttonValuesArraySize = 0; 73 | uint8_t _hatSwitchCount; 74 | uint8_t _includeAxisFlags; 75 | uint8_t _includeSimulatorFlags; 76 | int32_t _xAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM; 77 | int32_t _xAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM; 78 | int32_t _yAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM; 79 | int32_t _yAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM; 80 | int32_t _zAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM; 81 | int32_t _zAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM; 82 | int32_t _rxAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM; 83 | int32_t _rxAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM; 84 | int32_t _ryAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM; 85 | int32_t _ryAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM; 86 | int32_t _rzAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM; 87 | int32_t _rzAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM; 88 | int32_t _rudderMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM; 89 | int32_t _rudderMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM; 90 | int32_t _throttleMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM; 91 | int32_t _throttleMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM; 92 | int32_t _acceleratorMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM; 93 | int32_t _acceleratorMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM; 94 | int32_t _brakeMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM; 95 | int32_t _brakeMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM; 96 | int32_t _steeringMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM; 97 | int32_t _steeringMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM; 98 | 99 | uint8_t _hidReportId; 100 | uint8_t _hidReportSize; 101 | 102 | USBHID HID; 103 | 104 | protected: 105 | int buildAndSet16BitValue(bool includeValue, int32_t value, int32_t valueMinimum, int32_t valueMaximum, int32_t actualMinimum, int32_t actualMaximum, uint8_t dataLocation[]); 106 | int buildAndSetAxisValue(bool includeAxis, int32_t axisValue, int32_t axisMinimum, int32_t axisMaximum, uint8_t dataLocation[]); 107 | int buildAndSetSimulationValue(bool includeValue, int32_t value, int32_t valueMinimum, int32_t valueMaximum, uint8_t dataLocation[]); 108 | 109 | public: 110 | uint8_t *customHidReportDescriptor; 111 | int hidReportDescriptorSize; 112 | 113 | Joystick_( 114 | uint8_t hidReportId = JOYSTICK_DEFAULT_REPORT_ID, 115 | uint8_t joystickType = JOYSTICK_TYPE_JOYSTICK, 116 | uint8_t buttonCount = JOYSTICK_DEFAULT_BUTTON_COUNT, 117 | uint8_t hatSwitchCount = JOYSTICK_DEFAULT_HATSWITCH_COUNT, 118 | bool includeXAxis = true, 119 | bool includeYAxis = true, 120 | bool includeZAxis = true, 121 | bool includeRxAxis = true, 122 | bool includeRyAxis = true, 123 | bool includeRzAxis = true, 124 | bool includeRudder = true, 125 | bool includeThrottle = true, 126 | bool includeAccelerator = true, 127 | bool includeBrake = true, 128 | bool includeSteering = true); 129 | 130 | void begin(bool initAutoSendState = true, uint8_t interval_ms = 2); 131 | 132 | void end(); 133 | 134 | // Set Range Functions 135 | inline void setXAxisRange(int32_t minimum, int32_t maximum) 136 | { 137 | _xAxisMinimum = minimum; 138 | _xAxisMaximum = maximum; 139 | } 140 | inline void setYAxisRange(int32_t minimum, int32_t maximum) 141 | { 142 | _yAxisMinimum = minimum; 143 | _yAxisMaximum = maximum; 144 | } 145 | inline void setZAxisRange(int32_t minimum, int32_t maximum) 146 | { 147 | _zAxisMinimum = minimum; 148 | _zAxisMaximum = maximum; 149 | } 150 | inline void setRxAxisRange(int32_t minimum, int32_t maximum) 151 | { 152 | _rxAxisMinimum = minimum; 153 | _rxAxisMaximum = maximum; 154 | } 155 | inline void setRyAxisRange(int32_t minimum, int32_t maximum) 156 | { 157 | _ryAxisMinimum = minimum; 158 | _ryAxisMaximum = maximum; 159 | } 160 | inline void setRzAxisRange(int32_t minimum, int32_t maximum) 161 | { 162 | _rzAxisMinimum = minimum; 163 | _rzAxisMaximum = maximum; 164 | } 165 | inline void setRudderRange(int32_t minimum, int32_t maximum) 166 | { 167 | _rudderMinimum = minimum; 168 | _rudderMaximum = maximum; 169 | } 170 | inline void setThrottleRange(int32_t minimum, int32_t maximum) 171 | { 172 | _throttleMinimum = minimum; 173 | _throttleMaximum = maximum; 174 | } 175 | inline void setAcceleratorRange(int32_t minimum, int32_t maximum) 176 | { 177 | _acceleratorMinimum = minimum; 178 | _acceleratorMaximum = maximum; 179 | } 180 | inline void setBrakeRange(int32_t minimum, int32_t maximum) 181 | { 182 | _brakeMinimum = minimum; 183 | _brakeMaximum = maximum; 184 | } 185 | inline void setSteeringRange(int32_t minimum, int32_t maximum) 186 | { 187 | _steeringMinimum = minimum; 188 | _steeringMaximum = maximum; 189 | } 190 | 191 | // Set Axis Values 192 | void setXAxis(int32_t value); 193 | void setYAxis(int32_t value); 194 | void setZAxis(int32_t value); 195 | void setRxAxis(int32_t value); 196 | void setRyAxis(int32_t value); 197 | void setRzAxis(int32_t value); 198 | 199 | // Set Simulation Values 200 | void setRudder(int32_t value); 201 | void setThrottle(int32_t value); 202 | void setAccelerator(int32_t value); 203 | void setBrake(int32_t value); 204 | void setSteering(int32_t value); 205 | 206 | void setButton(uint8_t button, uint8_t value); 207 | void pressButton(uint8_t button); 208 | void releaseButton(uint8_t button); 209 | 210 | void setHatSwitch(int8_t hatSwitch, int16_t value); 211 | 212 | void sendState(); 213 | uint16_t _onGetDescriptor(uint8_t* buffer); 214 | }; 215 | 216 | 217 | #endif // JOYSTICK_h -------------------------------------------------------------------------------- /ESP32-S3-Analog/params.h: -------------------------------------------------------------------------------- 1 | // Enable this mode to pass raw analog data to the game without any post- 2 | // processing. 3 | // The game has a built-in mechanism to calculate which sensor is triggered 4 | // and the force of the hit, so it's recommended to enable this mode. 5 | // This also the provides the most similar experience with the arcade. 6 | // To disable this mode, remove or comment out this line 7 | #define RAW_ANALOG_MODE 8 | 9 | // For MODE_ADJUSTABLE 10 | // Select which player this board is. 1 for P1 and 2 for P2. 11 | #define PLAYER_SELECT 1 12 | 13 | // For MODE_TWO_PLAYERS 14 | // Sensitivity multipliers for each channel, 1.0 as the baseline. 15 | #define P1_L_DON_SENS 1.0 16 | #define P1_L_KAT_SENS 1.0 17 | #define P1_R_DON_SENS 1.0 18 | #define P1_R_KAT_SENS 1.0 19 | #define P2_L_DON_SENS 1.0 20 | #define P2_L_KAT_SENS 1.0 21 | #define P2_R_DON_SENS 1.0 22 | #define P2_R_KAT_SENS 1.0 23 | 24 | /********************************************** 25 | CHANGING THE FOLLOWING PARAMETERS ARE NOT 26 | RECOMMENDED UNLESS YOU KNOW HOW THEY WORK 27 | ***********************************************/ 28 | 29 | // Cache length must be the power of 2 (8, 16, 32, etc.). 30 | // See cache.h for the reason. 31 | #define SAMPLE_CACHE_LENGTH 32 32 | 33 | // The maximum value of a hit (not the minumum value to trigger a heavy hit) 34 | // To configure the light and heavy thresholds, do it in the game settings. 35 | #define MAX_THRES 5000 36 | 37 | // Input pins for each channel. 38 | #define P1_L_DON_IN 4 39 | #define P1_L_KAT_IN 5 40 | #define P1_R_DON_IN 6 41 | #define P1_R_KAT_IN 7 42 | 43 | #define P2_L_DON_IN 8 44 | #define P2_L_KAT_IN 1 45 | #define P2_R_DON_IN 9 46 | #define P2_R_KAT_IN 10 47 | 48 | // Sensitivity adjustment potentiometer input pins. 49 | #define P1_L_DON_SENS_IN 15 50 | #define P1_L_KAT_SENS_IN 16 51 | #define P1_R_DON_SENS_IN 17 52 | #define P1_R_KAT_SENS_IN 18 53 | 54 | #define AXIS_RANGE 1023 55 | 56 | #define PLAYERS 2 57 | #define CHANNELS 4 58 | 59 | // The minimum value to trigger a light hit. 60 | // Disabled if RAW_ANALOG_MODE is on. 61 | #define HIT_THRES 1000 62 | 63 | // If the reset time is too short, the game may not be able to 64 | // receive the input. From testing I found 40 seems to be the 65 | // minimum value so that the game won't miss any hit. If the game 66 | // occassionally miss the drum input, increase this value. 67 | // Disabled if RAW_ANALOG_MODE is on. 68 | #define RESET_TIME 40 69 | 70 | #include 71 | #include "joystick.h" 72 | #include "cache.h" -------------------------------------------------------------------------------- /ESP32-S3-Analog/two_players.h: -------------------------------------------------------------------------------- 1 | #include "params.h" 2 | 3 | const byte inPins[PLAYERS][CHANNELS] = { 4 | P1_L_DON_IN, P1_L_KAT_IN, P1_R_DON_IN, P1_R_KAT_IN, 5 | P2_L_DON_IN, P2_L_KAT_IN, P2_R_DON_IN, P2_R_KAT_IN 6 | }; 7 | 8 | const float sensitivities[PLAYERS][CHANNELS] = { 9 | P1_L_DON_SENS, P1_L_KAT_SENS, P1_R_DON_SENS, P1_R_KAT_SENS, 10 | P2_L_DON_SENS, P2_L_KAT_SENS, P2_R_DON_SENS, P2_R_KAT_SENS 11 | }; 12 | 13 | Cache inputWindow[PLAYERS][CHANNELS]; 14 | unsigned long power[PLAYERS][CHANNELS]; 15 | 16 | #ifndef RAW_ANALOG_MODE 17 | unsigned long lastPower[PLAYERS][CHANNELS]; 18 | bool triggered[PLAYERS]; 19 | unsigned long triggeredTime[PLAYERS][CHANNELS]; 20 | int outputValue[PLAYERS] = {0, 0}; 21 | uint resetTimer[PLAYERS] = {0, 0}; 22 | short maxIndex[PLAYERS] = {0, 0}; 23 | float maxPower[PLAYERS] = {0, 0}; 24 | #endif 25 | 26 | uint axisValues[PLAYERS][CHANNELS] = {0, 0, 0, 0, 0, 0, 0, 0}; 27 | 28 | Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 10, 4, 29 | true, true, false, true, true, false, 30 | false, false, false, false, false); 31 | 32 | void setup() { 33 | for (byte p = 0; p < PLAYERS; p++) { 34 | for (byte i = 0; i < CHANNELS; i++) { 35 | power[p][i] = 0; 36 | #ifndef RAW_ANALOG_MODE 37 | lastPower[p][i] = 0; 38 | triggered[p] = false; 39 | #endif 40 | pinMode(inPins[p][i], INPUT); 41 | } 42 | #ifndef RAW_ANALOG_MODE 43 | maxIndex[p] = -1; 44 | maxPower[p] = 0; 45 | #endif 46 | } 47 | USB.PID(0x4869); 48 | USB.VID(0x4869); 49 | USB.productName("Taiko Controller"); 50 | USB.manufacturerName("GitHub Community"); 51 | USB.begin(); 52 | Joystick.begin(false); 53 | Joystick.setXAxisRange(-AXIS_RANGE, AXIS_RANGE); 54 | Joystick.setYAxisRange(-AXIS_RANGE, AXIS_RANGE); 55 | Joystick.setRxAxisRange(-AXIS_RANGE, AXIS_RANGE); 56 | Joystick.setRyAxisRange(-AXIS_RANGE, AXIS_RANGE); 57 | } 58 | 59 | void loop() { 60 | 61 | for (byte p = 0; p < PLAYERS; p++) { 62 | 63 | for (byte i = 0; i < CHANNELS; i++) { 64 | inputWindow[p][i].put(analogRead(inPins[p][i])); 65 | power[p][i] = power[p][i] - inputWindow[p][i].get(1) + inputWindow[p][i].get(); 66 | #ifndef RAW_ANALOG_MODE 67 | if (lastPower[p][i] > maxPower[p] && power[p][i] < lastPower[p][i]) { 68 | maxPower[p] = lastPower[p][i]; 69 | maxIndex[p] = i; 70 | } 71 | lastPower[p][i] = power[p][i]; 72 | #else 73 | float v = power[p][i] * sensitivities[p][i]; 74 | axisValues[p][i] = AXIS_RANGE * (v >= MAX_THRES ? 1 : (v / MAX_THRES)); 75 | #endif 76 | } 77 | #ifndef RAW_ANALOG_MODE 78 | if (!triggered[p] && maxPower[p] >= HIT_THRES) { 79 | triggered[p] = true; 80 | outputValue[p] = (int)(AXIS_RANGE * (maxPower[p] >= MAX_THRES ? 1 : maxPower[p] / MAX_THRES)); 81 | } 82 | 83 | if (triggered[p] && resetTimer[p] >= RESET_TIME) { 84 | triggered[p] = false; 85 | resetTimer[p] = 0; 86 | maxPower[p] = 0; 87 | maxIndex[p] = -1; 88 | outputValue[p] = 0; 89 | } 90 | 91 | for (byte i = 0; i < CHANNELS; i++) { 92 | if (triggered[p] && i == maxIndex[p]) { 93 | axisValues[p][i] = outputValue[p]; 94 | } else { 95 | axisValues[p][i] = 0; 96 | } 97 | } 98 | 99 | if (triggered[p]) { 100 | resetTimer[p]++; 101 | } 102 | #endif 103 | } 104 | 105 | Joystick.setXAxis(axisValues[0][0] > axisValues[0][1] ? axisValues[0][0] : -axisValues[0][1]); 106 | Joystick.setYAxis(axisValues[0][2] > axisValues[0][3] ? axisValues[0][2] : -axisValues[0][3]); 107 | Joystick.setRxAxis(axisValues[1][0] > axisValues[1][1] ? axisValues[1][0] : -axisValues[1][1]); 108 | Joystick.setRyAxis(axisValues[1][2] > axisValues[1][3] ? axisValues[1][2] : -axisValues[1][3]); 109 | Joystick.sendState(); 110 | } 111 | -------------------------------------------------------------------------------- /ESP32-S3/ESP32-S3.ino: -------------------------------------------------------------------------------- 1 | #define CHANNELS 4 2 | 3 | // SAMPLE_CACHE_LENGTH must be power of 2 (8, 16, 32, etc.) 4 | // See cache.h for implementation 5 | #define SAMPLE_CACHE_LENGTH 32 6 | 7 | // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you 8 | // changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds 9 | #define HIT_THRES 1750 10 | #define RESET_THRES 200 11 | 12 | // Sampling period in μs, e.g., 500μs = 0.5ms = 2000Hz 13 | #define SAMPLING_PERIOD 500 14 | 15 | // Sensitivity multipliers for each channel, 1.0 as the baseline 16 | #define L_DON_SENS 1.0 17 | #define L_KAT_SENS 1.0 18 | #define R_DON_SENS 1.0 19 | #define R_KAT_SENS 1.0 20 | 21 | // Input pins for each channel 22 | #define L_DON_IN 4 23 | #define L_KAT_IN 5 24 | #define R_DON_IN 6 25 | #define R_KAT_IN 7 26 | 27 | // Output LED pins for each channel (just for visualization) 28 | #define L_DON_LED 10 29 | #define L_KAT_LED 11 30 | #define R_DON_LED 12 31 | #define R_KAT_LED 13 32 | 33 | // Keyboard output for each channel 34 | #define L_DON_KEY 'f' 35 | #define L_KAT_KEY 'd' 36 | #define R_DON_KEY 'j' 37 | #define R_KAT_KEY 'k' 38 | 39 | // Enable debug mode to view analog input values from the Serial 40 | // Enabling this also disables the keyboard simulation 41 | #define DEBUG 0 42 | 43 | #include "USB.h" 44 | #include "USBHIDKeyboard.h" 45 | #include "cache.h" 46 | 47 | USBHIDKeyboard Keyboard; 48 | 49 | Cache inputWindow[CHANNELS]; 50 | unsigned long power[CHANNELS]; 51 | unsigned long lastPower[CHANNELS]; 52 | 53 | bool triggered; 54 | unsigned long triggeredTime[CHANNELS]; 55 | 56 | const byte inPins[] = {L_DON_IN, L_KAT_IN, R_DON_IN, R_KAT_IN}; 57 | const byte outPins[] = {L_DON_LED, L_KAT_LED, R_DON_LED, R_KAT_LED}; 58 | const char outKeys[] = {L_DON_KEY, L_KAT_KEY, R_DON_KEY, R_KAT_KEY}; 59 | float sensitivities[] = {L_DON_SENS, L_KAT_SENS, R_DON_SENS, R_KAT_SENS}; 60 | 61 | short maxIndex; 62 | float maxPower; 63 | 64 | unsigned long lastTime; 65 | 66 | void setup() { 67 | Serial.begin(250000); 68 | for (byte i = 0; i < CHANNELS; i++) { 69 | power[i] = 0; 70 | lastPower[i] = 0; 71 | triggered = false; 72 | pinMode(inPins[i], INPUT); 73 | pinMode(outPins[i], OUTPUT); 74 | } 75 | maxIndex = -1; 76 | maxPower = 0; 77 | lastTime = micros(); 78 | #if !DEBUG 79 | Keyboard.begin(); 80 | USB.begin(); 81 | #endif 82 | } 83 | 84 | void loop() { 85 | if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) { 86 | triggered = false; 87 | digitalWrite(outPins[maxIndex], LOW); 88 | maxIndex = -1; 89 | maxPower = 0; 90 | } 91 | 92 | for (byte i = 0; i < CHANNELS; i++) { 93 | inputWindow[i].put(analogRead(inPins[i])); 94 | power[i] = sensitivities[i] * 95 | (power[i] - inputWindow[i].get(1) + inputWindow[i].get()); 96 | 97 | if (lastPower[i] > maxPower && power[i] < lastPower[i]) { 98 | maxPower = lastPower[i]; 99 | maxIndex = i; 100 | } 101 | lastPower[i] = power[i]; 102 | #if DEBUG 103 | Serial.print(power[i]); 104 | Serial.print(" "); 105 | #endif 106 | } 107 | 108 | if (!triggered && maxPower >= HIT_THRES) { 109 | triggered = true; 110 | digitalWrite(outPins[maxIndex], HIGH); 111 | #if !DEBUG 112 | Keyboard.write(outKeys[maxIndex]); 113 | #endif 114 | } 115 | #if DEBUG 116 | Serial.print("\n"); 117 | #endif 118 | 119 | unsigned int frameTime = micros() - lastTime; 120 | if (frameTime < SAMPLING_PERIOD) { 121 | delayMicroseconds(SAMPLING_PERIOD - frameTime); 122 | } 123 | lastTime = micros(); 124 | } 125 | -------------------------------------------------------------------------------- /ESP32-S3/cache.h: -------------------------------------------------------------------------------- 1 | /*************************************************************** 2 | * * 3 | * Taiko Sanro - Arduino * 4 | * Cache data structure * 5 | * * 6 | * Chris * 7 | * wisaly@gmail.com * 8 | * * 9 | ***************************************************************/ 10 | 11 | #ifndef CACHE_H 12 | #define CACHE_H 13 | 14 | template 15 | class Cache { 16 | public: 17 | Cache() { memset(data_, 0, sizeof(data_)); } 18 | inline void put(T value) { 19 | current_ = (current_ + 1) & (L - 1); 20 | data_[current_] = value; 21 | } 22 | inline T get(int offset = 0) const { 23 | return data_[(current_ + offset) & (L - 1)]; 24 | } 25 | 26 | private: 27 | T data_[L]; 28 | int current_ = 0; 29 | }; 30 | 31 | #endif // CACHE_H -------------------------------------------------------------------------------- /ESP32/ESP32.ino: -------------------------------------------------------------------------------- 1 | #define CHANNELS 4 2 | 3 | // SAMPLE_CACHE_LENGTH must be power of 2 (8, 16, 32, etc.) 4 | // See cache.h for implementation 5 | #define SAMPLE_CACHE_LENGTH 16 6 | 7 | // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you 8 | // changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds 9 | #define HIT_THRES 750 10 | #define RESET_THRES 200 11 | 12 | // Sampling period in μs, e.g., 500μs = 0.5ms = 2000Hz 13 | #define SAMPLING_PERIOD 500 14 | 15 | // Sensitivity multipliers for each channel, 1.0 as the baseline 16 | #define L_DON_SENS 1.0 17 | #define L_KAT_SENS 1.0 18 | #define R_DON_SENS 1.0 19 | #define R_KAT_SENS 1.0 20 | 21 | // Input pins for each channel 22 | #define L_DON_IN 36 23 | #define L_KAT_IN 39 24 | #define R_DON_IN 34 25 | #define R_KAT_IN 35 26 | 27 | // Output LED pins for each channel (just for visualization) 28 | #define L_DON_LED 25 29 | #define L_KAT_LED 26 30 | #define R_DON_LED 27 31 | #define R_KAT_LED 14 32 | 33 | // Keyboard output for each channel 34 | #define L_DON_KEY 'f' 35 | #define L_KAT_KEY 'd' 36 | #define R_DON_KEY 'j' 37 | #define R_KAT_KEY 'k' 38 | 39 | // Enable debug mode to view analog input values from the Serial 40 | // Enabling this also disables the keyboard simulation 41 | #define DEBUG 0 42 | 43 | #include "cache.h" 44 | #include "keyboard.h" 45 | 46 | Cache inputWindow[CHANNELS]; 47 | unsigned long power[CHANNELS]; 48 | unsigned long lastPower[CHANNELS]; 49 | 50 | bool triggered; 51 | unsigned long triggeredTime[CHANNELS]; 52 | 53 | const byte inPins[] = {L_DON_IN, L_KAT_IN, R_DON_IN, R_KAT_IN}; 54 | const byte outPins[] = {L_DON_LED, L_KAT_LED, R_DON_LED, R_KAT_LED}; 55 | const char outKeys[] = {L_DON_KEY, L_KAT_KEY, R_DON_KEY, R_KAT_KEY}; 56 | float sensitivities[] = {L_DON_SENS, L_KAT_SENS, R_DON_SENS, R_KAT_SENS}; 57 | 58 | short maxIndex; 59 | float maxPower; 60 | 61 | unsigned long lastTime; 62 | 63 | void setup() { 64 | Serial.begin(250000); 65 | for (byte i = 0; i < CHANNELS; i++) { 66 | power[i] = 0; 67 | lastPower[i] = 0; 68 | triggered = false; 69 | pinMode(inPins[i], INPUT); 70 | pinMode(outPins[i], OUTPUT); 71 | } 72 | maxIndex = -1; 73 | maxPower = 0; 74 | xTaskCreate(bluetoothTask, "bluetooth", 20000, NULL, 5, NULL); 75 | lastTime = micros(); 76 | } 77 | 78 | void loop() { 79 | if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) { 80 | triggered = false; 81 | digitalWrite(outPins[maxIndex], LOW); 82 | maxIndex = -1; 83 | maxPower = 0; 84 | } 85 | 86 | for (byte i = 0; i < CHANNELS; i++) { 87 | inputWindow[i].put(analogRead(inPins[i])); 88 | power[i] = sensitivities[i] * 89 | (power[i] - inputWindow[i].get(1) + inputWindow[i].get()); 90 | 91 | if (lastPower[i] > maxPower && power[i] < lastPower[i]) { 92 | maxPower = lastPower[i]; 93 | maxIndex = i; 94 | } 95 | lastPower[i] = power[i]; 96 | #if DEBUG 97 | Serial.print(power[i]); 98 | Serial.print(" "); 99 | #endif 100 | } 101 | 102 | if (!triggered && maxPower >= HIT_THRES) { 103 | triggered = true; 104 | digitalWrite(outPins[maxIndex], HIGH); 105 | #if !DEBUG 106 | if (isBleConnected) { 107 | typeChar(outKeys[maxIndex]); 108 | } 109 | #endif 110 | } 111 | #if DEBUG 112 | Serial.print("\n"); 113 | #endif 114 | 115 | unsigned int frameTime = micros() - lastTime; 116 | if (frameTime < SAMPLING_PERIOD) { 117 | delayMicroseconds(SAMPLING_PERIOD - frameTime); 118 | } 119 | lastTime = micros(); 120 | } 121 | -------------------------------------------------------------------------------- /ESP32/cache.h: -------------------------------------------------------------------------------- 1 | /*************************************************************** 2 | * * 3 | * Taiko Sanro - Arduino * 4 | * Cache data structure * 5 | * * 6 | * Chris * 7 | * wisaly@gmail.com * 8 | * * 9 | ***************************************************************/ 10 | 11 | #ifndef CACHE_H 12 | #define CACHE_H 13 | 14 | template 15 | class Cache { 16 | public: 17 | Cache() { memset(data_, 0, sizeof(data_)); } 18 | inline void put(T value) { 19 | current_ = (current_ + 1) & (L - 1); 20 | data_[current_] = value; 21 | } 22 | inline T get(int offset = 0) const { 23 | return data_[(current_ + offset) & (L - 1)]; 24 | } 25 | 26 | private: 27 | T data_[L]; 28 | int current_ = 0; 29 | }; 30 | 31 | #endif // CACHE_H -------------------------------------------------------------------------------- /ESP32/keyboard.h: -------------------------------------------------------------------------------- 1 | // Bluetooth keyboard implemetation by manuelbl: 2 | // https://gist.github.com/manuelbl/66f059effc8a7be148adb1f104666467 3 | 4 | #include "BLEDevice.h" 5 | #include "BLEHIDDevice.h" 6 | #include "HIDKeyboardTypes.h" 7 | #include "HIDTypes.h" 8 | 9 | #define DEVICE_NAME "ESP32 Taiko Controller" 10 | 11 | bool isBleConnected = false; 12 | 13 | // Message (report) sent when a key is pressed or released 14 | struct InputReport { 15 | uint8_t modifiers; // bitmask: CTRL = 1, SHIFT = 2, ALT = 4 16 | uint8_t reserved; // must be 0 17 | uint8_t pressedKeys[6]; // up to six concurrenlty pressed keys 18 | }; 19 | 20 | // Message (report) received when an LED's state changed 21 | struct OutputReport { 22 | uint8_t leds; // bitmask: num lock = 1, caps lock = 2, scroll lock = 4, 23 | // compose = 8, kana = 16 24 | }; 25 | 26 | // The report map describes the HID device (a keyboard in this case) and 27 | // the messages (reports in HID terms) sent and received. 28 | static const uint8_t REPORT_MAP[] = { 29 | USAGE_PAGE(1), 30 | 0x01, // Generic Desktop Controls 31 | USAGE(1), 32 | 0x06, // Keyboard 33 | COLLECTION(1), 34 | 0x01, // Application 35 | REPORT_ID(1), 36 | 0x01, // Report ID (1) 37 | USAGE_PAGE(1), 38 | 0x07, // Keyboard/Keypad 39 | USAGE_MINIMUM(1), 40 | 0xE0, // Keyboard Left Control 41 | USAGE_MAXIMUM(1), 42 | 0xE7, // Keyboard Right Control 43 | LOGICAL_MINIMUM(1), 44 | 0x00, // Each bit is either 0 or 1 45 | LOGICAL_MAXIMUM(1), 46 | 0x01, 47 | REPORT_COUNT(1), 48 | 0x08, // 8 bits for the modifier keys 49 | REPORT_SIZE(1), 50 | 0x01, 51 | HIDINPUT(1), 52 | 0x02, // Data, Var, Abs 53 | REPORT_COUNT(1), 54 | 0x01, // 1 byte (unused) 55 | REPORT_SIZE(1), 56 | 0x08, 57 | HIDINPUT(1), 58 | 0x01, // Const, Array, Abs 59 | REPORT_COUNT(1), 60 | 0x06, // 6 bytes (for up to 6 concurrently pressed keys) 61 | REPORT_SIZE(1), 62 | 0x08, 63 | LOGICAL_MINIMUM(1), 64 | 0x00, 65 | LOGICAL_MAXIMUM(1), 66 | 0x65, // 101 keys 67 | USAGE_MINIMUM(1), 68 | 0x00, 69 | USAGE_MAXIMUM(1), 70 | 0x65, 71 | HIDINPUT(1), 72 | 0x00, // Data, Array, Abs 73 | REPORT_COUNT(1), 74 | 0x05, // 5 bits (Num lock, Caps lock, Scroll lock, Compose, Kana) 75 | REPORT_SIZE(1), 76 | 0x01, 77 | USAGE_PAGE(1), 78 | 0x08, // LEDs 79 | USAGE_MINIMUM(1), 80 | 0x01, // Num Lock 81 | USAGE_MAXIMUM(1), 82 | 0x05, // Kana 83 | LOGICAL_MINIMUM(1), 84 | 0x00, 85 | LOGICAL_MAXIMUM(1), 86 | 0x01, 87 | HIDOUTPUT(1), 88 | 0x02, // Data, Var, Abs 89 | REPORT_COUNT(1), 90 | 0x01, // 3 bits (Padding) 91 | REPORT_SIZE(1), 92 | 0x03, 93 | HIDOUTPUT(1), 94 | 0x01, // Const, Array, Abs 95 | END_COLLECTION(0) // End application collection 96 | }; 97 | 98 | BLEHIDDevice* hid; 99 | BLECharacteristic* input; 100 | BLECharacteristic* output; 101 | 102 | const InputReport NO_KEY_PRESSED = {}; 103 | 104 | /* 105 | * Callbacks related to BLE connection 106 | */ 107 | class BleKeyboardCallbacks : public BLEServerCallbacks { 108 | void onConnect(BLEServer* server) { 109 | isBleConnected = true; 110 | 111 | // Allow notifications for characteristics 112 | BLE2902* cccDesc = 113 | (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); 114 | cccDesc->setNotifications(true); 115 | 116 | Serial.println("Client has connected"); 117 | } 118 | 119 | void onDisconnect(BLEServer* server) { 120 | isBleConnected = false; 121 | 122 | // Disallow notifications for characteristics 123 | BLE2902* cccDesc = 124 | (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); 125 | cccDesc->setNotifications(false); 126 | 127 | Serial.println("Client has disconnected"); 128 | } 129 | }; 130 | 131 | /* 132 | * Called when the client (computer, smart phone) wants to turn on or off 133 | * the LEDs in the keyboard. 134 | * 135 | * bit 0 - NUM LOCK 136 | * bit 1 - CAPS LOCK 137 | * bit 2 - SCROLL LOCK 138 | */ 139 | class OutputCallbacks : public BLECharacteristicCallbacks { 140 | void onWrite(BLECharacteristic* characteristic) { 141 | OutputReport* report = (OutputReport*)characteristic->getData(); 142 | Serial.print("LED state: "); 143 | Serial.print((int)report->leds); 144 | Serial.println(); 145 | } 146 | }; 147 | 148 | void bluetoothTask(void*) { 149 | // initialize the device 150 | BLEDevice::init(DEVICE_NAME); 151 | BLEServer* server = BLEDevice::createServer(); 152 | server->setCallbacks(new BleKeyboardCallbacks()); 153 | 154 | // create an HID device 155 | hid = new BLEHIDDevice(server); 156 | input = hid->inputReport(1); // report ID 157 | output = hid->outputReport(1); // report ID 158 | output->setCallbacks(new OutputCallbacks()); 159 | 160 | // set manufacturer name 161 | hid->manufacturer()->setValue("Maker Community"); 162 | // set USB vendor and product ID 163 | hid->pnp(0x02, 0xe502, 0xa111, 0x0210); 164 | // information about HID device: device is not localized, device can be 165 | // connected 166 | hid->hidInfo(0x00, 0x02); 167 | 168 | // Security: device requires bonding 169 | BLESecurity* security = new BLESecurity(); 170 | security->setAuthenticationMode(ESP_LE_AUTH_BOND); 171 | 172 | // set report map 173 | hid->reportMap((uint8_t*)REPORT_MAP, sizeof(REPORT_MAP)); 174 | hid->startServices(); 175 | 176 | // set battery level to 100% 177 | hid->setBatteryLevel(100); 178 | 179 | // advertise the services 180 | BLEAdvertising* advertising = server->getAdvertising(); 181 | advertising->setAppearance(HID_KEYBOARD); 182 | advertising->addServiceUUID(hid->hidService()->getUUID()); 183 | advertising->addServiceUUID(hid->deviceInfo()->getUUID()); 184 | advertising->addServiceUUID(hid->batteryService()->getUUID()); 185 | advertising->start(); 186 | 187 | Serial.println("BLE ready"); 188 | delay(portMAX_DELAY); 189 | }; 190 | 191 | void typeText(const char* text) { 192 | int len = strlen(text); 193 | for (int i = 0; i < len; i++) { 194 | // translate character to key combination 195 | uint8_t val = (uint8_t)text[i]; 196 | if (val > KEYMAP_SIZE) 197 | continue; // character not available on keyboard - skip 198 | KEYMAP map = keymap[val]; 199 | 200 | // create input report 201 | InputReport report = {.modifiers = map.modifier, 202 | .reserved = 0, 203 | .pressedKeys = {map.usage, 0, 0, 0, 0, 0}}; 204 | 205 | // send the input report 206 | input->setValue((uint8_t*)&report, sizeof(report)); 207 | input->notify(); 208 | 209 | delay(5); 210 | 211 | // release all keys between two characters; otherwise two identical 212 | // consecutive characters are treated as just one key press 213 | input->setValue((uint8_t*)&NO_KEY_PRESSED, sizeof(NO_KEY_PRESSED)); 214 | input->notify(); 215 | 216 | delay(5); 217 | } 218 | } 219 | 220 | void typeChar(char c) { 221 | uint8_t val = (uint8_t)c; 222 | KEYMAP map = keymap[val]; 223 | InputReport report = {.modifiers = map.modifier, 224 | .reserved = 0, 225 | .pressedKeys = {map.usage, 0, 0, 0, 0, 0}}; 226 | input->setValue((uint8_t*)&report, sizeof(report)); 227 | input->notify(); 228 | delay(1); 229 | input->setValue((uint8_t*)&NO_KEY_PRESSED, sizeof(NO_KEY_PRESSED)); 230 | input->notify(); 231 | delay(1); 232 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 ShikyC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [日本語のドキュメントはこちら](README_ja-JP.md) 2 | 3 | [中文文档在这里](README_zh-CN.md) 4 | 5 | ![Taiko Drum Controller](./images/banner-taiko.png) 6 | 7 | # Taiko Drum Controller - Arduino (ATmega32U4/ESP32) 8 | 9 | Open-source hardware program to make your own Taiko no Tatsujin PC controller. It can emulate as a keyboard, or as an analog joystick to enable hitting force sensing - just like how you play on the arcade machine. Now also support 2 drums so you can enjoy the game together with your friends at home! 10 | 11 | ## About this Project 12 | 13 | This project aims to help you develop your own hardware taiko at home. 14 | 15 | **This program is for personal and non-commercial use only.** 16 | 17 | ## What You Need 18 | 19 | 1. An Arduino Micro/Leonardo (ATmega32U4) board or an Arduino Nano ESP (ESP32) board. 20 | 21 | Most ATmega32U4 boards work, but you need to verify that they support keyboard emulation; ATmega328P boards like Arduino Uno don't work. 22 | 23 | ESP32 is strongly recommended because it's significantly more powerful than ATmega32U4. This project uses an ESP32-WROOM-32 board. 24 | 25 | 2. 4 piezoelectric sensors. 26 | 27 | 3. 8 100kΩ resistors. 28 | 29 | 4. (Optional) 4 bridge rectifier chips such as [DB107](https://www.diodes.com/assets/Datasheets/products_inactive_data/ds21211_R5.pdf) (see the Additional Notes section for details). 30 | 31 | 5. (Optional) Some red and blue LEDs. 32 | 33 | 6. Necessary electronic components (breadboards and jumper wires, etc.). 34 | 35 | 7. Wood planks and cutting tools (only if you need to make your physical taiko drum from scratch). If you have an aftermarket taiko or a Big Power Lv. 5 drum, you can use them directly. 36 | 37 | ## Steps to Make the Controller 38 | 39 | 1. Make the drum and firmly glue the 4 piezoelectric sensors to the drum. Refer to the image for preferred locations of the sensors. 40 | 41 | ![Sensor Setup](./images/piezo_locations.png) 42 | 43 | 2. Connect the piezoelectric sensors and other components to the controller as follows (the polarity of the piezoelectric sensors don't matter); 44 | 45 | The following schemes are for Arduino Micro boards. If you use a different board, refer to its documentations for the connection. 46 | 47 | ![Controller scheme](./images/scheme.png) 48 | 49 | If you choose to add the bridge rectifiers, use the following scheme: 50 | 51 | ![Controller scheme with bridge rectifiers](./images/scheme_bridge.png) 52 | 53 | 3. Flash the firmware to the board. 54 | 55 | You may need to fine-tune some parameters like `SAMPLE_CACHE_LENGTH`, `HIT_THRES`, `RESET_THRES`, and `sensitivity`. See the following section for details. 56 | 57 | 4. Have fun! 58 | 59 | ## Tuning the Parameters 60 | 61 | 1. Hit and reset threshold 62 | 63 | Set `DEBUG 1` (this disables the keyboard output and sends signal values from the serial port), flash the firmware, roll on one of the 4 areas of the drum, and visualize the graph from the output of the serial monitor. The hit threshold should be lower than your heaviest hit on the drum, and the reset threshold should be greater than the trough between roll hits. The reset value should also be below the hit value. 64 | 65 | Repeat the process for the rest 3 areas and find the best one that fits all. 66 | 67 | ![Setting hit and reset values](./images/tune_hit_reset.png) 68 | 69 | 2. Sampling length 70 | 71 | For maximum runtime speed, the `cache.h` library has been optimized to work with `SAMPLE_CACHE_LENGTH` window sizes of powers of 2. That means 2, 8, 16, and 32, etc. Practically 16 is the best value for Arduino, but if you have a powerful microcontroller that samples the input at the speed of at least 4000Hz or more, you can change the value to 32 for a smoother (in other words, less noisy) curve. 72 | 73 | 3. Sensitivities 74 | 75 | Not all piezoelectric sensors are the same, and due to installation errors, the captured signals from the 4 sensors may vary significantly. The sensitivity values are multipliers to normalize the differences. In the following example, the right-don area generates a much higher value than the rest 3, so you can adjust `sensitivity` to `{1.0, 1.0, 0.5, 1.0}` to eliminate the issue. 76 | 77 | ![Setting sensitivity values](./images/tune_sensitivities.png) 78 | 79 | Note that the installation of the sensors is very critical. You should make sure that the sensors are firmly attached on the wood and located properly. 80 | 81 | ## Additional Notes 82 | 83 | 1. Why using bridge rectifiers 84 | 85 | Without biasing the voltage of the piezoelectric sensors, their output voltage range is about -5V to +5V. However, the ADCs of the analog inputs only accepts positive voltage values (0-3.3V for ESP32 and 0-5V for ATmega32U4). When they receive a negative voltage, it's simply truncated to 0. 86 | 87 | It's usually okay for normal electronic drums because we're just losing half of the input energy and it doesn't influence how we calculate the hitting time. But it can cause problems for *taiko* drums, especially with slow processors like ATmega32U4. 88 | 89 | In a taiko drum, all the 4 vibrating pieces are connected together, meaning that if you hit left-don, the processor also receives signals from left-kat, right-don, and right-kat. If the left-don piezoelectric sensor generates a negative voltage at the beginning and is truncated by the ADC, it will cause a minor "delay" of about 3 to 4 milliseconds, and the processor could incorrectly treat this hit as a right-don, a left-kat, or even a right-kat, whichever sends a highest positive value. 90 | 91 | Using a bridge rectifier, all negative values are converted to positive. In other words, it's like the `abs()` function, ensuring that we don't lose any negative voltages. 92 | 93 | ![Why using bridge rectifiers](./images/bridge_signal.png) 94 | 95 | # Taiko Controller - Analog Input Mode (Beta) 96 | 97 | With ESP32-S2 or ESP32-S3 controllers, instead of keyboard emulation, the drum controller can work as a gamepad and send its axes values to the game (which also must support analog input). In this way, the game can recognize different force levels of the hit. 98 | 99 | If you prefer to use the Arduino Micro/Leonardo board, please refer to the [Arduino XInput Library](https://github.com/dmadison/ArduinoXInput) to implement the gamepad. 100 | 101 | ## What You Need 102 | 103 | 1. Make your drum or use Taiko Force Lv.5. 104 | 105 | 2. Flash `ESP32-S3-Analog/ESP32-S3-Analog.ino` to your controller. 106 | 107 | 3. A working ***game***, with these modifications: 108 | 109 | - Backup and replace the `bnusio.dll` file in the game folder with the one here in the `extra/` folder. 110 | 111 | This file is compiled from [this fork](https://github.com/ShikyC/TaikoArcadeLoader/tree/Refactor) and you can compile it by yourself if you want. 112 | 113 | *This modified library only works with a specific game version. If it breaks your game, please clone the original repository, make the corrensponding modifications, and compile it.* 114 | 115 | - Open the `gamecontrollerdb.txt` file in the game folder and add one entry under `#Windows`: 116 | 117 | `030052a8694800006948000000000000,Taiko Controller,-leftx:-a0,+leftx:+a0,-lefty:-a1,+lefty:+a1,-rightx:-a2,+rightx:+a2,-righty:-a3,+righty:+a3,platform:Windows,` 118 | 119 | This will tell the game that our ESP32 controller is a gamepad called "Taiko Controller", and map the axis to the standard SDL2 library so that the game can recognize the analog inputs. 120 | 121 | - Open the `config.toml` file and add the following lines at the end: 122 | 123 | ``` 124 | [controller] 125 | analog = true 126 | ``` 127 | 128 | Note that with `analog = true`, all the keyboard drum inputs are disabled. Sorry for this but it need further refactoring to make them work together. If you want to switch back to keyboard inputs, set `analog = false`. 129 | 130 | 4. Launch the game and enjoy! 131 | 132 | ## Online Signal Visualizer Tool 133 | 134 | If you find debugging and finding the best sensitivity values very inconvenient with serial port's communications, you can use this [taiko signal visualizer](https://shiky.me/taiko). It's a simple tool I created to show the output of the Taiko Controller (ESP32-Analog version only). 135 | 136 | Just connect the board to the computer (using the USB port, not the serial port) and the waveform will be shown in the graphs. So you don't need to turn on debug mode or using `Serial.print()` command to obtain the values to tune the sensitivities. 137 | 138 | ### How it works 139 | 140 | The analog version of the controller simulates as a gamepad and sends the hit force values to the game as the axis values of the left and right joysticks. So with the built-in `Gamepad` API it's very easy to read the values directly. To prevent the tool reading from other gamepads, the controller must be named with `Taiko Controller (Vendor: 4869 Product: 4869)`, so please don't change the `pID` and `vID` values in the firmware. 141 | 142 | ![Taiko Test Tool](./images/taiko_test.webp) -------------------------------------------------------------------------------- /README_ja-JP.md: -------------------------------------------------------------------------------- 1 | [Click here for English documentation](README.md) 2 | 3 | [中文文档在这里](README_zh-CN.md) 4 | 5 | ![太鼓コントローラ](./images/banner-taiko.png) 6 | 7 | # Taiko Drum Controller - Arduino (ATmega32U4/ESP32) 8 | 9 | 自分の太鼓の達人PCコントローラを作るためのオープンソースハードウェアプログラム。キーボードとして、またはアーケードマシンで遊ぶように打撃力感知を有効にするアナログジョイスティックとしてエミュレートできます。今では2つのドラムもサポートしているので、家で友達と一緒にゲームを楽しむことができます! 10 | 11 | ## このプロジェクトについて 12 | 13 | このプロジェクトは、自宅で自分のハードウェア太鼓を開発するのを助けることを目指しています。 14 | 15 | **このプログラムは個人的かつ非商用のみに限ります。** 16 | 17 | ## 必要なもの 18 | 19 | 1. Arduino Micro/Leonardo (ATmega32U4) ボードまたは Arduino Nano ESP (ESP32) ボード。 20 | 21 | ほとんどのATmega32U4ボードは動作しますが、キーボードエミュレーションをサポートしているかを確認する必要があります。Arduino UnoのようなATmega328Pボードは動作しません。 22 | 23 | ESP32はATmega32U4よりも格段にパワフルなため、強く推奨されます。このプロジェクトではESP32-WROOM-32ボードを使用しています。 24 | 25 | 2. 4つの圧電センサ。 26 | 27 | 3. 8つの100kΩ抵抗。 28 | 29 | 4. (任意) DB107などのブリッジ整流器チップ(詳細は追加ノートセクションを参照)。 30 | 31 | 5. (任意) 赤と青のLEDいくつか。 32 | 33 | 6. 必要な電子部品(ブレッドボード、ジャンパーワイヤーなど)。 34 | 35 | 7. 木板と切削工具(自分で物理的な太鼓を一から作る必要がある場合のみ)。アフターマーケットの太鼓やBig Power Lv.5ドラムを持っている場合は、それらを直接使用できます。 36 | 37 | ## コントローラを作る手順 38 | 39 | 1. 太鼓を作り、4つの圧電センサを太鼓にしっかりと接着します。センサの好ましい位置については画像を参照してください。 40 | 41 | ![センサ設定](./images/piezo_locations.png) 42 | 43 | 2. 圧電センサと他のコンポーネントを以下の通りコントローラに接続します(圧電センサの極性は問いません); 44 | 45 | 以下のスキームはArduino Microボード用です。異なるボードを使用する場合は、接続についてそのドキュメンテーションを参照してください。 46 | 47 | ![コントローラスキーム](./images/scheme.png) 48 | 49 | ブリッジ整流器を追加することを選択した場合、以下のスキームを使用してください: 50 | 51 | ![ブリッジ整流器付きコントローラスキーム](./images/scheme_bridge.png) 52 | 53 | 3. ファームウェアをボードにフラッシュします。 54 | 55 | `SAMPLE_CACHE_LENGTH`、`HIT_THRES`、`RESET_THRES`、`sensitivity`などのパラメータを微調整する必要があるかもしれません。詳細は以下のセクションを参照してください。 56 | 57 | 4. 楽しんでください! 58 | 59 | ## パラメータの調整 60 | 61 | 1. ヒットとリセットの閾値 62 | 63 | `DEBUG 1`を設定し(これによりキーボード出力が無効になり、シリアルポートから信号値が送信されます)、ファームウェアをフラッシュし、太鼓の4つの領域のいずれかでロールして、シリアルモニターの出力からグラフを視覚化します。ヒット閾値は太鼓の最も重いヒットよりも低く、リセット閾値はロールヒットの間のトラフよりも大きくする必要があります。リセット値はまた、ヒット値よりも低くする必要があります。 64 | 65 | 残りの3つの領域についてもプロセスを繰り返し、すべてに適合する最適なものを見つけます。 66 | 67 | ![ヒットとリセット値の設定](./images/tune_hit_reset.png) 68 | 69 | 2. サンプリング長 70 | 71 | 最大の実行時間速度のために、`cache.h`ライブラリは2のべき乗の`SAMPLE_CACHE_LENGTH`ウィンドウサイズで動作するように最適化されています。つまり2、8、16、32などです。実用的には16がArduinoにとって最適な値ですが、少なくとも4000Hz以上の速度で入力をサンプリングするパワフルなマイクロコントローラを持っている場合は、よりスムーズな(つまり、ノイズが少ない)カーブのために値を32に変更することができます。 72 | 73 | 3. 感度 74 | 75 | すべての圧電センサが同じではなく、取り付けエラーにより、4つのセンサからキャプチャされる信号は大きく異なることがあります。感度値は、差異を正規化するための乗数です。以下の例では、右のドンエリアが他の3つよりもはるかに高い値を生成しているため、`sensitivity`を`{1.0, 1.0, 0.5, 1.0}`に調整して問題を解決できます。 76 | 77 | ![感度値の設定](./images/tune_sensitivities.png) 78 | 79 | センサの取り付けは非常に重要です。センサが木にしっかりと取り付けられ、適切な位置にあることを確認してください。 80 | 81 | ## 追加ノート 82 | 83 | 1. ブリッジ整流器の使用理由 84 | 85 | 圧電センサの電圧を偏らせない場合、その出力電圧範囲は約-5Vから+5Vです。しかし、アナログ入力のADCは正の電圧値(ESP32の場合は0-3.3V、ATmega32U4の場合は0-5V)のみを受け付けます。負の電圧を受け取ると、単に0に切り捨てられます。 86 | 87 | 通常の電子ドラムでは、入力エネルギーの半分を失っても、ヒットタイミングの計算には影響しませんので問題ありません。しかし、*太鼓*のドラムでは、特にATmega32U4のような遅いプロセッサを使用すると問題が発生することがあります。 88 | 89 | 太鼓のドラムでは、4つの振動する部分がすべて繋がっているため、左のドンを叩くと、プロセッサは左のカツ、右のドン、右のカツからも信号を受信します。左のドンの圧電センサが最初に負の電圧を生成し、ADCによって切り捨てられると、約3〜4ミリ秒のわずかな「遅延」が発生し、プロセッサはこのヒットを誤って右のドン、左のカツ、または右のカツとして処理する可能性があります。どちらかが最高の正の値を送信した場合です。 90 | 91 | ブリッジ整流器を使用すると、すべての負の値が正の値に変換されます。言い換えれば、それは`abs()`関数のようなもので、負の電圧を失わないことを保証します。 92 | 93 | ![ブリッジ整流器の使用理由](./images/bridge_signal.png) 94 | 95 | # Taiko Controller - アナログ入力モード(Beta) 96 | 97 | ESP32-S2またはESP32-S3コントローラーを使用すると、キーボードエミュレーションの代わりに、ドラムコントローラーがゲームパッドとして機能し、その軸の値をゲームに送信できます(ゲームもアナログ入力をサポートしている必要があります)。この方法では、ゲームはヒットの異なる力のレベルを認識できます。 98 | 99 | Arduino Micro/Leonardoボードを使用する場合は、[Arduino XInputライブラリ](https://github.com/dmadison/ArduinoXInput)を参照して、ゲームパッドを実装してください。 100 | 101 | ## 必要なもの 102 | 103 | 1. 自分のドラムを作るか、Taiko Force Lv.5を使用してください。詳細はメインブランチをチェックしてください。 104 | 105 | 2. `ESP32-S3-Analog/ESP32-S3-Analog.ino`をコントローラーにフラッシュします。 106 | 107 | 3. 以下の変更を加えたゲーム: 108 | 109 | - ゲームフォルダ内の`bnusio.dll`ファイルをバックアップし、`extra/`フォルダーこちらのファイルで置き換えてください。 110 | 111 | このファイルは[このフォーク](https://github.com/ShikyC/TaikoArcadeLoader/tree/Refactor)からコンパイルされており、必要に応じて自分でコンパイルすることができます。 112 | 113 | *この変更されたライブラリは特定のゲームバージョンでのみ機能します。ゲームが壊れた場合は、元のリポジトリをクローンして、対応する変更を加え、コンパイルしてください。* 114 | 115 | - ゲームフォルダ内の`gamecontrollerdb.txt`ファイルを開き、`#Windows`の下に次のエントリを追加します: 116 | 117 | `030052a8694800006948000000000000,Taiko Controller,-leftx:-a0,+leftx:+a0,-lefty:-a1,+lefty:+a1,-rightx:-a2,+rightx:+a2,-righty:-a3,+righty:+a3,platform:Windows,` 118 | 119 | これにより、ゲームはESP32コントローラーが「Taiko Controller」というゲームパッドであることを認識し、標準のSDL2ライブラリに軸をマップするため、ゲームがアナログ入力を認識できるようになります。 120 | 121 | - `config.toml`ファイルを開き、最後に次の行を追加します: 122 | 123 | ``` 124 | [controller] 125 | analog = true 126 | ``` 127 | 128 | `analog = true`で、すべてのキーボードドラム入力が無効になります。これはさらなるリファクタリングが必要ですが、一緒に機能させるために申し訳ありません。キーボード入力に戻したい場合は、`analog = false`に設定してください。 129 | 130 | 4. ゲームを起動して楽しんでください! 131 | 132 | ## オンライン信号ビジュアライザーツール 133 | 134 | シリアルポートの通信でデバッグを行うのが不便で、最適な感度値を見つけるのが困難な場合、この[太鼓信号ビジュアライザー](https://shiky.me/taiko)を使用することができます。これは私が作成したシンプルなツールで、太鼓コントローラー(ESP32-Analog版のみ)の出力を表示します。 135 | 136 | ボードをコンピュータに接続するだけ(USBポートを使用し、シリアルポートは使用しない)と、波形がグラフに表示されます。したがって、デバッグモードをオンにする必要もなく、`Serial.print()` コマンドを使用して感度を調整するための値を取得する必要もありません。 137 | 138 | ### 使い方 139 | 140 | コントローラのアナログバージョンはゲームパッドとして動作し、ヒット力の値をゲームに左右のジョイスティックの軸値として送信します。内蔵の `Gamepad` APIを使用すると、値を直接読み取るのが非常に簡単です。他のゲームパッドからのツールの読み取りを防ぐために、コントローラは `Taiko Controller (Vendor: 4869 Product: 4869)` という名前でなければなりませんので、ファームウェアの `pID` と `vID` の値を変更しないでください。 141 | 142 | ![太鼓テストツール](./images/taiko_test.webp) 143 | -------------------------------------------------------------------------------- /README_zh-CN.md: -------------------------------------------------------------------------------- 1 | [Click here for English documentation](README.md) 2 | 3 | [日本語のドキュメントはこちら](README_ja-JP.md) 4 | 5 | ![Taiko Drum Controller](./images/banner-taiko.png) 6 | 7 | # Taiko Drum Controller - Arduino (ATmega32U4/ESP32) 8 | 9 | 开源硬件程序,帮助你制作自己的太鼓达人PC控制器。它可以模拟作为键盘,或者作为模拟摇杆以启用击打力感应 - 就像您在街机上玩一样。现在还支持2个鼓,这样您就可以和朋友们一起在家享受游戏乐趣了! 10 | 11 | ## 关于本项目 12 | 13 | 本项目旨在帮助你在家制作自己的硬件太鼓。 14 | 15 | **该程序仅供个人和非商业用途。** 16 | 17 | ## 你需要准备 18 | 19 | 1. 一个Arduino Micro/Leonardo (ATmega32U4) 板或一个Arduino Nano ESP (ESP32) 板。 20 | 21 | 大多数ATmega32U4板都可以工作,但你需要验证它们是否支持键盘模拟;像Arduino Uno这样的ATmega328P板则不行。 22 | 23 | 强烈推荐使用ESP32,因为它比ATmega32U4强大得多。该项目使用了ESP32-WROOM-32板。 24 | 25 | 2. 4个压电传感器。 26 | 27 | 3. 8个100kΩ电阻。 28 | 29 | 4. (可选)4个桥式整流器芯片,比如[DB107](https://www.diodes.com/assets/Datasheets/products_inactive_data/ds21211_R5.pdf)(详见附加说明部分)。 30 | 31 | 5. (可选)一些红色和蓝色的LED灯。 32 | 33 | 6. 必要的电子组件(面包板、LED灯、跳线等)。 34 | 35 | 7. 木板和切割工具(仅在你需要从头开始制作实体太鼓时使用)。如果你有市售太鼓或大力鼓Lv.5,可以直接使用。 36 | 37 | ## 制作控制器的步骤 38 | 39 | 1. 制作鼓,并将4个压电传感器牢固地粘贴在鼓上。参考图片以了解传感器的首选位置。 40 | 41 | ![传感器设置](./images/piezo_locations.png) 42 | 43 | 2. 按照以下方案将压电传感器和其他组件连接到控制器(压电传感器的极性无关紧要); 44 | 45 | 以下方案适用于Arduino Micro板。如果你使用不同的板,请参考其文档了解连接信息。 46 | 47 | ![控制器方案](./images/scheme.png) 48 | 49 | 如果你选择添加桥式整流器,请使用以下方案: 50 | 51 | ![带桥式整流器的控制器方案](./images/scheme_bridge.png) 52 | 53 | 3. 将固件刷写到板上。 54 | 55 | 你可能需要微调一些参数,如`SAMPLE_CACHE_LENGTH`、`HIT_THRES`、`RESET_THRES`和`sensitivity`。详见下一节。 56 | 57 | 4. 玩得开心! 58 | 59 | ## 调整参数 60 | 61 | 1. 击打和重置阈值 62 | 63 | 设置`DEBUG 1`(这会禁用键盘输出并从串行端口发送信号值),刷写固件,在鼓的4个区域之一上滚动,然后从串行监视器的输出中查看图表。击打阈值应低于你在鼓上的最重击打,重置阈值应高于连续击打之间的低谷。重置值也应低于击打值。 64 | 65 | 为剩下的3个区域重复此过程,并找到适合所有区域的最佳值。 66 | 67 | ![设置击打和重置值](./images/tune_hit_reset.png) 68 | 69 | 2. 采样长度 70 | 71 | 为了最大化运行速度,`cache.h`库已优化为支持2的幂次方窗口大小的`SAMPLE_CACHE_LENGTH`。这意味着2、8、16、32等。实际上,对于Arduino来说16是最佳值,但如果你有一个能以至少4000Hz或更高速度采样输入的强大微控制器,你可以将值改为32,以获得更平滑(换句话说,更少噪声)的曲线。 72 | 73 | 3. 灵敏度 74 | 75 | 并非所有压电传感器都是相同的,由于安装错误,4个传感器捕获的信号可能有显著差异。灵敏度值是用来规范这些差异的乘数。在以下示例中,右边的don区域产生的值比其余3个区域高得多,所以你可以调整`sensitivity`为`{1.0, 1.0, 0.5, 1.0}`来解决这个问题。 76 | 77 | ![设置灵敏度值](./images/tune_sensitivities.png) 78 | 79 | 请注意,传感器的安装非常关键。你应该确保传感器牢固地贴在木头上,并且位置适当。 80 | 81 | ## 附加说明 82 | 83 | 1. 为什么使用桥式整流器 84 | 85 | 如果不对压电传感器的电压进行偏置,它们的输出电压范围约为-5V到+5V。然而,模拟输入的ADC只接受正电压值(ESP32为0-3.3V,ATmega32U4为0-5V)。当它们接收到负电压时,通常会被简单地截断为0。 86 | 87 | 对于普通的电子鼓来说,这通常没问题,因为我们只是失去了一半的输入能量,而这并不影响我们计算击打时间的方式。但是对于*太鼓*鼓来说,特别是像ATmega32U4这样的慢处理器,可能会造成问题。 88 | 89 | 在太鼓鼓中,所有4个振动部件都连接在一起,这意味着如果你击打左边的don,处理器也会接收到左边的kat、右边的don和右边的kat的信号。如果左边的don压电传感器一开始就产生负电压,并被ADC截断,它将导致约3到4毫秒的轻微“延迟”,处理器可能会错误地将这次击打视为右边的don、左边的kat甚至右边的kat,取决于哪个发送了最高的正值。 90 | 91 | 使用桥式整流器,所有负值都转换为正值。换句话说,就像`abs()`函数,确保我们不会丢失任何负电压。 92 | 93 | ![为什么使用桥式整流器](./images/bridge_signal.png) 94 | 95 | # Taiko Controller - 模拟输入模式 96 | 97 | 使用ESP32-S2或ESP32-S3控制器,代替键盘仿真,鼓控制器可以作为游戏手柄工作,并将其轴值发送给游戏(游戏也必须支持模拟输入)。这样,游戏可以识别击打的不同力度级别。 98 | 99 | 如果您更喜欢使用Arduino Micro/Leonardo板,请参考[Arduino XInput库](https://github.com/dmadison/ArduinoXInput)来实现游戏手柄。 100 | 101 | ## 你需要什么 102 | 103 | 1. 制作你的鼓或使用Taiko Force Lv.5。详情请查看主分支。 104 | 105 | 2. 将`ESP32-S3-Analog/ESP32-S3-Analog.ino`刷写到你的控制器。 106 | 107 | 3. 一个能正常运行的***游戏***,并进行以下修改: 108 | 109 | - 备份并替换游戏文件夹中的`bnusio.dll`文件,使用这里`extra/`文件夹中的文件。 110 | 111 | 这个文件是从[这个仓库](https://github.com/ShikyC/TaikoArcadeLoader/tree/Refactor)编译的,如果你愿意,你也可以自己编译。 112 | 113 | *这个修改过的库只适用于特定版本的游戏。如果它破坏了你的游戏,请克隆[原始仓库](https://github.com/BroGamer4256/TaikoArcadeLoader),进行相应的修改,并编译它。* 114 | 115 | - 打开游戏文件夹中的`gamecontrollerdb.txt`文件,并在`#Windows`下添加一条条目: 116 | 117 | `030052a8694800006948000000000000,Taiko Controller,-leftx:-a0,+leftx:+a0,-lefty:-a1,+lefty:+a1,-rightx:-a2,+rightx:+a2,-righty:-a3,+righty:+a3,platform:Windows,` 118 | 119 | 这将告诉游戏我们的ESP32控制器是一个名为“Taiko Controller”的游戏手柄,并将轴映射到标准SDL2库,以便游戏能够识别模拟输入。 120 | 121 | - 打开`config.toml`文件,并在末尾添加以下行: 122 | 123 | ``` 124 | [controller] 125 | analog = true 126 | ``` 127 | 128 | 请注意,使用`analog = true`时,所有键盘鼓输入都将被禁用。对此表示抱歉,但它需要进一步重构才能同时工作。如果你想切换回键盘输入,请设置`analog = false`。 129 | 130 | 4. 开始游玩咚! 131 | 132 | ## 在线信号可视化工具 133 | 134 | 如果您发现通过串行端口的通信进行调试并找到最佳灵敏度值非常不便,您可以使用这个[太鼓信号可视化工具](https://shiky.me/taiko)。这是一个简单的工具,我创建它是为了显示太鼓控制器(仅限ESP32-Analog版本)的输出。 135 | 136 | 只需将板连接到计算机(使用USB端口,不使用串行端口),波形就会显示在图表中。因此,您无需开启调试模式或使用`Serial.print()`命令来获取调整灵敏度所需的值。 137 | 138 | ### 如何工作 139 | 140 | 控制器的模拟版本作为游戏手柄模拟,并将击打力量值作为左右摇杆的轴值发送到游戏中。通过内置的`Gamepad` API,直接读取值非常容易。为了防止工具读取其他游戏手柄,控制器必须命名为`Taiko Controller (Vendor: 4869 Product: 4869)`,因此请不要更改固件中的`pID`和`vID`值。 141 | 142 | ![太鼓测试工具](./images/taiko_test.webp) 143 | -------------------------------------------------------------------------------- /images/banner-taiko.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/banner-taiko.png -------------------------------------------------------------------------------- /images/bridge_signal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/bridge_signal.png -------------------------------------------------------------------------------- /images/piezo_locations.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/piezo_locations.png -------------------------------------------------------------------------------- /images/scheme.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/scheme.png -------------------------------------------------------------------------------- /images/scheme_bridge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/scheme_bridge.png -------------------------------------------------------------------------------- /images/taiko_test.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/taiko_test.webp -------------------------------------------------------------------------------- /images/tune_hit_reset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/tune_hit_reset.png -------------------------------------------------------------------------------- /images/tune_sensitivities.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShikyC/Taiko-Drum-Controller-Arduino/fdb968f02c782d30eb28a0c0bd03e8bd52e20d5f/images/tune_sensitivities.png --------------------------------------------------------------------------------