Wii Code

 

Modified Darwiin Remote code, on WiiRemote.m

 

- (void) handleIRData:(unsigned char *) dp length:(size_t) dataLength

{

//  NSLog(@"Handling IR Data for 0x%00x", dp[1]);  

    int i = 0;

    //int* test = malloc(sizeof int *);

    char pressed = 0;

    //int blah =0;

 

    if (dp[1] == 0x33) { // 12 IR bytes

        int startByte = 0;

        for(i=0 ; i < 4 ; i++) {

            startByte = 7 + 3 * i;

            irData[i].x = (dp[startByte +0] | ((dp[startByte +2] & 0x30) << 4)) & 0x3FF;

            irData[i].y = (dp[startByte +1] | ((dp[startByte +2] & 0xC0) << 2)) & 0x3FF;

            irData[i].s =  dp[startByte +2] & 0x0F;

        }  

    } else { // 10 IR bytes

        int shift = (dp[1] == 0x36) ? 4 : 7;

        int startByte = 0;

        int i;

        for (i=0; i < 2; i++) {

            startByte = shift + 5 * i;

            irData[2*i].x = (dp[startByte +0] | ((dp[startByte +2] & 0x30) << 4)) & 0x3FF;

            irData[2*i].y = (dp[startByte +1] | ((dp[startByte +2] & 0xC0) << 2)) & 0x3FF;

            irData[2*i].s = ((irData[2*i].x == irData[2*i].y) && (irData[2*i].x == 0x3FF)) ? 0x0F : 0x05;  // No size is given in 10 byte report.

 

            irData[(2*i)+1].x = (dp[startByte +3] | ((dp[startByte +2] & 0x03) << 8)) & 0x3FF;

            irData[(2*i)+1].y = (dp[startByte +4] | ((dp[startByte +2] & 0x0C) << 6)) & 0x3FF;

            irData[(2*i)+1].s = ((irData[(2*i)+1].x == irData[(2*i)+1].y) && (irData[(2*i)+1].x == 0x3FF)) ? 0x0F : 0x05;  // No size is given in 10 byte report.

        }

    }

 

    [É more code we left intact É]

   

    if ([_delegate respondsToSelector:@selector (irPointMovedX:Y:)])

        [_delegate irPointMovedX:ox Y:oy];

 

    if ([_delegate respondsToSelector:@selector (rawIRData:)])

        [_delegate rawIRData:irData];

   

    //Send data through serial if A button is pressed

   

    if (buttonState[WiiRemoteAButton] == YES) {

        if (j ==0) //open serial port at first data set

        {

            test = serialPort(irData[0].x, irData[0].y);

            j++; //increment j, a flag to indicate that serial port is already opened

        } else if (j==11) { //only send data at every 10 cycles,pervents serial  port being too busy and crashes application

            InitializeModem(test, irData[0].x, irData[0].y); //send irData

            j =1; //reset to 1 to prevent serial port being opened multiple times

        }

        else j++;

     }

   

    //NSLog(@"test : %d\n", test);

   

} // handleIRData

 

 

Code modified in SerialPortSample.c, in addition to a lot of deleted code that we didnŐt need

 

extern int InitializeModem(int fileDescriptor, int x, int y, char pressed)

{

    ssize_t     numBytes;       // Number of bytes read or written

    char buffer[10];

    int xlen, ylen;

   

    if (x > 999)        xlen = 4;

    else if (x > 99)    xlen = 3;

    else if (x > 9)     xlen = 2;

    else                xlen = 1;

   

    if (y > 99)         ylen = 3;

    else if (y > 9)     ylen = 2;

    else                ylen = 1;

   

    if (x == 1023 && y == 1023) {

        x = 0;

        y = 0;

        xlen = 1;

        ylen = 1;

    }

   

    sprintf(buffer, "%d %d\r", x, y);

   

    numBytes = write(fileDescriptor, &buffer, xlen+ylen+2);

   

   

    return 1;

}

 

// Given the file descriptor for a serial device, close that device.

void CloseSerialPort(int fileDescriptor)

{

    close(fileDescriptor);

}

 

extern int serialPort(int x, int y)

{

    int         fileDescriptor;

    kern_return_t   kernResult; // on PowerPC this is an int (4 bytes)

/*

 *  error number layout as follows (see mach/error.h):

 *

 *  hi                    lo

 *  | system(6) | subsystem(12) | code(14) |

 */

 

   // io_iterator_t serialPortIterator;

    char        bsdPath[MAXPATHLEN] = "/dev/cu.usbserial-FTDVLEGA";

 

   // Now open the modem port we found, initialize the modem, then close it

    if (!bsdPath[0])

    {

        return EX_UNAVAILABLE;

    }

    fileDescriptor = OpenSerialPort(bsdPath);

   

    if (-1 == fileDescriptor)

    {

        return EX_IOERR;

    }

 

    return fileDescriptor;

}