GamePortAdapter
Dependencies: mbed-rtos mbed USBDevice USBJoystick
Diff: main.cpp
- Revision:
- 4:17b8ede8063a
- Parent:
- 3:20f3136e49fa
- Child:
- 5:c2e3d6e70d85
--- a/main.cpp Wed Dec 13 04:39:26 2017 +0000
+++ b/main.cpp Thu Dec 14 03:33:02 2017 +0000
@@ -1,16 +1,13 @@
#include "mbed.h"
-//#include "USBSerial.h"
#include "USBMouseKeyboard.h"
#include "USBJoystick.h"
+#include "USBHID.h"
#include "rtos.h"
#include "string.h"
-
Serial pc(USBTX, USBRX);
//macOS: screen /dev/tty.usbmodem{num} {baud rate}
//Windows: Realterm lol
-USBMouseKeyboard key_mouse;
-//USBJoystick joystick;
Timer y1_t;
Timer x1_t;
@@ -32,15 +29,21 @@
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'}};
+//input, min, max, output, value
+int numbindings = 4;
+/*int bindings[8][5] = {{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},
+ {1, 0, 1000, 1, 0},
+ {4, 128, 255, 4, 0x1},
+ {5, 128, 255, 4, 0x2}};
+
+volatile bool run = false;
void start_y1() {
y1_t.reset();
@@ -126,49 +129,84 @@
}
-void output_thread() {
- int joy[6];
+void keys_mouse_output_thread() {
+ USBMouseKeyboard keys_mouse;
int mouse[4];
int value, trigval;
+ run = true;
- while(true) {
- memset(joy, 0, sizeof(joy));
+ while(run) {
memset(mouse, 0, sizeof(mouse));
- for (int i=0; i<8; i++) {
+ for (int i=0; i<numbindings; i++) {
//pc.printf("Checking %d: ", i);
- switch (i) {
+ switch (bindings[i][0]) {
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;
+ case 4: trigval = (buttons & 0x1)*255; value = bindings[i][4]; break;
+ case 5: trigval = ((buttons>>1) & 0x1)*255; value = bindings[i][4]; break;
+ case 6: trigval = ((buttons>>2) & 0x1)*255; value = bindings[i][4]; break;
+ case 7: trigval = ((buttons>>3) & 0x1)*255; value = bindings[i][4]; break;
+ }
+
+ if (trigval >= bindings[i][1] && trigval <= bindings[i][2]) {
+ pc.printf("Triggered : %d, %d, %d\r\n", i, trigval, value);
+ switch (bindings[i][3]) {
+ case 0: mouse[0] = value/10; break; //Mouse X
+ 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
+ }
+ }
+ }
+ keys_mouse.update(mouse[0], mouse[1], mouse[2], mouse[3]);
+ Thread::wait(20);
+ }
+ keys_mouse.disconnect();
+}
+
+
+void joystick_output_thread() {
+ USBJoystick joystick;
+ int joy[6];
+ int value, trigval;
+ run = true;
+
+ while(run) {
+ memset(joy, 0, sizeof(joy));
+
+ for (int i=0; i<numbindings; i++) {
+ //pc.printf("Checking %d: ", i);
+ switch (bindings[i][0]) {
+ 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][4]; break;
+ case 5: trigval = ((buttons>>1) & 0x1)*255; value = bindings[i][4]; break;
+ case 6: trigval = ((buttons>>2) & 0x1)*255; value = bindings[i][4]; break;
+ case 7: trigval = ((buttons>>3) & 0x1)*255; value = bindings[i][4]; 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]) {
+ switch (bindings[i][3]) {
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
+ case 4: joy[4] |= bindings[i][4]; break; //Joy buttons
+ case 5: joy[5] |= bindings[i][4]; break; //Joy hat
}
}
}
- //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]);
+ joystick.update(joy[0], joy[1], joy[2], joy[3], joy[4], joy[5]);
Thread::wait(20);
}
+ joystick.disconnect();
}
@@ -178,7 +216,11 @@
analogThread.start(analog_thread);
Thread::wait(100);
- outputThread.start(output_thread);
+ outputThread.start(joystick_output_thread);
+ Thread::wait(2*60*1000);
+ run = false;
+ outputThread.terminate();
+ outputThread.start(keys_mouse_output_thread);
//Thread debugThread;
//debugThread.start(debug_thread);
