fork/GramFirmware

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

Jonathan Haylettfix: use C++17 for AVR to avoid compilation error9ba9ca8

v0.1.0
5.2 KiB131 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/SwitchMatrixInput.hpp"
15#include "joybus_utils.hpp"
16#include "modes/Melee20Button.hpp"
17#include "stdlib.hpp"
18
19#include <pico/bootrom.h>
20
21CommunicationBackend **backends;
22size_t backend_count;
23KeyboardMode *current_kb_mode = nullptr;
24
25const size_t num_rows = 5;
26const size_t num_cols = 13;
27uint row_pins[num_rows] = { 20, 19, 18, 17, 16 };
28uint col_pins[num_cols] = { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
29// clang-format off
30SwitchMatrixElement matrix[num_rows][num_cols] = {
31    {NA,      NA,        NA,         NA,         NA, BTN(select), BTN(start), BTN(home), NA, BTN(r),      BTN(y),    BTN(lightshield), BTN(midshield)},
32    { BTN(l), BTN(left), BTN(down),  BTN(right), NA, NA,          NA,         NA,        NA, BTN(b),      BTN(x),    BTN(z),           BTN(up)       },
33    { NA,     NA,        NA,         NA,         NA, NA,          NA,         NA,        NA, NA,          NA,        NA,               NA            },
34    { NA,     NA,        NA,         NA,         NA, NA,          NA,         NA,        NA, BTN(c_left), BTN(c_up), BTN(c_right),     NA            },
35    { NA,     NA,        BTN(mod_x), BTN(mod_y), NA, NA,          NA,         NA,        NA, BTN(c_down), BTN(a),    NA,               NA            },
36};
37// clang-format on
38DiodeDirection diode_direction = DiodeDirection::COL2ROW;
39
40const Pinout pinout = {
41    .joybus_data = 22,
42    .mux = -1,
43    .nunchuk_detect = -1,
44    .nunchuk_sda = -1,
45    .nunchuk_scl = -1,
46};
47
48void setup() {
49    // Create switch matrix input source and use it to read button states for checking button holds.
50    SwitchMatrixInput<num_rows, num_cols> *matrix_input =
51        new SwitchMatrixInput<num_rows, num_cols>(row_pins, col_pins, matrix, diode_direction);
52
53    InputState button_holds;
54    matrix_input->UpdateInputs(button_holds);
55
56    // Bootsel button hold as early as possible for safety.
57    if (button_holds.start) {
58        reset_usb_boot(0, 0);
59    }
60
61    // Turn on LED to indicate firmware booted.
62    gpio_init(PICO_DEFAULT_LED_PIN);
63    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
64    gpio_put(PICO_DEFAULT_LED_PIN, 1);
65
66    // Create array of input sources to be used.
67    static InputSource *input_sources[] = { matrix_input };
68    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
69
70    ConnectedConsole console = detect_console(pinout.joybus_data);
71
72    /* Select communication backend. */
73    CommunicationBackend *primary_backend;
74    if (console == ConnectedConsole::NONE) {
75        if (button_holds.x) {
76            // If no console detected and X is held on plugin then use Switch USB backend.
77            NintendoSwitchBackend::RegisterDescriptor();
78            backend_count = 1;
79            primary_backend = new NintendoSwitchBackend(input_sources, input_source_count);
80            backends = new CommunicationBackend *[backend_count] { primary_backend };
81
82            // Default to Ultimate mode on Switch.
83            primary_backend->SetGameMode(new Ultimate(socd::SOCD_2IP));
84            return;
85        } else if (button_holds.z) {
86            // If no console detected and Z is held on plugin then use DInput backend.
87            TUGamepad::registerDescriptor();
88            TUKeyboard::registerDescriptor();
89            backend_count = 2;
90            primary_backend = new DInputBackend(input_sources, input_source_count);
91            backends = new CommunicationBackend *[backend_count] {
92                primary_backend, new B0XXInputViewer(input_sources, input_source_count)
93            };
94        } else {
95            // Default to XInput mode if no console detected and no other mode forced.
96            backend_count = 2;
97            primary_backend = new XInputBackend(input_sources, input_source_count);
98            backends = new CommunicationBackend *[backend_count] {
99                primary_backend, new B0XXInputViewer(input_sources, input_source_count)
100            };
101        }
102    } else {
103        if (console == ConnectedConsole::GAMECUBE) {
104            primary_backend =
105                new GamecubeBackend(input_sources, input_source_count, pinout.joybus_data);
106        } else if (console == ConnectedConsole::N64) {
107            primary_backend = new N64Backend(input_sources, input_source_count, pinout.joybus_data);
108        }
109
110        // If console then only using 1 backend (no input viewer).
111        backend_count = 1;
112        backends = new CommunicationBackend *[backend_count] { primary_backend };
113    }
114
115    // Default to Melee mode.
116    primary_backend->SetGameMode(
117        new Melee20Button(socd::SOCD_2IP_NO_REAC, { .crouch_walk_os = false })
118    );
119}
120
121void loop() {
122    select_mode(backends[0]);
123
124    for (size_t i = 0; i < backend_count; i++) {
125        backends[i]->SendReport();
126    }
127
128    if (current_kb_mode != nullptr) {
129        current_kb_mode->SendReport(backends[0]->GetInputs());
130    }
131}