v1 Stable
Dependencies: F401RE-USBHost USBHostXpad mbed
main.cpp
- Committer:
- Ownasaurus
- Date:
- 2016-11-04
- Revision:
- 2:c20d8438f206
- Parent:
- 1:3c21da72660d
- Child:
- 3:52b2a7514406
File content as of revision 2:c20d8438f206:
//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" 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); struct __attribute__((packed)) N64ControllerData // all bits are in the correct order { 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; 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; } }; 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 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(); } // 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()) { pc.printf("Error: XBox controller not found.\n"); } //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(); 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 } }