Skip to content

Commit

Permalink
Fix up Last Win SOCD logic
Browse files Browse the repository at this point in the history
  • Loading branch information
FeralAI committed Aug 13, 2021
1 parent 041f475 commit 62d0ced
Show file tree
Hide file tree
Showing 12 changed files with 839 additions and 823 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .assets/images/button_label_template.xcf
Binary file not shown.
1,365 changes: 683 additions & 682 deletions .assets/updater/vsFIGHTER-Firmware.hex

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#0D3506",
"titleBar.activeBackground": "#134B09",
"activityBar.background": "#062635",
"titleBar.activeBackground": "#09414b",
"titleBar.activeForeground": "#F0FDEE"
},
"files.associations": {
Expand Down
34 changes: 17 additions & 17 deletions DS3Report.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

#include <stdint.h>

#define DS3_BUTTON_SELECT_MASK 0x01
#define DS3_BUTTON_L3_MASK 0x02
#define DS3_BUTTON_R3_MASK 0x04
#define DS3_BUTTON_START_MASK 0x08
#define DS3_DPAD_UP_MASK 0x10
#define DS3_DPAD_RIGHT_MASK 0x20
#define DS3_DPAD_DOWN_MASK 0x40
#define DS3_DPAD_LEFT_MASK 0x80
#define DS3_BUTTON_L2_MASK 0x100
#define DS3_BUTTON_R1_MASK 0x200
#define DS3_BUTTON_L1_MASK 0x400
#define DS3_BUTTON_R2_MASK 0x800
#define DS3_BUTTON_TRIANGLE_MASK 0x1000
#define DS3_BUTTON_CIRCLE_MASK 0x2000
#define DS3_BUTTON_CROSS_MASK 0x4000
#define DS3_BUTTON_SQUARE_MASK 0x8000
#define DS3_BUTTON_PS_MASK 0x10000
#define DS3_BUTTON_SELECT_MASK (1U << 0)
#define DS3_BUTTON_L3_MASK (1U << 1)
#define DS3_BUTTON_R3_MASK (1U << 2)
#define DS3_BUTTON_START_MASK (1U << 3)
#define DS3_DPAD_UP_MASK (1U << 4)
#define DS3_DPAD_RIGHT_MASK (1U << 5)
#define DS3_DPAD_DOWN_MASK (1U << 6)
#define DS3_DPAD_LEFT_MASK (1U << 7)
#define DS3_BUTTON_L2_MASK (1U << 8)
#define DS3_BUTTON_R1_MASK (1U << 9)
#define DS3_BUTTON_L1_MASK (1U << 10)
#define DS3_BUTTON_R2_MASK (1U << 11)
#define DS3_BUTTON_TRIANGLE_MASK (1U << 12)
#define DS3_BUTTON_CIRCLE_MASK (1U << 13)
#define DS3_BUTTON_CROSS_MASK (1U << 14)
#define DS3_BUTTON_SQUARE_MASK (1U << 15)
#define DS3_BUTTON_PS_MASK (1U << 16)

typedef struct {
uint8_t reportId;
Expand Down
2 changes: 1 addition & 1 deletion Descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ enum StringDescriptors_t {
// HID Endpoint Size
// The Switch -needs- this to be 64.
// The Wii U is flexible, allowing us to use the default of 8 (which did not match the original Hori descriptors).
#define JOYSTICK_EPSIZE_SWITCH 64
#define JOYSTICK_EPSIZE_DS3 64
#define JOYSTICK_EPSIZE_SWITCH 64
#define JOYSTICK_EPSIZE_XINPUT 20
// Descriptor Header Type - HID Class HID Descriptor
#define DTYPE_HID 0x21
Expand Down
25 changes: 8 additions & 17 deletions Gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,14 @@ void Gamepad::read() { }
void Gamepad::setup() { }

void Gamepad::update() {
if (areStatesEqual(currentState, previousState)) {
// No state change, just maintain USB connection
USB_USBTask();
} else {
// Gamepad state changed, send inputs to host
switch (inputMode) {
case InputMode::XINPUT:
sendXInputReport(&getXInputReport());
break;
case InputMode::SWITCH:
sendSwitchReport(&getSwitchReport());
break;
// case InputMode::DUALSHOCK3:
// sendDS3Report(&getDS3Report());
// break;
}
switch (inputMode) {
case InputMode::XINPUT:
sendXInputReport(&getXInputReport());
break;
case InputMode::SWITCH:
sendSwitchReport(&getSwitchReport());
break;
}

previousState = currentState;
memcpy(&previousState, &currentState, sizeof(GamepadState));
}
2 changes: 1 addition & 1 deletion Gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "DS3Report.h"
#include "SwitchReport.h"
#include "XInputReport.h"
#include "PersistentStorage.h"
#include "GamepadStorage.h"

#ifndef DEBOUNCE_MILLIS
#define DEBOUNCE_MILLIS 0
Expand Down
145 changes: 72 additions & 73 deletions GamepadState.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,6 @@
#define GAMEPAD_AXIS_MID 0x7FFF
#define GAMEPAD_AXIS_MAX 0xFFFF

static const uint16_t PROGMEM GAMEPAD_DPAD_MASKS[] = {
GAMEPAD_DPAD_UP,
GAMEPAD_DPAD_DOWN,
GAMEPAD_DPAD_LEFT,
GAMEPAD_DPAD_RIGHT,
};

static const uint16_t PROGMEM GAMEPAD_BUTTON_MASKS[] = {
GAMEPAD_BUTTON_01,
GAMEPAD_BUTTON_02,
GAMEPAD_BUTTON_03,
GAMEPAD_BUTTON_04,
GAMEPAD_BUTTON_05,
GAMEPAD_BUTTON_06,
GAMEPAD_BUTTON_07,
GAMEPAD_BUTTON_08,
GAMEPAD_BUTTON_09,
GAMEPAD_BUTTON_10,
GAMEPAD_BUTTON_11,
GAMEPAD_BUTTON_12,
GAMEPAD_BUTTON_13,
GAMEPAD_BUTTON_14,
GAMEPAD_BUTTON_15,
GAMEPAD_BUTTON_16,
};

typedef enum {
DIGITAL,
LEFT_ANALOG,
Expand All @@ -70,6 +44,14 @@ typedef enum {
LAST_INPUT, // U>D=D, L>R=R (Last Input Priority, aka Last Win)
} SOCDMode;

typedef enum {
DIRECTION_NONE,
DIRECTION_UP,
DIRECTION_DOWN,
DIRECTION_LEFT,
DIRECTION_RIGHT
} Direction;

typedef enum {
NONE = 0x00,
DPAD_DIGITAL = 0x01,
Expand Down Expand Up @@ -97,23 +79,18 @@ typedef struct {
uint8_t rt; // Right analog trigger
} GamepadState;

/**
* Var to track the last dpad state for LAST_INPUT SOCD
*/
static uint8_t lastDpadValue;

/**
* Check two GamepadStates for equality
*/
__attribute__((always_inline)) inline bool areStatesEqual(GamepadState a, GamepadState b) {
return a.dpad == b.dpad
&& a.buttons == b.buttons
&& a.lt == b.lt
&& a.rt == b.rt
&& a.lx == b.lx
&& a.ly == b.ly
&& a.rx == b.rx
&& a.ry == b.ry;
__attribute__((always_inline)) inline static bool areStatesEqual(GamepadState *a, GamepadState *b) {
return a->dpad == b->dpad
&& a->buttons == b->buttons
&& a->lt == b->lt
&& a->rt == b->rt
&& a->lx == b->lx
&& a->ly == b->ly
&& a->rx == b->rx
&& a->ry == b->ry;
}

/**
Expand Down Expand Up @@ -147,55 +124,77 @@ __attribute__((always_inline)) inline uint16_t dpadToAnalogY(uint8_t dpad) {
/**
* Run the SOCD cleaner
*/
__attribute__((always_inline)) inline uint8_t runSOCD(SOCDMode mode, uint8_t dpadValues) {
uint8_t newValues = dpadValues;
static uint8_t runSOCD(SOCDMode mode, uint8_t dpadValues) {
static Direction lastUD = Direction::DIRECTION_NONE;
static Direction lastLR = Direction::DIRECTION_NONE;
uint8_t newValues = 0;

switch (mode) {

case HITBOX:
if ((dpadValues & (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) == (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN))
newValues &= ~(GAMEPAD_DPAD_DOWN);
newValues |= GAMEPAD_DPAD_UP;
else
newValues &= (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN);
switch (dpadValues & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) {
case GAMEPAD_DPAD_LEFT:
newValues |= GAMEPAD_DPAD_LEFT;
break;
case GAMEPAD_DPAD_RIGHT:
newValues |= GAMEPAD_DPAD_RIGHT;
break;
}
if ((dpadValues & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) == (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT))
newValues &= ~(GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT);
break;

case NEUTRAL:
if ((dpadValues & (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) == (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN))
newValues &= ~(GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN);
if ((dpadValues & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) == (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT))
newValues &= ~(GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT);
switch (dpadValues & (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) {
case GAMEPAD_DPAD_UP:
newValues |= GAMEPAD_DPAD_UP;
break;
case GAMEPAD_DPAD_DOWN:
newValues |= GAMEPAD_DPAD_DOWN;
break;
}
switch (dpadValues & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) {
case GAMEPAD_DPAD_LEFT:
newValues |= GAMEPAD_DPAD_LEFT;
break;
case GAMEPAD_DPAD_RIGHT:
newValues |= GAMEPAD_DPAD_RIGHT;
break;
}
break;

case LAST_INPUT:
if ((dpadValues & (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) == (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) {
switch (lastDpadValue & (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) {
case GAMEPAD_DPAD_UP:
newValues &= ~(GAMEPAD_DPAD_UP);
break;
case GAMEPAD_DPAD_DOWN:
newValues &= ~(GAMEPAD_DPAD_DOWN);
break;
default:
newValues &= ~(GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN);
break;
}
switch (dpadValues & (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN)) {
case (GAMEPAD_DPAD_UP | GAMEPAD_DPAD_DOWN):
newValues |= (lastUD == Direction::DIRECTION_UP) ? GAMEPAD_DPAD_DOWN : GAMEPAD_DPAD_UP;
break;
case GAMEPAD_DPAD_UP:
newValues |= GAMEPAD_DPAD_UP;
lastUD = Direction::DIRECTION_UP;
break;
case GAMEPAD_DPAD_DOWN:
newValues |= GAMEPAD_DPAD_DOWN;
lastUD = Direction::DIRECTION_DOWN;
break;
}
if ((dpadValues & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) == (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) {
switch (lastDpadValue & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) {
case GAMEPAD_DPAD_LEFT:
newValues &= ~(GAMEPAD_DPAD_LEFT);
break;
case GAMEPAD_DPAD_RIGHT:
newValues &= ~(GAMEPAD_DPAD_RIGHT);
break;
default:
newValues &= ~(GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT);
break;
}
switch (dpadValues & (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT)) {
case (GAMEPAD_DPAD_LEFT | GAMEPAD_DPAD_RIGHT):
newValues |= (lastLR == Direction::DIRECTION_LEFT) ? GAMEPAD_DPAD_RIGHT : GAMEPAD_DPAD_LEFT;
break;
case GAMEPAD_DPAD_LEFT:
newValues |= GAMEPAD_DPAD_LEFT;
lastLR = Direction::DIRECTION_LEFT;
break;
case GAMEPAD_DPAD_RIGHT:
newValues |= GAMEPAD_DPAD_RIGHT;
lastLR = Direction::DIRECTION_RIGHT;
break;
}
lastDpadValue = dpadValues;
break;

}

return newValues;
Expand Down
8 changes: 4 additions & 4 deletions PersistentStorage.h → GamepadStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <EEPROM.h>
#include "GamepadState.h"

class PersistentStorage {
class GamepadStorage {
public:
DpadMode getDpadMode() {
DpadMode mode = DpadMode::DIGITAL;
Expand Down Expand Up @@ -44,18 +44,18 @@ class PersistentStorage {
/**
* Wrapper for "get" call to storage API
*/
template <typename T> T PersistentStorage::get(int index, T &value) {
template <typename T> T GamepadStorage::get(int index, T &value) {
return EEPROM.get(index, value);
}

/**
* Wrapper for "set" call to storage API
*/
template <typename T> T PersistentStorage::set(int index, const T &value) {
template <typename T> T GamepadStorage::set(int index, const T &value) {
return EEPROM.put(index, value);
}
};

static PersistentStorage Storage;
static GamepadStorage Storage;

#endif
Loading

0 comments on commit 62d0ced

Please sign in to comment.