From a2e069228515e40e78d85152d0d565b1cfbc8f4f Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Thu, 26 Jan 2017 23:41:45 -0800 Subject: [PATCH] Fix Inverted left/right stick axes --- lib/inputdev/DolphinSmashAdapter.cpp | 159 ++++++++++++++------------- 1 file changed, 80 insertions(+), 79 deletions(-) diff --git a/lib/inputdev/DolphinSmashAdapter.cpp b/lib/inputdev/DolphinSmashAdapter.cpp index 955ddab..6f82879 100644 --- a/lib/inputdev/DolphinSmashAdapter.cpp +++ b/lib/inputdev/DolphinSmashAdapter.cpp @@ -9,47 +9,41 @@ namespace boo * Reference: https://github.com/ToadKing/wii-u-gc-adapter/blob/master/wii-u-gc-adapter.c */ -DolphinSmashAdapter::DolphinSmashAdapter(DeviceToken* token) -: DeviceBase(token) -{ -} +DolphinSmashAdapter::DolphinSmashAdapter(DeviceToken* token) : DeviceBase(token) {} -DolphinSmashAdapter::~DolphinSmashAdapter() -{ -} +DolphinSmashAdapter::~DolphinSmashAdapter() {} static inline EDolphinControllerType parseType(unsigned char status) { - EDolphinControllerType type = EDolphinControllerType(status) & - (EDolphinControllerType::Normal | EDolphinControllerType::Wavebird); + EDolphinControllerType type = + EDolphinControllerType(status) & (EDolphinControllerType::Normal | EDolphinControllerType::Wavebird); switch (type) { - case EDolphinControllerType::Normal: - case EDolphinControllerType::Wavebird: - return type; - default: - return EDolphinControllerType::None; + case EDolphinControllerType::Normal: + case EDolphinControllerType::Wavebird: + return type; + default: + return EDolphinControllerType::None; } } -static inline EDolphinControllerType -parseState(DolphinControllerState* stateOut, uint8_t* payload, bool& rumble) +static inline EDolphinControllerType parseState(DolphinControllerState* stateOut, uint8_t* payload, bool& rumble) { memset(stateOut, 0, sizeof(DolphinControllerState)); unsigned char status = payload[0]; EDolphinControllerType type = parseType(status); - + rumble = ((status & 0x04) != 0) ? true : false; - + stateOut->m_btns = (uint16_t)payload[1] << 8 | (uint16_t)payload[2]; - - stateOut->m_leftStick[0] = payload[3]; - stateOut->m_leftStick[1] = payload[4]; - stateOut->m_rightStick[0] = payload[5]; - stateOut->m_rightStick[1] = payload[6]; + + stateOut->m_leftStick[0] = payload[4]; + stateOut->m_leftStick[1] = payload[3]; + stateOut->m_rightStick[0] = payload[6]; + stateOut->m_rightStick[1] = payload[5]; stateOut->m_analogTriggers[0] = payload[7]; stateOut->m_analogTriggers[1] = payload[8]; - + return type; } @@ -65,7 +59,7 @@ void DolphinSmashAdapter::transferCycle() size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload)); if (recvSz != 37 || payload[0] != 0x21) return; - //printf("RECEIVED DATA %zu %02X\n", recvSz, payload[0]); + // printf("RECEIVED DATA %zu %02X\n", recvSz, payload[0]); if (!m_callback) return; @@ -73,24 +67,24 @@ void DolphinSmashAdapter::transferCycle() /* Parse controller states */ uint8_t* controller = &payload[1]; uint8_t rumbleMask = 0; - for (int i=0 ; i<4 ; i++, controller += 9) + for (int i = 0; i < 4; i++, controller += 9) { DolphinControllerState state; bool rumble = false; EDolphinControllerType type = parseState(&state, controller, rumble); - if (type != EDolphinControllerType::None && !(m_knownControllers & 1<controllerConnected(i, type); } - else if (type == EDolphinControllerType::None && (m_knownControllers & 1<controllerDisconnected(i); } - if (m_knownControllers & 1<controllerUpdate(i, type, state); - rumbleMask |= rumble ? 1<controllerDisconnected(i); } } @@ -161,21 +155,26 @@ static uint8_t pad_clampregion[8] = {30, 180, 15, 72, 40, 15, 59, 31}; static void pad_clampstick(int8_t& px, int8_t& py, int8_t max, int8_t xy, int8_t min) { - int32_t x, y, signX, signY, d; + int x = px; + int y = py; + int signX; + int signY; + int d; - x = px; - y = py; - - if (x >= 0) + if (x > 0) + { signX = 1; + } else { signX = -1; x = -x; } - if (y >= 0) + if (y > 0) + { signY = 1; + } else { signY = -1; @@ -188,40 +187,41 @@ static void pad_clampstick(int8_t& px, int8_t& py, int8_t max, int8_t xy, int8_t x -= min; if (y <= min) - y = 0; - else - y -= min; - - if (x || y) { - int32_t xx, yy, maxy; - - xx = x * xy; - yy = y * xy; - maxy = max * xy; - if (yy <= xx) - { - d = (x * xy + (y * (max - xy))); - if (maxy < d) - { - x *= maxy / d; - y *= maxy / d; - } - } - else - { - d = (y * xy + (x * (max - xy))); - if (maxy < d) - { - x *= maxy / d; - y *= maxy / d; - } - } - px = int8_t(x * signX); - py = int8_t(y * signY); + y = 0; } else + { + y -= min; + } + + if (x == 0 && y == 0) + { px = py = 0; + return; + } + + if (xy * y <= xy * x) + { + d = xy * x + (max - xy) * y; + if (xy * max < d) + { + x = int8_t(xy * max * x / d); + y = int8_t(xy * max * y / d); + } + } + else + { + d = xy * y + (max - xy) * x; + if (xy * max < d) + { + x = int8_t(xy * max * x / d); + y = int8_t(xy * max * y / d); + } + } + + px = int8_t(signX * x); + py = int8_t(signY * y); } static void pad_clamptrigger(uint8_t& trigger) @@ -232,10 +232,12 @@ static void pad_clamptrigger(uint8_t& trigger) max = pad_clampregion[1]; if (min > trigger) trigger = 0; - else if (max < trigger) - trigger = max - min; else + { + if (max < trigger) + trigger = max - min; trigger -= min; + } } void DolphinControllerState::clamp() @@ -245,5 +247,4 @@ void DolphinControllerState::clamp() pad_clamptrigger(m_analogTriggers[0]); pad_clamptrigger(m_analogTriggers[1]); } - }