fork/GramFirmware

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

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

master
3.9 KiB121 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/KeyboardMode.hpp"
9#include "core/pinout.hpp"
10#include "core/socd.hpp"
11#include "core/state.hpp"
12#include "input/GpioButtonInput.hpp"
13#include "input/NunchukInput.hpp"
14#include "modes/Melee20Button.hpp"
15#include "stdlib.hpp"
16
17CommunicationBackend **backends = nullptr;
18size_t backend_count;
19KeyboardMode *current_kb_mode = nullptr;
20
21// Customise this to match your controller's pinout.
22GpioButtonMapping button_mappings[] = {
23    {&InputState::l,            15},
24    { &InputState::left,        16},
25    { &InputState::down,        14},
26    { &InputState::right,       1 },
27
28    { &InputState::mod_x,       12},
29    { &InputState::mod_y,       0 },
30
31    { &InputState::select,      2 },
32    { &InputState::start,       4 },
33    { &InputState::home,        3 },
34
35    { &InputState::c_left,      8 },
36    { &InputState::c_up,        10},
37    { &InputState::c_down,      6 },
38    { &InputState::a,           9 },
39    { &InputState::c_right,     5 },
40
41    { &InputState::b,           A2},
42    { &InputState::x,           A1},
43    { &InputState::z,           A0},
44    { &InputState::up,          13},
45
46    { &InputState::r,           7 },
47    { &InputState::y,           A5},
48    { &InputState::lightshield, A4},
49    { &InputState::midshield,   A3},
50};
51size_t button_count = sizeof(button_mappings) / sizeof(GpioButtonMapping);
52
53Pinout pinout = {
54    .joybus_data = 17,
55    .mux = -1,
56    .nunchuk_detect = -1,
57    .nunchuk_sda = -1,
58    .nunchuk_scl = -1,
59};
60
61void setup() {
62    // Create Nunchuk input source - must be done before GPIO input source otherwise it would
63    // disable the pullups on the i2c pins.
64    NunchukInput *nunchuk = new NunchukInput();
65
66    // Create GPIO input source and use it to read button states for checking button holds.
67    GpioButtonInput *gpio_input = new GpioButtonInput(button_mappings, button_count);
68
69    InputState button_holds;
70    gpio_input->UpdateInputs(button_holds);
71
72    // Create array of input sources to be used.
73    static InputSource *input_sources[] = { gpio_input, nunchuk };
74    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
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.a) {
91            // Hold A on plugin for GameCube adapter.
92            primary_backend =
93                new GamecubeBackend(input_sources, input_source_count, 0, pinout.joybus_data);
94        } else {
95            // Default to GameCube/Wii.
96            primary_backend =
97                new GamecubeBackend(input_sources, input_source_count, 125, pinout.joybus_data);
98        }
99
100        // If not DInput then only using 1 backend (no input viewer).
101        backend_count = 1;
102        backends = new CommunicationBackend *[backend_count] { primary_backend };
103    }
104
105    // Default to Melee mode.
106    primary_backend->SetGameMode(
107        new Melee20Button(socd::SOCD_2IP_NO_REAC, { .crouch_walk_os = false })
108    );
109}
110
111void loop() {
112    select_mode(backends[0]);
113
114    for (size_t i = 0; i < backend_count; i++) {
115        backends[i]->SendReport();
116    }
117
118    if (current_kb_mode != nullptr) {
119        current_kb_mode->SendReport(backends[0]->GetInputs());
120    }
121}