controller.c 2.01 KB
#include <ultra64.h>

#include <rom.h>
#include <ramrom.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* app specific includes */
#include "perf.h"
#include "funcs.h"

OSMesgQueue	controllerMsgQ;
OSMesg		controllerMsgBuf;

int initControllers(void);
OSContStatus     statusdata[MAXCONTROLLERS];
OSContPad        controllerdata[MAXCONTROLLERS];


/*
 *
 * Return the lowest number controller connected to system
 */
int initControllers()
{
    OSMesgQueue     serialMsgQ;
    OSMesg          serialMsg;
    int             i;
    u8              pattern;

    osCreateMesgQueue(&serialMsgQ, &serialMsg, 1);
    osSetEventMesg(OS_EVENT_SI, &serialMsgQ, (OSMesg)1);

    osContInit(&serialMsgQ, &pattern, &statusdata[0]);

    osCreateMesgQueue(&controllerMsgQ, &controllerMsgBuf, 1);
    osSetEventMesg(OS_EVENT_SI, &controllerMsgQ, (OSMesg)0);

    for (i = 0; i < MAXCONTROLLERS; i++) {
        if ((pattern & (1<<i)) &&
                !(statusdata[i].errno & CONT_NO_RESPONSE_ERROR))
            return i;
    }
    return -1;
}

void readController(void)
{
    static u16             button, lastbutton;
    
    osContStartReadData(&controllerMsgQ);
    (void)osRecvMesg(&controllerMsgQ, NULL, OS_MESG_BLOCK);
    osContGetReadData(controllerdata);
    button = controllerdata[0].button;
    
    if (button & CONT_L) 
	{
	    RSPFuncNum = (RSPFuncNum - 1) % NUMRSPFUNCS;
	    if (RSPFuncNum < 0) RSPFuncNum = NUMRSPFUNCS - 1;
	    rmonPrintf("Doing test %d \n", RSPFuncNum);
	}
	    
    if (button & CONT_R) 
	{
	    RSPFuncNum = (RSPFuncNum + 1) % NUMRSPFUNCS;
	    rmonPrintf("Doing test %d \n", RSPFuncNum);
	}

    if ((button & CONT_A) && !(lastbutton & CONT_A))
	{
	    RSPFuncNum = (RSPFuncNum - 1) % NUMRSPFUNCS;
	    if (RSPFuncNum < 0) RSPFuncNum = NUMRSPFUNCS - 1;
	    rmonPrintf("Doing test %d \n", RSPFuncNum);
	}
	    
    if ((button & CONT_E)  && !(lastbutton & CONT_E))
	{
	    RSPFuncNum = (RSPFuncNum + 1) % NUMRSPFUNCS;
	    rmonPrintf("Doing test %d \n", RSPFuncNum);
	}
	        
    lastbutton = button;
}