diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index ead4cfb09a..c6bbb619ab 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -24,7 +24,9 @@ #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Windows/LabChannel_Tests.h" #include "CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h" +#include "Controllers/MgbaLink/MgbaLink_Keys_Tests.h" #include "Controllers/MgbaLink/MgbaLink_Tests.h" +#include "NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.h" #include "CommonFramework/Windows/LabSnapshot_Tests.h" #include "CommonFramework/Windows/LabStateJson_Tests.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" @@ -74,6 +76,8 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_AudioFileLoader(ret); add_tests_LabChannel(ret); MgbaLink::add_tests(ret); + MgbaLink::add_key_tests(ret); + NintendoSwitch::add_tests_MgbaLinkKeyMap(ret); add_tests_VideoSource_MgbaLink(ret); add_tests_LabSnapshot(ret); add_tests_LabStateJson(ret); diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.cpp new file mode 100644 index 0000000000..13490c8a1f --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.cpp @@ -0,0 +1,89 @@ +/* MgbaLink Keys + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "MgbaLink_Keys.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +namespace{ + +// "duration" in (fractional) GBA frames. +double to_frames(WallDuration duration){ + double microseconds = (double)std::chrono::duration_cast(duration).count(); + return microseconds * FrameClock::GBA_FRAMES_PER_SECOND / 1000000.0; +} + +uint64_t offset_frame(const FrameClock& clock, double frames){ + double frame = (double)clock.anchor_frame() + frames; + return frame <= 0 ? 0 : (uint64_t)frame; +} + +} // namespace + + + +uint64_t frames_for_duration(WallDuration duration){ + if (duration <= WallDuration::zero()){ + return 0; + } + return std::max(1, (uint64_t)std::llround(to_frames(duration))); +} + +uint64_t frame_at(const FrameClock& clock, WallClock time){ + return offset_frame(clock, std::round(to_frames(time - clock.anchor_time()))); +} + +uint64_t grant_frame(const FrameClock& clock, WallClock now, uint64_t lead_frames){ + return offset_frame(clock, std::floor(to_frames(now - clock.anchor_time()))) + lead_frames; +} + + + +std::optional KeySchedule::push(WallClock now, uint16_t mask, WallDuration duration){ + if (duration <= WallDuration::zero()){ + return std::nullopt; + } + uint64_t start_frame = m_end_frame; + WallClock start_time = m_end_time; + uint64_t earliest = frame_at(m_clock, now) + LEAD_FRAMES; + if (start_frame < earliest){ + start_frame = earliest; + start_time = m_clock.timestamp(start_frame); + } + + m_end_time = start_time + duration; + m_end_frame = std::max(start_frame + 1, frame_at(m_clock, m_end_time)); + + if (m_held_mask == mask){ + return std::nullopt; + } + m_held_mask = mask; + return KeysCommand{start_frame, mask}; +} + + + +void InputLog::record(const KeysCommand& sent, uint64_t frames_late){ + m_entries.emplace_back(Entry{sent.frame + frames_late, sent.mask}); +} + +std::string InputLog::to_text() const{ + std::string text; + for (const Entry& entry : m_entries){ + text += std::format("{} {}\n", entry.frame, entry.mask); + } + return text; +} + + +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.h new file mode 100644 index 0000000000..93f53aaae0 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.h @@ -0,0 +1,133 @@ +/* MgbaLink Keys + * + * From: https://github.com/PokemonAutomation/ + * + * The input side of MgbaLink v1 (lab/mgba/PROTOCOL.md on the fork's agents + * branch): GBA key masks, the conversion of app time to emulated frames, + * the frames that "KEYS" and "GRANT" name, and the input log. Pure logic; + * the controller that sends the commands holds the connection. + * + * Programs keep milliseconds; this is the boundary that turns them into + * frames (design: docs/design/Lab-mGBA-Console.md, section 2). + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Keys_H +#define PokemonAutomation_Controllers_MgbaLink_Keys_H + +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "MgbaLink_Protocol.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// GBA KEYINPUT bits, as mGBA's C.GBA_KEY. +enum GbaKey : uint16_t{ + GBA_KEY_NONE = 0, + GBA_KEY_A = 1 << 0, + GBA_KEY_B = 1 << 1, + GBA_KEY_SELECT = 1 << 2, + GBA_KEY_START = 1 << 3, + GBA_KEY_RIGHT = 1 << 4, + GBA_KEY_LEFT = 1 << 5, + GBA_KEY_UP = 1 << 6, + GBA_KEY_DOWN = 1 << 7, + GBA_KEY_R = 1 << 8, + GBA_KEY_L = 1 << 9, +}; + + +// How many frames a state held for "duration" lasts: +// max(1, round(duration * 59.7275 / 1000)), so 17 ms is 1 frame and 80 ms +// is 5. A zero duration holds no frame. +uint64_t frames_for_duration(WallDuration duration); + +// The frame app time "time" lands on: F0 + round((time - t0) * 59.7275 / 1000), +// never below frame 0. +uint64_t frame_at(const FrameClock& clock, WallClock time); + +// The frame to "GRANT" at "now" in paced mode: +// F0 + floor((now - t0) * 59.7275 / 1000) + lead_frames. +uint64_t grant_frame(const FrameClock& clock, WallClock now, uint64_t lead_frames); + + +// One "KEYS " command. +struct KeysCommand{ + uint64_t frame = 0; + uint16_t mask = 0; + + friend bool operator==(const KeysCommand&, const KeysCommand&) = default; +}; + + +// Turns the controller's stream of states, each held for a duration, into +// "KEYS" commands on frames. States follow each other without gaps: a state +// starts on the frame the previous one ended, unless that frame has already +// passed (the controller was idle), in which case it starts LEAD_FRAMES +// after "now". The end of each state is placed from app time, so rounding +// does not add up over many states, but every state lasts at least one +// frame. Not thread-safe. +class KeySchedule{ +public: + // The pacer grants this far ahead, so commands for the next frames + // still reach the bridge before it runs them. + static constexpr uint64_t LEAD_FRAMES = 2; + + explicit KeySchedule(FrameClock clock) + : m_clock(clock) + , m_end_time(clock.anchor_time()) + , m_end_frame(clock.anchor_frame()) + {} + + // Appends "mask" held for "duration". Returns the command to send, or + // nullopt if the keys do not change (or "duration" is zero, which holds + // nothing and is dropped). + std::optional push(WallClock now, uint16_t mask, WallDuration duration); + + // The frame the last state ends on: the next state starts there. + uint64_t end_frame() const{ + return m_end_frame; + } + +private: + FrameClock m_clock; + WallClock m_end_time; + uint64_t m_end_frame; + std::optional m_held_mask; +}; + + +// What landed on which frame: one entry per "KEYS" sent, at the frame the +// bridge applied it (the frame named, plus how late it was). With the start +// state this replays the run in mGBA without the app. Not thread-safe. +class InputLog{ +public: + struct Entry{ + uint64_t frame = 0; + uint16_t mask = 0; + + friend bool operator==(const Entry&, const Entry&) = default; + }; + + void record(const KeysCommand& sent, uint64_t frames_late); + + const std::vector& entries() const{ + return m_entries; + } + + // One " " line per entry, in decimal. + std::string to_text() const; + +private: + std::vector m_entries; +}; + + +} // namespace MgbaLink +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.cpp new file mode 100644 index 0000000000..8c2a3b3dfd --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.cpp @@ -0,0 +1,302 @@ +/* MgbaLink Keys Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "MgbaLink_Keys.h" +#include "MgbaLink_Keys_Tests.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + +namespace{ + +using std::chrono::milliseconds; + +// Every clock below is anchored at (CLOCK_T0, frame 1000). +const WallClock CLOCK_T0 = WallClock() + std::chrono::hours(1000); +constexpr uint64_t CLOCK_F0 = 1000; + + + +// Durations to frames. +struct FramesForDurationInput{ + milliseconds duration{}; +}; +struct FramesForDurationExpectation{ + uint64_t frames = 0; +}; +struct FramesForDurationCase{ + std::string name; + FramesForDurationInput input; + FramesForDurationExpectation expectation; +}; + +class Test_FramesForDuration : public UnitTest{ +public: + Test_FramesForDuration(FramesForDurationCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - frames for duration - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + uint64_t frames = frames_for_duration(m_case.input.duration); + if (frames != m_case.expectation.frames){ + return std::format("{} frame(s), expected {}", frames, m_case.expectation.frames); + } + return true; + } + +private: + FramesForDurationCase m_case; +}; + + + +// App time to the frame it lands on (KEYS) and the frame to grant (GRANT). +struct FrameAtInput{ + milliseconds after_anchor{}; +}; +struct FrameAtExpectation{ + uint64_t frame = 0; + uint64_t grant = 0; +}; +struct FrameAtCase{ + std::string name; + FrameAtInput input; + FrameAtExpectation expectation; +}; + +class Test_FrameAt : public UnitTest{ +public: + Test_FrameAt(FrameAtCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - frame at time - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + FrameClock clock(CLOCK_T0, CLOCK_F0); + WallClock time = CLOCK_T0 + m_case.input.after_anchor; + uint64_t frame = frame_at(clock, time); + uint64_t grant = grant_frame(clock, time, KeySchedule::LEAD_FRAMES); + if (frame != m_case.expectation.frame || grant != m_case.expectation.grant){ + return std::format( + "frame {}, grant {}; expected frame {}, grant {}", + frame, grant, m_case.expectation.frame, m_case.expectation.grant + ); + } + return true; + } + +private: + FrameAtCase m_case; +}; + + + +// Controller states to KEYS commands. Frame 1002, the first one after the +// lead, starts 33.5 ms after the anchor. +struct StatePush{ + milliseconds now_after_anchor{}; + uint16_t mask = 0; + milliseconds duration{}; +}; +StatePush hold_at(milliseconds now_after_anchor, uint16_t mask, milliseconds duration){ + return StatePush{now_after_anchor, mask, duration}; +} +struct KeyScheduleInput{ + std::vector pushes; +}; +struct KeyScheduleExpectation{ + std::string commands; + uint64_t end_frame = 0; +}; +struct KeyScheduleCase{ + std::string name; + KeyScheduleInput input; + KeyScheduleExpectation expectation; +}; + +class Test_KeySchedule : public UnitTest{ +public: + Test_KeySchedule(KeyScheduleCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - key schedule - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + KeySchedule schedule(FrameClock(CLOCK_T0, CLOCK_F0)); + std::string commands; + for (const StatePush& push : m_case.input.pushes){ + std::optional command = schedule.push(CLOCK_T0 + push.now_after_anchor, push.mask, push.duration); + if (command){ + commands += commands.empty() ? "" : ", "; + commands += std::format("KEYS {} {}", command->frame, command->mask); + } + } + if (commands != m_case.expectation.commands || schedule.end_frame() != m_case.expectation.end_frame){ + return std::format( + "[{}] ending at {}, expected [{}] ending at {}", + commands, schedule.end_frame(), m_case.expectation.commands, m_case.expectation.end_frame + ); + } + return true; + } + +private: + KeyScheduleCase m_case; +}; + + + +// Commands sent and how late each landed, to the input log's text. +struct SentKeys{ + KeysCommand command; + uint64_t frames_late = 0; +}; +SentKeys sent_late(uint64_t frame, uint16_t mask, uint64_t frames_late){ + return SentKeys{KeysCommand{frame, mask}, frames_late}; +} +struct InputLogInput{ + std::vector sent; +}; +struct InputLogExpectation{ + std::string text; +}; +struct InputLogCase{ + std::string name; + InputLogInput input; + InputLogExpectation expectation; +}; + +class Test_InputLog : public UnitTest{ +public: + Test_InputLog(InputLogCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - input log - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + InputLog log; + for (const SentKeys& sent : m_case.input.sent){ + log.record(sent.command, sent.frames_late); + } + std::string text = log.to_text(); + if (text != m_case.expectation.text){ + return std::format("logged <{}>", text); + } + return true; + } + +private: + InputLogCase m_case; +}; + + + +void add_frame_math_tests(UnitTestDatabase& database){ + const FramesForDurationCase FRAMES_FOR_DURATION_CASES[] = { + {.name = "zero holds nothing", .input = {milliseconds(0)}, .expectation = {0}}, + {.name = "8 ms is still one frame", .input = {milliseconds(8)}, .expectation = {1}}, + {.name = "17 ms", .input = {milliseconds(17)}, .expectation = {1}}, + {.name = "80 ms", .input = {milliseconds(80)}, .expectation = {5}}, + {.name = "1000 ms", .input = {milliseconds(1000)}, .expectation = {60}}, + }; + for (const FramesForDurationCase& test_case : FRAMES_FOR_DURATION_CASES){ + database.add(test_case); + } + + // 100 ms is 5.97 frames: rounded for KEYS, floored for GRANT. + const FrameAtCase FRAME_AT_CASES[] = { + {.name = "at the anchor", .input = {milliseconds(0)}, .expectation = {1000, 1002}}, + {.name = "100 ms", .input = {milliseconds(100)}, .expectation = {1006, 1007}}, + {.name = "1000 ms", .input = {milliseconds(1000)}, .expectation = {1060, 1061}}, + {.name = "before the anchor", .input = {milliseconds(-100)}, .expectation = {994, 996}}, + }; + for (const FrameAtCase& test_case : FRAME_AT_CASES){ + database.add(test_case); + } +} + +void add_key_schedule_tests(UnitTestDatabase& database){ + const uint16_t A = GBA_KEY_A; + const uint16_t B = GBA_KEY_B; + const milliseconds T0(0), MS50(50); + const KeyScheduleCase KEY_SCHEDULE_CASES[] = { + { + .name = "80 ms press holds 5 frames", + .input = {{hold_at(T0, A, milliseconds(80)), hold_at(T0, 0, milliseconds(80))}}, + .expectation = {"KEYS 1002 1, KEYS 1007 0", 1012}, + }, + { + .name = "1000 ms press holds 60 frames", + .input = {{hold_at(T0, A, milliseconds(1000)), hold_at(T0, 0, MS50)}}, + .expectation = {"KEYS 1002 1, KEYS 1062 0", 1065}, + }, + { + .name = "17 ms tap holds 1 frame", + .input = {{hold_at(T0, A, milliseconds(17)), hold_at(T0, 0, milliseconds(17))}}, + .expectation = {"KEYS 1002 1, KEYS 1003 0", 1004}, + }, + { + .name = "5 ms tap holds 1 frame, the release ends at its own 100 ms", + .input = {{hold_at(T0, A, milliseconds(5)), hold_at(T0, 0, milliseconds(100))}}, + .expectation = {"KEYS 1002 1, KEYS 1003 0", 1008}, + }, + { + .name = "same keys again send nothing", + .input = {{hold_at(T0, A, MS50), hold_at(T0, A, MS50), hold_at(T0, 0, MS50)}}, + .expectation = {"KEYS 1002 1, KEYS 1008 0", 1011}, + }, + { + .name = "after idle the next state starts the lead after now", + .input = {{hold_at(T0, A, MS50), hold_at(milliseconds(1000), B, MS50)}}, + .expectation = {"KEYS 1002 1, KEYS 1062 2", 1065}, + }, + { + .name = "zero duration is dropped", + .input = {{hold_at(T0, A, milliseconds(0)), hold_at(T0, B, MS50)}}, + .expectation = {"KEYS 1002 2", 1005}, + }, + { + .name = "first state with no keys is still sent", + .input = {{hold_at(T0, 0, MS50)}}, + .expectation = {"KEYS 1002 0", 1005}, + }, + }; + for (const KeyScheduleCase& test_case : KEY_SCHEDULE_CASES){ + database.add(test_case); + } +} + +void add_input_log_tests(UnitTestDatabase& database){ + const InputLogCase INPUT_LOG_CASES[] = { + {.name = "empty", .input = {}, .expectation = {""}}, + {.name = "on time", .input = {{sent_late(1002, GBA_KEY_A, 0)}}, .expectation = {"1002 1\n"}}, + { + .name = "late command logged where it landed", + .input = {{sent_late(1002, GBA_KEY_A, 0), sent_late(1007, GBA_KEY_NONE, 3)}}, + .expectation = {"1002 1\n1010 0\n"}, + }, + }; + for (const InputLogCase& test_case : INPUT_LOG_CASES){ + database.add(test_case); + } +} + +} // namespace + + + +void add_key_tests(UnitTestDatabase& database){ + add_frame_math_tests(database); + add_key_schedule_tests(database); + add_input_log_tests(database); +} + + +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.h new file mode 100644 index 0000000000..2560d71f0c --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.h @@ -0,0 +1,22 @@ +/* MgbaLink Keys Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Keys_Tests_H +#define PokemonAutomation_Controllers_MgbaLink_Keys_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// Durations and app times to frames, the key schedule and the input log. +void add_key_tests(UnitTestDatabase& database); + + +} // namespace MgbaLink +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.cpp new file mode 100644 index 0000000000..1b6de3acf8 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.cpp @@ -0,0 +1,92 @@ +/* Nintendo Switch: Pro Controller to GBA keys (MgbaLink) + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Controllers/MgbaLink/MgbaLink_Keys.h" +#include "NintendoSwitch_MgbaLink_KeyMap.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +using namespace MgbaLink; + + +namespace{ + +struct ButtonKey{ + Button button; + uint16_t key; +}; + +constexpr ButtonKey BUTTON_KEYS[] = { + {BUTTON_A, GBA_KEY_A}, + {BUTTON_B, GBA_KEY_B}, + {BUTTON_L, GBA_KEY_L}, + {BUTTON_R, GBA_KEY_R}, + {BUTTON_PLUS, GBA_KEY_START}, + {BUTTON_MINUS, GBA_KEY_SELECT}, + {BUTTON_UP, GBA_KEY_UP}, + {BUTTON_RIGHT, GBA_KEY_RIGHT}, + {BUTTON_DOWN, GBA_KEY_DOWN}, + {BUTTON_LEFT, GBA_KEY_LEFT}, +}; + +// Stick tilt past which an axis presses its direction. +constexpr double STICK_THRESHOLD = 0.5; + +uint16_t dpad_keys(DpadPosition dpad){ + switch (dpad){ + case DPAD_UP: return GBA_KEY_UP; + case DPAD_UP_RIGHT: return GBA_KEY_UP | GBA_KEY_RIGHT; + case DPAD_RIGHT: return GBA_KEY_RIGHT; + case DPAD_DOWN_RIGHT: return GBA_KEY_DOWN | GBA_KEY_RIGHT; + case DPAD_DOWN: return GBA_KEY_DOWN; + case DPAD_DOWN_LEFT: return GBA_KEY_DOWN | GBA_KEY_LEFT; + case DPAD_LEFT: return GBA_KEY_LEFT; + case DPAD_UP_LEFT: return GBA_KEY_UP | GBA_KEY_LEFT; + case DPAD_NONE: return GBA_KEY_NONE; + } + return GBA_KEY_NONE; +} + +// y > 0 is up, as everywhere in the controller code. +uint16_t stick_keys(const JoystickPosition& stick){ + uint16_t keys = GBA_KEY_NONE; + if (stick.x > STICK_THRESHOLD){ + keys |= GBA_KEY_RIGHT; + }else if (stick.x < -STICK_THRESHOLD){ + keys |= GBA_KEY_LEFT; + } + if (stick.y > STICK_THRESHOLD){ + keys |= GBA_KEY_UP; + }else if (stick.y < -STICK_THRESHOLD){ + keys |= GBA_KEY_DOWN; + } + return keys; +} + +} // namespace + + + +GbaKeysFromSwitch gba_keys_from_switch(const SwitchControllerState& state){ + GbaKeysFromSwitch result; + Button mapped = BUTTON_NONE; + for (const ButtonKey& entry : BUTTON_KEYS){ + mapped |= entry.button; + if ((state.buttons & entry.button) != BUTTON_NONE){ + result.keys |= entry.key; + } + } + result.unmapped = state.buttons & (Button)~(ButtonFlagType)mapped; + result.keys |= dpad_keys(state.dpad); + result.keys |= stick_keys(state.left_joystick); + result.right_stick_moved = !state.right_joystick.is_neutral(); + return result; +} + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.h b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.h new file mode 100644 index 0000000000..b1ae696777 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.h @@ -0,0 +1,36 @@ +/* Nintendo Switch: Pro Controller to GBA keys (MgbaLink) + * + * From: https://github.com/PokemonAutomation/ + * + * How a Pro Controller state drives an mGBA instance: A, B, L and R as + * themselves, Plus as Start, Minus as Select, the D-pad as the D-pad, and + * the left stick as the D-pad past half tilt on each axis. Everything else + * has no GBA key; the controller logs it. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_MgbaLink_KeyMap_H +#define PokemonAutomation_NintendoSwitch_MgbaLink_KeyMap_H + +#include +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerWithScheduler.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The GBA keys one Pro Controller state holds, and what it presses that +// has no GBA key. +struct GbaKeysFromSwitch{ + uint16_t keys = 0; // MgbaLink::GbaKey bits. + Button unmapped = BUTTON_NONE; // Pressed buttons with no GBA key. + bool right_stick_moved = false; // The right stick has no GBA key. +}; + +// Maps one state; the left stick adds to the D-pad, never cancels it. +GbaKeysFromSwitch gba_keys_from_switch(const SwitchControllerState& state); + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.cpp new file mode 100644 index 0000000000..cb814e8ed9 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.cpp @@ -0,0 +1,103 @@ +/* Nintendo Switch: Pro Controller to GBA keys (MgbaLink) Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Controllers/MgbaLink/MgbaLink_Keys.h" +#include "NintendoSwitch_MgbaLink_KeyMap.h" +#include "NintendoSwitch_MgbaLink_KeyMap_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +using namespace MgbaLink; + + +namespace{ + +struct KeyMapInput{ + Button buttons = BUTTON_NONE; + DpadPosition dpad = DPAD_NONE; + JoystickPosition left_joystick = {}; + JoystickPosition right_joystick = {}; +}; +struct KeyMapExpectation{ + uint16_t keys = 0; + Button unmapped = BUTTON_NONE; + bool right_stick_moved = false; +}; +struct KeyMapCase{ + std::string name; + KeyMapInput input; + KeyMapExpectation expectation; +}; + +class Test_KeyMap : public UnitTest{ +public: + Test_KeyMap(KeyMapCase test_case) + : UnitTest(std::format("NintendoSwitch::MgbaLink - key map - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + SwitchControllerState state; + state.buttons = m_case.input.buttons; + state.dpad = m_case.input.dpad; + state.left_joystick = m_case.input.left_joystick; + state.right_joystick = m_case.input.right_joystick; + GbaKeysFromSwitch result = gba_keys_from_switch(state); + const KeyMapExpectation& expected = m_case.expectation; + if (result.keys != expected.keys + || result.unmapped != expected.unmapped + || result.right_stick_moved != expected.right_stick_moved + ){ + return std::format( + "keys {}, unmapped <{}>, right stick {}; expected keys {}, unmapped <{}>, right stick {}", + result.keys, button_to_string(result.unmapped), result.right_stick_moved, + expected.keys, button_to_string(expected.unmapped), expected.right_stick_moved + ); + } + return true; + } + +private: + KeyMapCase m_case; +}; + +} // namespace + + + +void add_tests_MgbaLinkKeyMap(UnitTestDatabase& database){ + const KeyMapCase CASES[] = { + {.name = "nothing pressed", .input = {}, .expectation = {}}, + {.name = "A", .input = {.buttons = BUTTON_A}, .expectation = {.keys = GBA_KEY_A}}, + {.name = "B", .input = {.buttons = BUTTON_B}, .expectation = {.keys = GBA_KEY_B}}, + {.name = "L and R", .input = {.buttons = BUTTON_L | BUTTON_R}, .expectation = {.keys = GBA_KEY_L | GBA_KEY_R}}, + {.name = "Plus is Start", .input = {.buttons = BUTTON_PLUS}, .expectation = {.keys = GBA_KEY_START}}, + {.name = "Minus is Select", .input = {.buttons = BUTTON_MINUS}, .expectation = {.keys = GBA_KEY_SELECT}}, + {.name = "D-pad up", .input = {.dpad = DPAD_UP}, .expectation = {.keys = GBA_KEY_UP}}, + {.name = "D-pad down-left", .input = {.dpad = DPAD_DOWN_LEFT}, .expectation = {.keys = GBA_KEY_DOWN | GBA_KEY_LEFT}}, + {.name = "split D-pad buttons", .input = {.buttons = BUTTON_RIGHT | BUTTON_DOWN}, .expectation = {.keys = GBA_KEY_RIGHT | GBA_KEY_DOWN}}, + {.name = "stick full up", .input = {.left_joystick = {0, 1}}, .expectation = {.keys = GBA_KEY_UP}}, + {.name = "stick full left", .input = {.left_joystick = {-1, 0}}, .expectation = {.keys = GBA_KEY_LEFT}}, + {.name = "stick at half tilt does nothing", .input = {.left_joystick = {0.5, -0.5}}, .expectation = {}}, + {.name = "stick down-right past half", .input = {.left_joystick = {0.7, -0.7}}, .expectation = {.keys = GBA_KEY_RIGHT | GBA_KEY_DOWN}}, + { + .name = "X Y ZL ZR Home Capture unmapped", + .input = {.buttons = BUTTON_A | BUTTON_X | BUTTON_Y | BUTTON_ZL | BUTTON_ZR | BUTTON_HOME | BUTTON_CAPTURE}, + .expectation = {.keys = GBA_KEY_A, .unmapped = BUTTON_X | BUTTON_Y | BUTTON_ZL | BUTTON_ZR | BUTTON_HOME | BUTTON_CAPTURE}, + }, + {.name = "right stick moved", .input = {.right_joystick = {1, 0}}, .expectation = {.right_stick_moved = true}}, + }; + for (const KeyMapCase& test_case : CASES){ + database.add(test_case); + } +} + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.h b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.h new file mode 100644 index 0000000000..fd787484dc --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.h @@ -0,0 +1,22 @@ +/* Nintendo Switch: Pro Controller to GBA keys (MgbaLink) Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_MgbaLink_KeyMap_Tests_H +#define PokemonAutomation_NintendoSwitch_MgbaLink_KeyMap_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// Pro Controller states to GBA keys. +void add_tests_MgbaLinkKeyMap(UnitTestDatabase& database); + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6dc016aff1..fce49ccc5c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -282,6 +282,10 @@ file(GLOB CORE_LIBRARY_SOURCES Source/Controllers/JoystickTools.h Source/Controllers/MgbaLink/MgbaLink_Connection.cpp Source/Controllers/MgbaLink/MgbaLink_Connection.h + Source/Controllers/MgbaLink/MgbaLink_Keys.cpp + Source/Controllers/MgbaLink/MgbaLink_Keys.h + Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.cpp + Source/Controllers/MgbaLink/MgbaLink_Keys_Tests.h Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp Source/Controllers/MgbaLink/MgbaLink_Protocol.h Source/Controllers/MgbaLink/MgbaLink_Tests.cpp @@ -322,6 +326,10 @@ file(GLOB CORE_LIBRARY_SOURCES Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_Joycon_from_Keyboard.h Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconState.cpp Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconState.h + Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.cpp + Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap.h + Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.cpp + Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.h Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.cpp Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerSettings.cpp