fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
fb9ad76
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/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 = 50sizeof (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 () { 68static InputState inputs ; 69 70// Create GPIO input source and use it to read button states for checking button holds. 71gpio_input .UpdateInputs (inputs ); 72 73// Check bootsel button hold as early as possible for safety. 74if (inputs .mb1 ) { 75reboot_bootloader (); 76 } 77 78// Also scan I/O expander for inputs, but after the bootloader check in case it causes a crash. 79expander_input .UpdateInputs (inputs ); 80 81// Turn on LED to indicate firmware booted. 82gpio_init (PICO_DEFAULT_LED_PIN ); 83gpio_set_dir (PICO_DEFAULT_LED_PIN ,GPIO_OUT ); 84gpio_put (PICO_DEFAULT_LED_PIN ,1 ); 85 86// Attempt to load config, or write default config to flash if failed to load config. 87if (!persistence .LoadConfig (config )) { 88persistence .SaveConfig (config ); 89 } 90 91// Create array of input sources to be used. 92static InputSource * input_sources []= {}; 93size_t input_source_count = sizeof (input_sources ) /sizeof (InputSource * ); 94 95backend_count = 96initialize_backends (backends ,inputs ,input_sources ,input_source_count ,config ,pinout ); 97 98setup_mode_activation_bindings (config .game_mode_configs ,config .game_mode_configs_count ); 99} 100 101void loop () { 102select_mode (backends ,backend_count ,config ); 103 104for (size_t i = 0 ;i < backend_count ;i ++ ) { 105backends [i ]-> SendReport (); 106 } 107 108if (current_kb_mode != nullptr ) { 109current_kb_mode -> SendReport (backends [0 ]-> GetInputs ()); 110 } 111} 112 113/* Button inputs are read from the second core */ 114 115void setup1 () { 116while (backends == nullptr ) { 117tight_loop_contents (); 118 } 119} 120 121void loop1 () { 122if (backends != nullptr ) { 123InputState & inputs = backends [0 ]-> GetInputs (); 124gpio_input .UpdateInputs (inputs ); 125expander_input .UpdateInputs (inputs ); 126bool isMelee = false; 127for (size_t i = 0 ;i < backend_count ;i ++ ) { 128if (backends [i ]-> isMelee ()) { 129isMelee = true; 130 } 131 } 132if (isMelee ) { 133//get exactly 2 khz input scanning 134const uint32_t interval = 500 ;//microseconds 135const uint32_t quarterInterval = interval /4 ;//unit of 4 microseconds 136const uint32_t beforeMicros = micros (); 137uint32_t afterMicros = beforeMicros ; 138while ((afterMicros - beforeMicros )< interval ) { 139afterMicros = micros (); 140 } 141 142gpio_input .UpdateInputs (inputs ); 143expander_input .UpdateInputs (inputs ); 144for (size_t i = 0 ;i < backend_count ;i ++ ) { 145backends [i ]-> ScanInputs (); 146backends [i ]-> UpdateOutputs (); 147backends [i ]-> LimitOutputs (quarterInterval ); 148 } 149 } 150 } 151}