N64 to USB HID interface

Dependencies:   mbed

Revision:
4:fee2d61ad6bc
Parent:
3:b9cecf3e2496
Child:
5:eb93a4f91396
--- a/main.cpp	Fri Sep 30 00:15:10 2011 +0000
+++ b/main.cpp	Fri Sep 30 23:16:52 2011 +0000
@@ -112,8 +112,8 @@
 #define WRITE_EEPROM        0x05
 #define RESET_CONTROLLER    0xff
 
-#define T1                  24      // Corresponds to 1 us        
-#define T2                  72      // Corresponds to 3 us
+#define T1                  22      // Corresponds to 1 us        
+#define T2                  68      // Corresponds to 3 us
 
 DigitalInOut inout(p14);            // Connect controller here
 // using 220 ohm pull-up
@@ -121,8 +121,8 @@
 
 uint8_t x, y;
 union controls { /* A definition and a declaration */
-    uint8_t array[4];
-    uint32_t result_32;
+    uint8_t array[8];
+    uint32_t result[2];
 } controls;
 
 // Temp array for controller data
@@ -151,7 +151,7 @@
     //inout.input();                // Not sure about this..
 
     sample = inout.read();
-    for (i=0;i < n ;i++) {
+    for (i=0; i < n ; i++) {
         byte = 0;
         bit  = 0;
         while (bit<8) {
@@ -170,18 +170,21 @@
         }
 
     }
-     /* 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;
+    /* 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;
+    */
+    //data_array[2]= 5;
     return n;
 }
 /* void send_byte(unsigned char byte) is used to send a single
@@ -211,27 +214,99 @@
     delay(T1);
     inout.write(1);
     delay(T1);
+}
+
+/* void send_byte(unsigned char byte) is used to send a single
+   byte to the controller.
+ */
+void send(uint8_t * cmd, uint8_t n) {
+    uint8_t i,j,byte;
+
+    inout.write(1);             // Makes sure line is left high
+    wait_us(500);
+    for (j=0; j<n; j++) {
+        byte=cmd[j];
+        for (i=0; i<8; i++) {
+            if (byte & 128>>i) {
+                inout.write(0);
+                delay(T1);
+                inout.write(1);
+                delay(T2);
+            } else {
+                inout.write(0);
+                delay(T2);
+                inout.write(1);
+                delay(T1);
+            }
+        }
+
+    }/* Send Stop Bit */
+    inout.write(0);
+    delay(T1);
+    inout.write(1);
+    delay(T1);
 
 }
 
+
+uint8_t cmd[8];
+
+
 int main() {
     inout.mode(OpenDrain);           // Must use open-drain for N64 Controller!
-
+    uint8_t cmd[8];
+    int8_t n;
+    int temp;
     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
+        n=3;
+        cmd[0]=2; // Request Controls
+        cmd[1]=3;
+        cmd[0]=64;
+        send(cmd,3);
+        receive(controls.array, 8);           // Start receiving bit stream
+        //pc.printf("%3d %3d %d %d\n ", controls.array[0], controls.array[1], (int8_t)controls.array[2], (int8_t)controls.array[3]);
+
+        for (n=7; n>=0; n--){
+            if (controls.array[2] & (1 << n))
+            pc.printf("1");
+            else 
+            pc.printf("0");
+            }
+          //pc.printf("%d %d", (int8_t)(controls.array[2]^= (1 << 7)),controls.array[3]);
+        pc.printf("\n");
+/*
+        for (n=0; n<16; n++)
+            pc.printf("%d", (1 & ((uint16_t)controls.result[0] >> n)));
+        pc.printf(" ");
+
+        for (n=17; n<32; n++)
+            pc.printf("%d", (1 & ((uint32_t)controls.result[0] >> n)));
+        pc.printf(" ");
         
-        if (controls.result_32==previous_result) continue;
+        for (n=0; n<16; n++)
+            pc.printf("%d", (1 & ((uint16_t)controls.result[1] >> n)));
+        pc.printf(" ");
         
-        
+        for (n=17; n<32; n++)
+        pc.printf("%d", (1 & ((uint32_t)controls.result[1] >> n)));
+        pc.printf("\n");
+*/
+    }
+
+    while (1) {
+        wait_ms(10);
+        //previous_result=controls.result_32;
+        send_byte(1);                // Send the request byte
+        receive(controls.array, 8);           // Start receiving bit stream
+
+        // 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("%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