#include #include #include #include #include #define PIXEL_PIN 11 // pin connect to NEOPIXELS #define PIXEL_COUNT 64 #define SD_CS 8 #define BUFFPIXEL 20 #define PIXEL_RUN 15 #define TOP_BUTTON 0 #define RIGHT_BUTTON 1 #define BOTTOM_BUTTON 2 #define LEFT_BUTTON 3 #define R 200 #define G_RED 0 #define G_ORANGE 50 #define G_YELLOW 90 #define B 0 File imageFile; char* fileName; char* filesList[] = {"balls.txt", "flower.txt", "jfish.txt", "rainbow.txt", "tulips.txt"}; uint16_t index = 0; uint8_t lineY[] = {0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120}; uint8_t lineX = 0; Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); const uint32_t RED = strip.Color(R, G_RED, B); const uint32_t ORANGE = strip.Color(R, G_ORANGE, B); const uint32_t YELLOW = strip.Color(R, G_YELLOW, B); const uint32_t BLACK = strip.Color(B, B, B); const uint16_t BEGINNING_ROW_0 = 0; const uint16_t BEGINNING_ROW_1 = 16; const uint16_t BEGINNING_ROW_2 = 32; const uint16_t BEGINNING_ROW_3 = 48; // keeps pixels aligned at bottom of square (so not in zig-zag pattern) const uint8_t PIXEL_AT[] = {0, 1, 2, 3, 4 , 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48}; int oldButtonState[] = {HIGH, HIGH, HIGH, HIGH}; boolean buttonTracker[] = {false, false, false, false}; //Keeps track of buttons on/off int next; int previous; int oldNext = 0; int oldPrevious = 0; int fName = 1; void setup() { Serial.begin(9600); EsploraTFT.begin(); EsploraTFT.background(255, 255, 255); strip.begin(); strip.show(); // while (!Serial) { // ; // wait for serial port to connect. Needed for Leonardo only // } EsploraTFT.stroke(0, 0, 0); EsploraTFT.text("Initializing SD card...", lineX, 0); if (!SD.begin(SD_CS)) { EsploraTFT.stroke(255, 0, 0); EsploraTFT.text("initialization failed!", lineX, 8); return; } EsploraTFT.stroke(0, 0, 0); EsploraTFT.text("initialization done.", lineX, 8); } void loop() { //START Keeping track of button and switch int newButtonState[4] = { Esplora.readButton(SWITCH_UP), Esplora.readButton(SWITCH_RIGHT), Esplora.readButton(SWITCH_DOWN), Esplora.readButton(SWITCH_LEFT), }; int x = Esplora.readJoystickX(); if(oldButtonState[TOP_BUTTON] != newButtonState[TOP_BUTTON]) { if(newButtonState[TOP_BUTTON] == LOW) { trackButtons(TOP_BUTTON); strip.show(); } } if(oldButtonState[RIGHT_BUTTON] != newButtonState[RIGHT_BUTTON]) { if(newButtonState[RIGHT_BUTTON] == LOW) { trackButtons(RIGHT_BUTTON); //Unquote for future implementation strip.show(); } } if(oldButtonState[BOTTOM_BUTTON] != newButtonState[BOTTOM_BUTTON]) { if(newButtonState[BOTTOM_BUTTON] == LOW) { //trackButtons(BOTTOM_BUTTON); //Unquote for future implementation } } if(oldButtonState[LEFT_BUTTON] != newButtonState[LEFT_BUTTON]) { if(newButtonState[LEFT_BUTTON] == LOW) { //trackButtons(LEFT_BUTTON); //Unquote for future implementation } } oldButtonState[TOP_BUTTON] = newButtonState[TOP_BUTTON]; oldButtonState[RIGHT_BUTTON] = newButtonState[RIGHT_BUTTON]; oldButtonState[BOTTOM_BUTTON] = newButtonState[BOTTOM_BUTTON]; oldButtonState[LEFT_BUTTON] = newButtonState[LEFT_BUTTON]; //END Keeping track of button and switch //START of program tracker if(buttonTracker[TOP_BUTTON]) { trackImageProcess(x); } else if(buttonTracker[RIGHT_BUTTON]) { processFireEffect(); } else if(buttonTracker[BOTTOM_BUTTON]) { } else if(buttonTracker[LEFT_BUTTON]) { } //END of program tracker } void processFireEffect() { for(int i = 0; i <= 15; i++) { strip.setPixelColor(PIXEL_AT[i + BEGINNING_ROW_0], randomColor()); delay(wait()); strip.setPixelColor(PIXEL_AT[i + BEGINNING_ROW_1], randomColor()); delay(wait()); strip.setPixelColor(PIXEL_AT[i + BEGINNING_ROW_2], randomColor()); delay(wait()); strip.setPixelColor(PIXEL_AT[i + BEGINNING_ROW_3], randomColor()); delay(wait()); strip.show(); } } void trackImageProcess(int x) { next = getNext(x); previous = getPrevious(x); if(oldNext != next) { if(next > 0) { fName++; if(fName > (sizeof(filesList) / 2) - 1) { fName = 0; } fileName = filesList[fName]; processImage(); } } if(oldPrevious != previous) { if(previous > 0) { fName--; if(fName < 0) { fName = (sizeof(filesList) / 2) - 1; } fileName = filesList[fName]; processImage(); } } oldNext = next; oldPrevious = previous; } /** * Keeps track of which progam to run accourding to button pressed */ void trackButtons(int button) { for(int i = 0; i < sizeof(oldButtonState); i++) { if(i == button) { buttonTracker[i] = true; } else { buttonTracker[i] = false; } } } /** * Runs the image to Pixel program. */ void processImage() { imageFile = SD.open(fileName); if(imageFile) { lineEraser(2); EsploraTFT.stroke(0, 0, 0); EsploraTFT.text(fileName, lineX, lineY[2]); readFileToPixels(); closeFile(); } else { lineEraser(2); EsploraTFT.stroke(255, 0, 0); EsploraTFT.text("error opening ", lineX, lineY[2]); EsploraTFT.text(fileName, lineX, lineY[3]); } } void readFileToPixels() { while(imageFile.available()) { uint8_t r, g, b; unsigned char nextChar = imageFile.read(); switch(nextChar) { case 'r': r = imageFile.parseInt(); break; case 'g': g = imageFile.parseInt(); break; case 'b': b = imageFile.parseInt(); break; case ',': setColor(r, g, b); break; } } } void setColor(uint8_t r, uint8_t g, uint8_t b) { if(index >= 64) { index = 0; } strip.setPixelColor(PIXEL_AT[index], r, g, b); strip.show(); index++; } /** * Close file */ void closeFile() { imageFile.close(); } /** * assign next value 1 for on and 0 for off * param x the sticks value * return next value */ int getNext(int x) { x = map(x, -512, 512, 100, 0); if(x > 90) { x = 1; } else { x = 0; } return x; } /** * assign previous value 1 for on and 0 for off * param x the sticks value * return previous value */ int getPrevious(int x) { x = map(x, -512, 512, 100, 0); if(x < 10) { x = 1; } else { x = 0; } return x; } // Erases printed lines on the tft screen // Line number a 1 through 16 // param lineNum the line to erase void lineEraser(int lineNum) { EsploraTFT.fill(255, 255, 255); EsploraTFT.stroke(255 ,255 , 255); switch(lineNum) { case 0: EsploraTFT.rect(0, 0, 160, 8); //break; case 1: EsploraTFT.rect(0, 8, 160, 8); //break; case 2: EsploraTFT.rect(0, 16, 160, 8); //break; case 3: EsploraTFT.rect(0, 24, 160, 8); //break; case 4: EsploraTFT.rect(0, 32, 160, 8); //break; case 5: EsploraTFT.rect(0, 40, 160, 8); //break; case 6: EsploraTFT.rect(0, 48, 160, 8); //break; case 7: EsploraTFT.rect(0, 56, 160, 8); //break; case 8: EsploraTFT.rect(0, 64, 160, 8); //break; case 9: EsploraTFT.rect(0, 72, 160, 8); //break; case 10: EsploraTFT.rect(0, 80, 160, 8); //break; case 11: EsploraTFT.rect(0, 88, 160, 8); //break; case 12: EsploraTFT.rect(0, 96, 160, 8); //break; case 13: EsploraTFT.rect(0, 104, 160, 8); //break; case 14: EsploraTFT.rect(0, 112, 160, 8); //break; case 15: EsploraTFT.rect(0, 120, 160, 8); //break; } } uint32_t randomColor() { int randColor = random(101); if(randColor < 60) { return RED; } else if(randColor >= 60 && randColor < 90) { return ORANGE; } else if(randColor >= 90) { return YELLOW; } } /** * Receive a uint32_t value and return single color value. */ uint8_t splitColor ( uint32_t c, char value ) { switch ( value ) { case 'r': return (uint8_t)(c >> 16); case 'g': return (uint8_t)(c >> 8); case 'b': return (uint8_t)(c >> 0); default: return 1; } } /** * assigns a random wait time for fading an LED from one color to the next */ uint16_t wait() { return random(25, 75); }