fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
1a77a04
v0.1.1
1#include "comms/B0XXInputViewer.hpp" 2#include "comms/DInputBackend.hpp" 3#include "comms/GamecubeBackend.hpp" 4#include "comms/N64Backend.hpp" 5#include "comms/NintendoSwitchBackend.hpp" 6#include "comms/XInputBackend.hpp" 7#include "config/mode_selection.hpp" 8#include "core/CommunicationBackend.hpp" 9#include "core/InputMode.hpp" 10#include "core/KeyboardMode.hpp" 11#include "core/pinout.hpp" 12#include "core/socd.hpp" 13#include "core/state.hpp" 14#include "input/GpioButtonInput.hpp" 15#include "input/NunchukInput.hpp" 16#include "joybus_utils.hpp" 17#include "modes/Melee20Button.hpp" 18#include "stdlib.hpp" 19 20#include <pico/bootrom.h> 21 22CommunicationBackend ** backends = nullptr ; 23size_t backend_count ; 24KeyboardMode * current_kb_mode = nullptr ; 25 26GpioButtonMapping button_mappings []= { 27 {& InputState ::l ,5 }, 28 {& InputState ::left ,4 }, 29 {& InputState ::down ,3 }, 30 {& InputState ::right ,2 }, 31 {& InputState ::up2 ,1 }, 32 33 {& InputState ::mod_x ,6 }, 34 {& InputState ::mod_y ,7 }, 35 36 {& InputState ::select ,10 }, 37 {& InputState ::start ,0 }, 38 {& InputState ::home ,11 }, 39 40 {& InputState ::c_left ,13 }, 41 {& InputState ::c_up ,12 }, 42 {& InputState ::c_down ,15 }, 43 {& InputState ::a ,14 }, 44 {& InputState ::c_right ,16 }, 45 46 {& InputState ::b ,26 }, 47 {& InputState ::x ,21 }, 48 {& InputState ::z ,19 }, 49 {& InputState ::up ,17 }, 50 51 {& InputState ::r ,27 }, 52 {& InputState ::y ,22 }, 53 {& InputState ::lightshield ,20 }, 54 {& InputState ::midshield ,18 }, 55}; 56size_t button_count = sizeof (button_mappings ) /sizeof (GpioButtonMapping ); 57 58const Pinout pinout = { 59 .joybus_data = 28 , 60 .mux = -1 , 61 .nunchuk_detect = -1 , 62 .nunchuk_sda = -1 , 63 .nunchuk_scl = -1 , 64}; 65 66void setup () { 67// Create GPIO input source and use it to read button states for checking button holds. 68GpioButtonInput * gpio_input = new GpioButtonInput (button_mappings ,button_count ); 69 70InputState button_holds ; 71gpio_input -> UpdateInputs (button_holds ); 72 73// Bootsel button hold as early as possible for safety. 74if (button_holds .start ) { 75reset_usb_boot (0 ,0 ); 76 } 77 78// Turn on LED to indicate firmware booted. 79gpio_init (PICO_DEFAULT_LED_PIN ); 80gpio_set_dir (PICO_DEFAULT_LED_PIN ,GPIO_OUT ); 81gpio_put (PICO_DEFAULT_LED_PIN ,1 ); 82 83// Create array of input sources to be used. 84static InputSource * input_sources []= {gpio_input }; 85size_t input_source_count = sizeof (input_sources ) /sizeof (InputSource * ); 86 87ConnectedConsole console = detect_console (pinout .joybus_data ); 88 89/* Select communication backend. */ 90CommunicationBackend * primary_backend ; 91if (console == ConnectedConsole ::NONE ) { 92if (button_holds .x ) { 93// If no console detected and X is held on plugin then use Switch USB backend. 94NintendoSwitchBackend ::RegisterDescriptor (); 95backend_count = 1 ; 96primary_backend = new NintendoSwitchBackend (input_sources ,input_source_count ); 97backends = new CommunicationBackend * [backend_count ] {primary_backend }; 98 99// Default to Ultimate mode on Switch. 100primary_backend -> SetGameMode (new UltimateUSB (socd::SOCD_2IP )); 101return ; 102 }else if (button_holds .z ) { 103// If no console detected and Z is held on plugin then use DInput backend. 104TUGamepad ::registerDescriptor (); 105TUKeyboard ::registerDescriptor (); 106backend_count = 2 ; 107primary_backend = new DInputBackend (input_sources ,input_source_count ); 108backends = new CommunicationBackend * [backend_count ] { 109primary_backend ,new B0XXInputViewer (input_sources ,input_source_count ) 110 }; 111 }else { 112// Default to XInput mode if no console detected and no other mode forced. 113backend_count = 2 ; 114primary_backend = new XInputBackend (input_sources ,input_source_count ); 115backends = new CommunicationBackend * [backend_count ] { 116primary_backend ,new B0XXInputViewer (input_sources ,input_source_count ) 117 }; 118 } 119 }else { 120if (console == ConnectedConsole ::GAMECUBE ) { 121primary_backend = 122new GamecubeBackend (input_sources ,input_source_count ,pinout .joybus_data ); 123 }else if (console == ConnectedConsole ::N64 ) { 124primary_backend = new N64Backend (input_sources ,input_source_count ,pinout .joybus_data ); 125 } 126 127// If console then only using 1 backend (no input viewer). 128backend_count = 1 ; 129backends = new CommunicationBackend * [backend_count ] {primary_backend }; 130 } 131 132// Default to Melee mode. 133primary_backend -> SetGameMode ( 134new Melee20Button (socd::SOCD_2IP_NO_REAC , { .crouch_walk_os = false }) 135 ); 136} 137 138void loop () { 139select_mode (backends [0 ]); 140 141for (size_t i = 0 ;i < backend_count ;i ++ ) { 142backends [i ]-> SendReport (); 143 } 144 145if (current_kb_mode != nullptr ) { 146current_kb_mode -> SendReport (backends [0 ]-> GetInputs ()); 147 } 148} 149 150/* Nunchuk code runs on the second core */ 151NunchukInput * nunchuk = nullptr ; 152 153void setup1 () { 154while (backends == nullptr ) { 155tight_loop_contents (); 156 } 157 158// Create Nunchuk input source. 159nunchuk = new NunchukInput (Wire ,pinout .nunchuk_detect ,pinout .nunchuk_sda ,pinout .nunchuk_scl ); 160} 161 162void loop1 () { 163if (backends != nullptr ) { 164nunchuk -> UpdateInputs (backends [0 ]-> GetInputs ()); 165busy_wait_us (50 ); 166 } 167}