N64 to USB HID interface

Dependencies:   mbed

Revision:
3:b9cecf3e2496
Parent:
2:b38d6345dd14
Child:
4:fee2d61ad6bc
--- a/main.cpp	Sun Sep 25 17:34:08 2011 +0000
+++ b/main.cpp	Fri Sep 30 00:15:10 2011 +0000
@@ -48,7 +48,7 @@
 #include "mbed.h"
 #include "stdint.h"
 #include "usbhid.h"
-#include "IOMacros.h"
+
 
 USBJoystick joystick;
 
@@ -118,49 +118,13 @@
 DigitalInOut inout(p14);            // Connect controller here
 // using 220 ohm pull-up
 Serial pc(USBTX, USBRX);
-DigitalOut led(LED1);
+
 uint8_t x, y;
-volatile char bit_count;
 union controls { /* A definition and a declaration */
     uint8_t array[4];
     uint32_t result_32;
 } controls;
 
-/** EINT3_IRQHandler
- */
- void delay (unsigned int);
-void ISR(void);
-extern "C" void EINT3_IRQHandler(void) {
-
-    // The "event" is connected to pin p10 which is LPC1768 P0_1
-    // so lets trap that and ignore all other GPIO interrupts.
-    // Test for IRQ on Port0.
-
-    // if (LPC_GPIOINT->IntStatus & 0x1) {
-    // If P0_16/p14 rises, call atint()
-    if (LPC_GPIOINT->IO0IntStatF & (1 << 16)) {
-        delay(30);
-        if (inout.read()) controls.result_32 |= (1UL<< bit_count);
-        else controls.result_32  &= ~(1UL << bit_count);
-        bit_count++;
-    }
-    //}
-    // Clear this and all other possible GPIO generated interrupts as they don't concern us.
-     LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF);
-    LPC_GPIOINT->IO0IntClr = (LPC_GPIOINT->IO0IntStatR | LPC_GPIOINT->IO0IntStatF);
-
-}
-
-void event_irq_init(void) {
-    // Use macro to set p10 as an input.
-    //p10_AS_INPUT;
-    // Enable P0_16/p14 for falling edge interrupt generation.
-    LPC_GPIOINT->IO0IntEnF |= (1UL << 16);
-    // Enable the interrupt.
-    //NVIC_EnableIRQ(EINT3_IRQn);
-}
-
-
 // Temp array for controller data
 
 /* void delay(unsigned int n) is used as a short delay to
@@ -172,13 +136,6 @@
         ;
 }
 
-void ISR() {
-    wait_us(2);
-    if (inout.read()) controls.result_32 |= (1UL<< bit_count);
-    else controls.result_32  &= ~(1UL << bit_count);
-    bit_count++;
-}
-
 /* int receive(char *data_array, unsigned char n) is used to
    receive a bit stream of bits into an array of n bytes coming
    from the controller. This must be called immediately after
@@ -186,29 +143,47 @@
  */
 
 int receive(uint8_t *data_array, unsigned char n) {
-
+    unsigned char sample, previous_sample;
+    unsigned char bit, byte;
     int i;
-    bit_count=0;
-    //inout.write(1);
-    NVIC_EnableIRQ(EINT3_IRQn);
+
+
+    //inout.input();                // Not sure about this..
 
-    wait_us(500);
-    //led=1;
-    NVIC_DisableIRQ(EINT3_IRQn);
+    sample = inout.read();
+    for (i=0;i < n ;i++) {
+        byte = 0;
+        bit  = 0;
+        while (bit<8) {
+            previous_sample = sample;
+            sample = inout.read();
+            if ((previous_sample ^ sample) & previous_sample) {
 
-    x=y=0;
-    for (i=0; i<=7; i++) {
-        if (controls.array[2] & (1 << i))
-            x=x | (1 << (7-i));
-        if (controls.array[3] & (1 << i))
-            y=y | (1 << (7-i));
+                delay(60);
+                sample=inout.read();
+                if (sample)
+                    byte = byte  | (1<<bit);
+                bit++;
+            }
+
+            data_array[i]= byte;
+        }
+
     }
-    data_array[2]=x;
-    data_array[3]=y;
+     /* The for loop here is used to reverse the bits for the x and y values retured
+           by the controller. I am not entirely sure of the format. I'm missing a bit
+           somewhere.. Values don't range from -127..128. */
+        x=y=0;
+        for (i=0;i<=7;i++) {
+            if (controls.array[2] & (1 << i))
+                x=x | (1 << (7-i));
+            if (controls.array[3] & (1 << i))
+                y=y | (1 << (7-i));
+        }
+        data_array[2]=x;
+        data_array[3]=y;
     return n;
 }
-
-
 /* void send_byte(unsigned char byte) is used to send a single
    byte to the controller.
  */
@@ -241,22 +216,22 @@
 
 int main() {
     inout.mode(OpenDrain);           // Must use open-drain for N64 Controller!
-    event_irq_init();
-    uint32_t previous_result=0;                           // Used for the bit-shifting for x and y values
-    //send_byte(RESET_CONTROLLER);
-    wait_ms(10);
+
+    uint32_t i, previous_result=0;                           // Used for the bit-shifting for x and y values
+    send_byte(RESET_CONTROLLER);
+              wait_ms(10);
     while (1) {
         wait_ms(10);
         previous_result=controls.result_32;
         send_byte(1);                // Send the request byte
         receive(controls.array, 4);           // Start receiving bit stream
-        pc.printf("bitcount: %d\n",bit_count);
+        
         if (controls.result_32==previous_result) continue;
-
-
-        // pc.printf("%3d %3d %d %d\n ", controls.array[0], controls.array[1], (int8_t)controls.array[2], (int8_t)controls.array[3]);
+        
+        
+        //pc.printf("%3d %3d %d %d\n ", controls.array[0], controls.array[1], (int8_t)controls.array[2], (int8_t)controls.array[3]);
         //pc.printf("%d\n",controls.result_32);
-//joystick.joystick(controls.array[0],controls.array[1], controls.array[2], controls.array[3]);
+joystick.joystick(controls.array[0],controls.array[1], controls.array[2], controls.array[3]);
     }
 
 }
\ No newline at end of file