fork/GramFirmware

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

CarVacrebase to configurator818d962

gram
2.4 KiB93 linesraw
1#include "core/ControllerMode.hpp"
2
3ControllerMode::ControllerMode() : InputMode() {
4    // Set up initial state.
5    ResetDirections();
6}
7
8void ControllerMode::UpdateOutputs(const InputState &inputs, OutputState &outputs) {
9    // Create a copy of the input state here so remapping can be many-to-one (many physical buttons
10    // to one activated button).
11    InputState remapped_inputs = inputs;
12    HandleRemap(inputs, remapped_inputs);
13    HandleSocd(remapped_inputs);
14    UpdateDigitalOutputs(remapped_inputs, outputs);
15    UpdateAnalogOutputs(remapped_inputs, outputs);
16}
17
18void ControllerMode::ResetDirections() {
19    directions = {
20        .horizontal = false,
21        .vertical = false,
22        .diagonal = false,
23        .x = 0,
24        .y = 0,
25        .cx = 0,
26        .cy = 0,
27    };
28}
29
30void ControllerMode::UpdateDirections(
31    bool lsLeft,
32    bool lsRight,
33    bool lsDown,
34    bool lsUp,
35    bool rsLeft,
36    bool rsRight,
37    bool rsDown,
38    bool rsUp,
39    uint8_t analogStickMin,
40    uint8_t analogStickNeutral,
41    uint8_t analogStickMax,
42    OutputState &outputs
43) {
44    ResetDirections();
45
46    outputs.leftStickX = analogStickNeutral;
47    outputs.leftStickY = analogStickNeutral;
48    outputs.rightStickX = analogStickNeutral;
49    outputs.rightStickY = analogStickNeutral;
50
51    // Coordinate calculations to make modifier handling simpler.
52    if (lsLeft || lsRight) {
53        directions.horizontal = true;
54        if (lsLeft) {
55            directions.x = -1;
56            outputs.leftStickX = analogStickMin;
57        } else {
58            directions.x = 1;
59            outputs.leftStickX = analogStickMax;
60        }
61    }
62    if (lsDown || lsUp) {
63        directions.vertical = true;
64        if (lsDown) {
65            directions.y = -1;
66            outputs.leftStickY = analogStickMin;
67        } else {
68            directions.y = 1;
69            outputs.leftStickY = analogStickMax;
70        }
71    }
72    if (directions.horizontal && directions.vertical)
73        directions.diagonal = true;
74
75    if (rsLeft || rsRight) {
76        if (rsLeft) {
77            directions.cx = -1;
78            outputs.rightStickX = analogStickMin;
79        } else {
80            directions.cx = 1;
81            outputs.rightStickX = analogStickMax;
82        }
83    }
84    if (rsDown || rsUp) {
85        if (rsDown) {
86            directions.cy = -1;
87            outputs.rightStickY = analogStickMin;
88        } else {
89            directions.cy = 1;
90            outputs.rightStickY = analogStickMax;
91        }
92    }
93}