fork/GramFirmware

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

CarVacrebase to configurator818d962

gram
3.2 KiB93 linesraw
1#include "comms/XInputBackend.hpp"
2
3#include "core/CommunicationBackend.hpp"
4#include "core/state.hpp"
5
6#include <Adafruit_USBD_XInput.hpp>
7
8XInputBackend::XInputBackend(
9    InputState &inputs,
10    InputSource **input_sources,
11    size_t input_source_count
12)
13    : CommunicationBackend(inputs, input_sources, input_source_count),
14      _xinput() {
15    Serial.end();
16    _xinput.begin();
17    Serial.begin(115200);
18
19    TinyUSBDevice.setID(0x0738, 0x4726);
20}
21
22CommunicationBackendId XInputBackend::BackendId() {
23    return COMMS_BACKEND_XINPUT;
24}
25
26void XInputBackend::SendReport() {
27    if (!_gamemode->isMelee()) {
28        ScanInputs(InputScanSpeed::SLOW);
29        ScanInputs(InputScanSpeed::MEDIUM);
30    }
31
32    while (!_xinput.ready()) {
33        tight_loop_contents();
34    }
35
36    if (!_gamemode->isMelee()) {
37        ScanInputs(InputScanSpeed::FAST);
38
39        UpdateOutputs();
40    }
41
42    if (!_gamemode->isMelee()) {
43        // Digital outputs
44        _report.a = _outputs.a;
45        _report.b = _outputs.b;
46        _report.x = _outputs.x;
47        _report.y = _outputs.y;
48        _report.lb = _outputs.buttonL;
49        _report.rb = _outputs.buttonR;
50        _report.lt = _outputs.triggerLDigital ? 255 : _outputs.triggerLAnalog;
51        _report.rt = _outputs.triggerRDigital ? 255 : _outputs.triggerRAnalog;
52        _report.start = _outputs.start;
53        _report.back = _outputs.select;
54        _report.home = _outputs.home;
55        _report.dpad_up = _outputs.dpadUp;
56        _report.dpad_down = _outputs.dpadDown;
57        _report.dpad_left = _outputs.dpadLeft;
58        _report.dpad_right = _outputs.dpadRight;
59        _report.ls = _outputs.leftStickClick;
60        _report.rs = _outputs.rightStickClick;
61
62        _report.lx = (_outputs.leftStickX - 128) * 65535 / 255 + 128;
63        _report.ly = (_outputs.leftStickY - 128) * 65535 / 255 + 128;
64        _report.rx = (_outputs.rightStickX - 128) * 65535 / 255 + 128;
65        _report.ry = (_outputs.rightStickY - 128) * 65535 / 255 + 128;
66    } else {
67        // Digital outputs
68        _report.a = _finalOutputs.a;
69        _report.b = _finalOutputs.b;
70        _report.x = _finalOutputs.x;
71        _report.y = _finalOutputs.y;
72        _report.lb = _finalOutputs.buttonL;
73        _report.rb = _finalOutputs.buttonR;
74        _report.lt = _finalOutputs.triggerLDigital ? 255 : _finalOutputs.triggerLAnalog;
75        _report.rt = _finalOutputs.triggerRDigital ? 255 : _finalOutputs.triggerRAnalog;
76        _report.start = _finalOutputs.start;
77        _report.back = _finalOutputs.select;
78        _report.home = _finalOutputs.home;
79        _report.dpad_up = _finalOutputs.dpadUp;
80        _report.dpad_down = _finalOutputs.dpadDown;
81        _report.dpad_left = _finalOutputs.dpadLeft;
82        _report.dpad_right = _finalOutputs.dpadRight;
83        _report.ls = _finalOutputs.leftStickClick;
84        _report.rs = _finalOutputs.rightStickClick;
85
86        _report.lx = (_finalOutputs.leftStickX - 128) * 65535 / 255 + 128;
87        _report.ly = (_finalOutputs.leftStickY - 128) * 65535 / 255 + 128;
88        _report.rx = (_finalOutputs.rightStickX - 128) * 65535 / 255 + 128;
89        _report.ry = (_finalOutputs.rightStickY - 128) * 65535 / 255 + 128;
90    }
91
92    _xinput.sendReport(&_report);
93}