v1 Stable

Dependencies:   F401RE-USBHost USBHostXpad mbed

Revision:
3:52b2a7514406
Parent:
2:c20d8438f206
Child:
5:32c8b316582a
--- a/main.cpp	Fri Nov 04 21:29:33 2016 +0000
+++ b/main.cpp	Mon Dec 05 02:38:24 2016 +0000
@@ -1,14 +1,13 @@
 //TODO: save controller layout to sram
-// finish and make 100% functional for keyboard players
-// duplicate program and modify to work with x360
 
 #include "mbed.h"
 #include "USBHostXpad.h"
+#include "stm32f4xx_flash.h"
 
 DigitalOut myled(LED1);
 Serial pc(USBTX, USBRX); // tx, rx
 DigitalInOut data(PA_8);
-//DigitalIn button(PC_13); // eventually code to set controlls
+//DigitalIn button(PC_13); // eventually code to set controls
 
 /**
 @namespace AXYB
@@ -23,7 +22,7 @@
 /**
 @namespace XLBRB
 @brief Integer for storing the hex of the LB,RB and center X buttons
-@brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in second value
+@brief XPad returns a 4 digit hex for all buttons- XLBRB buttons are stored in second value
 @param LB - given as a 1
 @param R - given as a 2
 @param X - given as a 4
@@ -34,7 +33,7 @@
 /**
 @namespace bkStrtLCRC
 @brief Integer for storing the hex of the Left analog button,Right analog button,back and start buttons
-@brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in third value
+@brief XPad returns a 4 digit hex for all buttons- bkStrtLCRC buttons are stored in third value
 @param start - given as a 1
 @param back - given as a 2
 @param LC - given as a 4
@@ -44,7 +43,7 @@
 /**
 @namespace DPad
 @brief Integer for storing the hex of the Directional buttons
-@brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in fourth value
+@brief XPad returns a 4 digit hex for all buttons- DPad buttons are stored in fourth value
 @param Up - given as a 1
 @param Down - given as a 2
 @param Left - given as a 4
@@ -105,7 +104,7 @@
 @brief makes the range of the triggers 0 to 10
 */
 const float tN=0.03921568627;//(10/255)
-const int DEADZONE = 10;
+const int DEADZONE = 8;
 
 char reverse(char b) 
 {
@@ -143,104 +142,25 @@
 
 } n64_data;
 
-struct __attribute__((packed)) KeyboardControls
+void onXpadEvent (int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r)
 {
-    uint8_t KEYBOARD_a;
-    uint8_t KEYBOARD_b;
-    uint8_t KEYBOARD_z;
-    uint8_t KEYBOARD_start;
-    uint8_t KEYBOARD_d_up;
-    uint8_t KEYBOARD_d_down;
-    uint8_t KEYBOARD_d_left;
-    uint8_t KEYBOARD_d_right;
-    uint8_t KEYBOARD_l;
-    uint8_t KEYBOARD_r;
-    uint8_t KEYBOARD_c_up;
-    uint8_t KEYBOARD_c_down;
-    uint8_t KEYBOARD_c_left;
-    uint8_t KEYBOARD_c_right;
-    uint8_t KEYBOARD_a_up;
-    uint8_t KEYBOARD_a_down;
-    uint8_t KEYBOARD_a_left;
-    uint8_t KEYBOARD_a_right;
-    
-    KeyboardControls()
-    {
-        LoadDefault();
-    }
-    
-    void Print()
-    {
-        pc.printf("KEYBOARD_a = %x\r\n", KEYBOARD_a);
-        pc.printf("KEYBOARD_start = %x\r\n", KEYBOARD_start);
-        pc.printf("KEYBOARD_a_up = %x\r\n", KEYBOARD_a_up);
-    }
+    AXYB=buttons>>12;
+    XLBRB=(buttons&0x0f00)>>8;
+    bkStrtLCRC=(buttons&0x00f0)>>4;
+    DPad=buttons&0x000f;
     
-    void LoadDefault()
-    {
-        KEYBOARD_a =         0x0E;
-        KEYBOARD_b =         0x0D;
-        KEYBOARD_z =         0x0F;
-        KEYBOARD_start =     0x0B;
-        KEYBOARD_d_up =      0x1D;
-        KEYBOARD_d_down =    0x1B;
-        KEYBOARD_d_left =    0x06;
-        KEYBOARD_d_right =   0x19;
-        KEYBOARD_l =         0x1A;
-        KEYBOARD_r =         0x12;
-        KEYBOARD_c_up =      0x17;
-        KEYBOARD_c_down =    0x1C;
-        KEYBOARD_c_left =    0x18;
-        KEYBOARD_c_right =   0x0C;
-        KEYBOARD_a_up =      0x08;
-        KEYBOARD_a_down =    0x07;
-        KEYBOARD_a_left =    0x16;
-        KEYBOARD_a_right =   0x09;
-    }
+    LSY=(char)((int)(stick_ly*sN));
+    LSX=(char)((int)(stick_lx*sN));
+    RSY=stick_ry*sN;
+    RSX=stick_rx*sN;
     
-    void LoadBlank()
-    {
-        KEYBOARD_a =         0x00;
-        KEYBOARD_b =         0x00;
-        KEYBOARD_z =         0x00;
-        KEYBOARD_start =     0x00;
-        KEYBOARD_d_up =      0x00;
-        KEYBOARD_d_down =    0x00;
-        KEYBOARD_d_left =    0x00;
-        KEYBOARD_d_right =   0x00;
-        KEYBOARD_l =         0x00;
-        KEYBOARD_r =         0x00;
-        KEYBOARD_c_up =      0x00;
-        KEYBOARD_c_down =    0x00;
-        KEYBOARD_c_left =    0x00;
-        KEYBOARD_c_right =   0x00;
-        KEYBOARD_a_up =      0x00;
-        KEYBOARD_a_down =    0x00;
-        KEYBOARD_a_left =    0x00;
-        KEYBOARD_a_right =   0x00;
-    }
-};
-
-KeyboardControls kc;
-
-inline void XpadValues(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r)
-{
-    /*pc.printf("Xpad: %04x %-5d %-5d %-5d %-5d %d %d\r\n", buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r);//%04x makes it display (hex) 0 as 0000 %02x
-    pc.printf("buttons: %04x\r\n",buttons);
-    pc.printf("AXYB: %x\r\n",AXYB);
-    pc.printf("XLBRB: %x\r\n",XLBRB);
-    pc.printf("bkStrtLCRC: %x\r\n",bkStrtLCRC); 
-    pc.printf("Dpad: %x\r\n",DPad);
-    pc.printf("LSY: %u\r\n",LSY);
-    pc.printf("LSX: %u\r\n",LSX);
-    pc.printf("RSY: %.2f\r\n",RSY);
-    pc.printf("RSX: %.2f\r\n",RSX);
-    pc.printf("Lt: %.2f\r\n",Lt);
-    pc.printf("Rt: %.2f\r\n",Rt); */
+    Lt=trigger_l*tN;
+    Rt=trigger_r*tN;
     
     memset(&n64_data,0,4); // clear controller state
     
-    if(AXYB & 0x01) // a
+    // Owna's Controls
+    /*if(AXYB & 0x01) // a
     {
         n64_data.a = 1;
     }
@@ -267,7 +187,101 @@
     if(XLBRB & 0x01) // left bumper
     {
         n64_data.l = 1;
+    }*/
+    
+    // Supa's Controls
+    /*if(AXYB & 0x01) // a
+    {
+        n64_data.a = 1;
     }
+    if((AXYB >> 2) & 0x01) // x
+    {
+        n64_data.b = 1;
+    }
+    if((AXYB >> 1) & 0x01) // b
+    {
+        n64_data.c_down = 1;
+    }
+    if((AXYB >> 3) & 0x01) // y
+    {
+        n64_data.z = 1;
+    }
+    if(bkStrtLCRC & 0x01) // start
+    {
+        n64_data.start = 1;
+    }
+    if((XLBRB >> 1) & 0x01) // right bumper
+    {
+        n64_data.r = 1;
+    }
+    if(XLBRB & 0x01) // left bumper
+    {
+        n64_data.l = 1;
+    }
+    if(DPad & 0x01) // DPad Up
+    {
+        n64_data.c_up = 1;
+    }
+    if(DPad & 0x02) // DPad Down
+    {
+        n64_data.c_down = 1;
+    }
+    if(DPad & 0x04) // DPad Left
+    {
+        n64_data.c_left = 1;
+    }
+    if(DPad & 0x08) // DPad Right
+    {
+        n64_data.c_right = 1;
+    }*/
+    
+    // LD's Controls
+    if(AXYB & 0x01) // a
+    {
+        n64_data.a = 1;
+    }
+    if((AXYB >> 2) & 0x01) // x
+    {
+        n64_data.b = 1;
+    }
+    if((AXYB >> 3) & 0x01) // y
+    {
+        n64_data.c_up = 1;
+    }
+    if(bkStrtLCRC & 0x01) // start
+    {
+        n64_data.start = 1;
+    }
+    if((XLBRB >> 1) & 0x01) // right bumper
+    {
+        n64_data.r = 1;
+    }
+    if(XLBRB & 0x01) // left bumper
+    {
+        n64_data.l = 1;
+    }
+    if(DPad & 0x01) // DPad Up
+    {
+        n64_data.c_up = 1;
+    }
+    if(DPad & 0x02) // DPad Down
+    {
+        n64_data.c_down = 1;
+    }
+    if(DPad & 0x04) // DPad Left
+    {
+        n64_data.c_left = 1;
+    }
+    if(DPad & 0x08) // DPad Right
+    {
+        n64_data.c_right = 1;
+    }
+    if(Rt > 50) // Right trigger greater than threshold
+    {
+        n64_data.z = 1;
+    }
+    
+    // Generic analog stick
     if(LSX > DEADZONE)
     {
         n64_data.x_axis = reverse(LSX);
@@ -277,26 +291,6 @@
         n64_data.y_axis = reverse(LSY);
     }
 }
- 
-void onXpadEvent (int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r)
-{
-    AXYB=buttons>>12;
-    XLBRB=(buttons&0x0f00)>>8;
-    bkStrtLCRC=(buttons&0x00f0)>>4;
-    DPad=buttons&0x000f;
-    
-    LSY=(char)((int)(stick_ly*sN));
-    LSX=(char)((int)(stick_lx*sN));
-    RSY=stick_ry*sN;
-    RSX=stick_rx*sN;
-    
-    Lt=trigger_l*tN;
-    Rt=trigger_r*tN;
-    
-    XpadValues(buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r);
-}
-
-KeyboardControls* saveData = (KeyboardControls*)0x08000000; // sector 0 address
 
 // 0 is 3 microseconds low followed by 1 microsecond high
 // 1 is 1 microsecond low followed by 3 microseconds high
@@ -422,32 +416,9 @@
     SendStop();
 }
 
-// does not work.  need to unlock ?
-void SaveControls()
-{
-    memcpy(saveData,&kc,sizeof(KeyboardControls));
-    pc.printf("Controls have been saved!\r\n");
-}
-
-void LoadControls()
-{
-    memcpy(&kc,saveData,sizeof(KeyboardControls));
-    pc.printf("Controls have been loaded!\r\n");
-}
-
-void Test()
-{
-    pc.printf("sizeof(KeyboardControls) is %d\r\n",sizeof(KeyboardControls));
-    saveData->Print();
-    SaveControls();
-    saveData->Print();
-}
-
 int main()
 {
     pc.printf("\r\nNow loaded! SystemCoreClock = %d Hz\r\n", SystemCoreClock);
-    //Test();
-    //LoadControls();
 
     USBHostXpad xpad;
     if (!xpad.connect()) {