An example program using the NeoMatrix library to control an Adafruit NeoPixel 8x8 NeoMatrix (http://www.adafruit.com/products/1487)

Dependencies:   NeoMatrix mbed

Fork of NeoMatrix_Demo by Taylor Powell

Files at this revision

API Documentation at this revision

Comitter:
tpowell33
Date:
Tue Mar 17 06:17:13 2015 +0000
Parent:
0:f38492690f0e
Commit message:
First Publish

Changed in this revision

NeoMatrix.lib Show annotated file Show diff for this revision Revisions of this file
NeoStrip.lib Show diff for this revision Revisions of this file
gt.h 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/NeoMatrix.lib	Tue Mar 17 06:17:13 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/tpowell33/code/NeoMatrix/#9a2779957e46
--- a/NeoStrip.lib	Wed Mar 12 18:41:42 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/aswild/code/NeoStrip/#9f237b11f0a8
--- a/gt.h	Wed Mar 12 18:41:42 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-/**** gt.h ****/
-
-const int gt_img[] = {
-	0x122446, 0xffff00, 0xffff00, 0xffff00, 0xffff00, 0x122446, 0x122446, 0x122446, 
-	0xffff00, 0xffff00, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 
-	0xffff00, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 
-	0xffff00, 0x122446, 0x122446, 0xffff00, 0xffff00, 0xffff00, 0xffff00, 0xffff00, 
-	0xffff00, 0xffff00, 0x122446, 0x122446, 0xffff00, 0x122446, 0xffff00, 0x122446, 
-	0x122446, 0xffff00, 0xffff00, 0xffff00, 0xffff00, 0x122446, 0xffff00, 0x122446, 
-	0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0xffff00, 0x122446, 
-	0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0x122446, 0xffff00, 0x122446};
-
--- a/main.cpp	Wed Mar 12 18:41:42 2014 +0000
+++ b/main.cpp	Tue Mar 17 06:17:13 2015 +0000
@@ -1,180 +1,135 @@
 /*
  * Adafruit NeoPixel 8x8 matrix example
  *
- * This program displays a couple simple patterns on an 8x8 NeoPixel matrix.
+ * This program demonstrates the functions from the NeoArray library by creating sample patterns on a NeoPixel NeoMatrix 8x8 array
  *
- * 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 "gt.h"
+#include "NeoMatrix.h"
 
-#define N 64
-#define PATTERNS 3
-
-int hueToRGB(float h);
-void pattern0();
-void pattern1();
-void pattern2();
+#define Color(r, g, b)  ((r&0xFF)<<16 | (g&0xFF) << 8 | (b&0xFF))   // pack colors
 
-// array of function pointers to the various patterns
-void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2};
+#define Red     (Color(255,0,0))
+#define Green   (Color(0,255,0))
+#define Blue    (Color(0,0,255))
 
-NeoStrip strip(p18, N);
-DigitalIn b1(p20); // brightness up
-DigitalIn b2(p19); // brightness down
-DigitalIn b3(p21); // next pattern
-
-// timer used for debugging
-Timer timer;
+NeoArr array(p18, 1);   // Initialize the array
 
 int main()
 {
-	b1.mode(PullDown);
-	b2.mode(PullDown);
-	b3.mode(PullDown);
-	
-	int pattern = 0;
-	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
-	
-	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
-
-		// button 1 increases brightness
-		if (b1 && bright < 1)
-		{
-			bright += 0.01;
-			if (bright > 1)
-				bright = 1;
-			strip.setBrightness(bright);
-		}
+    
+    float bright = 0.2; // 20% is bright enough for most indoor use and should run without need for external power
 
-		// 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();
-
-		timer.stop();
-		// print loop time if b3 is pressed
-		if (b3)
-			printf("Loop Time: %dus\n", timer.read_us());
-		
-		wait_ms(10);
-	}
-}
-
-// pattern0 displays a static image
-void pattern0()
-{
-	strip.setPixels(0, N, gt_img);
+    array.setBrightness(bright);    // set brightness to default 0.2
+    array.clear();
+    
+    while (true)
+    {
+        // Horrizontal scroll x y z
+        for(char c = 'x'; c <= 'z'; c++)
+            for(int i=7;i>=-6;i--){
+                array.clear();
+                array.drawChar(0,i,0,c, 255,255,255);
+                array.write();
+                wait_ms(100);
+                }
+                
+        // Fill screen with gradient of colors
+        for(int n = 0; n<=255; n++){
+            array.fillScreen(0,n,0,255-n);
+            array.write();
+            wait_ms(5);
+            }
+        for(int n = 0; n<=255; n++){
+            array.fillScreen(0,255-n,n,0);
+            array.write();
+            wait_ms(5);
+            }
+        for(int n = 0; n<=255; n++){
+            array.fillScreen(0,0,255-n,n);
+            array.write();
+            wait_ms(5);
+            }
+            
+        // Vertical scroll A B C
+        for(char c = 'A'; c <= 'C'; c++)
+            for(int i=8;i>=-8;i--){
+                array.clear();
+                array.drawChar(0,1,i,c, 255,255,255);
+                array.write();
+                wait_ms(100);
+                }
+            
+        // Alternating Checkerboard
+        for(int n=0; n<7; n++){
+            for(int i=-8; i<8; i+=2){
+                    array.drawLine(0, i+n%2,0,i+7+n%2,7,0,255,0);
+                    }
+                array.write();
+                array.clear();
+                wait_ms(500);
+                }
+            
+        // Red and green pinwheel
+        for(int i=0; i<8; i++){
+            array.clear();
+            array.drawLine(0,i,0,7-i,7,Red);
+            array.drawLine(0,7,i,0,7-i,Green);
+            array.write();
+            wait_ms(100);
+            }
+        for(int i=1; i<=6; i++){
+            array.clear();
+            array.drawLine(0,7,i,0,7-i,Red);
+            array.drawLine(0,i,0,7-i,7,Green);
+            array.write();
+            wait_ms(100);
+            }
+            
+        // Shrink and grow blue rectangle
+        for(int i=0; i<4; i++){
+            array.clear();
+            array.drawFilledRect(0,i,i,7-i,7-i,Blue);
+            array.write();
+            wait_ms(250);
+            }
+        for(int i=0; i<4; i++){
+            array.clear();
+            array.drawRect(0,3-i,3-i,4+i,4+i,Blue);
+            array.write();
+            wait_ms(250);
+            }
+            
+        // Move purple triangle
+        for(int i=7; i>=0; i--){
+            array.clear();
+            array.drawTriangle(0,i,i,0,7,7,0, 255,0,255);
+            array.write();
+            wait_ms(125);
+            }
+            
+        // Rotate purple triangle
+        for(int i=0; i<8; i++){
+            array.clear();
+            array.drawFilledTriangle(0,i,0,0,7-i,7,i, 255,0,255);
+            array.write();
+            wait_ms(250);
+            }
+     
+        // Diangonal Rainbow Scroll
+        for(int i=-1;i<=8;i++){
+            for(int n=-18; n<=6; n+=6){
+                array.drawLine(0,n+i,0,n+i+7,7, 255,0,0);
+                array.drawLine(0,n+i+1,0,n+i+8,7, 255,255,0);
+                array.drawLine(0,n+i+2,0,n+i+9,7, 0,255,0);
+                array.drawLine(0,n+i+3,0,n+i+10,7, 0,255,255);
+                array.drawLine(0,n+i+4,0,n+i+11,7, 0,0,255);
+                array.drawLine(0,n+i+5,0,n+i+12,7, 255,0,255);
+                }
+            array.write();
+            wait_ms(200);
+           }
+    }
 }
 
-// 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;
-}
-
-// 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
-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;
-}
-