working demo, with iPhone support

Dependencies:   NeoPixels NeoStrip mbed

Fork of NeoPixels by Allen Wild

Revision:
1:3b59b44884cd
Parent:
0:f38492690f0e
Child:
2:b322ea7290a5
--- a/main.cpp	Wed Mar 12 18:41:42 2014 +0000
+++ b/main.cpp	Fri Apr 25 14:23:32 2014 +0000
@@ -10,17 +10,25 @@
 #include "mbed.h"
 #include "NeoStrip.h"
 #include "gt.h"
-
-#define N 64
-#define PATTERNS 3
-
+#include "text.h"
+#define N 128
+#define PATTERNS 4
+#include <string>
 int hueToRGB(float h);
 void pattern0();
 void pattern1();
 void pattern2();
-
+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};
+//void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2};
 
 NeoStrip strip(p18, N);
 DigitalIn b1(p20); // brightness up
@@ -29,59 +37,36 @@
 
 // timer used for debugging
 Timer timer;
+int chars[128];
+char ch = 'A';
 
 int main()
 {
-	b1.mode(PullDown);
-	b2.mode(PullDown);
-	b3.mode(PullDown);
 	
-	int pattern = 0;
+	std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"};
 	float bright = 0.2;	// 20% is plenty for indoor use
-	bool b3o = b3;		// old copy of button 3 to poll for changes
-
 	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);
 
-		// button 1 increases brightness
-		if (b1 && bright < 1)
-		{
-			bright += 0.01;
-			if (bright > 1)
-				bright = 1;
-			strip.setBrightness(bright);
-		}
+//	fillBlue();
 
-		// button 2 decreases brightness
-		if (b2 && bright > 0)
-		{
-			bright -= 0.01;
-			if (bright < 0)
-				bright = 0;
-			strip.setBrightness(bright);
-		}
-		
-		// button 3 changes the pattern, only do stuff when its state has changed
-		if (b3 != b3o)
-		{
-			if (b3 && ++pattern == PATTERNS)
-				pattern = 0;
-			b3o = b3;
-		}
-		
-		// run the pattern update function which sets the strip's pixels
-		patterns[pattern]();
-		strip.write();
-
+	// write character
+//	MarioBox();
+//	displayEmoji();
+//	loopAscii(count);
+//	ch+=2;
+//	putString(emoji[count%9]);
+//	count++;
+	wait(2);
 		timer.stop();
-		// print loop time if b3 is pressed
-		if (b3)
-			printf("Loop Time: %dus\n", timer.read_us());
-		
+		// print loop time if b3 is pressed		
 		wait_ms(10);
 	}
 }
@@ -89,7 +74,12 @@
 // pattern0 displays a static image
 void pattern0()
 {
-	strip.setPixels(0, N, gt_img);
+	fillBlue();
+	// write character
+	int color = 0xffff00;	
+	putChar(0,0,'G',color);
+	putChar(0,6,'O',color);		
+	strip.setPixels(0, N, chars);
 }
 
 // display a shifting rainbow, all colors have maximum
@@ -107,43 +97,7 @@
 		x = 0;
 }
 
-// display a shifting gradient between red and blue
-void pattern2()
-{
-	// offset for each pixel to allow the pattern to move
-	static float x = 0;
 
-	float r, b, y;
-
-	for (int i = 0; i < N; i++)
-	{
-		// y is a scaled position between 0 (red) and 1.0 (blue)
-		y = 1.0 * i / (N - 1) + x;
-		if (y > 1)
-			y -= 1;
-
-		// if on the left half, red is decreasing and blue is increasng
-		if (y < 0.5)
-		{
-			b = 2 * y;
-			r = 1 - b;
-		}
-
-		// else red is increasing and blue is decreasing
-		else
-		{
-			r = 2 * (y - 0.5);
-			b = 1 - r;
-		}
-
-		// scale to integers and set the pixel
-		strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200));
-	}
-
-	x += 0.003;
-	if (x > 1)
-		x = 0;
-}
 
 // Converts HSV to RGB with the given hue, assuming
 // maximum saturation and value
@@ -178,3 +132,128 @@
 	return (R << 16) | (G << 8) | B;
 }
 
+void fillBlue(){
+	for(int i = 0; i < N; i++){
+		chars[i] = 0x122446;
+	}	
+}
+
+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;	
+				}
+			}
+	}
+}
+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;	
+				}
+			}
+	}
+}
+
+void MarioBox(){
+		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(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);		
+	}	
+	strip.setPixels(0, N, chars);
+	strip.write();
+
+}
+
+void loopAscii(int count){
+	int color = 0xffff00;
+	putChar(0,0,ch+count,color);
+	putChar(0,6,ch+count+1,color);
+	strip.setPixels(0, N, chars);
+	strip.write();
+}
+
+void drawBox(int row){
+	for(int i = 0; i < 64; ++i)
+		chars[row*8 + i] = 0x000000;
+	putCharV(row,0,'?',0xffff00);	
+}
+void emptyBox(int row){
+	for(int i = 0; i < 64; ++i)
+		chars[row*8 + i] = 0x000000;
+}
+void progressBar(bool up){
+	int index;
+	int init;
+	int increment;
+	int color;
+	if(up == true){
+		 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(1);
+	}
+}
\ No newline at end of file