-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS4ControllerManager.cpp
More file actions
136 lines (104 loc) · 4.02 KB
/
Copy pathPS4ControllerManager.cpp
File metadata and controls
136 lines (104 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "PS4ControllerManager.h"
ControllerPtr PS4ControllerManager::controller = nullptr;
void PS4ControllerManager::begin(bool forgetKeys) {
BP32.setup(&PS4ControllerManager::onConnected, &PS4ControllerManager::onDisconnected);
if (forgetKeys)
BP32.forgetBluetoothKeys();
BP32.enableVirtualDevice(false);
}
void PS4ControllerManager::update() {
bool updated = BP32.update();
if (!updated || !controller || !controller->isConnected())
return;
processButtons();
processSticks();
}
bool PS4ControllerManager::isConnected() {
return controller && controller->isConnected();
}
// ---------------------- Stick getters ----------------------
int PS4ControllerManager::leftX() { return controller ? controller->axisX() : 0; }
int PS4ControllerManager::leftY() { return controller ? controller->axisY() : 0; }
int PS4ControllerManager::rightX() { return controller ? controller->axisRX() : 0; }
int PS4ControllerManager::rightY() { return controller ? controller->axisRY() : 0; }
// ---------------------- LED & rumble ----------------------
void PS4ControllerManager::setLED(uint8_t r, uint8_t g, uint8_t b) {
if (controller)
controller->setColorLED(r, g, b);
}
void PS4ControllerManager::rumble(uint8_t weak, uint8_t strong, uint16_t durationMs) {
if (controller)
controller->playDualRumble(0, durationMs, weak, strong);
}
// ---------------------- Callback registration ----------------------
void PS4ControllerManager::onButtonEvent(void (*callback)(PS4Button, PS4ButtonEventType)) {
buttonEventCallback = callback;
}
void PS4ControllerManager::onStickMove(void (*callback)(int, int, int, int)) {
stickMoveCallback = callback;
}
// ---------------------- Static callbacks ----------------------
void PS4ControllerManager::onConnected(ControllerPtr ctl) {
controller = ctl;
Serial.println("PS4 Controller Connected");
}
void PS4ControllerManager::onDisconnected(ControllerPtr ctl) {
if (controller == ctl) {
controller = nullptr;
Serial.println("PS4 Controller Disconnected");
}
}
// ---------------------- Internal processing ----------------------
void PS4ControllerManager::processButtons() {
if (!buttonEventCallback)
return;
uint32_t btn = controller->buttons();
uint32_t misc = controller->miscButtons();
uint32_t changed = (btn ^ lastButtons);
auto emit = [&](PS4Button b, bool pressed) {
if (pressed)
buttonEventCallback(b, PS4ButtonEventType::Pressed);
else
buttonEventCallback(b, PS4ButtonEventType::Released);
if (pressed)
buttonEventCallback(b, PS4ButtonEventType::Held);
};
// ABXY (Cross, Circle, Square, Triangle)
emit(PS4Button::Cross, btn & BUTTON_A);
emit(PS4Button::Circle, btn & BUTTON_B);
emit(PS4Button::Square, btn & BUTTON_X);
emit(PS4Button::Triangle, btn & BUTTON_Y);
// L1 / R1 / L2 / R2 / L3 / R3
emit(PS4Button::L1, btn & 0x0100);
emit(PS4Button::R1, btn & 0x0200);
emit(PS4Button::L2, btn & 0x0400);
emit(PS4Button::R2, btn & 0x0800);
emit(PS4Button::L3, btn & 0x1000);
emit(PS4Button::R3, btn & 0x2000);
// Share / Options / PS
emit(PS4Button::Share, misc & MISC_BUTTON_BACK);
emit(PS4Button::Options, misc & MISC_BUTTON_HOME);
emit(PS4Button::PSButton, misc & MISC_BUTTON_SYSTEM);
// Dpad
uint8_t dpad = controller->dpad();
emit(PS4Button::DpadUp, dpad == DPAD_UP);
emit(PS4Button::DpadDown, dpad == DPAD_DOWN);
emit(PS4Button::DpadLeft, dpad == DPAD_LEFT);
emit(PS4Button::DpadRight, dpad == DPAD_RIGHT);
lastButtons = btn;
}
void PS4ControllerManager::processSticks() {
if (!stickMoveCallback)
return;
int lx = controller->axisX();
int ly = controller->axisY();
int rx = controller->axisRX();
int ry = controller->axisRY();
if (lx != lastLX || ly != lastLY || rx != lastRX || ry != lastRY) {
stickMoveCallback(lx, ly, rx, ry);
}
lastLX = lx;
lastLY = ly;
lastRX = rx;
lastRY = ry;
}