31#define kPadModeStandard 0x4
32#define kPadModeAnalog 0x7
34#define kPadSetLockModeUnchanged 0
35#define kPadSetLockModeLock 3
36#define kPadSetLockModeUnlock 1
38#define kStickMaxRadius 120
39#define kStickDeadRadius 25
58 if (SifLoadModule(
"rom0:SIO2MAN", 0, NULL) < 0) {
59 printf(
"Can't load module sio2man\n");
63 if (SifLoadModule(
"rom0:PADMAN", 0, NULL) < 0) {
64 printf(
"Can't load module padman\n");
71 printf(
"Couldn't open Pad0.\n");
85CPad::CPad(
unsigned int port)
89 memset(&CurStatus, 0,
sizeof(tPadStatus));
90 memset(&LastStatus, 0,
sizeof(tPadStatus));
93 CurStatus.buttons = 0xffff;
94 LastStatus.buttons = 0xffff;
100 return padPortOpen(uiPort, kSlot0, DmaBuffer);
105 t32 padState = padGetState(kPort0, kSlot0);
106 if (padState != PAD_STATE_STABLE)
112 if (padSetMainMode(uiPort, kSlot0, 1, kPadSetLockModeUnlock) == 1)
115 tPadStatus padStatus;
116 padRead(uiPort, kSlot0, (padButtonStatus*)&padStatus);
118 if (padStatus.success == 0) {
119 LastStatus = CurStatus;
120 padStatus.rightStick = CurStatus.rightStick;
121 padStatus.leftStick = CurStatus.leftStick;
122 CurStatus = padStatus;
131 if (WasPushed(Pads::kRightStickButton)) {
132 CurStatus.leftStick.isCentered =
false;
133 CurStatus.rightStick.isCentered =
false;
135 CurStatus.leftStick.xVal = CurStatus.l3h;
136 CurStatus.leftStick.yVal = CurStatus.l3v;
137 CurStatus.rightStick.xVal = CurStatus.r3h;
138 CurStatus.rightStick.yVal = CurStatus.r3v;
139 UpdateStick(&CurStatus.leftStick, &LastStatus.leftStick);
140 UpdateStick(&CurStatus.rightStick, &LastStatus.rightStick);
145bool CPad::UpdateStick(tStickData* stickCur, tStickData* stickLast)
148 bool isChanged =
false;
150 using namespace Math;
152 if (!stickCur->isCentered) {
153 stickCur->xCenter = stickCur->xVal;
154 stickCur->yCenter = stickCur->yVal;
155 stickCur->xPos = 0.0f;
156 stickCur->yPos = 0.0f;
157 stickCur->isCentered =
true;
161 if (!FuzzyEqualsi(stickCur->xVal, stickCur->xCenter, kStickDeadRadius)) {
163 temp = ((stickCur->xVal > stickCur->xCenter) ? -kStickDeadRadius : kStickDeadRadius);
164 stickCur->xPos = (float)(stickCur->xVal - stickCur->xCenter + temp) / (float)kStickMaxRadius;
168 stickCur->xPos = 0.0f;
170 if (!FuzzyEqualsi(stickLast->xVal, stickCur->xCenter, kStickDeadRadius))
173 if (!FuzzyEqualsi(stickCur->yVal, stickCur->yCenter, kStickDeadRadius)) {
175 temp = (stickCur->yVal > stickCur->yCenter) ? kStickDeadRadius : -kStickDeadRadius;
176 stickCur->yPos = (float)(stickCur->yCenter - stickCur->yVal + temp) / (float)kStickMaxRadius;
180 stickCur->yPos = 0.0f;
182 if (!FuzzyEqualsi(stickLast->yVal, stickCur->yCenter, kStickDeadRadius))
186 stickCur->xPos = Clamp(stickCur->xPos, -1.0f, 1.0f);
187 stickCur->yPos = Clamp(stickCur->yPos, -1.0f, 1.0f);
193bool CPad::IsDown(tPadStatus status,
unsigned int button)
195 return !IsUp(status, button);
198bool CPad::IsUp(tPadStatus status,
unsigned int button)
200 return status.buttons & (1 << button);
203bool CPad::IsDown(
unsigned int button)
205 return IsDown(CurStatus, button);
208bool CPad::IsUp(
unsigned int button)
210 return IsUp(CurStatus, button);
213bool CPad::WasPushed(
unsigned int button)
215 return IsUp(LastStatus, button) && IsDown(CurStatus, button);
218bool CPad::WasReleased(
unsigned int button)
220 return IsDown(LastStatus, button) && IsUp(CurStatus, button);