fork/GramFirmware

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

CarVacrebase to configurator818d962

gram
3.1 KiB93 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/SwitchMatrixInput.hpp"
10#include "reboot.hpp"
11#include "stdlib.hpp"
12
13Config config = default_config;
14
15CommunicationBackend **backends;
16size_t backend_count;
17KeyboardMode *current_kb_mode = nullptr;
18
19const size_t num_rows = 5;
20const size_t num_cols = 13;
21const uint row_pins[num_rows] = { 20, 19, 18, 17, 16 };
22const uint col_pins[num_cols] = { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
23// clang-format off
24const Button matrix[num_rows][num_cols] = {
25    { BTN_LF8,   BTN_LF7,  BTN_LF6,  BTN_LF5, NA, BTN_MB3,  BTN_MB1,  BTN_MB2,  NA, BTN_RF5, BTN_RF6,  BTN_RF7,  BTN_RF8 },
26    { BTN_LF4,  BTN_LF3,  BTN_LF2,  BTN_LF1, NA, BTN_MB4,  BTN_MB5,  BTN_MB6,  NA, BTN_RF1, BTN_RF2,  BTN_RF3,  BTN_RF4 },
27    { BTN_LF12, BTN_LF11, BTN_LF10, BTN_LF9, NA, BTN_MB7,  BTN_MB8,  BTN_MB9,  NA, BTN_RF9, BTN_RF10, BTN_RF11, BTN_RF12},
28    { NA,       BTN_LT5,  BTN_LT4,  BTN_LT3, NA, BTN_MB10, BTN_MB11, BTN_MB12, NA, BTN_RT3, BTN_RT4,  BTN_RT5,  NA      },
29    { NA,       NA,       BTN_LT1,  BTN_LT2, NA, BTN_LT6,  BTN_RT7,  BTN_RT6,  NA, BTN_RT2, BTN_RT1,  NA,       NA      },
30};
31// clang-format on
32DiodeDirection diode_direction = DiodeDirection::COL2ROW;
33
34const Pinout pinout = {
35    .joybus_data = 22,
36    .mux = -1,
37    .nunchuk_detect = -1,
38    .nunchuk_sda = -1,
39    .nunchuk_scl = -1,
40};
41
42void setup() {
43    static InputState inputs;
44
45    // Create switch matrix input source and use it to read button states for checking button holds.
46    static SwitchMatrixInput<num_rows, num_cols>
47        matrix_input(row_pins, col_pins, matrix, diode_direction);
48    matrix_input.UpdateInputs(inputs);
49
50    // Bootsel button hold as early as possible for safety.
51    if (inputs.mb1) {
52        reboot_bootloader();
53    }
54
55    // Turn on LED to indicate firmware booted.
56    gpio_init(PICO_DEFAULT_LED_PIN);
57    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
58    gpio_put(PICO_DEFAULT_LED_PIN, 1);
59
60    // Attempt to load config, or write default config to flash if failed to load config.
61    Persistence *persistence = new Persistence();
62    if (!persistence->LoadConfig(config)) {
63        persistence->SaveConfig(config);
64    }
65    delete persistence;
66
67    // Create array of input sources to be used.
68    static InputSource *input_sources[] = { &matrix_input };
69    size_t input_source_count = sizeof(input_sources) / sizeof(InputSource *);
70
71    backend_count =
72        initialize_backends(backends, inputs, input_sources, input_source_count, config, pinout);
73
74    setup_mode_activation_bindings(config.game_mode_configs, config.game_mode_configs_count);
75}
76
77void loop() {
78    select_mode(
79        backends[0],
80        config.game_mode_configs,
81        config.game_mode_configs_count,
82        config.keyboard_modes,
83        config.keyboard_modes_count
84    );
85
86    for (size_t i = 0; i < backend_count; i++) {
87        backends[i]->SendReport();
88    }
89
90    if (current_kb_mode != nullptr) {
91        current_kb_mode->SendReport(backends[0]->GetInputs());
92    }
93}