GamePortAdapter

Dependencies:   mbed-rtos mbed USBDevice USBJoystick

Revision:
3:20f3136e49fa
Parent:
2:c7f76dac3723
Child:
4:17b8ede8063a
--- a/main.cpp	Mon Dec 11 22:30:59 2017 +0000
+++ b/main.cpp	Wed Dec 13 04:39:26 2017 +0000
@@ -1,11 +1,16 @@
 #include "mbed.h"
+//#include "USBSerial.h"
+#include "USBMouseKeyboard.h"
+#include "USBJoystick.h"
 #include "rtos.h"
+#include "string.h"
+
 
 Serial pc(USBTX, USBRX);
 //macOS: screen /dev/tty.usbmodem{num} {baud rate}
 //Windows: Realterm lol
-
-Thread analogThread;
+USBMouseKeyboard key_mouse;
+//USBJoystick joystick;
 
 Timer y1_t;
 Timer x1_t;
@@ -21,11 +26,21 @@
 volatile int x1_pulse;
 volatile int y2_pulse;
 volatile int x2_pulse;
-volatile int buts;
+volatile int buttons;
 
 DigitalOut trig(p19);
 
-BusIn buttons(p21, p22, p23, p24);
+BusIn buttons_raw(p21, p22, p23, p24);
+
+//output, min, max, value
+int bindings[8][4] = {{10, 0, 100, (int)'a'},
+                      {10, 0, 100, (int)'w'},
+                      {10, 255, 0, 0},
+                      {10, 255, 0, 0},
+                      {10, 128, 255, (int)'1'},
+                      {10, 128, 255, (int)'2'},
+                      {10, 128, 255, (int)'3'},
+                      {10, 128, 255, (int)'4'}};
 
 void start_y1() {
     y1_t.reset();
@@ -68,6 +83,9 @@
 
 
 void analog_thread() {
+    
+    buttons_raw.mode(PullUp);
+    
     trig = 1;
     y1_pulse = 0;
     x1_pulse = 0;
@@ -85,27 +103,85 @@
     
     x2.rise(&start_x2);
     x2.fall(&stop_x2);
-    
-    buttons.mode(PullUp);
 
     while(1) {
         trig = 0;
         trig = 1;
-        buts = ~buttons&0xF;
-        wait(.1);
+        buttons = ~buttons_raw&0xF;
+        Thread::wait(1);
     }
 }
 
-int main() {
+
+void debug_thread() {
     pc.printf("Beginning Joystick Test...\r\n");
     pc.printf("---------------------------------\r\n");
     
-    analogThread.start(analog_thread);
-    
     while(1) {
-        pc.printf("Joystick 1 - %d, %d, %X \r\n", x1_pulse, y1_pulse, buts&0x3);
-        pc.printf("Joystick 2 - %d, %d, %X \r\n", x2_pulse, y2_pulse, (buts>>2)&0x3);
+        pc.printf("Joystick 1 - %d, %d, %X \r\n", x1_pulse, y1_pulse, buttons&0x3);
+        pc.printf("Joystick 2 - %d, %d, %X \r\n", x2_pulse, y2_pulse, (buttons>>2)&0x3);
         pc.printf("\r\n");
-        wait(.5);
+        Thread::wait(500);
     }
 }
+
+
+void output_thread() {
+    int joy[6];
+    int mouse[4];
+    int value, trigval;
+    
+    while(true) {
+        memset(joy, 0, sizeof(joy));
+        memset(mouse, 0, sizeof(mouse));
+        
+        for (int i=0; i<8; i++) {
+            //pc.printf("Checking %d: ", i);
+            switch (i) {
+                case 0: value = x1_pulse; trigval = x1_pulse; break;
+                case 1: value = y1_pulse; trigval = y1_pulse; break;
+                case 2: value = x2_pulse; trigval = x2_pulse; break;
+                case 3: value = y2_pulse; trigval = y2_pulse; break;
+                case 4: trigval = (buttons & 0x1)*255; value = bindings[i][3]; break;
+                case 5: trigval = ((buttons>>1) & 0x1)*255; value = bindings[i][3]; break;
+                case 6: trigval = ((buttons>>2) & 0x1)*255; value = bindings[i][3]; break;
+                case 7: trigval = ((buttons>>3) & 0x1)*255; value = bindings[i][3]; break;
+            }
+            
+            if (trigval >= bindings[i][1] && trigval <= bindings[i][2]) {
+                //pc.printf("Triggered : %d, %d, %d\r\n", i, trigval, value);
+                switch (bindings[i][0]) {
+                    case 0: joy[2] = value; break;//Joy X
+                    case 1: joy[3] = value; break; //Joy Y
+                    case 2: joy[0] = value; break; //Joy throttle
+                    case 3: joy[1] = value; break; //Joy rudder
+                    case 4: joy[4] |= bindings[i][3]; break; //Joy buttons
+                    case 5: joy[5] |= bindings[i][3]; break; //Joy hat
+                    case 6: mouse[0] = value/10; break; //Mouse X
+                    case 7: mouse[1] = value/10; break; //Mouse Y
+                    case 8: mouse[2] |= bindings[i][3]; break; //Mouse buttons
+                    case 9: mouse[3] = value/10; break; //Mouse scroll
+                    case 10: key_mouse.keyCode(bindings[i][3]); break; //Keypress
+                }  
+            }
+        }
+        //key_mouse.update(mouse[0], mouse[1], mouse[2], mouse[3]);
+        //joystick.update(joy[0], joy[1], joy[2], joy[3], joy[4], joy[5]);
+        Thread::wait(20);
+    }
+}
+
+
+int main() {
+    Thread analogThread;
+    Thread outputThread;
+    
+    analogThread.start(analog_thread);
+    Thread::wait(100);
+    outputThread.start(output_thread);
+    
+    //Thread debugThread;
+    //debugThread.start(debug_thread);
+    
+    while(1) Thread::yield();
+}