fork/GramFirmware

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

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

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