controller.c 6.5 KB
#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;
        } 
    }
}