Mynput: Game controller for Color Quest.

Dependencies:   Adafruit_NeoPixel MMA8451Q PinDetect_KL25Z USBDevice mbed

Fork of idd_hw3 by IDD HW3

Revision:
4:0527cd963c14
Parent:
3:888c255f87a7
Child:
5:f547509e5583
--- a/main.cpp	Thu Sep 17 23:06:39 2015 +0000
+++ b/main.cpp	Sat Sep 19 01:11:12 2015 +0000
@@ -11,14 +11,16 @@
 Timer timer;
 int timer_begin;
 
-#define LED_COUNT 16
+#define WRIST_LED_COUNT 16
+#define CHEST_LED_COUNT 24
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
 #define LEAN_LEFT_THRESH -0.4
 #define LEAN_RIGHT_THRESH 0.4
-#define LEAN_BACK_THRESH -0.5
+#define LEAN_BACK_THRESH 0.5
 #define JUMP_THRESH 0.4
-#define FLEX_THRESH 0.4
+#define LASER_THRESH 0.4
+#define SHIELD_THRESH 0.41
 const char LEFT =  'A';
 const char RIGHT = 'D';
 const char LASER = 'J';
@@ -33,7 +35,7 @@
 PololuLedStrip wristLedStrip(D6);
 PololuLedStrip chestLedStrip(D7);
 
-rgb_color colors[LED_COUNT];
+//rgb_color colors[WRIST_LED_COUNT];
 
 USBKeyboard keyboard;
 AnalogIn rightArm(A0);
@@ -67,21 +69,23 @@
   }
   return pixelColor;
 }
-
+/*
 void cleanRing() {
     for(uint32_t i = 0; i < LED_COUNT; i++) {
         colors[i] = (rgb_color){ 0, 0, 0 };
     }
     wristLedStrip.write(colors, LED_COUNT);
 }
+*/
 
-void pulse(char color, PololuLedStrip led) {
+void pulse(char color, PololuLedStrip led, int led_count) {
+    rgb_color colors[led_count];
     // Update the colors array.
     for( int j = 0; j < 255; j++) {
-        for( uint32_t i = 0; i < LED_COUNT; i++ ) {
+        for( uint32_t i = 0; i < led_count; i++ ) {
             colors[i] = getColor(color, j);
         }
-        led.write(colors, LED_COUNT);
+        led.write(colors, led_count);
     }
     //wait_ms(100);
 }
@@ -108,40 +112,40 @@
             y = acc.getAccY();
             z = acc.getAccZ();
             
-            if ( y > LEAN_RIGHT_THRESH ) {
+            if ( x > LEAN_RIGHT_THRESH ) {
                 pc.printf("Lean right\n");
                 keyboard.putc(RIGHT);
-            } else if (y < LEAN_LEFT_THRESH) {
+            } else if (x < LEAN_LEFT_THRESH) {
                 pc.printf("Lean left\n"); 
                 keyboard.putc(LEFT);
             }
             
-            if( x < JUMP_THRESH && timer.read_ms() - timer_begin > 300 ) {                  
+            if( y < 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) {
+            if(wrist.read() < LASER_THRESH) {
                 pc.printf("Shoot\n");
                 keyboard.putc(LASER);
-                pulse('r', wristLedStrip);
-                pulse('r', chestLedStrip);
+                pulse('r', wristLedStrip, WRIST_LED_COUNT);
+                pulse('r', chestLedStrip, CHEST_LED_COUNT);
             } 
-            else if(rightArm.read() < FLEX_THRESH) {
+            else if(rightArm.read() < SHIELD_THRESH) {
                 pc.printf("Shield\n");   
                 keyboard.putc(SHIELD); 
-                pulse('g', wristLedStrip);
-                pulse('g', chestLedStrip);
+                pulse('g', wristLedStrip, WRIST_LED_COUNT);
+                pulse('g', chestLedStrip, CHEST_LED_COUNT);
             } 
-            else if ( z < LEAN_BACK_THRESH) {
+            else if ( z > LEAN_BACK_THRESH) {
                 pc.printf("Leaning back\n");
                 keyboard.putc(SLOW);
-                pulse('b', wristLedStrip);
-                pulse('b', chestLedStrip);
+                pulse('b', wristLedStrip, WRIST_LED_COUNT);
+                pulse('b', chestLedStrip, CHEST_LED_COUNT);
             } else{
-                pulse('w', wristLedStrip);
-                pulse('w', chestLedStrip);
+                pulse('w', wristLedStrip, WRIST_LED_COUNT);
+                pulse('w', chestLedStrip, CHEST_LED_COUNT);
             }
         }
     }