fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1#include "comms/GamecubeBackend.hpp" 2 3#include "core/ControllerMode.hpp" 4#include "core/InputSource.hpp" 5 6#include <Nintendo.h> 7 8GamecubeBackend ::GamecubeBackend ( 9InputState & inputs , 10InputSource ** input_sources , 11size_t input_source_count , 12int polling_rate , 13int data_pin 14) 15 :CommunicationBackend (inputs ,input_sources ,input_source_count ), 16_gamecube (data_pin ) { 17_data = defaultGamecubeData ; 18 19/* 20if (polling_rate > 0) { 21// Delay used between input updates to postpone them until right before the 22// next poll, while also leaving time (850us) for processing to finish. 23_delay = (1000000 / polling_rate) - 850; 24} else { 25// If polling rate is set to 0, disable the delay. 26_delay = 0; 27} 28*/ 29_delay = 0 ; 30 31//Set up hardware timer 32TCCR1A = 0 ; 33TCCR1B = (1 <<CS11 ) | (1 <<CS10 );//64 clock divider -> 4 microsecond resolution 34zeroTcnt1 (); 35} 36 37CommunicationBackendId GamecubeBackend ::BackendId () { 38return COMMS_BACKEND_GAMECUBE ; 39} 40 41void GamecubeBackend ::SendReport () { 42static bool status = false;//true = normal poll 43static uint32_t minLoop = 16384 ; 44static uint loopCount = 0 ; 45static bool detect = true;//false means run 46static uint sampleCount = 1 ; 47static uint sampleSpacing = 0 ; 48static uint16_t loopTime = 0 ; 49const uint fastestLoop = 975 ;//fastest possible loop; platform-dependent 50//const uint fastestLoop = 450; //fastest possible loop; platform-dependent 51 52//read how long this loop took 53loopTime = readTcnt1 ();//each unit is 4 microseconds 54//zero the counter so that it doesn't overflow 55zeroTcnt1 (); 56 57if (detect && status ) { 58//run loop time detection procedure 59#ifdef TIMINGDEBUG 60digitalWrite (21 ,HIGH ); 61#endif 62loopCount ++ ; 63if (loopCount > 5 && loopTime > 300 /4 ) {//screen out implausibly fast samples; the limit is 2500 Hz (400 us) so we use 300 us as the limit, ignore first few polls 64minLoop = min (minLoop ,loopTime ); 65 } 66if (loopCount >=100 ) { 67detect = false; 68sampleCount = 1 ; 69while (1000 /4 * sampleCount <=minLoop ) {//we want [sampleCount] ms-spaced samples within the smallest possible loop 70sampleCount ++ ; 71 } 72sampleSpacing = minLoop /sampleCount ; 73if (sampleSpacing < (fastestLoop /4 )&& sampleCount > 1 ) { 74sampleCount -- ; 75sampleSpacing = minLoop /sampleCount ; 76 } 77 } 78// Make sure to respond while measuring. 79ScanInputs (); 80 81// Run gamemode logic. 82UpdateOutputs (); 83 84// Digital outputs 85_data .report .a = _outputs .a ; 86_data .report .b = _outputs .b ; 87_data .report .x = _outputs .x ; 88_data .report .y = _outputs .y ; 89_data .report .z = _outputs .buttonR ; 90_data .report .l = _outputs .triggerLDigital ; 91_data .report .r = _outputs .triggerRDigital ; 92_data .report .start = _outputs .start ; 93_data .report .dleft = _outputs .dpadLeft |_outputs .select ; 94_data .report .dright = _outputs .dpadRight |_outputs .home ; 95_data .report .ddown = _outputs .dpadDown ; 96_data .report .dup = _outputs .dpadUp ; 97 98// Analog outputs 99_data .report .xAxis = _outputs .leftStickX ; 100_data .report .yAxis = _outputs .leftStickY ; 101_data .report .cxAxis = _outputs .rightStickX ; 102_data .report .cyAxis = _outputs .rightStickY ; 103_data .report .left = _outputs .triggerLAnalog + 31 ; 104_data .report .right = _outputs .triggerRAnalog + 31 ; 105 }else { 106if (loopTime > minLoop + (minLoop >>1 )) {//if the loop time is 50% longer than expected 107detect = true;//stop scanning inputs briefly and re-measure timings 108loopCount = 0 ; 109sampleCount = 1 ; 110sampleSpacing = 0 ; 111loopTime = 0 ; 112 } 113//run the delay procedure based on samplespacing 114//in the stock arduino software, it samples 850 us after the end of the poll response 115//we want the last sample to begin [850 + extra computation time] before the beginning of the last poll to give room for the sample and the travel time+nerf computation 116// 117for (uint i = 0 ;i < sampleCount ;i ++ ) { 118 119const uint16_t nerfTime = 250 /4 ; 120const uint16_t computationTime = 700 /4 + nerfTime ;//*_nerfOn;//depends on the platform; 4us steps. 121//700 microseconds is sufficient with no travel time computation 122const uint16_t targetTime = ((i + 1 )* sampleSpacing )- computationTime ; 123//const uint16_t targetTime = i*sampleSpacing; 124int count = 0 ; 125while (readTcnt1 ()< targetTime ) { 126count ++ ;//do something? 127//spinlock 128 } 129 130ScanInputs (); 131 132// Run gamemode logic. 133UpdateOutputs (); 134 135//if(_nerfOn) { 136if (_gamemode -> isMelee ()) { 137//APPLY NERFS HERE 138OutputState nerfedOutputs ; 139limitOutputs (sampleSpacing ,_nerfOn ?AB_A :AB_B ,_inputs ,_outputs ,nerfedOutputs ); 140// Digital outputs 141_data .report .a = nerfedOutputs .a ; 142_data .report .b = nerfedOutputs .b ; 143_data .report .x = nerfedOutputs .x ; 144_data .report .y = nerfedOutputs .y ; 145_data .report .z = nerfedOutputs .buttonR ; 146_data .report .l = nerfedOutputs .triggerLDigital ; 147_data .report .r = nerfedOutputs .triggerRDigital ; 148_data .report .start = nerfedOutputs .start ; 149_data .report .dleft = nerfedOutputs .dpadLeft |nerfedOutputs .select ; 150_data .report .dright = nerfedOutputs .dpadRight |nerfedOutputs .home ; 151_data .report .ddown = nerfedOutputs .dpadDown ; 152_data .report .dup = nerfedOutputs .dpadUp ; 153 154// Analog outputs 155_data .report .xAxis = nerfedOutputs .leftStickX ; 156_data .report .yAxis = nerfedOutputs .leftStickY ; 157_data .report .cxAxis = nerfedOutputs .rightStickX ; 158_data .report .cyAxis = nerfedOutputs .rightStickY ; 159_data .report .left = nerfedOutputs .triggerLAnalog + 31 ; 160_data .report .right = nerfedOutputs .triggerRAnalog + 31 ; 161 }else { 162// Digital outputs 163_data .report .a = _outputs .a ; 164_data .report .b = _outputs .b ; 165_data .report .x = _outputs .x ; 166_data .report .y = _outputs .y ; 167_data .report .z = _outputs .buttonR ; 168_data .report .l = _outputs .triggerLDigital ; 169_data .report .r = _outputs .triggerRDigital ; 170_data .report .start = _outputs .start ; 171_data .report .dleft = _outputs .dpadLeft |_outputs .select ; 172_data .report .dright = _outputs .dpadRight |_outputs .home ; 173_data .report .ddown = _outputs .dpadDown ; 174_data .report .dup = _outputs .dpadUp ; 175 176// Analog outputs 177_data .report .xAxis = _outputs .leftStickX ; 178_data .report .yAxis = _outputs .leftStickY ; 179_data .report .cxAxis = _outputs .rightStickX ; 180_data .report .cyAxis = _outputs .rightStickY ; 181_data .report .left = _outputs .triggerLAnalog + 31 ; 182_data .report .right = _outputs .triggerRAnalog + 31 ; 183 } 184 } 185 } 186 187// Send outputs to console. 188status = _gamecube -> write (_data ); 189}