fork/GramFirmware

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

CarVacrebase to configurator818d962

gram
3.8 KiB143 linesraw
1#include "comms/backend_init.hpp"
2#include "config_defaults.hpp"
3#include "core/CommunicationBackend.hpp"
4#include "core/KeyboardMode.hpp"
5#include "core/Persistence.hpp"
6#include "core/mode_selection.hpp"
7#include "core/pinout.hpp"
8#include "core/state.hpp"
9#include "input/DebouncedGpioButtonInput.hpp"
10#include "input/NunchukInput.hpp"
11#include "reboot.hpp"
12#include "stdlib.hpp"
13
14#include <config.pb.h>
15
16Config config = default_config;
17
18GpioButtonMapping button_mappings[] = {
19    {BTN_LF1,  9 },
20    { BTN_LF2, 8 },
21    { BTN_LF3, 7 },
22    { BTN_LF4, 6 },
23
24    { BTN_LT1, 10},
25    { BTN_LT2, 11},
26
27    { BTN_MB1, 12},
28
29    { BTN_RT1, 28},
30    { BTN_RT2, 27},
31    { BTN_RT3, 14},
32    { BTN_RT4, 13},
33    { BTN_RT5, 15},
34
35    { BTN_RF1, 19},
36    { BTN_RF2, 18},
37    { BTN_RF3, 17},
38    { BTN_RF4, 16},
39
40    { BTN_RF5, 26},
41    { BTN_RF6, 22},
42    { BTN_RF7, 21},
43    { BTN_RF8, 20},
44};
45const size_t button_count = sizeof(button_mappings) / sizeof(GpioButtonMapping);
46
47const Pinout pinout = {
48    .joybus_data = 2,
49    .mux = -1,
50    .nunchuk_detect = 3,
51    .nunchuk_sda = 4,
52    .nunchuk_scl = 5,
53};
54
55DebouncedGpioButtonInput<button_count> gpio_input(button_mappings);
56NunchukInput *nunchuk = nullptr;
57
58CommunicationBackend **backends = nullptr;
59size_t backend_count;
60KeyboardMode *current_kb_mode = nullptr;
61
62void setup() {
63    static InputState inputs;
64
65    // Create GPIO input source and use it to read button states for checking button holds.
66    gpio_input.UpdateInputs(inputs);
67
68    // Check bootsel button hold as early as possible for safety.
69    if (inputs.mb1) {
70        reboot_bootloader();
71    }
72
73    // Turn on LED to indicate firmware booted.
74    gpio_init(PICO_DEFAULT_LED_PIN);
75    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
76    gpio_put(PICO_DEFAULT_LED_PIN, 1);
77
78    // Attempt to load config, or write default config to flash if failed to load config.
79    if (!persistence.LoadConfig(config)) {
80        persistence.SaveConfig(config);
81    }
82
83    // Create Nunchuk input source.
84    nunchuk = new NunchukInput(Wire, pinout.nunchuk_detect, pinout.nunchuk_sda, pinout.nunchuk_scl);
85
86    // Create array of input sources to be used.
87    static InputSource *input_sources[] = { nunchuk };
88    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
89
90    backend_count =
91        initialize_backends(backends, inputs, input_sources, input_source_count, config, pinout);
92
93    setup_mode_activation_bindings(config.game_mode_configs, config.game_mode_configs_count);
94}
95
96void loop() {
97    select_mode(backends, backend_count, config);
98
99    for (size_t i = 0; i < backend_count; i++) {
100        backends[i]->SendReport();
101    }
102
103    if (current_kb_mode != nullptr) {
104        current_kb_mode->SendReport(backends[0]->GetInputs());
105    }
106}
107
108/* Button inputs are read from the second core */
109
110void setup1() {
111    while (backends == nullptr) {
112        tight_loop_contents();
113    }
114}
115
116void loop1() {
117    if (backends != nullptr) {
118        gpio_input.UpdateInputs(backends[0]->GetInputs());
119        bool isMelee = false;
120        for (size_t i = 0; i < backend_count; i++) {
121            if(backends[i]->isMelee()) {
122                isMelee = true;
123            }
124        }
125        if (isMelee) {
126            //get exactly 2 khz input scanning
127            const uint32_t interval = 500;//microseconds
128            const uint32_t quarterInterval = interval/4;//unit of 4 microseconds
129            const uint32_t beforeMicros = micros();
130            uint32_t afterMicros = beforeMicros;
131            while ((afterMicros - beforeMicros) < interval) {
132                afterMicros = micros();
133            }
134
135            gpio_input.UpdateInputs(backends[0]->GetInputs());
136            for (size_t i = 0; i < backend_count; i++) {
137                backends[i]->ScanInputs();
138                backends[i]->UpdateOutputs();
139                backends[i]->LimitOutputs(quarterInterval);
140            }
141        }
142    }
143}