v1 Stable

Dependencies:   F401RE-USBHost USBHostXpad mbed

Revision:
2:c20d8438f206
Parent:
1:3c21da72660d
Child:
3:52b2a7514406
--- a/main.cpp	Mon Oct 31 19:26:40 2016 +0000
+++ b/main.cpp	Fri Nov 04 21:29:33 2016 +0000
@@ -3,7 +3,117 @@
 // duplicate program and modify to work with x360
 
 #include "mbed.h"
-#include "USBHostKeyboard.h"
+#include "USBHostXpad.h"
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); // tx, rx
+DigitalInOut data(PA_8);
+//DigitalIn button(PC_13); // eventually code to set controlls
+
+/**
+@namespace AXYB
+@brief Integer for storing the hex of the A X Y B buttons
+@brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in first value
+@param A - given as a 1
+@param B - given as a 2
+@param X - given as a 4
+@param Y - given as a 8
+*/
+uint8_t AXYB=0x0;
+/**
+@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
+@param LB - given as a 1
+@param R - given as a 2
+@param X - given as a 4
+ 
+*/
+uint8_t XLBRB=0x0;
+ 
+/**
+@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
+@param start - given as a 1
+@param back - given as a 2
+@param LC - given as a 4
+@param RC - given as a 8
+*/
+uint8_t bkStrtLCRC=0x0;
+/**
+@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
+@param Up - given as a 1
+@param Down - given as a 2
+@param Left - given as a 4
+@param Right - given as a 8
+*/
+uint8_t DPad=0x0;
+/**
+@namespace LSY
+@brief float for storing the value of the Left Analogue Stick's Y axis
+@brief XPad returns a value between -32768(down) and 32767(up)
+@there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point)
+*/
+char LSY=0x0;
+/**
+@namespace LSX
+@brief float for storing the value of the Left Analogue Stick's X axis
+@brief XPad returns a value between -32768(left) and 32767(right)
+@there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point)
+*/
+char LSX=0x0;
+/**
+@namespace RSY
+@brief float for storing the value of the Right Analogue Stick's Y axis
+@brief XPad returns a value between -32768() and 32767(up)
+@there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point)
+*/
+float RSY=0x0;
+/**
+@namespace RSX
+@brief float for storing the value of the Right Analogue Stick's X axis
+@brief XPad returns a value between -32768(left) and 32767(right)
+@there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point)
+*/
+float RSX=0x0;
+/**
+@namespace sN
+@brief float for storing the stick Normalising value
+@brief makes the range of the sticks -80 to 80
+*/
+const float sN=0.00244140625;//(80/32768)
+/**
+@namespace Lt
+@brief float for storing the value of the Left trigger
+@brief XPad returns a value between 0(not pressed) and 255(fully pressed)
+@
+*/
+float Lt=0x0;
+/**
+@namespace Rt
+@brief float for storing the value of the Left trigger
+@brief XPad returns a value between 0(not pressed) and 255(fully pressed)
+@
+*/
+float Rt=0x0;
+/**
+@namespace tN
+@brief float for storing the trigger Normalising value
+@brief makes the range of the triggers 0 to 10
+*/
+const float tN=0.03921568627;//(10/255)
+const int DEADZONE = 10;
+
+char reverse(char b) 
+{
+   b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
+   b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
+   b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
+   return b;
+}
 
 extern "C" void my_wait_us_asm (int n);
 
@@ -33,29 +143,160 @@
 
 } n64_data;
 
-const uint8_t KEYBOARD_a =         0x0E;
-const uint8_t KEYBOARD_b =         0x0D;
-const uint8_t KEYBOARD_z =         0x0F;
-const uint8_t KEYBOARD_start =     0x0B;
-const uint8_t KEYBOARD_d_up =      0x1D;
-const uint8_t KEYBOARD_d_down =    0x1B;
-const uint8_t KEYBOARD_d_left =    0x06;
-const uint8_t KEYBOARD_d_right =   0x19;
-const uint8_t KEYBOARD_l =         0x1A;
-const uint8_t KEYBOARD_r =         0x12;
-const uint8_t KEYBOARD_c_up =      0x17;
-const uint8_t KEYBOARD_c_down =    0x1C;
-const uint8_t KEYBOARD_c_left =    0x18;
-const uint8_t KEYBOARD_c_right =   0x0C; 
-const uint8_t KEYBOARD_a_up =      0x08;
-const uint8_t KEYBOARD_a_down =    0x07;
-const uint8_t KEYBOARD_a_left =    0x16;
-const uint8_t KEYBOARD_a_right =   0x09;
+struct __attribute__((packed)) KeyboardControls
+{
+    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);
+    }
+    
+    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;
+    }
+    
+    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;
+    }
+};
 
-DigitalOut myled(LED1);
-Serial pc(USBTX, USBRX); // tx, rx
-DigitalInOut data(PA_8);
-//DigitalIn button(PC_13); // eventually code to set controlls
+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); */
+    
+    memset(&n64_data,0,4); // clear controller state
+    
+    if(AXYB & 0x01) // a
+    {
+        n64_data.a = 1;
+    }
+    if((AXYB >> 2) & 0x01) // x
+    {
+        n64_data.b = 1;
+    }
+    if((AXYB >> 1) & 0x01) // b
+    {
+        n64_data.z = 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(LSX > DEADZONE)
+    {
+        n64_data.x_axis = reverse(LSX);
+    }
+    if(LSY > DEADZONE)
+    {
+        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
@@ -181,107 +422,47 @@
     SendStop();
 }
 
-// keyboard buttons are stored in cells 2 3 4 5 6 7?  cell 0 and 1 are modifiers?  cell8 is an F?
-// the buttons all become 1 if overflow, i think.  or in short, [2] == [3]
-void onKeyboardEvent(uint8_t rep[9])
+// does not work.  need to unlock ?
+void SaveControls()
+{
+    memcpy(saveData,&kc,sizeof(KeyboardControls));
+    pc.printf("Controls have been saved!\r\n");
+}
+
+void LoadControls()
 {
-    /*printf("Report = [");
-    for(int i = 0;i < 8;i++)
-    {
-        printf("%X, ", rep[i]);
-    }
-    printf("%X]\r\n", rep[8]);*/
-    
-    memset(&n64_data,0,4); // clear controller state
-    
-    bool leaveLoop = false;
-    
-    for(int index = 2;index < 8;index++)
-    {
-        switch(rep[index]) // the key code
-        {
-            case 0: // no more keys to process
-                leaveLoop = true;
-                break;
-            case KEYBOARD_a:
-                n64_data.a = 1;
-                break;
-            case KEYBOARD_b:
-                n64_data.b = 1;
-                break;
-            case KEYBOARD_z:
-                n64_data.z = 1;
-                break;
-            case KEYBOARD_start:
-                n64_data.start = 1;
-                break;
-            case KEYBOARD_d_up:
-                n64_data.up = 1;
-                break;
-            case KEYBOARD_d_down:
-                n64_data.down = 1;
-                break;
-            case KEYBOARD_d_left:
-                n64_data.left = 1;
-                break;
-            case KEYBOARD_d_right:
-                n64_data.right = 1;
-                break;
-            case KEYBOARD_l:
-                n64_data.l = 1;
-                break;
-            case KEYBOARD_r:
-                n64_data.r = 1;
-                break;
-            case KEYBOARD_c_up:
-                n64_data.c_up = 1;
-                break;
-            case KEYBOARD_c_down:
-                n64_data.c_down = 1;
-                break;
-            case KEYBOARD_c_left:
-                n64_data.c_left = 1;
-                break;
-            case KEYBOARD_c_right:
-                n64_data.c_right = 1;
-                break;
-            // NOTE: THESE BITS MUST BE WRITTEN IN REVERSE ORDER.  HIGH BIT IS IN THE LOW POSITION
-            case KEYBOARD_a_up:
-                n64_data.y_axis = 0x0A;
-                break;
-            case KEYBOARD_a_down:
-                n64_data.y_axis = 0x0D;
-                break;
-            case KEYBOARD_a_left:
-                n64_data.x_axis = 0x0D;
-                break;
-            case KEYBOARD_a_right:
-                n64_data.x_axis = 0x0A;
-                break;
-        }
-        
-        if(leaveLoop) break;
-    }
+    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("Now loaded! SystemCoreClock = %d Hz\r\n", SystemCoreClock);
-    memset(&n64_data,0,4); // start controller in the neutral state
+    pc.printf("\r\nNow loaded! SystemCoreClock = %d Hz\r\n", SystemCoreClock);
+    //Test();
+    //LoadControls();
 
-    USBHostKeyboard kb;
-    if (!kb.connect()) {
-        pc.printf("Error: USB kb not found.\n");
+    USBHostXpad xpad;
+    if (!xpad.connect()) {
+        pc.printf("Error: XBox controller not found.\n");
     }
-    // when connected, attach handler called on kb event
-    kb.attach(onKeyboardEvent);
+    
+    //int USBHostXpad::read (PAD pad) <-- try using this instead of callbacks
+    xpad.attachEvent(onXpadEvent);
+    xpad.led(USBHostXpad::LED1_ON);
     
     while(1)
     {
         // Set pin mode to input
         data.input();
         
-        // Read keyboard state?
         USBHost::poll();
         
         __disable_irq();    // Disable Interrupts