fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

Skywalker1127Changed default mode to Melee WASD with Neutral SOCDebb5a7e

WASD_NeutralSOCD
5.5 KiB167 linesraw
1#include "comms/B0XXInputViewer.hpp"
2#include "comms/DInputBackend.hpp"
3#include "comms/GamecubeBackend.hpp"
4#include "comms/N64Backend.hpp"
5#include "comms/NintendoSwitchBackend.hpp"
6#include "comms/XInputBackend.hpp"
7#include "config/mode_selection.hpp"
8#include "core/CommunicationBackend.hpp"
9#include "core/InputMode.hpp"
10#include "core/KeyboardMode.hpp"
11#include "core/pinout.hpp"
12#include "core/socd.hpp"
13#include "core/state.hpp"
14#include "input/GpioButtonInput.hpp"
15#include "input/NunchukInput.hpp"
16#include "joybus_utils.hpp"
17#include "modes/Melee20Button.hpp"
18#include "stdlib.hpp"
19
20#include <pico/bootrom.h>
21
22CommunicationBackend **backends = nullptr;
23size_t backend_count;
24KeyboardMode *current_kb_mode = nullptr;
25
26GpioButtonMapping button_mappings[] = {
27    { &InputState::l,           5 },
28    { &InputState::left,        4 },
29    { &InputState::down,        3 },
30    { &InputState::right,       2 },
31    { &InputState::up2,         1 },
32
33    { &InputState::mod_x,       6 },
34    { &InputState::mod_y,       7 },
35
36    { &InputState::select,      10},
37    { &InputState::start,       0 },
38    { &InputState::home,        11},
39
40    { &InputState::c_left,      13},
41    { &InputState::c_up,        12},
42    { &InputState::c_down,      15},
43    { &InputState::a,           14},
44    { &InputState::c_right,     16},
45
46    { &InputState::b,           26},
47    { &InputState::x,           21},
48    { &InputState::z,           19},
49    { &InputState::up,          17},
50
51    { &InputState::r,           27},
52    { &InputState::y,           22},
53    { &InputState::lightshield, 20},
54    { &InputState::midshield,   18},
55};
56size_t button_count = sizeof(button_mappings) / sizeof(GpioButtonMapping);
57
58const Pinout pinout = {
59    .joybus_data = 28,
60    .mux = -1,
61    .nunchuk_detect = -1,
62    .nunchuk_sda = -1,
63    .nunchuk_scl = -1,
64};
65
66void setup() {
67    // Create GPIO input source and use it to read button states for checking button holds.
68    GpioButtonInput *gpio_input = new GpioButtonInput(button_mappings, button_count);
69
70    InputState button_holds;
71    gpio_input->UpdateInputs(button_holds);
72
73    // Bootsel button hold as early as possible for safety.
74    if (button_holds.start) {
75        reset_usb_boot(0, 0);
76    }
77
78    // Turn on LED to indicate firmware booted.
79    gpio_init(PICO_DEFAULT_LED_PIN);
80    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
81    gpio_put(PICO_DEFAULT_LED_PIN, 1);
82
83    // Create array of input sources to be used.
84    static InputSource *input_sources[] = { gpio_input };
85    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
86
87    ConnectedConsole console = detect_console(pinout.joybus_data);
88
89    /* Select communication backend. */
90    CommunicationBackend *primary_backend;
91    if (console == ConnectedConsole::NONE) {
92        if (button_holds.x) {
93            // If no console detected and X is held on plugin then use Switch USB backend.
94            NintendoSwitchBackend::RegisterDescriptor();
95            backend_count = 1;
96            primary_backend = new NintendoSwitchBackend(input_sources, input_source_count);
97            backends = new CommunicationBackend *[backend_count] { primary_backend };
98
99            // Default to Ultimate mode on Switch.
100            primary_backend->SetGameMode(new UltimateUSB(socd::SOCD_NEUTRAL));
101            return;
102        } else if (button_holds.z) {
103            // If no console detected and Z is held on plugin then use DInput backend.
104            TUGamepad::registerDescriptor();
105            TUKeyboard::registerDescriptor();
106            backend_count = 2;
107            primary_backend = new DInputBackend(input_sources, input_source_count);
108            backends = new CommunicationBackend *[backend_count] {
109                primary_backend, new B0XXInputViewer(input_sources, input_source_count)
110            };
111        } else {
112            // Default to XInput mode if no console detected and no other mode forced.
113            backend_count = 2;
114            primary_backend = new XInputBackend(input_sources, input_source_count);
115            backends = new CommunicationBackend *[backend_count] {
116                primary_backend, new B0XXInputViewer(input_sources, input_source_count)
117            };
118        }
119    } else {
120        if (console == ConnectedConsole::GAMECUBE) {
121            primary_backend =
122                new GamecubeBackend(input_sources, input_source_count, pinout.joybus_data);
123        } else if (console == ConnectedConsole::N64) {
124            primary_backend = new N64Backend(input_sources, input_source_count, pinout.joybus_data);
125        }
126
127        // If console then only using 1 backend (no input viewer).
128        backend_count = 1;
129        backends = new CommunicationBackend *[backend_count] { primary_backend };
130    }
131
132    // Default to Melee mode.
133    primary_backend->SetGameMode(
134        new Melee20ButtonWASD(socd::SOCD_NEUTRAL, { .crouch_walk_os = false })
135    );
136}
137
138void loop() {
139    select_mode(backends[0]);
140
141    for (size_t i = 0; i < backend_count; i++) {
142        backends[i]->SendReport();
143    }
144
145    if (current_kb_mode != nullptr) {
146        current_kb_mode->SendReport(backends[0]->GetInputs());
147    }
148}
149
150/* Nunchuk code runs on the second core */
151NunchukInput *nunchuk = nullptr;
152
153void setup1() {
154    while (backends == nullptr) {
155        tight_loop_contents();
156    }
157
158    // Create Nunchuk input source.
159    nunchuk = new NunchukInput(Wire, pinout.nunchuk_detect, pinout.nunchuk_sda, pinout.nunchuk_scl);
160}
161
162void loop1() {
163    if (backends != nullptr) {
164        nunchuk->UpdateInputs(backends[0]->GetInputs());
165        busy_wait_us(50);
166    }
167}