fork/GramFirmware

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

CarVacfix ci and remove builds for avr for nowfb9ad76

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