From 87a96ab0f81f8df782549beed76567bb50d7e88a Mon Sep 17 00:00:00 2001 From: MickGyver Date: Tue, 2 Jun 2020 08:45:15 +0300 Subject: [PATCH] Optimisations made to some adapters - SEGA two player adapter optimisations. - NES/SNES adapter optimisations. - Two ports default for Saturn adapter, increased default SELECT_PAUSE. --- SNESControllersUSB/SNESControllersUSB.ino | 77 ++++++- SaturnControllerUSB/SaturnControllerUSB.ino | 10 +- SegaControllerUSB/SegaControllerUSB.ino | 1 - SegaTwoControllersUSB/SegaControllers32U4.cpp | 195 +++++++++++++----- SegaTwoControllersUSB/SegaControllers32U4.h | 20 +- .../SegaTwoControllersUSB.ino | 23 +-- 6 files changed, 229 insertions(+), 97 deletions(-) diff --git a/SNESControllersUSB/SNESControllersUSB.ino b/SNESControllersUSB/SNESControllersUSB.ino index fc6e4bb..b975855 100644 --- a/SNESControllersUSB/SNESControllersUSB.ino +++ b/SNESControllersUSB/SNESControllersUSB.ino @@ -27,13 +27,13 @@ // Additionally serial number is used to differentiate arduino projects to have different button maps! const char *gp_serial = "NES/SNES to USB"; -#define GAMEPAD_COUNT 2 // NOTE: No more than TWO gamepads are possible at the moment due to a USB HID issue. -#define GAMEPAD_COUNT_MAX 4 // NOTE: For some reason, can't have more than two gamepads without serial breaking. Can someone figure out why? - // (It has something to do with how Arduino handles HID devices) -#define BUTTON_READ_DELAY 30 // Delay between button reads in µs -#define MICROS_LATCH 12 // 12µs according to specs (8 seems to work fine) -#define MICROS_CLOCK 6 // 6µs according to specs (4 seems to work fine) -#define MICROS_PAUSE 6 // 6µs according to specs (4 seems to work fine) +#define GAMEPAD_COUNT 2 // NOTE: No more than TWO gamepads are possible at the moment due to a USB HID issue. +#define GAMEPAD_COUNT_MAX 4 // NOTE: For some reason, can't have more than two gamepads without serial breaking. Can someone figure out why? + // (It has something to do with how Arduino handles HID devices) +#define BUTTON_READ_DELAY 20 // Delay between button reads in µs +#define MICROS_LATCH 10 // 12µs according to specs (8 seems to work fine) +#define MICROS_CLOCK 5 // 6µs according to specs (4 seems to work fine) +#define MICROS_PAUSE 5 // 6µs according to specs (4 seems to work fine) #define UP 0x01 #define DOWN 0x02 @@ -55,6 +55,13 @@ const char *gp_serial = "NES/SNES to USB"; // D1 (GP3: DATA) A2 (PF5, Gamepad 3, not currently used) // D1 (GP4: DATA) A3 (PF4, Gamepad 4, not currently used) +enum ControllerType { + NONE, + NES, + SNES, + NTT +}; + // Set up USB HID gamepads Gamepad_ Gamepad[GAMEPAD_COUNT]; @@ -62,6 +69,7 @@ Gamepad_ Gamepad[GAMEPAD_COUNT]; uint32_t buttons[GAMEPAD_COUNT_MAX] = {0,0,0,0}; uint32_t buttonsPrev[GAMEPAD_COUNT_MAX] = {0,0,0,0}; uint8_t gpBit[GAMEPAD_COUNT_MAX] = {B10000000,B01000000,B00100000,B00010000}; +ControllerType controllerType[GAMEPAD_COUNT_MAX] = {NONE,NONE}; uint32_t btnBits[32] = {0x10,0x40,0x400,0x800,UP,DOWN,LEFT,RIGHT,0x20,0x80,0x100,0x200, // Standard SNES controller 0x10000000,0x20000000,0x40000000,0x80000000,0x1000,0x2000,0x4000,0x8000, // NTT Data Keypad (NDK10) 0x10000,0x20000,0x40000,0x80000,0x100000,0x200000,0x400000,0x800000, @@ -81,12 +89,15 @@ void setup() // Setup data pins (A0-A3 or PF7-PF4) DDRF &= ~B11110000; // inputs PORTF |= B11110000; // enable internal pull-ups + + delay(500); + detectControllerTypes(); } void loop() { while(1) { // See if enough time has passed since last button read - if(micros() - microsButtons > BUTTON_READ_DELAY) + if((micros() - microsButtons) > BUTTON_READ_DELAY) { // Pulse latch sendLatch(); @@ -101,14 +112,14 @@ void loop() { while(1) // Check gamepad type for(gp=0; gp> 5; - Gamepad[gp]._GamepadReport.Y = ((currentState[gp] & SC_BTN_DOWN) >> SC_BIT_DOWN) - ((currentState[gp] & SC_BTN_UP) >> SC_BIT_UP); - Gamepad[gp]._GamepadReport.X = ((currentState[gp] & SC_BTN_RIGHT) >> SC_BIT_RIGHT) - ((currentState[gp] & SC_BTN_LEFT) >> SC_BIT_LEFT); + Gamepad[gp]._GamepadReport.buttons = controllers.currentState[gp] >> 5; + Gamepad[gp]._GamepadReport.Y = ((controllers.currentState[gp] & SC_BTN_DOWN) >> SC_BIT_DOWN) - ((controllers.currentState[gp] & SC_BTN_UP) >> SC_BIT_UP); + Gamepad[gp]._GamepadReport.X = ((controllers.currentState[gp] & SC_BTN_RIGHT) >> SC_BIT_RIGHT) - ((controllers.currentState[gp] & SC_BTN_LEFT) >> SC_BIT_LEFT); Gamepad[gp].send(); - lastState[gp] = currentState[gp]; + lastState[gp] = controllers.currentState[gp]; } }