ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
pad.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 #
10 # Pad demo app
11 # Quick and dirty, little or no error checks etc..
12 # Distributed as is
13 */
14 
15 #include <tamtypes.h>
16 #include <kernel.h>
17 #include <sifrpc.h>
18 #include <loadfile.h>
19 #include <stdio.h>
20 
21 #include "libpad.h"
22 
23 /*
24  * Global var's
25  */
26 // pad_dma_buf is provided by the user, one buf for each pad
27 // contains the pad's current state
28 static char padBuf[256] __attribute__((aligned(64)));
29 
30 static char actAlign[6];
31 static int actuators;
32 
33 
34 /*
35  * Local functions
36  */
37 
38 /*
39  * loadModules()
40  */
41 static void
43 {
44  int ret;
45 
46 
47  ret = SifLoadModule("rom0:SIO2MAN", 0, NULL);
48  if (ret < 0) {
49  printf("sifLoadModule sio failed: %d\n", ret);
50  SleepThread();
51  }
52 
53  ret = SifLoadModule("rom0:PADMAN", 0, NULL);
54  if (ret < 0) {
55  printf("sifLoadModule pad failed: %d\n", ret);
56  SleepThread();
57  }
58 }
59 
60 /*
61  * waitPadReady()
62  */
63 static int waitPadReady(int port, int slot)
64 {
65  int state;
66  int lastState;
67  char stateString[16];
68 
69  state = padGetState(port, slot);
70  lastState = -1;
71  while((state != PAD_STATE_STABLE) && (state != PAD_STATE_FINDCTP1)) {
72  if (state != lastState) {
73  padStateInt2String(state, stateString);
74  printf("Please wait, pad(%d,%d) is in state %s\n",
75  port, slot, stateString);
76  }
77  lastState = state;
78  state=padGetState(port, slot);
79  }
80  // Were the pad ever 'out of sync'?
81  if (lastState != -1) {
82  printf("Pad OK!\n");
83  }
84  return 0;
85 }
86 
87 
88 /*
89  * initializePad()
90  */
91 static int
93 {
94 
95  int ret;
96  int modes;
97  int i;
98 
100 
101  // How many different modes can this device operate in?
102  // i.e. get # entrys in the modetable
103  modes = padInfoMode(port, slot, PAD_MODETABLE, -1);
104  printf("The device has %d modes\n", modes);
105 
106  if (modes > 0) {
107  printf("( ");
108  for (i = 0; i < modes; i++) {
109  printf("%d ", padInfoMode(port, slot, PAD_MODETABLE, i));
110  }
111  printf(")");
112  }
113 
114  printf("It is currently using mode %d\n",
116 
117  // If modes == 0, this is not a Dual shock controller
118  // (it has no actuator engines)
119  if (modes == 0) {
120  printf("This is a digital controller?\n");
121  return 1;
122  }
123 
124  // Verify that the controller has a DUAL SHOCK mode
125  i = 0;
126  do {
128  break;
129  i++;
130  } while (i < modes);
131  if (i >= modes) {
132  printf("This is no Dual Shock controller\n");
133  return 1;
134  }
135 
136  // If ExId != 0x0 => This controller has actuator engines
137  // This check should always pass if the Dual Shock test above passed
138  ret = padInfoMode(port, slot, PAD_MODECUREXID, 0);
139  if (ret == 0) {
140  printf("This is no Dual Shock controller??\n");
141  return 1;
142  }
143 
144  printf("Enabling dual shock functions\n");
145 
146  // When using MMODE_LOCK, user cant change mode with Select button
148 
150  printf("infoPressMode: %d\n", padInfoPressMode(port, slot));
151 
153  printf("enterPressMode: %d\n", padEnterPressMode(port, slot));
154 
156  actuators = padInfoAct(port, slot, -1, 0);
157  printf("# of actuators: %d\n",actuators);
158 
159  if (actuators != 0) {
160  actAlign[0] = 0; // Enable small engine
161  actAlign[1] = 1; // Enable big engine
162  actAlign[2] = 0xff;
163  actAlign[3] = 0xff;
164  actAlign[4] = 0xff;
165  actAlign[5] = 0xff;
166 
168  printf("padSetActAlign: %d\n",
170  }
171  else {
172  printf("Did not find any actuators.\n");
173  }
174 
176 
177  return 1;
178 }
179 
180 int
182 {
183  int ret;
184  int port, slot;
185  int i;
186  struct padButtonStatus buttons;
187  u32 paddata;
188  u32 old_pad = 0;
189  u32 new_pad;
190 
191 
192  SifInitRpc(0);
193 
194  printf("Hi!\n"
195  "Very welcome to this small and stupid pad test application\n"
196  "Hope you will find the source useful though =)\n"
197  "Please, use & abuse the code, but I would not mind \n"
198  "a small greeting when you do :)\n"
199  "I myself would like to leave a special thanks to Gustavo "
200  "S:\nWithout your psx2lib this would've been impossible\n\n"
201  " - pukko\n\n");
202 
203 
204  loadModules();
205 
206  padInit(0);
207 
208 
209 
210  port = 0; // 0 -> Connector 1, 1 -> Connector 2
211  slot = 0; // Always zero if not using multitap
212 
213  printf("PortMax: %d\n", padGetPortMax());
214  printf("SlotMax: %d\n", padGetSlotMax(port));
215 
216 
217  if((ret = padPortOpen(port, slot, padBuf)) == 0) {
218  printf("padOpenPort failed: %d\n", ret);
219  SleepThread();
220  }
221 
222  if(!initializePad(port, slot)) {
223  printf("pad initalization failed!\n");
224  SleepThread();
225  }
226 
227  for (;;) { // We are phorever people
228 
229  i=0;
230  ret=padGetState(port, slot);
231  while((ret != PAD_STATE_STABLE) && (ret != PAD_STATE_FINDCTP1)) {
232  if(ret==PAD_STATE_DISCONN) {
233  printf("Pad(%d, %d) is disconnected\n", port, slot);
234  }
235  ret=padGetState(port, slot);
236  }
237  if(i==1) {
238  printf("Pad: OK!\n");
239  }
240 
241  ret = padRead(port, slot, &buttons); // port, slot, buttons
242 
243  if (ret != 0) {
244  paddata = 0xffff ^ buttons.btns;
245 
246  new_pad = paddata & ~old_pad;
247  old_pad = paddata;
248 
249  // Directions
250  if(new_pad & PAD_LEFT) {
251  printf("LEFT\n");
252  }
253  if(new_pad & PAD_DOWN) {
254  printf("DOWN\n");
255  }
256  if(new_pad & PAD_RIGHT) {
257  printf("RIGHT\n");
258  /*
259  padSetMainMode(port, slot,
260  PAD_MMODE_DIGITAL, PAD_MMODE_LOCK));
261  */
262  }
263  if(new_pad & PAD_UP) {
264  printf("UP\n");
265  }
266  if(new_pad & PAD_START) {
267  printf("START\n");
268  }
269  if(new_pad & PAD_R3) {
270  printf("R3\n");
271  }
272  if(new_pad & PAD_L3) {
273  printf("L3\n");
274  }
275  if(new_pad & PAD_SELECT) {
276  printf("SELECT\n");
277  }
278  if(new_pad & PAD_SQUARE) {
279  printf("SQUARE\n");
280  }
281  if(new_pad & PAD_CROSS) {
283  printf("CROSS - Enter press mode\n");
284  }
285  if(new_pad & PAD_CIRCLE) {
287  printf("CIRCLE - Exit press mode\n");
288  }
289  if(new_pad & PAD_TRIANGLE) {
290  // Check for the reason below..
291  printf("TRIANGLE (press mode disabled, see code)\n");
292  }
293  if(new_pad & PAD_R1) {
294  actAlign[0] = 1; // Start small engine
296  printf("R1 - Start little engine\n");
297  }
298  if(new_pad & PAD_L1) {
299  actAlign[0] = 0; // Stop engine 0
301  printf("L1 - Stop little engine\n");
302  }
303  if(new_pad & PAD_R2) {
304  printf("R2\n");
305  }
306  if(new_pad & PAD_L2) {
307  printf("L2\n");
308  }
309 
310  // Test the press mode
311  /* Calling SetActAlign repetedly will kill the iop :P
312  * (guess the EE is to fast for IOP to handle..)
313  * So I'd recommend to change the actuator values only once per
314  * vblank or so..
315  */
316  if(buttons.triangle_p) {
317 #if 0
318  actAlign[1] = (i >> 3); buttons.triangle_p; // Big engine
320 #else
321  printf("TRIANGLE %d\n", buttons.triangle_p);
322 #endif
323  }
324  // Start little engine if we move right analogue stick right
325  if(buttons.rjoy_h > 0xf0)
326  {
327  // Stupid check to see if engine is already running,
328  // just to prevent overloading the IOP with requests
329  if (actAlign[0] == 0) {
330  actAlign[0] = 1;
332  }
333  }
334  }
335  } // for
336 
337  printf("Goto sleep!\n");
338  SleepThread();
339 
340  return 0;
341 }
int SifLoadModule(const char *path, int arg_len, const char *args)
#define PAD_CROSS
Definition: input.h:26
#define PAD_SELECT
Definition: input.h:24
#define PAD_START
Definition: input.h:21
#define PAD_R3
Definition: input.h:22
#define PAD_CIRCLE
Definition: input.h:27
#define PAD_DOWN
Definition: input.h:18
#define PAD_RIGHT
Definition: input.h:19
#define PAD_SQUARE
Definition: input.h:25
#define PAD_TRIANGLE
Definition: input.h:28
#define PAD_L2
Definition: input.h:32
#define PAD_L3
Definition: input.h:23
#define PAD_UP
Definition: input.h:20
#define PAD_L1
Definition: input.h:30
#define PAD_LEFT
Definition: input.h:17
#define PAD_R2
Definition: input.h:31
#define PAD_R1
Definition: input.h:29
s32 SleepThread(void)
s32 slot
Definition: libpad.c:176
s32 port
Definition: libpad.c:176
int padGetPortMax(void)
Definition: libpad.c:548
#define PAD_MODECUREXID
Definition: libpad.h:73
int padPortOpen(int port, int slot, void *padArea)
Definition: libpad.c:392
unsigned char padRead(int port, int slot, struct padButtonStatus *data)
Definition: libpad.c:470
int padInfoMode(int port, int slot, int infoMode, int index)
Definition: libpad.c:588
int padExitPressMode(int port, int slot)
Definition: libpad.c:696
#define PAD_MMODE_DUALSHOCK
Definition: libpad.h:81
int padSetActAlign(int port, int slot, char act_align[6])
Definition: libpad.c:777
int padSetActDirect(int port, int slot, char act_align[6])
Definition: libpad.c:800
int padInit(int mode)
Definition: libpad.c:297
#define PAD_STATE_STABLE
Definition: libpad.h:47
#define PAD_TYPE_DUALSHOCK
Definition: libpad.h:65
int padGetState(int port, int slot)
Definition: libpad.c:482
#define PAD_MODECURID
Definition: libpad.h:72
unsigned char padInfoAct(int port, int slot, int word, int byte)
Definition: libpad.c:738
int padInfoPressMode(int port, int slot)
Definition: libpad.c:675
#define PAD_MMODE_LOCK
Definition: libpad.h:84
#define PAD_STATE_DISCONN
Definition: libpad.h:43
void padStateInt2String(int state, char buf[16])
Definition: libpad.c:532
int padSetMainMode(int port, int slot, int mode, int lock)
Definition: libpad.c:656
int padEnterPressMode(int port, int slot)
Definition: libpad.c:690
int padGetSlotMax(int port)
Definition: libpad.c:560
#define PAD_STATE_FINDCTP1
Definition: libpad.h:45
#define PAD_MODETABLE
Definition: libpad.h:75
static char padBuf[256]
Definition: pad.c:28
static int initializePad(int port, int slot)
Definition: pad.c:92
static char actAlign[6]
Definition: pad.c:30
static void loadModules(void)
Definition: pad.c:42
static int actuators
Definition: pad.c:31
static int waitPadReady(int port, int slot)
Definition: pad.c:63
int main()
Definition: pad.c:181
static u32 new_pad[2]
Definition: padx.c:20
static u32 old_pad[2]
Definition: padx.c:19
static u32 paddata[2]
Definition: padx.c:18
void SifInitRpc(int mode)
unsigned short btns
Definition: libpad.h:99
unsigned char triangle_p
Definition: libpad.h:110
unsigned char rjoy_h
Definition: libpad.h:101
#define NULL
Definition: tamtypes.h:91
unsigned int u32
Definition: tamtypes.h:30