UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
PadPS5.hpp
[詳解]
1//
2// PS5コントローラー受信クラス
3//
4// Copyright (c) 2022 udonrobo
5//
6// Controller --> Sender --> Master
7// ^^^^^^
8//
9
10#pragma once
11
14#include <Udon/Com/Message.hpp>
17
18namespace Udon
19{
20
23 template <template <typename> class Reader>
24 class PadPS5
25 : public Reader<Message::PadPS5>
26 {
27 static_assert(Traits::IsReader<Reader>::value, "Reader type must meet recipient requirements.");
28
29 public:
32
34 using ReaderType = Reader<MessageType>;
35
37 using ReaderType::ReaderType;
38
40 explicit operator bool() const noexcept { return isConnected; }
41
46 bool isEmergencyStop() const noexcept
47 {
48 // パッドが接続されていない場合は非常停止
49 if (not *this)
50 {
51 return true;
52 }
53
54 if (getCross().toggle)
55 {
56 return false;
57 }
58 else
59 {
60 // 起動時は非常停止
61 return true;
62 }
63 }
64
70 bool isOperable() const noexcept
71 {
72 return not isEmergencyStop();
73 }
74
76 Input getTriangle() const noexcept { return triangle; }
77
79 Input getCircle() const noexcept { return circle; }
80
82 Input getCross() const noexcept { return cross; }
83
85 Input getSquare() const noexcept { return square; }
86
88 Input getUp() const noexcept { return up; }
89
91 Input getRight() const noexcept { return right; }
92
94 Input getDown() const noexcept { return down; }
95
97 Input getLeft() const noexcept { return left; }
98
100 Input getL1() const noexcept { return l1; }
101
103 Input getR1() const noexcept { return r1; }
104
106 Input getL2() const noexcept { return l2; }
107
109 Input getR2() const noexcept { return r2; }
110
112 Input getL3() const noexcept { return l3; }
113
115 Input getR3() const noexcept { return r3; }
116
118 Input getCreate() const noexcept { return create; }
119
121 Input getOption() const noexcept { return option; }
122
124 Input getTouch() const noexcept { return touch; }
125
127 Input getMic() const noexcept { return mic; }
128
130 Input getPs() const noexcept { return ps; }
131
133 Vec2 getLeftStick() const noexcept { return leftStick; }
134
136 Vec2 getRightStick() const noexcept { return rightStick; }
137
140 Stick getMoveInfo() const noexcept { return { leftStick, rightStick.x }; }
141
144 void update(int deadZone = 20) noexcept
145 {
147 const auto message = ReaderType::getMessage();
148
149 if (message && message->isConnected)
150 {
151 isConnected = true;
152
153 triangle.update(message->triangle);
154 circle.update(message->circle);
155 cross.update(message->cross);
156 square.update(message->square);
157
158 up.update(message->up);
159 right.update(message->right);
160 down.update(message->down);
161 left.update(message->left);
162
163 l1.update(message->l1);
164 r1.update(message->r1);
165 l2.update(message->l2);
166 r2.update(message->r2);
167 l3.update(message->l3);
168 r3.update(message->r3);
169
170 create.update(message->create);
171 option.update(message->option);
172 touch.update(message->touch);
173 mic.update(message->mic);
174 ps.update(message->ps);
175
176 // -128 ~ 127(int8_t) -> -255 ~ 255(int16_t)
177 const auto decodeStick = [](int8_t raw) -> int16_t
178 {
179 return raw * 2 + 1; // (raw + 128) * 2 - 255
180 };
181
182 leftStick = {
183 CutDeadZone(decodeStick(message->analogLeftX), deadZone),
184 CutDeadZone(decodeStick(message->analogLeftY), deadZone),
185 };
186 rightStick = {
187 CutDeadZone(decodeStick(message->analogRightX), deadZone),
188 CutDeadZone(decodeStick(message->analogRightY), deadZone),
189 };
190 }
191 else
192 {
193 isConnected = false;
194
195 triangle.update(false);
196 circle.update(false);
197 cross.update(false);
198 square.update(false);
199
200 up.update(false);
201 right.update(false);
202 down.update(false);
203 left.update(false);
204
205 l1.update(false);
206 r1.update(false);
207 l2.update(false);
208 r2.update(false);
209 l3.update(false);
210 r3.update(false);
211
212 create.update(false);
213 option.update(false);
214 touch.update(false);
215 mic.update(false);
216
217 leftStick.clear();
218 rightStick.clear();
219 }
220 }
221
224 {
225 return {
226 isConnected,
227 triangle.press,
228 circle.press,
229 cross.press,
230 square.press,
231
232 up.press,
233 right.press,
234 down.press,
235 left.press,
236
237 l1.press,
238 r1.press,
239 l2.press,
240 r2.press,
241 l3.press,
242 r3.press,
243
244 create.press,
245 option.press,
246 touch.press,
247 mic.press,
248 ps.press,
249
250 static_cast<int8_t>(rightStick.x / 2),
251 static_cast<int8_t>(rightStick.y / 2),
252 static_cast<int8_t>(leftStick.x / 2),
253 static_cast<int8_t>(leftStick.y / 2),
254 };
255 }
256
257 private:
258 // 接続状態
259 bool isConnected = false;
260
261 // ボタン
262 Input triangle;
263 Input circle;
264 Input cross;
265 Input square;
266 Input up;
267 Input right;
268 Input down;
269 Input left;
270 Input l1;
271 Input r1;
272 Input l2;
273 Input r2;
274 Input l3;
275 Input r3;
276 Input create;
277 Input option;
278 Input touch;
279 Input mic;
280 Input ps;
281
282 // アナログスティック [-255~255]
283 Vec2 rightStick;
284 Vec2 leftStick;
285
287 static double CutDeadZone(double value, uint8_t deadZone)
288 {
289 if (value > deadZone)
290 return 255 * (value - deadZone) / (255 - deadZone);
291 else if (value < -deadZone)
292 return 255 * (value + deadZone) / (255 - deadZone);
293 else
294 return 0.0;
295 };
296 };
297
298} // namespace Udon
Udon::Optional< MessageType > getMessage() const
メッセージを取得する
Definition PadPS5BT.hpp:179
通信経由PS5コントローラークラス
Definition PadPS5.hpp:26
Input getDown() const noexcept
十字キー下
Definition PadPS5.hpp:94
void update(int deadZone=20) noexcept
更新
Definition PadPS5.hpp:144
Input getPs() const noexcept
PSボタン
Definition PadPS5.hpp:130
Reader< MessageType > ReaderType
受信クラス型
Definition PadPS5.hpp:34
Stick getMoveInfo() const noexcept
ロボットの移動に必要なスティックの情報 Udon::Stick オブジェクト {{x,y},turn} を取得
Definition PadPS5.hpp:140
bool isEmergencyStop() const noexcept
非常停止を行うべきかを取得
Definition PadPS5.hpp:46
Input getCircle() const noexcept
○ボタン
Definition PadPS5.hpp:79
Message::PadPS5 MessageType
受信メッセージ型
Definition PadPS5.hpp:31
Input getRight() const noexcept
十字キー右
Definition PadPS5.hpp:91
Vec2 getRightStick() const noexcept
右スティック [x,y: -255~255]
Definition PadPS5.hpp:136
Input getLeft() const noexcept
十字キー左
Definition PadPS5.hpp:97
Vec2 getLeftStick() const noexcept
左スティック [x,y: -255~255]
Definition PadPS5.hpp:133
Input getMic() const noexcept
マイクボタン
Definition PadPS5.hpp:127
Input getR3() const noexcept
右スティック押し込み
Definition PadPS5.hpp:115
Input getL2() const noexcept
L2ボタン
Definition PadPS5.hpp:106
Input getCreate() const noexcept
クリエイトボタン(左上ボタン)
Definition PadPS5.hpp:118
Input getL3() const noexcept
左スティック押し込み
Definition PadPS5.hpp:112
Input getUp() const noexcept
十字キー上
Definition PadPS5.hpp:88
Input getSquare() const noexcept
□ボタン
Definition PadPS5.hpp:85
Input getTriangle() const noexcept
▵ボタン
Definition PadPS5.hpp:76
Input getR2() const noexcept
R2ボタン
Definition PadPS5.hpp:109
bool isOperable() const noexcept
動作可能であるか
Definition PadPS5.hpp:70
Input getOption() const noexcept
オプションボタン(右上ボタン)
Definition PadPS5.hpp:121
Input getCross() const noexcept
×ボタン
Definition PadPS5.hpp:82
Input getR1() const noexcept
R1ボタン
Definition PadPS5.hpp:103
MessageType getMessage() const
メッセージ構造体に変換
Definition PadPS5.hpp:223
Input getTouch() const noexcept
タッチパッドボタン
Definition PadPS5.hpp:124
Input getL1() const noexcept
L1ボタン
Definition PadPS5.hpp:100
void MaybeInvokeUpdate(HasMemberFunctionUpdate &rhs)
T に T::update 関数が存在する場合呼び出す。それ以外の場合何もしない。
Definition HasMemberFunction.hpp:111
Definition Bit.hpp:12
Pos Stick
Definition Position.hpp:259
入力値
Definition Input.hpp:17
PS5コントローラーのボタン情報
Definition PadPS5.hpp:21
送信クラスであるか判定
Definition ReaderWriterTraits.hpp:42
二次元ベクトル
Definition Vector2D.hpp:22