controller.c
6.5 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <sched.h>
#include "viewer.h"
#include "status.h"
#include "audrenderer.h"
#include "page.h"
#include "uilogic.h"
/**** Variables shared with other files ****/
u8 gValidControllers = 0;
u8 gControllers[MAXCONTROLLERS];
u8 gControllerCnt = 0;
/**** viewer global control ***/
s8 gCursorX, gCursorY;
int gCursorCnt;
int gStickCnt = 0;
u32 gButton = 0;
u32 gMove = VW_MOVE_STILL;
extern u32 __osBbHackFlags;
/**** variables used by this file for handling the controllers ****/
static OSContStatus statusdata[MAXCONTROLLERS];
static OSContPad controllerdata[MAXCONTROLLERS];
//static OSScMsg controllermsg;
static u16 lastButArray = 0;
/**********************************************************************
*
* Routine for initializing the controllers. After initialized, set the
* controller interrupt to be posted to the gGfxFrameMsgQ, used by the
* gameproc in viewer.c.
*
*********************************************************************/
void initCntrl(void)
{
OSMesgQueue serialMsgQ;
OSMesg serialMsg;
s32 i;
#ifndef _PARTNER
if (__osBbIsBb<2 || (__osBbIsBb > 1 && skGetId(&i) == 0 && (i == 1 || i == 5396)))
__osBbHackFlags = 1;
else
__osBbHackFlags = 0;
#endif
osCreateMesgQueue(&serialMsgQ, &serialMsg, 1);
osSetEventMesg(OS_EVENT_SI, &serialMsgQ, (OSMesg)1);
if((i = osContInit(&serialMsgQ, &gValidControllers, &statusdata[0])) != 0)
PRINTF("Failure initing controllers\n");
osSetEventMesg(OS_EVENT_SI, &gControllerMsgQ, (OSMesg)0);
}
//#define SCRIPTING
#ifdef SCRIPTING
#define SC_A 1
#define SC_B 2
#define SC_G 3
#define SC_LEFT 4
#define SC_RIGHT 5
#define SC_UP 6
#define SC_DOWN 7
#define SC_DELAY 8
#define SC_END 9
int __sc_ptr;
int __sc_delay;
u8 __sc_script[] = {
SC_DELAY, SC_DELAY, SC_DELAY, SC_DELAY,
SC_DELAY, SC_DELAY, SC_DELAY, SC_DELAY,
SC_B, SC_DELAY,
SC_B, SC_DELAY,
SC_DOWN, SC_DELAY,
SC_DOWN, SC_DELAY,
SC_A, SC_DELAY,
SC_A, SC_DELAY,
SC_B, SC_DELAY,
SC_A, SC_DELAY,
SC_A, SC_DELAY,
SC_A,
SC_END
};
u8 __sc_script2[] = {
SC_DELAY, SC_DELAY, SC_DELAY, SC_DELAY,
SC_DELAY, SC_DELAY, SC_DELAY, SC_DELAY,
SC_END
};
#endif
int THRESHOLD=45;
int DURATION=13;
/**********************************************************************
*
* Called after the controller read has completed. Check the information
* from the read, and set state variables accordingly. If you were only
* interested in one button press at a time, you could use a switch or
* a jump table. If you are going to respond to multiple button presses,
* you will need to do a series of if's. In this example we only use
* new button presses, ignoring buttons held down. Do this by exclusive
* or'ing the lastbutton with pad->button, and then and pad->button
* with the result to only get the button downs, and ignore button ups.
*
*********************************************************************/
void updateController(void)
{
int i;
OSContPad *pad;
u16 newbutton;
(void)osRecvMesg(&gControllerMsgQ, NULL, OS_MESG_BLOCK);
osContGetReadData(controllerdata);
gControllerCnt = 0;
for (i=0; i<MAXCONTROLLERS; i++) {
pad = &controllerdata[i];
if (!pad->errno) {
gControllers[i] = 1;
gControllerCnt ++;
} else {
gControllers[i] = 0;
}
}
if (gCid!=0) {
gCpakCnt = uiGetCpakCnt( gCid );
PRINTF("gCpakCnt=%d c0=%d c1=%d c2=%d c3=%d\n", gCpakCnt, gControllers[0], gControllers[1], gControllers[2], gControllers[3]);
}
pad = &controllerdata[0];
if (pad->errno) {
PRINTF("read error from controller 1\n");
return;
}
#ifdef SCRIPTING
if (pad->button & CONT_START) __sc_ptr = gStatus==VW_ERR_FS ? sizeof __sc_script2 : sizeof __sc_script;
if (__sc_ptr < ( gStatus==VW_ERR_FS ? sizeof __sc_script2 : sizeof __sc_script )) {
if (__sc_delay) {
__sc_delay--;
return;
}
switch( gStatus==VW_ERR_FS ? __sc_script2[__sc_ptr++] : __sc_script[__sc_ptr++] ) {
case SC_A:
gButton = CONT_A; break;
case SC_B:
gButton = CONT_B; break;
case SC_G:
gButton = CONT_G;
case SC_LEFT:
gMove |= VW_MOVE_LEFT; break;
case SC_RIGHT:
gMove |= VW_MOVE_RIGHT; break;
case SC_UP:
gMove |= VW_MOVE_UP; break;
case SC_DOWN:
gMove |= VW_MOVE_DOWN; break;
case SC_DELAY:
__sc_delay = 30; break;
case SC_END:
break;
}
return;
}
#endif
if (gLogoCnt >= MAX_LOGO_COUNT) {
/* get bits that have changed, ignore button ups */
newbutton = pad->button ^ lastButArray;
newbutton &= pad->button;
lastButArray = pad->button;
if ( pad->stick_y<-THRESHOLD || pad->stick_y>THRESHOLD || pad->stick_x<-THRESHOLD || pad->stick_x>THRESHOLD) {
if (pad->stick_y<-THRESHOLD) {
if (gCursorY >= pad->stick_y) {
gStickCnt ++;
if ((gStickCnt==DURATION) || (gCursorY >= -THRESHOLD)) {
gMove |= VW_MOVE_DOWN;
gStickCnt = 0;
}
}
} else if (pad->stick_y>THRESHOLD) {
if (gCursorY <= pad->stick_y) {
gStickCnt ++;
if ((gStickCnt==DURATION)||(gCursorY <= THRESHOLD)) {
gMove |= VW_MOVE_UP;
gStickCnt = 0;
}
}
}
if (pad->stick_x<-THRESHOLD) {
if (gCursorX >= pad->stick_x) {
gStickCnt ++;
if ((gStickCnt==DURATION) || (gCursorX >= -THRESHOLD)) {
gMove |= VW_MOVE_LEFT;
gStickCnt = 0;
}
}
} else if (pad->stick_x>THRESHOLD) {
if (gCursorX <= pad->stick_x) {
gStickCnt ++;
if ((gStickCnt==DURATION)||(gCursorX <= THRESHOLD)) {
gMove |= VW_MOVE_RIGHT;
gStickCnt = 0;
}
}
}
}else
gStickCnt = 0;
gCursorX = pad->stick_x;
gCursorY = pad->stick_y;
if (gCursorX==0 && gCursorY==0)
gCursorCnt = 0;
else
gCursorCnt ++;
if (newbutton & CONT_A) {
PRINTF("Button A pressed\n");
gButton = CONT_A;
} else if (newbutton & CONT_B) {
PRINTF("Button B pressed\n");
gButton = CONT_B;
} else if (newbutton & CONT_G) {
PRINTF("Button Z pressed\n");
gButton = CONT_G;
}
}
}