fork/GramFirmware

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

CarVacrebase to configurator818d962

gram
1.8 KiB62 linesraw
1#include "comms/N64Backend.hpp"
2
3#include <Nintendo.h>
4
5N64Backend::N64Backend(
6    InputState &inputs,
7    InputSource **input_sources,
8    size_t input_source_count,
9    int polling_rate,
10    int data_pin
11)
12    : CommunicationBackend(inputs, input_sources, input_source_count),
13      _n64(data_pin) {
14    _data = defaultN64Data;
15
16    if (polling_rate > 0) {
17        // Delay used between input updates to postpone them until right before the
18        // next poll, while also leaving time (850us) for processing to finish.
19        _delay = (1000000 / polling_rate) - 850;
20    } else {
21        // If polling rate is set to 0, disable the delay.
22        _delay = 0;
23    }
24}
25
26CommunicationBackendId N64Backend::BackendId() {
27    return COMMS_BACKEND_N64;
28}
29
30void N64Backend::SendReport() {
31    // Update inputs from all sources at once.
32    ScanInputs();
33
34    // Run gamemode logic.
35    UpdateOutputs();
36
37    // Digital outputs
38    _data.report.a = _outputs.a;
39    _data.report.b = _outputs.b;
40    _data.report.z = _outputs.buttonR;
41    _data.report.l = _outputs.triggerLDigital;
42    _data.report.r = _outputs.triggerRDigital;
43    _data.report.start = _outputs.start;
44    _data.report.dleft = _outputs.dpadLeft;
45    _data.report.dright = _outputs.dpadRight;
46    _data.report.ddown = _outputs.dpadDown;
47    _data.report.dup = _outputs.dpadUp;
48    // Somewhat ugly way of mapping right stick to C-Pad
49    _data.report.cleft = _outputs.rightStickX < 128;
50    _data.report.cright = _outputs.rightStickX > 128;
51    _data.report.cdown = _outputs.rightStickY < 128;
52    _data.report.cup = _outputs.rightStickY > 128;
53
54    // Analog outputs - converted from unsigned to signed 8-bit integers
55    _data.report.xAxis = _outputs.leftStickX - 128;
56    _data.report.yAxis = _outputs.leftStickY - 128;
57
58    // Send outputs to console.
59    _n64->write(_data);
60
61    delayMicroseconds(_delay);
62}