fork/GramFirmware

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

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

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