Modification of USB Joystick to work as a Hitbox-like game controller using the F401RE. Works reasonably well under Windows.

Dependencies:   USBDevice mbed

This is a hobby project trying to recreate a Hitbox Controller for fun .

The original code was based off of http://developer.mbed.org/users/wim/notebook/usb-joystick-device/ Slight modifications were made to the USB HID descriptor to up the number of buttons reported to 13.

Currently, this works well under Windows and Linux, but no luck getting it to work with Mac OS X or a PS3 so far.

If anybody has useful information, feel free to contribute. :)

p.s: My thread for hooking up an F401RE board as a USB device is here: http://developer.mbed.org/questions/5364/Cannot-get-the-Nucleo-F401RE-to-work-as-/

p.s.2: Cabling information and <del>pictures</del> will be uploaded in the near future.

p.s.3: 2015-04-20: added support for FRDM-KL25Z board. Due to the better usb functionality, this hitbox now works on Windows, Mac OS X, Linux and PS3 too! (minus the "PS" button...)

Revision:
0:c8e9c90ca7f4
Child:
1:6b3335ac3862
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 03 07:54:52 2014 +0000
@@ -0,0 +1,116 @@
+#include "mbed.h"
+#include "USBJoystick.h"
+
+USBJoystick joystick;
+// Variables for Heartbeat and Status monitoring
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut heartbeatLED(LED4);
+#define IN_UP PC_0
+#define IN_RIGHT PC_1
+#define IN_DOWN PB_0
+#define IN_LEFT PA_4
+
+#define UP 0x01
+#define RIGHT 0x02
+#define DOWN 0x04
+#define LEFT 0x08
+BusIn hatin(PC_0, PC_1, PB_0, PA_4);
+BusIn buttonin(PH_0, PA_15, PB_7, PH_1, PC_3, PC_2, PC_15, PC_14, PA_13, PA_14);
+DigitalIn psbutton(PC_12);
+Ticker heartbeat;
+
+// Heartbeat monitor
+void pulse() {
+  heartbeatLED = !heartbeatLED;
+}
+
+void heartbeat_start() {
+  heartbeat.attach(&pulse, 0.5);
+}
+
+void heartbeat_stop() {
+  heartbeat.detach();
+}
+
+
+int main() {
+    hatin.mode(PullUp);
+    buttonin.mode(PullUp);
+    psbutton.mode(PullUp);
+    int16_t throttle = 0;
+    int16_t rudder = 0;    
+    int16_t x = 0;
+    int16_t y = 0;
+
+    int16_t button = 0;    
+    int8_t hat = 0;
+    int8_t hat_reverse = 0;    
+    int8_t isUp = 0;
+    int8_t isDown = 0;
+    int8_t isLeft = 0;
+    int8_t isRight = 0;
+    int16_t ps = 0;
+    
+    heartbeat_start();
+
+    while (1) {
+
+        throttle = 0;
+        rudder = 0;
+        x = 0;
+        y = 0;
+        
+        button = 0;
+        button = ( ~buttonin) & 0x03ff;
+        ps = (psbutton)? 0 : 1;
+        button |= ( ps << 12);
+        hat_reverse = (~hatin) & 0x0F ;        
+        isUp = isDown = isLeft = isRight = 0;
+        if (hat_reverse & DOWN){
+            isDown = 1;
+        }
+        if (hat_reverse & UP){
+            isUp =1;
+            isDown = 0;
+        }
+        
+        if (hat_reverse & RIGHT){
+            isRight = 1;
+        }
+        if (hat_reverse & LEFT){
+            isLeft = 1;
+        }
+        if (isLeft && isRight){
+            isLeft = 0;
+            isRight = 0;
+        }
+        
+        if (isUp && isLeft) {
+            hat = 7;
+        } else if (isUp && isRight){
+            hat = 1;
+        } else if (isDown && isLeft){
+            hat = 5;
+        } else if (isDown && isRight){
+            hat = 3;
+        } else if (isUp){
+            hat = 0;
+        } else if (isDown){
+            hat = 4;
+        } else if (isRight){
+            hat = 2;
+        } else if (isLeft){
+            hat = 6;
+        } else {
+            hat = 9;
+        }
+        
+        joystick.update(throttle, rudder, x, y, button, hat);
+        
+        
+        wait(0.003);
+    }
+                     
+}
\ No newline at end of file