fork/GramFirmware

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

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

codespace-scaling-space-guide-56rpxqvxpg2v57v
3.9 KiB125 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
15CommunicationBackend **backends = nullptr;
16size_t backend_count;
17KeyboardMode *current_kb_mode = nullptr;
18
19GpioButtonMapping button_mappings[] = {
20    {&InputState::l,            2 },
21    { &InputState::left,        10},
22    { &InputState::down,        13},
23    { &InputState::right,       5 },
24
25    { &InputState::mod_x,       7 },
26    { &InputState::mod_y,       3 },
27
28    { &InputState::select,      9 },
29    { &InputState::start,       6 },
30    { &InputState::home,        8 },
31
32    { &InputState::c_left,      A1},
33    { &InputState::c_up,        A3},
34    { &InputState::c_down,      A0},
35    { &InputState::a,           A2},
36    { &InputState::c_right,     A4},
37
38    { &InputState::b,           A5},
39    { &InputState::x,           14},
40    { &InputState::z,           16},
41    { &InputState::up,          15},
42
43    { &InputState::r,           12},
44    { &InputState::y,           4 },
45    { &InputState::lightshield, 1 },
46    { &InputState::midshield,   0 },
47};
48size_t button_count = sizeof(button_mappings) / sizeof(GpioButtonMapping);
49
50Pinout pinout = {
51    .joybus_data = 17,
52    .mux = 11,
53    .nunchuk_detect = -1,
54    .nunchuk_sda = -1,
55    .nunchuk_scl = -1,
56};
57
58void setup() {
59    // Create GPIO input source and use it to read button states for checking button holds.
60    GpioButtonInput *gpio_input = new GpioButtonInput(button_mappings, button_count);
61
62    InputState button_holds;
63    gpio_input->UpdateInputs(button_holds);
64
65    // Create array of input sources to be used.
66    static InputSource *input_sources[] = { gpio_input };
67    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
68
69    // Hold B on plugin for Brook board mode.
70    pinMode(pinout.mux, OUTPUT);
71    if (button_holds.b)
72        digitalWrite(pinout.mux, HIGH);
73    else
74        digitalWrite(pinout.mux, LOW);
75
76    CommunicationBackend *primary_backend = new DInputBackend(input_sources, input_source_count);
77    delay(500);
78    bool usb_connected = UDADDR & _BV(ADDEN);
79
80    /* Select communication backend. */
81    if (usb_connected) {
82        // Default to DInput mode if USB is connected.
83        // Input viewer only used when connected to PC i.e. when using DInput mode.
84        backend_count = 2;
85        backends = new CommunicationBackend *[backend_count] {
86            primary_backend, new B0XXInputViewer(input_sources, input_source_count)
87        };
88    } else {
89        delete primary_backend;
90        if (button_holds.c_left) {
91            // Hold C-Left on plugin for N64.
92            primary_backend =
93                new N64Backend(input_sources, input_source_count, 60, pinout.joybus_data);
94        } else if (button_holds.a) {
95            // Hold A on plugin for GameCube adapter.
96            primary_backend =
97                new GamecubeBackend(input_sources, input_source_count, 0, pinout.joybus_data);
98        } else {
99            // Default to GameCube/Wii.
100            primary_backend =
101                new GamecubeBackend(input_sources, input_source_count, 125, pinout.joybus_data);
102        }
103
104        // If not DInput then only using 1 backend (no input viewer).
105        backend_count = 1;
106        backends = new CommunicationBackend *[backend_count] { primary_backend };
107    }
108
109    // Default to Melee mode.
110    primary_backend->SetGameMode(
111        new Melee20Button(socd::SOCD_2IP_NO_REAC, { .crouch_walk_os = false })
112    );
113}
114
115void loop() {
116    select_mode(backends[0]);
117
118    for (size_t i = 0; i < backend_count; i++) {
119        backends[i]->SendReport();
120    }
121
122    if (current_kb_mode != nullptr) {
123        current_kb_mode->SendReport(backends[0]->GetInputs());
124    }
125}