fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
3.4 KiB105 linesraw
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(
10    InputState &inputs,
11    InputSource **input_sources,
12    size_t input_source_count,
13    uint data_pin,
14    PIO pio,
15    int sm,
16    int 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() {
24    return COMMS_BACKEND_GAMECUBE;
25}
26
27void GamecubeBackend::SendReport() {
28    // Update slower inputs before we start waiting for poll.
29    if (!_gamemode->isMelee()) {
30        ScanInputs(InputScanSpeed::SLOW);
31        ScanInputs(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.
40    busy_wait_us(40);
41    if (!_gamemode->isMelee()) {
42        ScanInputs(InputScanSpeed::FAST);
43
44        // Run gamemode logic.
45        UpdateOutputs();
46    } else {
47        //wait a bit extra to ensure comms are reliable
48        busy_wait_us(3);
49    }
50
51    if (!_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.
98    if (_gamecube.WaitForPollEnd() != PollStatus::ERROR) {
99        _gamecube.SendReport(&_report);
100    }
101}
102
103int GamecubeBackend::GetOffset() {
104    return _gamecube.GetOffset();
105}