From c3d9294019d1b54d29ab12f3974c0f5777d23087 Mon Sep 17 00:00:00 2001 From: OniriCorpe Date: Wed, 11 Jun 2025 22:41:29 +0200 Subject: [PATCH 1/2] switch support: 1st draft (not working for now) --- ESP32-S3/ESP32-S3.ino | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/ESP32-S3/ESP32-S3.ino b/ESP32-S3/ESP32-S3.ino index e25c83b..60d9c40 100644 --- a/ESP32-S3/ESP32-S3.ino +++ b/ESP32-S3/ESP32-S3.ino @@ -6,8 +6,8 @@ // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you // changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds -#define HIT_THRES 1750 -#define RESET_THRES 200 +#define HIT_THRES 300 +#define RESET_THRES 70 // Sampling period in μs, e.g., 500μs = 0.5ms = 2000Hz #define SAMPLING_PERIOD 500 @@ -36,10 +36,21 @@ #define R_DON_KEY 'j' #define R_KAT_KEY 'k' +// Switch controller output for each channel +#define L_DON_KEY_NS NSGAMEPAD_DPAD_RIGHT +#define L_KAT_KEY_NS NSButton_LeftTrigger +#define R_DON_KEY_NS NSButton_A +#define R_KAT_KEY_NS NSButton_RightTrigger + // Enable debug mode to view analog input values from the Serial // Enabling this also disables the keyboard simulation #define DEBUG 0 +// 0 = keyboard; 1 = switch controller +#define controller_mode 1 + +#include "switch_ESP32.h" +NSGamepad Gamepad; #include "USB.h" #include "USBHIDKeyboard.h" #include "cache.h" @@ -56,6 +67,7 @@ unsigned long triggeredTime[CHANNELS]; const byte inPins[] = {L_DON_IN, L_KAT_IN, R_DON_IN, R_KAT_IN}; const byte outPins[] = {L_DON_LED, L_KAT_LED, R_DON_LED, R_KAT_LED}; const char outKeys[] = {L_DON_KEY, L_KAT_KEY, R_DON_KEY, R_KAT_KEY}; +const uint8_t outKeysNS[] = {L_DON_KEY_NS, L_KAT_KEY_NS, R_DON_KEY_NS, R_KAT_KEY_NS}; float sensitivities[] = {L_DON_SENS, L_KAT_SENS, R_DON_SENS, R_KAT_SENS}; short maxIndex; @@ -76,7 +88,11 @@ void setup() { maxPower = 0; lastTime = micros(); #if !DEBUG - Keyboard.begin(); + if (controller_mode) { + Gamepad.begin(); + } else { + Keyboard.begin(); + } USB.begin(); #endif } @@ -108,9 +124,14 @@ void loop() { if (!triggered && maxPower >= HIT_THRES) { triggered = true; digitalWrite(outPins[maxIndex], HIGH); -#if !DEBUG - Keyboard.write(outKeys[maxIndex]); -#endif + #if !DEBUG + if (controller_mode) { + Gamepad.press(outKeysNS[maxIndex]); + Gamepad.loop(); + } else { + Keyboard.write(outKeys[maxIndex]); + } + #endif } #if DEBUG Serial.print("\n"); From 85e15a0955f831e1fd5af2b814bd147888e87eb6 Mon Sep 17 00:00:00 2001 From: OniriCorpe Date: Thu, 12 Jun 2025 21:04:43 +0200 Subject: [PATCH 2/2] =?UTF-8?q?switch=20support:=20it=20somewhat=20works?= =?UTF-8?q?=20=E2=84=A2=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ESP32-S3/ESP32-S3.ino | 45 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/ESP32-S3/ESP32-S3.ino b/ESP32-S3/ESP32-S3.ino index 60d9c40..b127cdb 100644 --- a/ESP32-S3/ESP32-S3.ino +++ b/ESP32-S3/ESP32-S3.ino @@ -6,8 +6,8 @@ // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you // changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds -#define HIT_THRES 300 -#define RESET_THRES 70 +#define HIT_THRES 400 +#define RESET_THRES 80 // Sampling period in μs, e.g., 500μs = 0.5ms = 2000Hz #define SAMPLING_PERIOD 500 @@ -37,9 +37,10 @@ #define R_KAT_KEY 'k' // Switch controller output for each channel -#define L_DON_KEY_NS NSGAMEPAD_DPAD_RIGHT +// this default config should work for all the 3 game settings +#define L_DON_KEY_NS NSGAMEPAD_DPAD_DOWN #define L_KAT_KEY_NS NSButton_LeftTrigger -#define R_DON_KEY_NS NSButton_A +#define R_DON_KEY_NS NSButton_B #define R_KAT_KEY_NS NSButton_RightTrigger // Enable debug mode to view analog input values from the Serial @@ -87,20 +88,23 @@ void setup() { maxIndex = -1; maxPower = 0; lastTime = micros(); -#if !DEBUG - if (controller_mode) { - Gamepad.begin(); - } else { - Keyboard.begin(); - } - USB.begin(); -#endif + #if !DEBUG + if (controller_mode) { + Gamepad.begin(); + } else { + Keyboard.begin(); + } + USB.begin(); + #endif } void loop() { if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) { triggered = false; digitalWrite(outPins[maxIndex], LOW); + if (controller_mode) { + Gamepad.releaseAll(); + } maxIndex = -1; maxPower = 0; } @@ -115,10 +119,10 @@ void loop() { maxIndex = i; } lastPower[i] = power[i]; -#if DEBUG + #if DEBUG Serial.print(power[i]); Serial.print(" "); -#endif + #endif } if (!triggered && maxPower >= HIT_THRES) { @@ -126,16 +130,21 @@ void loop() { digitalWrite(outPins[maxIndex], HIGH); #if !DEBUG if (controller_mode) { - Gamepad.press(outKeysNS[maxIndex]); + // special case for the DPAD + if (maxIndex == 0) { + Gamepad.dPad(outKeysNS[maxIndex]); + } else { + Gamepad.press(outKeysNS[maxIndex]); + } Gamepad.loop(); } else { Keyboard.write(outKeys[maxIndex]); } #endif } -#if DEBUG - Serial.print("\n"); -#endif + #if DEBUG + Serial.print("\n"); + #endif unsigned int frameTime = micros() - lastTime; if (frameTime < SAMPLING_PERIOD) {