fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
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 () { 43static InputState inputs ; 44 45// Create switch matrix input source and use it to read button states for checking button holds. 46static SwitchMatrixInput < num_rows ,num_cols > 47matrix_input (row_pins ,col_pins ,matrix ,diode_direction ); 48matrix_input .UpdateInputs (inputs ); 49 50// Bootsel button hold as early as possible for safety. 51if (inputs .mb1 ) { 52reboot_bootloader (); 53 } 54 55// Turn on LED to indicate firmware booted. 56gpio_init (PICO_DEFAULT_LED_PIN ); 57gpio_set_dir (PICO_DEFAULT_LED_PIN ,GPIO_OUT ); 58gpio_put (PICO_DEFAULT_LED_PIN ,1 ); 59 60// Attempt to load config, or write default config to flash if failed to load config. 61Persistence * persistence = new Persistence (); 62if (!persistence -> LoadConfig (config )) { 63persistence -> SaveConfig (config ); 64 } 65delete persistence ; 66 67// Create array of input sources to be used. 68static InputSource * input_sources []= {& matrix_input }; 69size_t input_source_count = sizeof (input_sources ) /sizeof (InputSource * ); 70 71backend_count = 72initialize_backends (backends ,inputs ,input_sources ,input_source_count ,config ,pinout ); 73 74setup_mode_activation_bindings (config .game_mode_configs ,config .game_mode_configs_count ); 75} 76 77void loop () { 78select_mode ( 79backends [0 ], 80config .game_mode_configs , 81config .game_mode_configs_count , 82config .keyboard_modes , 83config .keyboard_modes_count 84 ); 85 86for (size_t i = 0 ;i < backend_count ;i ++ ) { 87backends [i ]-> SendReport (); 88 } 89 90if (current_kb_mode != nullptr ) { 91current_kb_mode -> SendReport (backends [0 ]-> GetInputs ()); 92 } 93}