demo, add text and string

Dependencies:   NeoStrip mbed

Fork of Led_demo by ze chen

Revision:
1:0d37843b8283
Parent:
0:4c7b2475af40
Child:
2:bf21ec069402
diff -r 4c7b2475af40 -r 0d37843b8283 main.cpp
--- a/main.cpp	Fri Apr 25 14:32:24 2014 +0000
+++ b/main.cpp	Fri Apr 25 18:58:35 2014 +0000
@@ -13,87 +13,160 @@
 #define N 128
 #define PATTERNS 4
 #include <string>
+#include "helper.h"
+//callback function for incoming serial communication
+void callback();
+//necessary functions for distanceDemo and bikeDemo
+void blinkOn();
+void blinkOff();
+void patternLeft();
+void patternRight();
+void patternStop();
+void patternNone();
+void distanceDemo();
+
+//necessary functions for Animation Demo
 int hueToRGB(float h);
-void pattern0();
-void pattern1();
-void pattern2();
+void rainBow();
 void fillBlue();
 void progressBar(bool);
-int getIndex(int, int);
 void putChar(int row, int col, char ch, int color);
 void putString(std::string);
 void loopAscii(int count);
 void MarioBox();
 void drawBox(int row);void emptyBox(int row);
 void putCharV(int row, int col, char ch, int color);
-// array of function pointers to the various patterns
-//void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2};
+
+
 
+
+//hardware initilization
 NeoStrip strip(p18, N);
-DigitalIn b1(p20); // brightness up
-DigitalIn b2(p19); // brightness down
-DigitalIn b3(p21); // next pattern
+Serial device(p13,p14);
+AnalogIn ir(p20);
 
-// timer used for debugging
-Timer timer;
+
+//variable initilization
+Timer t;
 int chars[128];
 char ch = 'A';
+std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"};
+void (*patterns[])(void) = {&patternLeft, &patternRight, &patternStop, &patternNone};
+int bikeDir;
 
 int main()
-{
-    
-    std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"};
+{    
     float bright = 0.2; // 20% is plenty for indoor use
     strip.setBrightness(bright);    // set default brightness
-    int count = 0;
-    while (true)
-    {
-        timer.reset(); // use a timer to measure loop execution time for debugging purposes
-        timer.start(); // for this application, the main loop takes approximately 3ms to run
-progressBar(true);
-wait(5);
-progressBar(false);
-
-//  fillBlue();
+    //setup serial device
+    device.baud(57600);
+    device.attach(&callback);
 
-    // write character
-//  MarioBox();
-//  displayEmoji();
-//  loopAscii(count);
-//  ch+=2;
-//  putString(emoji[count%9]);
-//  count++;
-    wait(2);
-        timer.stop();
-        // print loop time if b3 is pressed     
-        wait_ms(10);
+    //wait until phone is connected
+    progressBar(false);
+    while(1){
+        if(device.readable()) break; 
     }
+    //progress Bar light up on T-shirt to demonstrate connection is established.
+    
+    progressBar(true);
+    //ready for commands, clear serael buffer
+    while(device.readable()) device.getc();
+    
+    //phone is disconnected, flash red light warning
+    while(1){
+    if(t.read_ms()>3000){
+        blinkOn();
+        wait_ms(200);
+        blinkOff();
+        wait_ms(200);
+    }
+    
+    }
+
+
+
 }
 
-// pattern0 displays a static image
-void pattern0()
-{
-    fillBlue();
-    // write character
-    int color = 0xffff00;   
-    putChar(0,0,'G',color);
-    putChar(0,6,'O',color);     
-    strip.setPixels(0, N, chars);
+
+void callback(){   
+    t.reset();
+    t.stop();
+   switch(device.getc()){
+    
+       case PhoneDemo:
+       //discard second byte
+       device.getc();      
+       //reply ack
+       device.putc('P');
+       t.start();
+       break;
+       
+       case MusicDemo:
+       device.getc();
+       rainBow();
+       break;
+       
+       case DistanceDemo:
+       device.getc();    
+       device.putc('D');  
+       break;
+       
+       case AnimationOneDemo:
+       device.getc();
+       MarioBox();
+       break;
+       
+       case AnimationTwoDemo:
+       device.getc();      
+       for(int i=0;i<8;i++){
+        putString(emoji[i]);
+       }
+       
+       
+       break;
+       
+       case AnimationThreeDemo:
+       device.getc();
+       for(int i=0;i<126;i++) loopAscii(i);
+       break;
+       
+       case BikeDemo:
+       bikeDir=device.getc();
+       if (bikeDir==LEFT)  patternLeft();
+       else if(bikeDir==RIGHT) patternRight();
+       else if(bikeDir==STOP)patternStop();
+       else patternNone();
+       break;
+       
+       default:
+       break;
+   }
+   
+
 }
 
+
 // display a shifting rainbow, all colors have maximum
 // saturation and value, with evenly spaced hue
-void pattern1()
+void rainBow()
 {
     static float dh = 360.0 / N;
     static float x = 0;
 
+    //rainbow three times
+for(int j=0;j<3;j++){
+
     for (int i = 0; i < N; i++)
         strip.setPixel(i, hueToRGB((dh * i) - x));
     
     x += 1;
     if (x > 360)
         x = 0;
+
+}
+
+
 }
 
 
@@ -137,16 +210,7 @@
     }   
 }
 
-int getIndex(int r, int c){
-    if(c < 8){
-        return (r * 8 + c);
-    }else if(c > 15){
-        return -1;      
-    }
-    else{
-        return (64 + r * 8 + (c-8));
-    }
-}
+
 
 
 void putChar(int row, int col, char ch, int color){
@@ -174,7 +238,7 @@
 
 void MarioBox(){
         int color = 0xffff00;
-        int red = 0xff0000;
+    //    int red = 0xff0000;
         
         drawBox(8);
         strip.setPixels(0, N, chars);
@@ -255,4 +319,109 @@
     strip.write();
         wait(1);
     }
-}
\ No newline at end of file
+}
+
+
+
+void patternLeft()
+{
+    for (int i = 0; i < 59; i++)
+    {
+        if (maskLeft[i] == 1)
+            strip.setPixel(i, 0, 0xff, 0);   
+        else
+            strip.setPixel(i, 0);  
+    }
+}
+ 
+void patternRight()
+{
+    for (int i = 0; i < 59; i++)
+    {
+        if (maskRight[i] == 1)
+            strip.setPixel(i, 0, 0xff, 0);   
+        else
+            strip.setPixel(i, 0);  
+    }
+}
+ 
+void patternStop()
+{
+    for (int i = 0; i < 59; i++)
+    {
+        if (maskStop[i] == 1)
+            strip.setPixel(i, 0xff, 0, 0);   
+        else
+            strip.setPixel(i, 0);  
+    }
+}
+ 
+void patternNone()
+{
+    for (int i = 0; i < 59; i++)
+    {
+        strip.setPixel(i, 0);  
+    }
+}
+
+
+
+void blinkOn()
+{
+    for (int i = 0; i < N; i++)
+    {
+        if (i % 2)
+            strip.setPixel(i, 0xff, 0, 0);   
+        else
+            strip.setPixel(i, 0, 0, 0);  
+    }
+    wait_ms(50);
+   strip.write();
+}
+ 
+void blinkOff()
+{
+    for (int i = 0; i < N; i++)
+    {
+        strip.setPixel(i, 0, 0, 0);   
+    }
+    wait_ms(60);
+   strip.write();
+}
+
+
+
+void distanceDemo(){
+    float temp[10];
+    for (int i = 0; i < 10; i++)
+            {
+                float val;
+                val = 21/ir;
+                temp[i] = val;
+                float sum = 0;
+                for (int j = 0; j < 10; j++)
+                {
+                    sum += temp[j];
+                }
+                float avg = sum/10;            
+                if (avg < 70)
+                {
+                    blinkOn();
+                    wait_ms(100);
+                    blinkOff();
+                    wait_ms(100);            
+                    blinkOn();
+                    wait_ms(100);
+                    blinkOff();
+                    wait_ms(100);              
+                    blinkOn();
+                    wait_ms(100);
+                    blinkOff();
+                    wait_ms(100); 
+                
+                }
+            }
+       
+}
+
+