v1 Stable
Dependencies: F401RE-USBHost USBHostXpad mbed
main.cpp
- Committer:
- Ownasaurus
- Date:
- 2016-12-12
- Revision:
- 5:32c8b316582a
- Parent:
- 3:52b2a7514406
- Child:
- 6:21365f733399
File content as of revision 5:32c8b316582a:
//TODO: save controller layout to sram #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 controls /** @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- 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 */ 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- 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 @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- 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 @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 = 3; 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); struct __attribute__((packed)) N64ControllerData // all bits are in the correct order... except for the analog { unsigned int a : 1; // 1 bit wide unsigned int b : 1; unsigned int z : 1; unsigned int start : 1; unsigned int up : 1; unsigned int down : 1; unsigned int left : 1; unsigned int right : 1; unsigned int dummy1 : 1; unsigned int dummy2 : 1; unsigned int l :1 ; unsigned int r : 1; unsigned int c_up : 1; unsigned int c_down : 1; unsigned int c_left : 1; unsigned int c_right : 1; char x_axis; char y_axis; } n64_data; 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; //pc.printf("AXYB: %u, XLBRB, %u, bkStrtLCRC %u, DPad, %u\r\n",AXYB,XLBRB,bkStrtLCRC,DPad); // normalize the analog stick values to be 80 max LSY=(char)((int)(stick_ly*sN)); LSX=(char)((int)(stick_lx*sN)); RSY=stick_ry*sN; RSX=stick_rx*sN; // normalize the trigger values to be 10 max Lt=trigger_l*tN; Rt=trigger_r*tN; memset(&n64_data,0,4); // clear controller state // Owna'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.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; } // 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 > 5) // Right trigger greater than threshold { n64_data.z = 1; }*/ // JJ'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_left = 1; } if((AXYB >> 1) & 0x01) // b { n64_data.c_right = 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.z = 1; } if(DPad & 0x01) // DPad Up { n64_data.up = 1; } if(DPad & 0x02) // DPad Down { n64_data.down = 1; } if(DPad & 0x04) // DPad Left { n64_data.left = 1; } if(DPad & 0x08) // DPad Right { n64_data.right = 1; } if(Lt > 2 || Rt > 2) // Triggers greater than threshold { n64_data.z = 1; }*/ // Generic analog stick if(LSX > DEADZONE) { n64_data.x_axis = reverse(LSX); } if(LSY > DEADZONE) { n64_data.y_axis = reverse(LSY); } } // 0 is 3 microseconds low followed by 1 microsecond high // 1 is 1 microsecond low followed by 3 microseconds high unsigned int GetMiddleOfPulse() { // wait for line to go high while(1) { if(data.read() == 1) break; } // wait for line to go low while(1) { if(data.read() == 0) break; } // now we have the falling edge // wait 2 microseconds to be in the middle of the pulse, and read. high --> 1. low --> 0. my_wait_us_asm(2); return (unsigned int) data.read(); } // continuously read bits until at least 9 are read, confirm valid command, return without stop bit unsigned int readCommand() { unsigned int command = GetMiddleOfPulse(), bits_read = 1; while(1) // read at least 9 bits (2 bytes + stop bit) { //my_wait_us_asm(4); command = command << 1; // make room for the new bit //command += data.read(); // place the new bit into the command command += GetMiddleOfPulse(); command &= 0x1FF; // remove all except the last 9 bits bits_read++; if(bits_read >= 9) // only consider when at least a whole command's length has been read { if(command == 0x3 || command == 0x1 || command == 0x1FF || command == 0x5 || command == 0x7) { // 0x3 = 0x1 + stop bit --> get controller state // 0x1 = 0x0 + stop bit --> who are you? // 0x1FF = 0xFF + stop bit --> reset signal // 0x5 = 0x10 + stop bit --> read // 0x7 = 0x11 + stop bit --> write command = command >> 1; // get rid of the stop bit return command; } } } } void write_1() { data = 0; my_wait_us_asm(1); data = 1; my_wait_us_asm(3); //pc.printf("1"); } void write_0() { data = 0; my_wait_us_asm(3); data = 1; my_wait_us_asm(1); //pc.printf("0"); } void SendStop() { data = 0; my_wait_us_asm(1); data = 1; } // send a byte from LSB to MSB (proper serialization) void SendByte(unsigned char b) { for(int i = 0;i < 8;i++) // send all 8 bits, one at a time { if((b >> i) & 1) { write_1(); } else { write_0(); } } } void SendIdentity() { // reply 0x05, 0x00, 0x02 SendByte(0x05); SendByte(0x00); SendByte(0x02); SendStop(); } void SendControllerData() { unsigned long data = *(unsigned long*)&n64_data; unsigned int size = sizeof(data) * 8; // should be 4 bytes * 8 = 32 bits for(unsigned int i = 0;i < size;i++) { if((data >> i) & 1) { write_1(); } else { write_0(); } } SendStop(); } int main() { //Timer t; pc.printf("\r\nNow loaded! SystemCoreClock = %d Hz\r\n", SystemCoreClock); USBHostXpad xpad; if (!xpad.connect()) { pc.printf("Error: XBox controller not found.\n"); } xpad.attachEvent(onXpadEvent); xpad.led(USBHostXpad::LED1_ON); while(1) { //t.start(); // Set pin mode to input data.input(); USBHost::poll(); __disable_irq(); // Disable Interrupts // Read 64 command unsigned int cmd = readCommand(); my_wait_us_asm(2); // wait a small amount of time before replying //-------- SEND RESPONSE // Set pin mode to output data.output(); switch(cmd) { case 0x00: // identity case 0xFF: // reset SendIdentity(); break; case 0x01: // poll for state SendControllerData(); break; default: // we do not process the read and write commands (memory pack) break; } __enable_irq(); // Enable Interrupts //-------- DONE SENDING RESPOSE //t.stop(); pc.printf("Time: %d\r\n",t.read_us()); //t.reset(); } }