switch support: it somewhat works ™️

This commit is contained in:
OniriCorpe 2025-06-12 21:04:43 +02:00
parent c3d9294019
commit 85e15a0955

View file

@ -6,8 +6,8 @@
// The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you
// changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds // changed SAMPLE_CACHE_LENGTH, you should also adjust thresholds
#define HIT_THRES 300 #define HIT_THRES 400
#define RESET_THRES 70 #define RESET_THRES 80
// Sampling period in μs, e.g., 500μs = 0.5ms = 2000Hz // Sampling period in μs, e.g., 500μs = 0.5ms = 2000Hz
#define SAMPLING_PERIOD 500 #define SAMPLING_PERIOD 500
@ -37,9 +37,10 @@
#define R_KAT_KEY 'k' #define R_KAT_KEY 'k'
// Switch controller output for each channel // 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 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 #define R_KAT_KEY_NS NSButton_RightTrigger
// Enable debug mode to view analog input values from the Serial // Enable debug mode to view analog input values from the Serial
@ -87,20 +88,23 @@ void setup() {
maxIndex = -1; maxIndex = -1;
maxPower = 0; maxPower = 0;
lastTime = micros(); lastTime = micros();
#if !DEBUG #if !DEBUG
if (controller_mode) { if (controller_mode) {
Gamepad.begin(); Gamepad.begin();
} else { } else {
Keyboard.begin(); Keyboard.begin();
} }
USB.begin(); USB.begin();
#endif #endif
} }
void loop() { void loop() {
if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) { if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) {
triggered = false; triggered = false;
digitalWrite(outPins[maxIndex], LOW); digitalWrite(outPins[maxIndex], LOW);
if (controller_mode) {
Gamepad.releaseAll();
}
maxIndex = -1; maxIndex = -1;
maxPower = 0; maxPower = 0;
} }
@ -115,10 +119,10 @@ void loop() {
maxIndex = i; maxIndex = i;
} }
lastPower[i] = power[i]; lastPower[i] = power[i];
#if DEBUG #if DEBUG
Serial.print(power[i]); Serial.print(power[i]);
Serial.print(" "); Serial.print(" ");
#endif #endif
} }
if (!triggered && maxPower >= HIT_THRES) { if (!triggered && maxPower >= HIT_THRES) {
@ -126,16 +130,21 @@ void loop() {
digitalWrite(outPins[maxIndex], HIGH); digitalWrite(outPins[maxIndex], HIGH);
#if !DEBUG #if !DEBUG
if (controller_mode) { 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(); Gamepad.loop();
} else { } else {
Keyboard.write(outKeys[maxIndex]); Keyboard.write(outKeys[maxIndex]);
} }
#endif #endif
} }
#if DEBUG #if DEBUG
Serial.print("\n"); Serial.print("\n");
#endif #endif
unsigned int frameTime = micros() - lastTime; unsigned int frameTime = micros() - lastTime;
if (frameTime < SAMPLING_PERIOD) { if (frameTime < SAMPLING_PERIOD) {