From 362c9d136da174a80986112d689689c5c73bc6c8 Mon Sep 17 00:00:00 2001 From: MickGyver Date: Fri, 4 Jun 2021 16:52:45 +0300 Subject: [PATCH] Fixed a bug for the SNES adapter (+small optimization) --- SNESControllersUSB/SNESControllersUSB.ino | 50 +++++++++++-------- .../SNESNTTControllersUSB.ino | 2 +- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/SNESControllersUSB/SNESControllersUSB.ino b/SNESControllersUSB/SNESControllersUSB.ino index 95d764f..37f1a06 100644 --- a/SNESControllersUSB/SNESControllersUSB.ino +++ b/SNESControllersUSB/SNESControllersUSB.ino @@ -27,15 +27,18 @@ // Additionally serial number is used to differentiate arduino projects to have different button maps! const char *gp_serial = "NES/SNES to USB"; -#define DEBUG +//#define DEBUG #define GAMEPAD_COUNT 2 // NOTE: To have more than 2 two gamepads you need to disable the CDC of the Arduino, there is a specific project for that. #define GAMEPAD_COUNT_MAX 2 #define BUTTON_READ_DELAY 20 // Delay between button reads in µs -#define CYCLES_LATCH 128 // 12µs according to specs (8 seems to work fine) (1 cycle @ 16MHz takes 62.5ns so 62.5ns * 128 = 8000ns = 8µs) -#define CYCLES_CLOCK 64 // 6µs according to specs (4 seems to work fine) -#define CYCLES_PAUSE 64 // 6µs according to specs (4 seems to work fine) +#define CYCLES_LATCH 128 // 128 12µs according to specs (8 seems to work fine) (1 cycle @ 16MHz takes 62.5ns so 62.5ns * 128 = 8000ns = 8µs) +#define CYCLES_CLOCK 64 // 6µs according to specs (4 seems to work fine) +#define CYCLES_PAUSE1 64 // 6µs according to specs (4 seems to work fine) +#define CYCLES_PAUSE2 58 // 6µs according to specs (4 seems to work fine) +#define BUTTONS 0 +#define AXES 1 #define UP 0x01 #define DOWN 0x02 #define LEFT 0x04 @@ -43,6 +46,9 @@ const char *gp_serial = "NES/SNES to USB"; #define DELAY_CYCLES(n) __builtin_avr_delay_cycles(n) +inline void sendLatch() __attribute__((always_inline)); +inline void sendClock() __attribute__((always_inline)); + // Wire it all up according to the following table: // // NES SNES Arduino Pro Micro @@ -93,15 +99,13 @@ void setup() // Setup data pins A0-A3 (PF7-PF4) DDRF &= ~B11110000; // inputs PORTF |= B11110000; // enable internal pull-ups - DDRC &= ~B01000000; // input - PORTC |= B01000000; // enable internal pull-up #ifdef DEBUG Serial.begin(115200); delay(2000); #endif - delay(3000); + delay(300); detectControllerTypes(); } @@ -118,10 +122,16 @@ void loop() { while(1) // Pulse latch sendLatch(); + for(gp=0; gp> 1) - (buttons[gp][1] & UP); - Gamepad[gp]._GamepadReport.X = ((buttons[gp][1] & RIGHT) >> 3) - ((buttons[gp][1] & LEFT) >> 2); - buttonsPrev[gp][0] = buttons[gp][0]; - buttonsPrev[gp][1] = buttons[gp][1]; + Gamepad[gp]._GamepadReport.buttons = buttons[gp][BUTTONS]; + Gamepad[gp]._GamepadReport.Y = ((buttons[gp][AXES] & DOWN) >> 1) - (buttons[gp][AXES] & UP); + Gamepad[gp]._GamepadReport.X = ((buttons[gp][AXES] & RIGHT) >> 3) - ((buttons[gp][AXES] & LEFT) >> 2); + buttonsPrev[gp][BUTTONS] = buttons[gp][BUTTONS]; + buttonsPrev[gp][AXES] = buttons[gp][AXES]; Gamepad[gp].send(); } } @@ -207,7 +217,7 @@ void sendLatch() PORTD |= B00000010; // Set HIGH DELAY_CYCLES(CYCLES_LATCH); PORTD &= ~B00000010; // Set LOW - DELAY_CYCLES(CYCLES_PAUSE); + DELAY_CYCLES(CYCLES_PAUSE2); } void sendClock() @@ -216,5 +226,5 @@ void sendClock() PORTD |= B10000001; // Set HIGH DELAY_CYCLES(CYCLES_CLOCK); PORTD &= ~B10000001; // Set LOW - DELAY_CYCLES(CYCLES_PAUSE); + DELAY_CYCLES(CYCLES_PAUSE1); } diff --git a/SNESNTTControllersUSB/SNESNTTControllersUSB.ino b/SNESNTTControllersUSB/SNESNTTControllersUSB.ino index 4807bd1..94b852f 100644 --- a/SNESNTTControllersUSB/SNESNTTControllersUSB.ino +++ b/SNESNTTControllersUSB/SNESNTTControllersUSB.ino @@ -27,7 +27,7 @@ // Additionally serial number is used to differentiate arduino projects to have different button maps! const char *gp_serial = "NES/SNES to USB"; -#define DEBUG +//#define DEBUG #define GAMEPAD_COUNT 1 // NOTE: To have more than 2 two gamepads you need to disable the CDC of the Arduino. #define GAMEPAD_COUNT_MAX 4