Adafruit_NeoMatrix icon indicating copy to clipboard operation
Adafruit_NeoMatrix copied to clipboard

Want to Scroll Shapes in matrix

Open LedArts opened this issue 7 years ago • 1 comments

// This #include statement was automatically added by the Spark IDE. #include "neopixel/neopixel.h"

// This #include statement was automatically added by the Spark IDE. #include "neomatrix/neomatrix.h"

// This #include statement was automatically added by the Spark IDE. #include "Adafruit_GFX/Adafruit_GFX.h"

// Color definitions #define BLUE 0x001F

// IMPORTANT: Set pixel COUNT, PIN and TYPE #define PIXEL_PIN D6 #define PIXEL_COUNT 238 #define PIXEL_TYPE WS2812B

// I define the pixels as both a strip and a matrix. Mainly because // I like some of the strip effects from the demo program. I can // reference either the strip or the matrix in the rest of my code.

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); // Define as a strip to do some cool rainbow effects.

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(17, 14, 6, // Define a NeoMatrix 17 pixels wide by 14 NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + // pixel high on pin 6. Spark core hooked to NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG); // the BOTTOM RIGHT and wired by COLUMNS in // a ZIGZAG pattern. See: http://goo.gl/ci2VhY

// Easier to define all colors needed here. uint32_t PEACH = matrix.Color(221,171,127);
uint32_t ROSY = matrix.Color(255,176,193); uint32_t LIGHTBLUE = matrix.Color(94,185,247); uint32_t RED = matrix.Color(255,0,0); uint32_t GREEN = matrix.Color(0,255,0); uint32_t GRAY = matrix.Color(100,100,100); uint32_t BROWN = matrix.Color(117,76,41); uint32_t PURPLE = matrix.Color(152,5,229); uint32_t DARKGREEN = matrix.Color(12,158,17); uint32_t PINK = matrix.Color(250,101,148); uint32_t ORANGE = matrix.Color(241,90,41); uint32_t YELLOW = matrix.Color(255,242,0); uint32_t BLACK = matrix.Color(0,0,0); uint32_t WHITE = matrix.Color(255,255,255);

//Variables needed for when we're doing live drawing. bool isDrawing = false; bool startDrawing = false;

void setup() { //Register our Spark function here Spark.function("showPicture", showPicture); // Register for the showPicture function from iPhone App Spark.function("drawThePixel", drawThePixel); // Register for the drawPixel function from iPad App matrix.begin();

// This is just a sequence of RED, GREEN, then OFF(BLACK) so I know the screen reset.
matrix.fillScreen(RED);
matrix.show();                
delay(2000);
matrix.fillScreen(GREEN);
matrix.show();
delay(2000);
matrix.fillScreen(BLACK);
matrix.show();

}

void loop() {

if (startDrawing) {               // Only way I could consistently get it to clear the screen
    matrix.fillScreen(BLACK);     // was to track a temporaray startDrawing boolean to set the 
    matrix.show();                // display to black the first time we hit the main loop again.
    startDrawing = false;         // Otherwise I would always end up with part of the image that
}                                 // was displaying before drawing began on the screen.

//If we are not drawing, run the normal routine.
if(!isDrawing) {                
    normalRoutine();
}

}

// This is my normal routine that just runs through all the different // images we're created. void normalRoutine() { if (startDrawing) { return; } // Check before each item whether we have started drawing. scrollText(" Merry Christmas!",GREEN,RED); if (startDrawing) { return; } delay(1000); if (startDrawing) { return; } drawReindeer(); if (startDrawing) { return; } delay(5000); if (startDrawing) { return; } drawHarperPresent(); if (startDrawing) { return; } delay(5000); if (startDrawing) { return; } drawSanta1(); if (startDrawing) { return; } delay(5000);

}

//Rainbow function from Adafruit Strip Demo. void rainbow(uint8_t wait) { uint16_t i, j;

for(j=0; j<256; j++) { for(i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, Wheel((i+j) & 255)); } strip.show(); delay(wait); } }

// Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { if(WheelPos < 85) { return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } }

// Function run when we receive a "showPicture" command from the // iPhone app. Only a few cases are handled because I haven't expanded // it to handle all the different pictures. // Input: the string Command we received. // Return: An Integer that's passed back through the Spark API to the // iPhone app. 0 for false or "Not Displayed" 1 for true or "Displayed" int showPicture(String command) {

if (isDrawing) {
    return 0;
}

if (command == "reindeer"){ drawReindeer(); delay(10000); return 1; } else if (command == "santa1") { drawSanta1(); delay(10000); return 1; } else if (command == "snowman"){ drawSnowman(); delay(10000); return 1; }

return 0;

}

// Function run when we receive a "drawPixel" command from the iPad // app. // Input: String Command. iPad sends either "startDrawing", "stopDrawing", // "clearDisplay", or a pixel like "x,y,r,g,b" // example: "0,0,255,255,255" Pixel 0,0 RGB (255,255,255) or White.

int drawThePixel(String command) { if (command == "startDrawing") { if (isDrawing) { return 2; } isDrawing = true; startDrawing = true; matrix.fillScreen(BLACK); matrix.show(); return 1; } else if (command == "stopDrawing") { isDrawing = false; return 1; } else if (command == "clearDisplay") { matrix.fillScreen(BLACK); matrix.show(); return 1; } else { // Yes I know there has to be a more efficient way of parsing this string. // My "C" programing skills are lacking in the area of string manipulation. int commaIndex = command.indexOf(','); int secondCommaIndex = command.indexOf(',', commaIndex+1); int thirdCommaIndex = command.indexOf(',', secondCommaIndex+1); int fourthCommaIndex = command.indexOf(',', thirdCommaIndex+1);

    String xCoord = command.substring(0,commaIndex);
    String yCoord = command.substring(commaIndex+1, secondCommaIndex);
    String rValue = command.substring(secondCommaIndex+1, thirdCommaIndex);
    String gValue = command.substring(thirdCommaIndex+1, fourthCommaIndex);
    String bValue = command.substring(fourthCommaIndex+1);
    
    matrix.drawPixel(xCoord.toInt(),yCoord.toInt(),matrix.Color(rValue.toInt(),gValue.toInt(),bValue.toInt()));

    // Show three times because once causes weird things...
    // This tripped me up for a long time. If I only did matrix.show()
    // once, the screen would randomly display the wrong pixels and the
    // wrong colors. Then every third pixel command it would draw right.
    // It was very frustrating. I finally realized if I'd send the command
    // three times, it would work as expected with no noticeable issues
    // on the screen...
    matrix.show();
    matrix.show();
    matrix.show();

    return 1;
}
return 0;

}

// Scroll text function. Basis for this function from the following instructable // by turnturtle. http://goo.gl/l7tvX9 void scrollText(String text, uint32_t backgroundColor, uint32_t textColor) {

matrix.setTextColor(textColor);
matrix.setTextSize(1);
matrix.setTextWrap(false);

//String text = "    Merry Christmas!"; // sample text

const int width = 3; // width of the marquee display (in characters)

// Loop once through the string

for (int offset = 0; offset < text.length(); offset++)

{

    // Construct the string to display for this iteration

    String t = "";

    for (int i = 0; i < width; i++)

    t += text.charAt((offset + i) % text.length());

    // Print the string for this iteration
   // matrix.setTextColor(randomColor());
    matrix.setCursor(0, 3); // display will be halfway down screen

    matrix.print(t);
    
    matrix.show();

    // Short delay so the text doesn't move too fast

    delay(350);
    
    matrix.fillScreen(backgroundColor);
    matrix.show();

}

}

//function to generate a random color. ;) uint32_t randomColor() { uint32_t colorToReturn = matrix.Color(random(0,256),random(0,256),random(0,256)); return colorToReturn; }

// This is untested. I just found the setBrightness() function. // It should fade a color to screen... void fadeToColor(uint32_t color, uint8_t duration) { // Valid values for setBrightness are 0-16 for (int i = 0; i <= 16; i++) { matrix.fillScreen(color); matrix.setBrightness(i); matrix.show(); delay(duration / 16); }

matrix.setBrightness(16); //Reset brightness for next drawing?

}

// Below here are all the functions to draw the individual pictures.

void drawSanta1() { matrix.fillScreen(BLACK); matrix.drawFastHLine(10,1,5,RED); matrix.drawFastHLine(9,2,7,RED); matrix.drawFastHLine(8,3,9,RED); matrix.drawFastHLine(6,4,3,RED); matrix.drawFastHLine(9,4,8,WHITE); matrix.drawPixel(5,5,WHITE); matrix.drawPixel(9,5,WHITE); matrix.drawPixel(10,5,PEACH); matrix.drawPixel(11,5,LIGHTBLUE); matrix.drawPixel(12,5,PEACH); matrix.drawPixel(13,5,LIGHTBLUE); matrix.drawPixel(14,5,PEACH); matrix.drawPixel(15,5,WHITE); matrix.drawPixel(9,6,WHITE); matrix.drawPixel(10,6,ROSY); matrix.drawFastHLine(11,6,3,PEACH); matrix.drawPixel(14,6,ROSY); matrix.drawPixel(15,6,WHITE); matrix.drawPixel(9,7,WHITE); matrix.drawPixel(10,7,PEACH); matrix.drawFastHLine(11,7,3,WHITE); matrix.drawPixel(14,7,PEACH); matrix.drawPixel(15,7,WHITE); matrix.drawFastHLine(9,8,3,WHITE); matrix.drawPixel(12,8,PEACH); matrix.drawFastHLine(13,8,3,WHITE); matrix.drawFastHLine(9,9,7,WHITE); matrix.drawFastHLine(10,10,5,WHITE); matrix.drawFastHLine(11,11,3,WHITE);

matrix.show();

}

void drawReindeer() { matrix.fillScreen(WHITE); matrix.drawFastHLine(5,0,3,GRAY); matrix.drawFastHLine(10,0,3,GRAY); matrix.drawPixel(7,1,GRAY); matrix.drawPixel(10,1,GRAY); matrix.drawFastHLine(3,2,3,BROWN); matrix.drawPixel(7,2,GRAY); matrix.drawPixel(10,2,GRAY); matrix.drawFastHLine(12,2,3,BROWN); matrix.drawFastHLine(2,3,4,BROWN); matrix.drawFastHLine(7,3,4,BROWN); matrix.drawFastHLine(12,3,4,BROWN); matrix.drawFastHLine(6,4,6,BROWN); matrix.drawFastHLine(6,5,6,BROWN); //Draw whole row for eyes matrix.drawFastHLine(6,6,6,BROWN); //Draw Eyes matrix.drawPixel(7,6,BLACK); matrix.drawPixel(10,6,BLACK); matrix.drawFastHLine(6,7,6,BROWN); matrix.drawFastHLine(6,8,6,BROWN); matrix.drawFastHLine(7,9,4,BROWN); matrix.drawFastHLine(7,10,4,BROWN); matrix.drawFastHLine(7,11,4,BROWN); matrix.drawPixel(7,12,BROWN); matrix.drawPixel(10,12,BROWN); matrix.drawFastHLine(8,12,2,RED); matrix.drawFastHLine(8,13,2,BROWN);

matrix.show();

}

void drawHarperTree() { matrix.fillScreen(LIGHTBLUE); matrix.drawPixel(8,1,YELLOW); matrix.drawPixel(8,2,GREEN); matrix.drawFastHLine(7,3,3,GREEN); matrix.drawPixel(6,4,GREEN); matrix.drawPixel(7,4,BLUE); matrix.drawPixel(8,4,GREEN); matrix.drawPixel(9,4,PURPLE); matrix.drawFastHLine(5,5,5,GREEN); matrix.drawFastHLine(5,6,5,GREEN); matrix.drawFastHLine(4,7,8,GREEN); matrix.drawFastHLine(4,8,8,GREEN); matrix.drawPixel(6,8,PINK); matrix.drawFastHLine(3,9,9,GREEN); matrix.drawFastHLine(3,10,9,GREEN); matrix.drawFastHLine(5,11,4,BROWN); matrix.drawFastHLine(5,12,4,BROWN); matrix.drawFastHLine(0,13,17,DARKGREEN);

matrix.show();

}

hello sir, i want to scroll this shape or any shape horizotal or vertical in matrix plz give me a function to scroll this. plz help me

LedArts avatar Jan 11 '18 20:01 LedArts