Mynput: Game controller for Color Quest.

Dependencies:   Adafruit_NeoPixel MMA8451Q PinDetect_KL25Z USBDevice mbed

Fork of idd_hw3 by IDD HW3

Revision:
1:1f45953fccf9
Parent:
0:d307eb0be182
Child:
2:bab0105de714
--- a/main.cpp	Tue Sep 15 01:15:13 2015 +0000
+++ b/main.cpp	Wed Sep 16 02:32:55 2015 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
 #include "MMA8451Q.h"
 #include "Timer.h"
+#include "USBKeyboard.h"
+#include "PinDetect.h"
 
 // define I2C Pins and address for KL25Z. Taken from default sample code.
 PinName const SDA = PTE25;
@@ -9,45 +11,81 @@
 int timer_begin;
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
-#define leanLeftThresh -0.4
-#define leanRightThresh 0.4
-#define jumpThresh 0.4
+#define LEAN_LEFT_THRESH -0.4
+#define LEAN_RIGHT_THRESH 0.4
+#define LEAN_BACK_THRESH -0.5
+#define JUMP_THRESH 0.4
+#define FLEX_THRESH 0.4
+const char LEFT =  'A';
+const char RIGHT = 'D';
+const char LASER = 'J';
+const char SHIELD = 'K';
+const char SLOW = 'L';
+const char ENTER = '\n';
+const char SPACE = ' ';
 
 //serial connection to PC via USB
 Serial pc(USBTX, USBRX);
-bool timerStart = false;
 
+USBKeyboard keyboard;
+AnalogIn leftArm(A0);
+AnalogIn rightArm(A1);
+AnalogIn wrist(A2);
+PinDetect on_off(D15);
+bool on = false;
+
+
+void on_off_pressed() {
+    on = !on;
+}
 int main(void)
 {
     //configure on-board I2C accelerometer on KL25Z
     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 
     //map read acceleration to PWM output on red status LED
     PwmOut rled(LED_RED);
-    float x,y,z, x_new, y_new, z_new;
+    float x,y,z;
+    
+    on_off.attach_asserted(&on_off_pressed);
+    on_off.setAssertValue(0); //pins are PullUp, so there activelow buttons.
+    on_off.setSampleFrequency(); // Defaults to 20ms.
+    
     timer.start();
     timer_begin = timer.read_ms();
     while (true) {
-        x = acc.getAccX();
-        y = acc.getAccY();
-        z = acc.getAccZ();
-        
-        wait(.2); //wait 0.2 ms
-        
-        x_new = acc.getAccX();
-        y_new = acc.getAccY();
-        z_new = acc.getAccZ();
-        
-        if ( (y > leanRightThresh) && (y_new > leanRightThresh)) {
-            pc.printf("Lean right\n");
-        } else if ((y < leanLeftThresh) && (y_new < leanLeftThresh)) {
-            pc.printf("Lean left\n"); 
+        if (on) {
+            x = acc.getAccX();
+            y = acc.getAccY();
+            z = acc.getAccZ();
+            
+            if ( y > LEAN_RIGHT_THRESH ) {
+                pc.printf("Lean right\n");
+                keyboard.putc(RIGHT);
+            } else if (y < LEAN_LEFT_THRESH) {
+                pc.printf("Lean left\n"); 
+                keyboard.putc(LEFT);
+            }
+            
+            if ( z < LEAN_BACK_THRESH) {
+                pc.printf("Leaning back\n");
+                keyboard.putc(SLOW);
+            }
+            if( x < JUMP_THRESH && timer.read_ms() - timer_begin > 300 ) {                  
+                timer_begin = timer.read_ms();
+                pc.printf("Jump\n");
+                keyboard.putc(ENTER);
+                keyboard.putc(SPACE);
+            }
+            
+            if(wrist.read() < FLEX_THRESH) {
+                pc.printf("Shoot\n");    
+                keyboard.putc(LASER);
+            }
+            
+            if(leftArm.read() < FLEX_THRESH && rightArm.read()) {
+                pc.printf("Shield\n");   
+                keyboard.putc(SHIELD); 
+            }
         }
-        if( x < jumpThresh && timer.read_ms() - timer_begin > 300 ) {
-            timerStart = true;
-            pc.printf("Jump\n");                    
-            timer_begin = timer.read_ms();
-        }
-
-        
     }
 }
\ No newline at end of file