fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
9ba9ca8
codespace-scaling-space-guide-56rpxqvxpg2v57v
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/SwitchMatrixInput.hpp" 15#include "joybus_utils.hpp" 16#include "modes/Melee20Button.hpp" 17#include "stdlib.hpp" 18 19#include <pico/bootrom.h> 20 21CommunicationBackend ** backends ; 22size_t backend_count ; 23KeyboardMode * current_kb_mode = nullptr ; 24 25const size_t num_rows = 5 ; 26const size_t num_cols = 13 ; 27uint row_pins [num_rows ]= {20 ,19 ,18 ,17 ,16 }; 28uint col_pins [num_cols ]= {12 ,11 ,10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1 ,0 }; 29// clang-format off 30SwitchMatrixElement matrix [num_rows ][num_cols ]= { 31 {NA ,NA ,NA ,NA ,NA ,BTN (select ),BTN (start ),BTN (home ),NA ,BTN (r ),BTN (y ),BTN (lightshield ),BTN (midshield )}, 32 {BTN (l ),BTN (left ),BTN (down ),BTN (right ),NA ,NA ,NA ,NA ,NA ,BTN (b ),BTN (x ),BTN (z ),BTN (up ) }, 33 {NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA }, 34 {NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,BTN (c_left ),BTN (c_up ),BTN (c_right ),NA }, 35 {NA ,NA ,BTN (mod_x ),BTN (mod_y ),NA ,NA ,NA ,NA ,NA ,BTN (c_down ),BTN (a ),NA ,NA }, 36}; 37// clang-format on 38DiodeDirection diode_direction = DiodeDirection ::COL2ROW ; 39 40const Pinout pinout = { 41 .joybus_data = 22 , 42 .mux = -1 , 43 .nunchuk_detect = -1 , 44 .nunchuk_sda = -1 , 45 .nunchuk_scl = -1 , 46}; 47 48void setup () { 49// Create switch matrix input source and use it to read button states for checking button holds. 50SwitchMatrixInput < num_rows ,num_cols > * matrix_input = 51new SwitchMatrixInput < num_rows ,num_cols > (row_pins ,col_pins ,matrix ,diode_direction ); 52 53InputState button_holds ; 54matrix_input -> UpdateInputs (button_holds ); 55 56// Bootsel button hold as early as possible for safety. 57if (button_holds .start ) { 58reset_usb_boot (0 ,0 ); 59 } 60 61// Turn on LED to indicate firmware booted. 62gpio_init (PICO_DEFAULT_LED_PIN ); 63gpio_set_dir (PICO_DEFAULT_LED_PIN ,GPIO_OUT ); 64gpio_put (PICO_DEFAULT_LED_PIN ,1 ); 65 66// Create array of input sources to be used. 67static InputSource * input_sources []= {matrix_input }; 68size_t input_source_count = sizeof (input_sources ) /sizeof (InputSource * ); 69 70ConnectedConsole console = detect_console (pinout .joybus_data ); 71 72/* Select communication backend. */ 73CommunicationBackend * primary_backend ; 74if (console == ConnectedConsole ::NONE ) { 75if (button_holds .x ) { 76// If no console detected and X is held on plugin then use Switch USB backend. 77NintendoSwitchBackend ::RegisterDescriptor (); 78backend_count = 1 ; 79primary_backend = new NintendoSwitchBackend (input_sources ,input_source_count ); 80backends = new CommunicationBackend * [backend_count ] {primary_backend }; 81 82// Default to Ultimate mode on Switch. 83primary_backend -> SetGameMode (new Ultimate (socd::SOCD_2IP )); 84return ; 85 }else if (button_holds .z ) { 86// If no console detected and Z is held on plugin then use DInput backend. 87TUGamepad ::registerDescriptor (); 88TUKeyboard ::registerDescriptor (); 89backend_count = 2 ; 90primary_backend = new DInputBackend (input_sources ,input_source_count ); 91backends = new CommunicationBackend * [backend_count ] { 92primary_backend ,new B0XXInputViewer (input_sources ,input_source_count ) 93 }; 94 }else { 95// Default to XInput mode if no console detected and no other mode forced. 96backend_count = 2 ; 97primary_backend = new XInputBackend (input_sources ,input_source_count ); 98backends = new CommunicationBackend * [backend_count ] { 99primary_backend ,new B0XXInputViewer (input_sources ,input_source_count ) 100 }; 101 } 102 }else { 103if (console == ConnectedConsole ::GAMECUBE ) { 104primary_backend = 105new GamecubeBackend (input_sources ,input_source_count ,pinout .joybus_data ); 106 }else if (console == ConnectedConsole ::N64 ) { 107primary_backend = new N64Backend (input_sources ,input_source_count ,pinout .joybus_data ); 108 } 109 110// If console then only using 1 backend (no input viewer). 111backend_count = 1 ; 112backends = new CommunicationBackend * [backend_count ] {primary_backend }; 113 } 114 115// Default to Melee mode. 116primary_backend -> SetGameMode ( 117new Melee20Button (socd::SOCD_2IP_NO_REAC , { .crouch_walk_os = false }) 118 ); 119} 120 121void loop () { 122select_mode (backends [0 ]); 123 124for (size_t i = 0 ;i < backend_count ;i ++ ) { 125backends [i ]-> SendReport (); 126 } 127 128if (current_kb_mode != nullptr ) { 129current_kb_mode -> SendReport (backends [0 ]-> GetInputs ()); 130 } 131}