void loop() { // Some example procedures showing how to display to the pixels colorWipe(Color(255, 0, 0), 50); colorWipe(Color(0, 255, 0), 50); colorWipe(Color(0, 0, 255), 50); rainbow(20); rainbowCycle(20); }
//display the given time (if different from time given with prior call) void GoldieClock::displayClock(time_t utc) { static time_t utcLast; if ( utc != utcLast ) { utcLast = utc; time_t local = (*tz).toLocal(utc, &tcr); uint8_t h = hour(local); uint8_t m = minute(local); uint8_t s = second(local); uint8_t hourHand = ( h >= 12 ? h - 12 : h ) * 5; hourHand = hourHand + m / 12; //adjust hour hand between hours //one rainbow on the quarter hour, two on the half, four on the hour if ( s == 0 && _showRainbows ) { if ( m == 15 || m == 45 ) rainbowCycle(2, 1); else if ( m == 30 ) rainbowCycle(2, 2); else if ( m == 0 ) rainbowCycle(2, 4); } clear(); //hour hand (3 pixels wide) setPixelColor( LAST_PIXEL - hourHand, HOUR_HAND ); setPixelColor( LAST_PIXEL - (hourHand + 1U > LAST_PIXEL ? FIRST_PIXEL : hourHand + 1U), HOUR_HAND_DIM ); setPixelColor( LAST_PIXEL - (hourHand - 1U > LAST_PIXEL ? LAST_PIXEL : hourHand - 1U), HOUR_HAND_DIM ); //minute hand & second hand -- additive colors setPixelColor( LAST_PIXEL - m, getPixelColor(LAST_PIXEL - m) + MINUTE_HAND ); setPixelColor( LAST_PIXEL - s, getPixelColor(LAST_PIXEL - s) + SECOND_HAND ); //hour markers (do not overlay hands) for (uint16_t i = 0; i <= LAST_PIXEL; i += 5) if ( getPixelColor(LAST_PIXEL - i) == 0 ) setPixelColor(LAST_PIXEL - i, HOUR_MARKER); show(); } }
//run the clock state machine. call this function frequently from the main loop. void GoldieClock::run(time_t utc) { static clockStates_t CLOCK_STATE; btnSet.read(); btnIncr.read(); switch ( CLOCK_STATE) { case RUN_CLOCK: if ( btnSet.pressedFor(SET_LONGPRESS) ) { CLOCK_STATE = SET_CLOCK; } else if ( btnIncr.wasReleased() ) //toggle the quarter-hour rainbows { if ( _showRainbows = !_showRainbows ) { rainbowCycle(2, 1); //show a rainbow if rainbow mode is on } else { clear(); //else just blank the display for a second show(); delay(1000); } } else { displayClock(utc); } break; case SET_CLOCK: if ( setClock() ) //returns true when setting complete { CLOCK_STATE = RUN_CLOCK; } break; } }
void quarterHour(uint8_t hour, uint8_t minute, uint16_t wait) { uint8_t no_rows; uint8_t y; uint8_t x; int16_t j; // light number of rows depending on the hour quarter (floored to be safe) no_rows = floor(minute / 15) * height; // one hour indicator is actually 0 but we want full display no_rows = (no_rows == 0) ? (4 * height) : no_rows; solidColor(0,0,0); // turn them all off show(); if ((hour == 12 || hour == 0 ) && minute == 0) { rainbowCycle(25); // special edition hour for midday } else { // fade columns in and out for (j = 0; j <= 255; j += 5) { for (y = 0; y < no_rows; y++) { for (x = 0; x < PIXEL_ROW; x++) { setPixel(pixelMap[x][y],1,Color(0,0,j)); } } show(); usleep(10*1000); } usleep(wait*1000); // show for 10s for (j = 255; j >= 0; j -= 5) { for (y = 0; y < no_rows; y++) { for (x = 0; x < PIXEL_ROW; x++) { setPixel(pixelMap[x][y],1,Color(0,0,j)); } } show(); usleep(10*1000); } } }
int main(int argc, char **argv) { // Check "Single Instance" int pid_file = open("/var/run/ws2812RPi.pid", O_CREAT | O_RDWR, 0666); int rc = flock(pid_file, LOCK_EX | LOCK_NB); if(rc) { if(EWOULDBLOCK == errno) { // another instance is running printf("Instance already running\n"); exit(EXIT_FAILURE); } } srand(time(NULL)); // seed the random number generator; should be done only once // Catch all signals possible - it's vital we kill the DMA engine on process exit! int i; for (i = 0; i < 64; i++) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = terminate; sigaction(i, &sa, NULL); } // Don't buffer console output setvbuf(stdout, NULL, _IONBF, 0); // How many LEDs? numLEDs = NUM_PIXELS; // How bright? (Recommend 0.2 for direct viewing @ 3.3V) setBrightness(DEFAULT_BRIGHTNESS); // Init PWM generator and clear LED buffer initHardware(); clearLEDBuffer(); // time_t t = time(NULL); // struct tm tm = *localtime(&t); // printf("Start: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); while(true) { // goto Twinkle_Test; rainbowCycle_wipe(5); // // tm = *localtime(&t); // // printf("rainbowCycle_wipe() Done: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); for (i = 0; i < loops; i++) { rainbowCycle_r(5); } // tm = *localtime(&t); // printf("rainbowCycle_f() Done: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); for (i = 0; i < loops; i++) { rainbowCycle(5); } // time_t t = time(NULL); // struct tm tm = *localtime(&t); // printf("rainbowCycle() Done: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); // RainFall(Color(0, 255, 0),5); int lastcolour; int randnum; for (i = 0; i < 10; i++) { RainLoop: randnum = rand() % 6; if (randnum == lastcolour) { goto RainLoop; } lastcolour = randnum; switch(randnum) { case 0: RainFall(Color(255, 0, 0),5,3); break; case 1: RainFall(Color(255, 128, 0),5,3); break; case 2: RainFall(Color(255, 255, 0),5,3); break; case 3: RainFall(Color(128, 255, 0),5,3); break; case 4: RainFall(Color(0, 255, 0),5,3); break; case 5: RainFall(Color(0, 0, 255),5,3); break; case 6: RainFall(Color(255, 0, 255),5,3); break; } } WipeColour0: randnum = rand() % 6; if (randnum == lastcolour) { goto WipeColour0; } lastcolour = randnum; switch(randnum) { case 0: colorWipe(Color(255, 0, 0),50); break; case 1: colorWipe(Color(255, 128, 0),50); break; case 2: colorWipe(Color(255, 255, 0),50); break; case 3: colorWipe(Color(128, 255, 0),50); break; case 4: colorWipe(Color(0, 255, 0),50); break; case 5: colorWipe(Color(0, 0, 255),50); break; case 6: colorWipe(Color(255, 0, 255),50); break; } sleep(5); for (i = 0; i < 5; i++) { WipeColour1: randnum = rand() % 6; if (randnum == lastcolour) { goto WipeColour1; } lastcolour = randnum; switch(randnum) { case 0: colorWipe_r(Color(255, 0, 0),50); break; case 1: colorWipe_r(Color(255, 128, 0),50); break; case 2: colorWipe_r(Color(255, 255, 0),50); break; case 3: colorWipe_r(Color(128, 255, 0),50); break; case 4: colorWipe_r(Color(0, 255, 0),50); break; case 5: colorWipe_r(Color(0, 0, 255),50); break; case 6: colorWipe_r(Color(255, 0, 255),50); break; } sleep(5); WipeColour2: randnum = rand() % 6; if (randnum == lastcolour) { goto WipeColour2; } lastcolour = randnum; switch(randnum) { case 0: colorWipe(Color(255, 0, 0),50); break; case 1: colorWipe(Color(255, 128, 0),50); break; case 2: colorWipe(Color(255, 255, 0),50); break; case 3: colorWipe(Color(128, 255, 0),50); break; case 4: colorWipe(Color(0, 255, 0),50); break; case 5: colorWipe(Color(0, 0, 255),50); break; case 6: colorWipe(Color(255, 0, 255),50); break; } sleep(5); } // Twinkle_Test: // RainFall(Color(0, 0, 0),5,3); // for (i=0; i < 600; i++) { // TwinkleColour2: // randnum = rand() % 3; // if (randnum == lastcolour) { // goto TwinkleColour2; // } // lastcolour = randnum; // switch(randnum) { // case 0: // Twinkle_T(Color(0, 0, 255)); // break; // case 1: // Twinkle_T(Color(255, 128, 0)); // break; // case 2: // Twinkle_T(Color(0, 255, 0)); // break; // case 3: // Twinkle_T(Color(0, 0, 0)); // break; // } // } Twinkle_Test: colorWipe(Color(0, 0, 0),50); usleep(500000); // for (i=0; i < 600; i++) { for (i=0; i < 600; i++) { Twinkle(); } Twinkle_Fade(); sleep(3); // colorWipe(Color(0, 0, 0),50); } // Exit cleanly, freeing memory and stopping the DMA & PWM engines // We trap all signals (including Ctrl+C), so even if you don't get here, it terminates correctly terminate(0); return 0; }