demo, add text and string

Dependencies:   NeoStrip mbed

Fork of Led_demo by ze chen

Files at this revision

API Documentation at this revision

Comitter:
zchen311
Date:
Mon Apr 28 02:17:00 2014 +0000
Parent:
0:4c7b2475af40
Commit message:
latest

Changed in this revision

NeoStrip.lib Show annotated file Show diff for this revision Revisions of this file
helper.h 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
diff -r 4c7b2475af40 -r eecf2e4bf4f1 NeoStrip.lib
--- a/NeoStrip.lib	Fri Apr 25 14:32:24 2014 +0000
+++ b/NeoStrip.lib	Mon Apr 28 02:17:00 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/ece4180final/code/NeoStrip/#2afb4da99560
+http://mbed.org/users/aswild/code/NeoStrip/#9f237b11f0a8
diff -r 4c7b2475af40 -r eecf2e4bf4f1 helper.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/helper.h	Mon Apr 28 02:17:00 2014 +0000
@@ -0,0 +1,56 @@
+
+                        
+//#define Connected 0x00
+#define PhoneDemo 0x01
+#define MusicDemo 0x02
+#define DistanceDemo 0x03
+#define AnimationOneDemo 0x04
+#define AnimationTwoDemo 0x05
+#define AnimationThreeDemo 0x06
+#define BikeDemo 0x07
+#define LEFT 0x08
+#define STOP 0x09
+#define RIGHT 0x0a
+#define LessPixelDensity 59
+#define MorePixelDensity 128
+
+
+
+int maskNone128[128] = {0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0};
+                        
+int maskLeft128[128] = {0, 0, 0, 1, 1, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 1, 1, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 1, 1, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        1, 1, 1, 1, 1, 1, 1, 1,   0, 0, 0, 0, 0, 0, 0, 0,
+                        1, 1, 1, 1, 1, 1, 1, 1,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 1, 1, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 1, 1, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 1, 1, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0};
+                        
+int maskRight128[128] ={0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 1, 1, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 1, 1, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 1, 1, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   1, 1, 1, 1, 1, 1, 1, 1,
+                        0, 0, 0, 0, 0, 0, 0, 0,   1, 1, 1, 1, 1, 1, 1, 1,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 1, 1, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 1, 1, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 1, 1, 0, 0, 0};
+                        
+                        
+                        
+int maskStop128[128] = {0, 0, 0, 0, 0, 0, 0, 1,   1, 0, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 1, 1,   1, 1, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 1, 1, 1, 1,   1, 1, 1, 1, 0, 0, 0, 0,
+                        0, 0, 0, 0, 1, 1, 1, 1,   1, 1, 1, 1, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 1, 1,   1, 1, 0, 0, 0, 0, 0, 0,
+                        0, 0, 0, 0, 0, 0, 0, 1,   1, 0, 0, 0, 0, 0, 0, 0};
+                    
diff -r 4c7b2475af40 -r eecf2e4bf4f1 main.cpp
--- a/main.cpp	Fri Apr 25 14:32:24 2014 +0000
+++ b/main.cpp	Mon Apr 28 02:17:00 2014 +0000
@@ -1,135 +1,158 @@
-/*
- * Adafruit NeoPixel 8x8 matrix example
- *
- * This program displays a couple simple patterns on an 8x8 NeoPixel matrix.
- *
- * 3 buttons are used for DigitalIns, 2 control the brightness up and down,
- * and the third switches patterns
- */
-
 #include "mbed.h"
 #include "NeoStrip.h"
 #include "text.h"
 #define N 128
 #define PATTERNS 4
 #include <string>
-int hueToRGB(float h);
-void pattern0();
-void pattern1();
-void pattern2();
+#include "helper.h"
+
+
+NeoStrip strip(p18, N);
+
+//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();
+void rainBow();
+void emojiDemo();
+//necessary functions for Animation Demo
 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 loopAscii();
 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};
+int getIndex(int r, int c);
+
+
 
-NeoStrip strip(p18, N);
-DigitalIn b1(p20); // brightness up
-DigitalIn b2(p19); // brightness down
-DigitalIn b3(p21); // next pattern
+//hardware initilization
+Serial device(p13,p14);
+AnalogIn ir(p20);
+Serial pc(USBTX,USBRX);
 
-// timer used for debugging
-Timer timer;
+//variable initilization
+Timer t;
 int chars[128];
+std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"};
+void (*patterns[])(void) = {&patternLeft, &patternRight, &patternStop, &patternNone};
+int bikeDir;
 char ch = 'A';
 
 int main()
-{
-    
-    std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"};
+{    
+//    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);
+
+    //setup serial device
+    device.baud(57600);    
+    device.attach(&callback);
 
-//  fillBlue();
+    //wait until phone is connected
+    while(1){
+        if(device.readable()) break; 
+    }
+    pc.printf("Device connected\n");
 
-    // 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);
+    //ready for commands, clear serael buffer
+    while(device.readable()) device.getc();
+    pc.printf("Device ready for commands;\n");
+    //phone is disconnected, flash red light warning
+    while(1){
+        if(t.read_ms()>3000){
+            pc.printf("time passed i %d\n",t.read_ms());
+            blinkOn();
+        //progressBar(true);
+            wait_ms(200);
+            blinkOff();
+      //  wait_ms(200);
+        }
+        wait(5);
     }
+
+
+
 }
 
-// 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(){   
+    pc.printf("new commands arrive \n");
+    t.reset();
+    switch(device.getc()){
+
+     case PhoneDemo:
+     pc.printf("Lost Phone Demo\n");
+     while(device.readable()) device.getc();
+       //reply ack
+     device.putc('P');
+     t.start();
+     break;
+
+     case MusicDemo:
+     pc.printf("Music Demo\n");
+     rainBow();
+     //progressBar(true);
+     //progressBar(false);
+     break;
+
+     case DistanceDemo:
+     pc.printf("Distance Demo\n");
+     device.putc('D');
+     distanceDemo();  
+     break;
+
+     case AnimationOneDemo:
+     MarioBox();
+     break;
+
+     case AnimationTwoDemo:
+     pc.printf("Emoji Demo\n");
+     emojiDemo();
+     break;
+
+    case AnimationThreeDemo:
+    pc.printf("ascii demo\n");
+    loopAscii();
+     break;
+
+ case LEFT:
+ pc.printf("left arrow\n");
+ patternLeft();
+ break;
+
+ case RIGHT:
+ pc.printf("right arrow\n");
+
+ patternRight();
+ break;       
+
+ case STOP:
+ pc.printf("stop\n");
+
+ patternStop();
+ break;
+
+ default:
+ break;
 }
 
-// display a shifting rainbow, all colors have maximum
-// saturation and value, with evenly spaced hue
-void pattern1()
-{
-    static float dh = 360.0 / N;
-    static float x = 0;
 
-    for (int i = 0; i < N; i++)
-        strip.setPixel(i, hueToRGB((dh * i) - x));
-    
-    x += 1;
-    if (x > 360)
-        x = 0;
 }
 
 
 
-// Converts HSV to RGB with the given hue, assuming
-// maximum saturation and value
-int hueToRGB(float h)
-{
-    // lots of floating point magic from the internet and scratching my head
-    float r, g, b;
-    if (h > 360)
-        h -= 360;
-    if (h < 0)
-        h += 360;
-    int i = (int)(h / 60.0);
-    float f = (h / 60.0) - i;
-    float q = 1 - f;
-    
-    switch (i % 6)
-    {
-        case 0: r = 1; g = f; b = 0; break;
-        case 1: r = q; g = 1; b = 0; break;
-        case 2: r = 0; g = 1; b = f; break;
-        case 3: r = 0; g = q; b = 1; break;
-        case 4: r = f; g = 0; b = 1; break;
-        case 5: r = 1; g = 0; b = q; break;
-        default: r = 0; g = 0; b = 0; break;
-    }
-    
-    // scale to integers and return the packed value
-    uint8_t R = (uint8_t)(r * 255);
-    uint8_t G = (uint8_t)(g * 255);
-    uint8_t B = (uint8_t)(b * 255);
+
 
-    return (R << 16) | (G << 8) | B;
-}
+
 
 void fillBlue(){
     for(int i = 0; i < N; i++){
@@ -137,87 +160,85 @@
     }   
 }
 
-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){
-        for(int r = 0; r < 8; r++){
-            for(int c = 0; c < 6; c++){
-                if(fontdata_6x8[ch * 48 + r * 6 +c]){
-                    int idx = getIndex(row+r, col+c);
-                    if(idx != -1)
-                        chars[idx] = color; 
-                }
+    for(int r = 0; r < 8; r++){
+        for(int c = 0; c < 6; c++){
+            if(fontdata_6x8[ch * 48 + r * 6 +c]){
+                int idx = getIndex(row+r, col+c);
+                if(idx != -1)
+                    chars[idx] = color; 
             }
+        }
     }
 }
 void putCharV(int row, int col, char ch, int color){
-        for(int r = 0; r < 8; r++){
-            for(int c = 0; c < 6; c++){
-                if(fontdata_6x8[ch * 48 + r * 6 +c]){
-                    int idx = (row + r) *8 +( col +c);
-                    if(idx != -1)
-                        chars[idx] = color; 
-                }
+    for(int r = 0; r < 8; r++){
+        for(int c = 0; c < 6; c++){
+            if(fontdata_6x8[ch * 48 + r * 6 +c]){
+                int idx = (row + r) *8 +( col +c);
+                if(idx != -1)
+                    chars[idx] = color; 
             }
+        }
     }
 }
 
 void MarioBox(){
-        int color = 0xffff00;
-        int red = 0xff0000;
-        
-        drawBox(8);
+    int color = 0xffff00;
+    //    int red = 0xff0000;
+
+    drawBox(8);
+    strip.setPixels(0, N, chars);
+    strip.write();      
+    wait(5);
+
+    for(int i = 1; i < 3; i++){
+        fillBlue();
+        drawBox(8-i);
         strip.setPixels(0, N, chars);
         strip.write();      
-        wait(5);
+        wait(0.3);
+    }       
+    for(int i = 0; i < 3; i++){
+        fillBlue();
+        drawBox(6 + i);
+        strip.setPixels(0, N, chars);
+        strip.write();      
+        wait(0.3);
+    }
+    fillBlue();
+    emptyBox(8);
+    putChar(0,0,'0',color);
+    strip.setPixels(0, N, chars);
+    strip.write();
+    wait(5);    
+    strip.clear();
+    strip.write();
 
-        for(int i = 1; i < 3; i++){
-            fillBlue();
-            drawBox(8-i);
-            strip.setPixels(0, N, chars);
-            strip.write();      
-            wait(0.3);
-        }       
-        for(int i = 0; i < 3; i++){
-            fillBlue();
-            drawBox(6 + i);
-            strip.setPixels(0, N, chars);
-            strip.write();      
-            wait(0.3);
-        }
-        fillBlue();
-        emptyBox(8);
-        putChar(0,0,'0',color);
-        strip.setPixels(0, N, chars);
-        strip.write();
-        wait(30);       
 }
 void putString(std::string str){
     int color = 0xffff00;
     for(int i = 0; i < str.length(); ++i){
-        putChar(0,5*i,str[i], color);       
+        putChar(0,5*i,str[i], color); 
+        strip.setPixels(0, N, chars);
     }   
-    strip.setPixels(0, N, chars);
     strip.write();
-
+    wait_ms(100);
 }
 
-void loopAscii(int count){
+void loopAscii(){
     int color = 0xffff00;
-    putChar(0,0,ch+count,color);
-    putChar(0,6,ch+count+1,color);
-    strip.setPixels(0, N, chars);
-    strip.write();
+    char ch = 'A';
+    int count = 50;
+    
+    for(int i = 0; i < count; i+=2) {
+        putChar(0,0,ch+i,color);
+        putChar(0,7,ch+i+1,color);
+        strip.setPixels(0, N, chars);
+        strip.write();    
+        wait_ms(200);
+    }       
+
 }
 
 void drawBox(int row){
@@ -235,24 +256,190 @@
     int increment;
     int color;
     if(up == true){
-         init = 0;
-         increment = 1;
-         color = 0x122446;
-    }else {
-         init = 15;
-         increment = -1;
-        fillBlue();
-         color = 0x000000;
+       init = 0;
+       increment = 1;
+       color = 0x122446;
+   }else {
+       init = 15;
+       increment = -1;
+       fillBlue();
+       color = 0x000000;
+   }
+
+   for(int x = 0; x < 16 ; x++){
+
+    for (int j = 0; j < 8; j++){
+        index = getIndex(j,init + increment * x);
+        chars[index] = color;
     }
-    
+    strip.setPixels(0, N, chars);
+    strip.write();
+    wait_ms(200);
+}
+}
+
+
+void rainBow(){
+    int index;
+    unsigned int black = 0x000000;
+    unsigned int color[7] = {0x5b00ff,0xff0d19, 0xf0ff03, 0x17ff29, 0x1fffee,0x0d65ff, 0xfb12ff};
+    int init = 0;
+    int increment = 1;
+
     for(int x = 0; x < 16 ; x++){
-        
+        for (int j = 0; j < 8; j++){
+            index = getIndex(j,init + increment * x);
+            chars[index] = color[index%7];            
+        }
+        strip.setPixels(0, N, chars);
+        strip.write();
+        wait_ms(200);
+    }
+
+    for(int x = 15; x >= 0 ; x--){
         for (int j = 0; j < 8; j++){
             index = getIndex(j,init + increment * x);
-            chars[index] = color;
+            chars[index] = black;            
+        }
+
+        strip.setPixels(0, N, chars);
+        strip.write();
+        wait_ms(200);
+    }
+
+}   
+
+
+
+void patternLeft()
+{
+    for(int j=0;j<3;j++){
+        for (int i = 0; i < N; i++)
+        {
+            if (maskLeft128[i] == 1)
+                chars[i]=0x00FF00;
+            else
+                chars[i]=0x000000;
+        }
+    
+        strip.setPixels(0, N, chars);
+        strip.write();
+        wait_ms(200);
+        strip.clear();
+        strip.write();
+        wait_ms(200);
+
+    }
+}
+
+void patternRight()
+{
+    for(int j=0;j<3;j++){
+        for (int i = 0; i < N; i++)
+        {
+            if (maskRight128[i] == 1)
+                chars[i]=0x00FF00;
+            else
+                chars[i]=0x000000;
         }
+        strip.setPixels(0, N, chars);
+        strip.write();
+        wait_ms(200);
+        strip.clear();
+        strip.write();
+        wait_ms(200);
+
+    }
+
+}
+
+void patternStop()
+{
+    for(int j=0;j<3;j++){
+        for (int i = 0; i < N; i++)
+        {
+            if (maskStop128[i] == 1)
+                chars[i]=0xFF0000;
+            else
+                chars[i]=0x000000;
+        }
+        strip.setPixels(0, N, chars);
+        strip.write();
+        wait_ms(200);
+        strip.clear();
+        strip.write();
+        wait_ms(200);
+
+    }
+
+}
+
+void patternNone()
+{
+    strip.clear();
+}
+
+
+
+void blinkOn()
+{
+    strip.clear();
+    for (int i = 0; i < N; i++)
+    {
+        chars[i]=0xFF0000;
+    }
+
     strip.setPixels(0, N, chars);
     strip.write();
-        wait(1);
+    wait_ms(200);
+}
+
+void blinkOff()
+{
+    strip.clear();
+    for (int i = 0; i < N; i++)
+    {
+        chars[i]=0x000000;
+    }
+    strip.setPixels(0, N, chars);
+    strip.write();
+    wait_ms(200);
+}
+
+
+
+void distanceDemo(){
+    float val = 21/ir;  
+    pc.printf("dist avg=%f\n",val);       
+    
+    if(val<70) {
+        blinkOn();
+        wait_ms(200);
+        blinkOff();
+    }
+
+}
+
+void emojiDemo(){
+     for(int i=0;i<8;i++){
+        putString(emoji[i]);
+        wait_ms(200);
+        strip.write();
+        wait_ms(200);
+        strip.clear();
+        strip.write();
+        wait_ms(200);
+    }    
+}
+
+
+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));
     }
 }
\ No newline at end of file