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