fork/GramFirmware

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

CarVacfix ranadigital remapping31430bd

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