Nugget
Loading...
Searching...
No Matches
advancedpad.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2025 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <EASTL/functional.h>
30#include <stdint.h>
31
33
34namespace psyqo {
35
46 public:
47 enum class Pad : unsigned { Pad1a, Pad1b, Pad1c, Pad1d, Pad2a, Pad2b, Pad2c, Pad2d };
48
49 enum Button {
50 Select = 0,
51 L3 = 1,
52 R3 = 2,
53 Start = 3,
54 Up = 4,
55 Right = 5,
56 Down = 6,
57 Left = 7,
58 L2 = 8,
59 R2 = 9,
60 L1 = 10,
61 R1 = 11,
63 Circle = 13,
64 Cross = 14,
65 Square = 15,
66 };
67
68 enum PadType : uint8_t {
69 Mouse = 0x12, // (two button mouse)
70 NegCon = 0x23, // (steering twist/wheel/paddle)
71 KonamiLightgun = 0x31, // (IRQ10-type)
72 DigitalPad = 0x41, // (or analog pad/stick in digital mode; LED=Off)
73 AnalogStick = 0x53, // (or analog pad in "flight mode"; LED=Green)
74 NamcoLightGun = 0x63, // (Cinch-type)
75 AnalogPad = 0x73, // (in normal analog mode; LED=Red)
76 Multitap = 0x80, // (multiplayer adaptor) (when activated)
77 JogCon = 0xe3, // (steering dial)
78 FishingCon = 0xe5, // (fishing rod)
79 ConfigMode = 0xf3, // (when in config mode; see rumble command 43h)
80 None = 0xff // (no controller connected, pins floating High-Z)
81 };
82
88
102 enum class PollingMode { Normal, Fast };
104
123 void setOnEvent(eastl::function<void(Event)>&& callback) { m_callback = eastl::move(callback); }
124
134 bool isPadConnected(Pad pad) const { return m_padData[toUnderlying(pad)].connected == 0; }
135
146 bool isButtonPressed(Pad pad, Button button) const {
147 return (m_padData[toUnderlying(pad)].buttons & (1 << button)) == 0;
148 }
149
164 uint8_t getAdc(Pad pad, unsigned int index) const {
165 const unsigned padIndex = toUnderlying(pad);
166
167 return index <= 7 ? m_padData[padIndex].adc[index] : 0;
168 }
169
187 uint16_t getHalfword(Pad pad, unsigned int index) const { return m_padData[toUnderlying(pad)].packed[index % 4]; }
188
199 uint8_t getPadType(Pad pad) const { return m_padData[toUnderlying(pad)].padType; }
200
201 private:
202 enum Command : uint8_t {
203 PadSelect = 0x01,
204 ReadPad = 0x42, // 'B' Read Buttons AND analog inputs
205 // Config mode commands
206 ToggleConfigMode = 0x43, // 'C' Enter/Exit Configuration Mode
207 SetLED = 0x44, // 'D' Set LED State (analog mode on/off)
208 GetLED = 0x45, // 'E' Get LED State (and whatever values)
209 GetMotorInfo = 0x46, // 'F' Allegedly get info about a motor
210 GetMotorList = 0x47, // 'G' Allegedly get list of motors
211 GetMotorState = 0x48, // 'H' Allegedly get motor state
212 GetSupportedModes = 0x4c, // 'L' Allegedly get supported modes
213 ConfigRequestFormat = 0x4d, // 'M' Allegedly configure poll request format
214 ConfigResponseFormat = 0x4f, // 'O' Allegedly configure poll response format
215 };
216
217 void busyLoop(unsigned delay) {
218 unsigned cycles = 0;
219 while (++cycles < delay) asm("");
220 };
221
222 void configurePort(uint8_t port);
223
224 void flushRxBuffer();
225 uint8_t outputDefault(unsigned ticks);
226 uint8_t outputMultitap(unsigned ticks);
227 void processChanges(Pad pad);
228 void readPad();
229 uint8_t transceive(uint8_t dataOut);
230 bool waitForAck(); // true if ack received, false if timeout
231
232 union PadData {
233 struct {
234 uint8_t connected;
235 uint8_t padType;
236 uint16_t buttons;
237 uint8_t adc[8];
238 };
239 uint16_t packed[6];
240 };
241
242 PadData m_padData[8];
243 eastl::function<void(Event)> m_callback;
244 bool m_connected[8] = {false, false, false, false, false, false, false, false};
245 uint16_t m_buttons[8] = {
246 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
247 };
248 uint8_t m_portToProbe = 0;
249 uint8_t m_portsToProbeByVSync = 0;
250};
251
252// prefix increment operator
254 return pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) + 1) & 7);
255}
256
257// postfix increment operator
259 psyqo::AdvancedPad::Pad copy(pad);
260 pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) + 1) & 7);
261 return copy;
262}
263
264// prefix decrement operator
266 return pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) - 1) & 7);
267}
268
269// postfix decrement operator
271 psyqo::AdvancedPad::Pad copy(pad);
272 pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) - 1) & 7);
273 return copy;
274}
275
276} // namespace psyqo
An advanced class to access the pads.
Definition advancedpad.hh:45
PadType
Definition advancedpad.hh:68
@ JogCon
Definition advancedpad.hh:77
@ None
Definition advancedpad.hh:80
@ Mouse
Definition advancedpad.hh:69
@ NamcoLightGun
Definition advancedpad.hh:74
@ DigitalPad
Definition advancedpad.hh:72
@ NegCon
Definition advancedpad.hh:70
@ AnalogStick
Definition advancedpad.hh:73
@ Multitap
Definition advancedpad.hh:76
@ KonamiLightgun
Definition advancedpad.hh:71
@ ConfigMode
Definition advancedpad.hh:79
@ AnalogPad
Definition advancedpad.hh:75
@ FishingCon
Definition advancedpad.hh:78
PollingMode
Initializes the pads.
Definition advancedpad.hh:102
Pad
Definition advancedpad.hh:47
uint8_t getAdc(Pad pad, unsigned int index) const
Returns the state of an Analog Input.
Definition advancedpad.hh:164
Button
Definition advancedpad.hh:49
@ Up
Definition advancedpad.hh:54
@ Select
Definition advancedpad.hh:50
@ L3
Definition advancedpad.hh:51
@ L1
Definition advancedpad.hh:60
@ Circle
Definition advancedpad.hh:63
@ R2
Definition advancedpad.hh:59
@ Triangle
Definition advancedpad.hh:62
@ Cross
Definition advancedpad.hh:64
@ Square
Definition advancedpad.hh:65
@ Right
Definition advancedpad.hh:55
@ Left
Definition advancedpad.hh:57
@ Start
Definition advancedpad.hh:53
@ R1
Definition advancedpad.hh:61
@ Down
Definition advancedpad.hh:56
@ L2
Definition advancedpad.hh:58
@ R3
Definition advancedpad.hh:52
bool isPadConnected(Pad pad) const
Returns the state of a pad.
Definition advancedpad.hh:134
bool isButtonPressed(Pad pad, Button button) const
Returns the state of a button.
Definition advancedpad.hh:146
uint8_t getPadType(Pad pad) const
Returns the type of the pad.
Definition advancedpad.hh:199
void setOnEvent(eastl::function< void(Event)> &&callback)
Sets the event callback function.
Definition advancedpad.hh:123
uint16_t getHalfword(Pad pad, unsigned int index) const
Returns raw pad data as an unsigned 16-bit value.
Definition advancedpad.hh:187
void initialize(PollingMode mode=PollingMode::Normal)
Definition advancedpad.cpp:37
Definition cdrom-loader.hh:39
psyqo::AdvancedPad::Pad & operator++(psyqo::AdvancedPad::Pad &pad)
Definition advancedpad.hh:253
psyqo::AdvancedPad::Pad & operator--(psyqo::AdvancedPad::Pad &pad)
Definition advancedpad.hh:265
constexpr std::underlying_type_t< E > toUnderlying(E v)
Definition utility-polyfill.h:38
Definition advancedpad.hh:83
Button button
Definition advancedpad.hh:86
Pad pad
Definition advancedpad.hh:85
enum psyqo::AdvancedPad::Event::@16 type
@ PadConnected
Definition advancedpad.hh:84
@ PadDisconnected
Definition advancedpad.hh:84
@ ButtonPressed
Definition advancedpad.hh:84
@ ButtonReleased
Definition advancedpad.hh:84
static void uint32_t mode
Definition syscalls.h:230
void void(ptr, size)