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

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

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

#include <controller.h>

OSMesgQueue	controllerMsgQ;
OSMesg		controllerMsgBuf;

int initControllers(void);
void readController(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) && !(lastbutton & CONT_L)) {
    shadowScaleX -= 0.1;
    rmonPrintf("shadowScaleX = %f\n",shadowScaleX);
  }
	    
  if ((button & CONT_R) && !(lastbutton & CONT_R)) {
    shadowScaleX += 0.1;
    rmonPrintf("shadowScaleX = %f\n",shadowScaleX);
  }
	    
  if ((button & CONT_LEFT) && !(lastbutton & CONT_LEFT)) {
    shadowScaleZ -= 0.1;
    rmonPrintf("shadowScaleZ = %f\n",shadowScaleZ);
  }
	    
  if ((button & CONT_RIGHT) && !(lastbutton & CONT_RIGHT)) {
    shadowScaleZ += 0.1;
    rmonPrintf("shadowScaleZ = %f\n",shadowScaleZ);
  }
	    
  if (button & CONT_A) {
    shadowTranslateX += 1.0;
    rmonPrintf("shadowTranslateX = %f\n",shadowTranslateX);
  }
	    
  if (button & CONT_C) {
    shadowTranslateX -= 1.0;
    rmonPrintf("shadowTranslateX = %f\n",shadowTranslateX);
  }
	    
  if (button & CONT_B) {
    shadowTranslateZ += 1.0;
    rmonPrintf("shadowTranslateZ = %f\n",shadowTranslateZ);
  }
	    
  if (button & CONT_D) {
    shadowTranslateZ -= 1.0;
    rmonPrintf("shadowTranslateZ = %f\n",shadowTranslateZ);
  }
	    
  lastbutton = button;
}