fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
dccae31
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 ,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 () { 65static InputState inputs ; 66 67// Create GPIO input source and use it to read button states for checking button holds. 68gpio_input .UpdateInputs (inputs ); 69 70// Check bootsel button hold as early as possible for safety. 71if (inputs .mb1 ) { 72reboot_bootloader (); 73 } 74 75// Turn on LED to indicate firmware booted. 76gpio_init (PICO_DEFAULT_LED_PIN ); 77gpio_set_dir (PICO_DEFAULT_LED_PIN ,GPIO_OUT ); 78gpio_put (PICO_DEFAULT_LED_PIN ,1 ); 79 80// Attempt to load config, or write default config to flash if failed to load config. 81if (!persistence .LoadConfig (config )) { 82persistence .SaveConfig (config ); 83 } 84 85// Create array of input sources to be used. 86static InputSource * input_sources []= {}; 87size_t input_source_count = sizeof (input_sources ) /sizeof (InputSource * ); 88 89backend_count = 90initialize_backends (backends ,inputs ,input_sources ,input_source_count ,config ,pinout ); 91 92setup_mode_activation_bindings (config .game_mode_configs ,config .game_mode_configs_count ); 93} 94 95void loop () { 96select_mode (backends ,backend_count ,config ); 97 98for (size_t i = 0 ;i < backend_count ;i ++ ) { 99backends [i ]-> SendReport (); 100 } 101 102if (current_kb_mode != nullptr ) { 103current_kb_mode -> SendReport (backends [0 ]-> GetInputs ()); 104 } 105} 106 107/* Button inputs are read from the second core */ 108 109void setup1 () { 110while (backends == nullptr ) { 111tight_loop_contents (); 112 } 113} 114 115void loop1 () { 116if (backends != nullptr ) { 117gpio_input .UpdateInputs (backends [0 ]-> GetInputs ()); 118bool isMelee = false; 119for (size_t i = 0 ;i < backend_count ;i ++ ) { 120if (backends [i ]-> isMelee ()) { 121isMelee = true; 122 } 123 } 124if (isMelee ) { 125//get exactly 2 khz input scanning 126const uint32_t interval = 500 ;//microseconds 127const uint32_t quarterInterval = interval /4 ;//unit of 4 microseconds 128const uint32_t beforeMicros = micros (); 129uint32_t afterMicros = beforeMicros ; 130while ((afterMicros - beforeMicros )< interval ) { 131afterMicros = micros (); 132 } 133 134gpio_input .UpdateInputs (backends [0 ]-> GetInputs ()); 135for (size_t i = 0 ;i < backend_count ;i ++ ) { 136backends [i ]-> ScanInputs (); 137/* 138//begin timing macro 139//times given in half millisecond intervals 140const uint32_t frame = 16; 141const uint32_t hold = 8; 142static uint32_t counter = 0; 143static uint32_t test = 0; 144if((backends[i]->_inputs.lt1 && backends[i]->_inputs.rf7) || test == 1) { //modx and lightshield 145test = 1; 146const uint32_t loop = 6*frame; 147if(counter % loop <= 6*hold) { 148backends[i]->_inputs.lf1 = true; 149} else { 150backends[i]->_inputs.lf1 = false; 151} 152backends[i]->_inputs.lt1 = false; 153backends[i]->_inputs.rf7 = false; 154counter++; 155if(counter > 2000) { 156backends[i]->_inputs.lf1 = false; 157counter = 0; 158test = 0; 159} 160} 161if((backends[i]->_inputs.lt1 && backends[i]->_inputs.rf8) || test == 2) { //modx and midshield 162test = 2; 163const uint32_t loop = 5*frame; //should trigger sdi limits 164if(counter % loop <= 5*hold) { 165backends[i]->_inputs.lf1 = true; 166} else { 167backends[i]->_inputs.lf1 = false; 168} 169backends[i]->_inputs.lt1 = false; 170backends[i]->_inputs.rf8 = false; 171counter++; 172if(counter > 2000) { 173backends[i]->_inputs.lf1 = false; 174counter = 0; 175test = 0; 176} 177} 178//end timing macro 179*/ 180backends [i ]-> UpdateOutputs (); 181backends [i ]-> LimitOutputs (quarterInterval ); 182 } 183 } 184 } 185}