Christopher Blackstone / Mbed 2 deprecated GamePortAdapter

Dependencies:   mbed-rtos mbed USBDevice USBJoystick

Files at this revision

API Documentation at this revision

Comitter:
obsoleet37
Date:
Thu Dec 14 06:55:10 2017 +0000
Parent:
4:17b8ede8063a
Child:
6:43b59cb5dcd9
Commit message:
Added loading from binding file and keymouse/joystick mode switch in bindings

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Dec 14 03:33:02 2017 +0000
+++ b/main.cpp	Thu Dec 14 06:55:10 2017 +0000
@@ -1,10 +1,11 @@
 #include "mbed.h"
 #include "USBMouseKeyboard.h"
 #include "USBJoystick.h"
-#include "USBHID.h"
 #include "rtos.h"
 #include "string.h"
 
+extern "C" void mbed_reset();
+
 Serial pc(USBTX, USBRX);
 //macOS: screen /dev/tty.usbmodem{num} {baud rate}
 //Windows: Realterm lol
@@ -29,21 +30,23 @@
 
 BusIn buttons_raw(p21, p22, p23, p24);
 
-//input, min, max, output, value
-int numbindings = 4;
-/*int bindings[8][5] = {{0, 0, 100, 4, (int)'a'},
+//first row: necessary garbage, size, mode, reserved x2
+//following rows: input, min, max, output, value
+int bindings[128][5];
+/*int bindings[128][5] = {{0xFFFFFFFF, 7, 0, 0, 0},
+                      {0, 0, 100, 4, (int)'a'},
                       {0, 400, 1000, 4, (int)'d'},
                       {1, 0, 100, 4, (int)'w'},
                       {1, 400, 1000, 4, (int)'s'},
                       {4, 128, 255, 4, (int)'1'},
                       {5, 128, 255, 4, (int)'2'}};*/
                       
-int bindings[8][5] = {{0, 0, 1000, 0, 0},
+/*int bindings[8][5] = {{0, 0, 1000, 0, 0},
                       {1, 0, 1000, 1, 0},
                       {4, 128, 255, 4, 0x1},
-                      {5, 128, 255, 4, 0x2}};
+                      {5, 128, 255, 4, 0x2}};*/
                       
-volatile bool run = false;
+LocalFileSystem local("local");
 
 void start_y1() {
     y1_t.reset();
@@ -133,12 +136,11 @@
     USBMouseKeyboard keys_mouse;
     int mouse[4];
     int value, trigval;
-    run = true;
     
-    while(run) {
+    while(true) {
         memset(mouse, 0, sizeof(mouse));
         
-        for (int i=0; i<numbindings; i++) {
+        for (int i=1; i<bindings[0][1]; i++) {
             //pc.printf("Checking %d: ", i);
             switch (bindings[i][0]) {
                 case 0: value = x1_pulse; trigval = x1_pulse; break;
@@ -158,14 +160,13 @@
                     case 1: mouse[1] = value/10; break; //Mouse Y
                     case 2: mouse[2] |= bindings[i][4]; break; //Mouse buttons
                     case 3: mouse[3] = value/10; break; //Mouse scroll
-                    case 4: key_mouse.keyCode(bindings[i][4]); break; //Keypress
+                    case 4: keys_mouse.keyCode(bindings[i][4]); break; //Keypress
                 }  
             }
         }
         keys_mouse.update(mouse[0], mouse[1], mouse[2], mouse[3]);
         Thread::wait(20);
     }
-    keys_mouse.disconnect();
 }
 
 
@@ -173,12 +174,11 @@
     USBJoystick joystick;
     int joy[6];
     int value, trigval;
-    run = true;
     
-    while(run) {
+    while(true) {
         memset(joy, 0, sizeof(joy));
         
-        for (int i=0; i<numbindings; i++) {
+        for (int i=1; i<bindings[0][1]; i++) {
             //pc.printf("Checking %d: ", i);
             switch (bindings[i][0]) {
                 case 0: value = x1_pulse; trigval = x1_pulse; break;
@@ -206,21 +206,26 @@
         joystick.update(joy[0], joy[1], joy[2], joy[3], joy[4], joy[5]);
         Thread::wait(20);
     }
-    joystick.disconnect();
 }
 
 
 int main() {
+    
+    //Load bindings from file
+    FILE *bf = fopen("/local/bindings", "rb");
+    fread(bindings, 5*sizeof(int), 1, bf);
+    fread(bindings[1], 5*sizeof(int), bindings[0][1]-1, bf);
+    fclose(bf);
+    
     Thread analogThread;
     Thread outputThread;
     
     analogThread.start(analog_thread);
     Thread::wait(100);
-    outputThread.start(joystick_output_thread);
-    Thread::wait(2*60*1000);
-    run = false;
-    outputThread.terminate();
-    outputThread.start(keys_mouse_output_thread);
+    switch(bindings[0][2]) {
+        case 0: outputThread.start(keys_mouse_output_thread); break;
+        case 1: outputThread.start(joystick_output_thread); break;
+    }
     
     //Thread debugThread;
     //debugThread.start(debug_thread);