fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1#include "comms/GamecubeBackend.hpp" 2 3#include "core/InputSource.hpp" 4 5#include <GamecubeConsole.hpp> 6#include <hardware/pio.h> 7#include <hardware/timer.h> 8 9GamecubeBackend ::GamecubeBackend ( 10InputState & inputs , 11InputSource ** input_sources , 12size_t input_source_count , 13uint data_pin , 14PIO pio , 15int sm , 16int offset 17) 18 :CommunicationBackend (inputs ,input_sources ,input_source_count ), 19_gamecube (data_pin ,pio ,sm ,offset ) { 20_report = default_gc_report ; 21} 22 23CommunicationBackendId GamecubeBackend ::BackendId () { 24return COMMS_BACKEND_GAMECUBE ; 25} 26 27void GamecubeBackend ::SendReport () { 28// Update slower inputs before we start waiting for poll. 29if (!_gamemode -> isMelee ()) { 30ScanInputs (InputScanSpeed ::SLOW ); 31ScanInputs (InputScanSpeed ::MEDIUM ); 32 } 33 34// Read inputs 35_gamecube .WaitForPollStart (); 36 37// Update fast inputs in response to poll. 38// But wait 40us first so that we read inputs at the start of the 3rd byte of the poll command 39// not the second, so inputs are maximum 40us old by the time we start sending the report. 40busy_wait_us (40 ); 41if (!_gamemode -> isMelee ()) { 42ScanInputs (InputScanSpeed ::FAST ); 43 44// Run gamemode logic. 45UpdateOutputs (); 46 }else { 47//wait a bit extra to ensure comms are reliable 48busy_wait_us (3 ); 49 } 50 51if (!_gamemode -> isMelee ()) { 52// Digital outputs 53_report .a = _outputs .a ; 54_report .b = _outputs .b ; 55_report .x = _outputs .x ; 56_report .y = _outputs .y ; 57_report .z = _outputs .buttonR ; 58_report .l = _outputs .triggerLDigital ; 59_report .r = _outputs .triggerRDigital ; 60_report .start = _outputs .start ; 61_report .dpad_left = _outputs .dpadLeft |_outputs .select ; 62_report .dpad_right = _outputs .dpadRight |_outputs .home ; 63_report .dpad_down = _outputs .dpadDown ; 64_report .dpad_up = _outputs .dpadUp ; 65 66// Analog outputs 67_report .stick_x = _outputs .leftStickX ; 68_report .stick_y = _outputs .leftStickY ; 69_report .cstick_x = _outputs .rightStickX ; 70_report .cstick_y = _outputs .rightStickY ; 71_report .l_analog = _outputs .triggerLAnalog ; 72_report .r_analog = _outputs .triggerRAnalog ; 73 }else { 74// Digital outputs 75_report .a = _finalOutputs .a ; 76_report .b = _finalOutputs .b ; 77_report .x = _finalOutputs .x ; 78_report .y = _finalOutputs .y ; 79_report .z = _finalOutputs .buttonR ; 80_report .l = _finalOutputs .triggerLDigital ; 81_report .r = _finalOutputs .triggerRDigital ; 82_report .start = _finalOutputs .start ; 83_report .dpad_left = _finalOutputs .dpadLeft |_finalOutputs .select ; 84_report .dpad_right = _finalOutputs .dpadRight |_finalOutputs .home ; 85_report .dpad_down = _finalOutputs .dpadDown ; 86_report .dpad_up = _finalOutputs .dpadUp ; 87 88// Analog outputs 89_report .stick_x = _finalOutputs .leftStickX ; 90_report .stick_y = _finalOutputs .leftStickY ; 91_report .cstick_x = _finalOutputs .rightStickX ; 92_report .cstick_y = _finalOutputs .rightStickY ; 93_report .l_analog = _finalOutputs .triggerLAnalog ; 94_report .r_analog = _finalOutputs .triggerRAnalog ; 95 } 96 97// Send outputs to console unless poll command is invalid. 98if (_gamecube .WaitForPollEnd ()!= PollStatus ::ERROR ) { 99_gamecube .SendReport (& _report ); 100 } 101} 102 103int GamecubeBackend ::GetOffset () { 104return _gamecube .GetOffset (); 105}