fork/GramFirmware

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

Julia RyanGram slim smashdccae31

gram
5.6 KiB185 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_LF6, 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                /*
138                //begin timing macro
139                //times given in half millisecond intervals
140                const uint32_t frame = 16;
141                const uint32_t hold = 8;
142                static uint32_t counter = 0;
143                static uint32_t test = 0;
144                if((backends[i]->_inputs.lt1 && backends[i]->_inputs.rf7) || test == 1) { //modx and lightshield
145                    test = 1;
146                    const uint32_t loop = 6*frame;
147                    if(counter % loop <= 6*hold) {
148                        backends[i]->_inputs.lf1 = true;
149                    } else {
150                        backends[i]->_inputs.lf1 = false;
151                    }
152                    backends[i]->_inputs.lt1 = false;
153                    backends[i]->_inputs.rf7 = false;
154                    counter++;
155                    if(counter > 2000) {
156                        backends[i]->_inputs.lf1 = false;
157                        counter = 0;
158                        test = 0;
159                    }
160                }
161                if((backends[i]->_inputs.lt1 && backends[i]->_inputs.rf8) || test == 2) { //modx and midshield
162                    test = 2;
163                    const uint32_t loop = 5*frame; //should trigger sdi limits
164                    if(counter % loop <= 5*hold) {
165                        backends[i]->_inputs.lf1 = true;
166                    } else {
167                        backends[i]->_inputs.lf1 = false;
168                    }
169                    backends[i]->_inputs.lt1 = false;
170                    backends[i]->_inputs.rf8 = false;
171                    counter++;
172                    if(counter > 2000) {
173                        backends[i]->_inputs.lf1 = false;
174                        counter = 0;
175                        test = 0;
176                    }
177                }
178                //end timing macro
179                */
180                backends[i]->UpdateOutputs();
181                backends[i]->LimitOutputs(quarterInterval);
182            }
183        }
184    }
185}