fork/GramFirmware

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

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

master
5.7 KiB164 linesraw
1#include "comms/B0XXInputViewer.hpp"
2#include "comms/DInputBackend.hpp"
3#include "comms/GamecubeBackend.hpp"
4#include "comms/N64Backend.hpp"
5#include "config/mode_selection.hpp"
6#include "core/CommunicationBackend.hpp"
7#include "core/InputMode.hpp"
8#include "core/pinout.hpp"
9#include "core/socd.hpp"
10#include "core/state.hpp"
11#include "input/GpioButtonInput.hpp"
12#include "modes/Melee20Button.hpp"
13#include "stdlib.hpp"
14
15const int brook_up_pin = 17;
16const int brook_l_pin = 30;
17
18CommunicationBackend **backends = nullptr;
19size_t backend_count;
20KeyboardMode *current_kb_mode = nullptr;
21bool brook_mode = false;
22
23GpioButtonMapping button_mappings[] = {
24    {&InputState::l,            11},
25    { &InputState::left,        15},
26    { &InputState::down,        16},
27    { &InputState::right,       14},
28
29    { &InputState::mod_x,       3 },
30    { &InputState::mod_y,       0 },
31    { &InputState::nunchuk_c,   2 }, // Dpad Toggle button
32
33    { &InputState::start,       A5},
34
35    { &InputState::c_left,      4 },
36    { &InputState::c_up,        8 },
37    { &InputState::c_down,      1 },
38    { &InputState::a,           12},
39    { &InputState::c_right,     6 },
40
41    { &InputState::b,           13},
42    { &InputState::x,           5 },
43    { &InputState::z,           10},
44    { &InputState::up,          9 },
45
46    { &InputState::r,           A0},
47    { &InputState::y,           A1},
48    { &InputState::lightshield, A2},
49    { &InputState::midshield,   A3},
50};
51size_t button_count = sizeof(button_mappings) / sizeof(GpioButtonMapping);
52
53GpioButtonMapping brook_button_mappings[] = {
54  // These are the only buttons which aren't also bound on brook board directly.
55  // And so the only buttons which can be bound to dpad_up and l3 on brook
56  // WARNING: Bind as few of these as you need, since it increases latency
57    {&InputState::l,          11},
58
59    { &InputState::mod_x,     3 },
60    { &InputState::mod_y,     0 },
61    { &InputState::nunchuk_c, 2 },
62
63    { &InputState::c_left,    4 },
64    { &InputState::c_up,      8 },
65    { &InputState::c_down,    1 },
66    { &InputState::a,         12},
67    { &InputState::c_right,   6 },
68};
69
70Pinout pinout = {
71    .joybus_data = 7,
72    .mux = A4,
73};
74
75void setup() {
76    // Create GPIO input source and use it to read button states for checking button holds.
77    GpioButtonInput *gpio_input = new GpioButtonInput(button_mappings, button_count);
78
79    InputState button_holds;
80    gpio_input->UpdateInputs(button_holds);
81
82    // Create array of input sources to be used.
83    static InputSource *input_sources[] = { gpio_input };
84    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
85
86    // Hold B on plugin for Brook board mode.
87    pinMode(pinout.mux, OUTPUT);
88    if (button_holds.b) {
89        digitalWrite(pinout.mux, HIGH);
90        brook_mode = true;
91        return;
92        // Remaining code is no-op if brook is enabled.
93        // Brook Firmware takes control, so we can't control layout/gamemode/backend in this branch
94        // IN Addition, you can force the following brook modes by holding the corresponding button
95        // on connecting. If none are held, brook will auto-detect. 1P/X = PS3 2P/Y = PS4 3P/RB =
96        // XID-PC 4P/LB = Nintendo Switch These listed buttons correspond to the mapping in brook
97        // mode (so can't be remapped) So in the case of the default layout for lbx these correspond
98        // to R, Y, LightShield, MidShield
99    }
100    digitalWrite(pinout.mux, LOW);
101    brook_mode = false;
102
103    CommunicationBackend *primary_backend = new DInputBackend(input_sources, input_source_count);
104    delay(500);
105    bool usb_connected = UDADDR & _BV(ADDEN);
106
107    /* Select communication backend. */
108    if (usb_connected) {
109        // Default to DInput mode if USB is connected.
110        // Input viewer only used when connected to PC i.e. when using DInput mode.
111        backend_count = 2;
112        backends = new CommunicationBackend *[backend_count] {
113            primary_backend, new B0XXInputViewer(input_sources, input_source_count)
114        };
115    } else {
116        delete primary_backend;
117        if (button_holds.c_left) {
118            // Hold C-Left on plugin for N64.
119            primary_backend =
120                new N64Backend(input_sources, input_source_count, 60, pinout.joybus_data);
121        } else if (button_holds.a) {
122            // Hold A on plugin for GameCube adapter.
123            primary_backend =
124                new GamecubeBackend(input_sources, input_source_count, 0, pinout.joybus_data);
125        } else {
126            // Default to GameCube/Wii.
127            primary_backend =
128                new GamecubeBackend(input_sources, input_source_count, 125, pinout.joybus_data);
129        }
130
131        // If not DInput then only using 1 backend (no input viewer).
132        backend_count = 1;
133        backends = new CommunicationBackend *[backend_count] { primary_backend };
134    }
135
136    // Default to Melee mode.
137    primary_backend->SetGameMode(
138        new Melee20Button(socd::SOCD_2IP_NO_REAC, { .crouch_walk_os = false })
139    );
140}
141
142void loop() {
143    if (brook_mode) {
144        bool button_l = digitalRead(brook_button_mappings[0].pin);
145        bool button_mod_x = digitalRead(brook_button_mappings[1].pin);
146        bool button_mod_y = digitalRead(brook_button_mappings[2].pin);
147        bool button_cstick_down = digitalRead(brook_button_mappings[6].pin);
148        bool button_a = digitalRead(brook_button_mappings[7].pin);
149
150        digitalWrite(brook_up_pin, button_mod_x && button_mod_y && button_cstick_down && button_a);
151        digitalWrite(brook_l_pin, button_l);
152        return;
153    }
154
155    select_mode(backends[0]);
156
157    for (size_t i = 0; i < backend_count; i++) {
158        backends[i]->SendReport();
159    }
160
161    if (current_kb_mode != nullptr) {
162        current_kb_mode->SendReport(backends[0]->GetInputs());
163    }
164}