fork/GramFirmware

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

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

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