fork/GramFirmware

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

Jonathan HaylettSOCD revamp (#57)1f92ae6

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