IDD HW3

Dependencies:   MMA8451Q mbed PinDetect_KL25Z USBDevice Adafruit_NeoPixel

Files at this revision

API Documentation at this revision

Comitter:
bkim54
Date:
Thu Sep 17 23:06:39 2015 +0000
Parent:
2:bab0105de714
Child:
4:0527cd963c14
Commit message:
put in LEDs

Changed in this revision

Adafruit_NeoPixel.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_NeoPixel.lib	Thu Sep 17 23:06:39 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/tomasero/code/Adafruit_NeoPixel/#21d6d7016965
--- a/main.cpp	Thu Sep 17 22:06:32 2015 +0000
+++ b/main.cpp	Thu Sep 17 23:06:39 2015 +0000
@@ -3,12 +3,15 @@
 #include "Timer.h"
 #include "USBKeyboard.h"
 #include "PinDetect.h"
+#include "PololuLedStrip.h"
 
 // define I2C Pins and address for KL25Z. Taken from default sample code.
 PinName const SDA = PTE25;
 PinName const SCL = PTE24;
 Timer timer;
 int timer_begin;
+
+#define LED_COUNT 16
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
 #define LEAN_LEFT_THRESH -0.4
@@ -27,6 +30,11 @@
 //serial connection to PC via USB
 Serial pc(USBTX, USBRX);
 
+PololuLedStrip wristLedStrip(D6);
+PololuLedStrip chestLedStrip(D7);
+
+rgb_color colors[LED_COUNT];
+
 USBKeyboard keyboard;
 AnalogIn rightArm(A0);
 AnalogIn wrist(A2);
@@ -37,6 +45,47 @@
 void on_off_pressed() {
     on = !on;
 }
+
+rgb_color getColor( char color, int intensity ) {
+  rgb_color pixelColor;
+  switch (color) {
+    case 'r':
+      pixelColor = (rgb_color){ intensity, 0, 0 };
+      break;
+    case 'g':
+      pixelColor = (rgb_color){ 0, intensity, 0 };
+      break;
+    case 'b':
+      pixelColor = (rgb_color){ 0, 0, intensity };
+      break;
+    case 'w':
+      pixelColor = (rgb_color){ intensity, intensity, intensity};
+      break;  
+    default: 
+        pixelColor = (rgb_color){ 0, 0 , intensity };
+        break;
+  }
+  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) {
+    // Update the colors array.
+    for( int j = 0; j < 255; j++) {
+        for( uint32_t i = 0; i < LED_COUNT; i++ ) {
+            colors[i] = getColor(color, j);
+        }
+        led.write(colors, LED_COUNT);
+    }
+    //wait_ms(100);
+}
+
 int main(void)
 {
     //configure on-board I2C accelerometer on KL25Z
@@ -52,6 +101,7 @@
     timer.start();
     timer_begin = timer.read_ms();
     pc.printf("helllo\n");
+    //pulse();
     while (true) {
         if (on) {
             x = acc.getAccX();
@@ -66,10 +116,6 @@
                 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");
@@ -79,11 +125,23 @@
             if(wrist.read() < FLEX_THRESH) {
                 pc.printf("Shoot\n");
                 keyboard.putc(LASER);
-            }
-            
-            if(rightArm.read() < FLEX_THRESH) {
+                pulse('r', wristLedStrip);
+                pulse('r', chestLedStrip);
+            } 
+            else if(rightArm.read() < FLEX_THRESH) {
                 pc.printf("Shield\n");   
                 keyboard.putc(SHIELD); 
+                pulse('g', wristLedStrip);
+                pulse('g', chestLedStrip);
+            } 
+            else if ( z < LEAN_BACK_THRESH) {
+                pc.printf("Leaning back\n");
+                keyboard.putc(SLOW);
+                pulse('b', wristLedStrip);
+                pulse('b', chestLedStrip);
+            } else{
+                pulse('w', wristLedStrip);
+                pulse('w', chestLedStrip);
             }
         }
     }