Moves a glow up and down a strip of WS2812 LEDs
Diff: main.cpp
- Revision:
- 0:35d68d1652e1
- Child:
- 1:054df9ecd479
diff -r 000000000000 -r 35d68d1652e1 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Nov 05 16:50:38 2014 +0000 @@ -0,0 +1,152 @@ +#include "mbed.h" +#include "wsDrive.h" + +// time period between each movement +#define updatePeriodMS 25 + +// number of LEDs in chain +#define chainLen 144 + +// length of the fade off behind the bright spot. +// 1/8th of the chain length seems to look good (18 LEDs for a 144 chain) +#define glowTail (chainLen/8) + +// background brightness left after the tail has passed. +// final brightness = peak/backgroundRatio +// set to be the same as the glow tail length for a smooth transition +#define backgroundRatio glowTail + +// set the pulldown and then create the driver +DigitalIn dummy(P0_21,PullDown); +wsDrive ledDriver(P0_21,P0_22,P1_15); + +// mbuino stnadard definitions +DigitalIn progMode(P0_3,PullDown); // fix the power wasted if we ever sleep. +BusOut LEDs(LED1, LED2, LED3, LED4, LED5, LED6, LED7); // control the LEDs + +Timer updateRateTimer; + +// pixel storage buffer +pixelInfo pixelData[chainLen]; + +void blankBuffer(pixelInfo *Ptr) +{ + memset( (void *)Ptr, 0, chainLen*sizeof(pixelInfo) ); +} + +void setPixel (pixelInfo *pixel, pixelInfo *colour, float level) +{ + + pixel->R = (unsigned char) (colour->R * level); + pixel->G = (unsigned char) (colour->G * level); + pixel->B = (unsigned char) (colour->B * level); +} + + +void setTrail (pixelInfo *colour, int peakPoint, bool increasing, int len) +{ + int pixelToUpdate = peakPoint; + for (int pixel = 0; pixel <= len; pixel++) { + if (pixel == len) { + setPixel((pixelData+pixelToUpdate), colour, 1.0/backgroundRatio); + break; + } + setPixel((pixelData+pixelToUpdate), colour, 1.0 - (float)pixel/(float)len); + increasing ? pixelToUpdate-- : pixelToUpdate++; + if (pixelToUpdate == chainLen) { + increasing = false; + pixelToUpdate = peakPoint - 1; + pixel = pixel*2; + } + if (pixelToUpdate == -1) { + increasing = true; + pixelToUpdate = peakPoint + 1; + pixel = pixel*2; + } + } +} + + +void setColour(pixelInfo *colour) +{ + static int cycleNumber = 0; + + const int maxCycle = 40; + if (cycleNumber > maxCycle) + cycleNumber = 0; + + switch (cycleNumber) { + case maxCycle: + cycleNumber = 0; + case 0: + colour->R = 0x80; + colour->G = 0x00; + colour->B = 0x00; + break; + case 10: + colour->R = 0x00; + colour->G = 0x70; + colour->B = 0x00; + break; + case 20: + colour->R = 0x00; + colour->G = 0x00; + colour->B = 0x80; + break; + case 30: + colour->R = 0x50; + colour->G = 0x40; + colour->B = 0x50; + break; + default: + break; + } + cycleNumber++; +} + + +int main () +{ + LEDs = 0; + // intialise variables + int peakPoint = 0; + bool increasing = true; + + pixelInfo colour; + setColour(&colour); + + blankBuffer(pixelData); + + // give the LED driver the buffer to use. + ledDriver.setData(pixelData, chainLen); + + LEDs = 1; + + setTrail (&colour, peakPoint, increasing, glowTail); // set the LED data + + updateRateTimer.start(); + while (true) { + ledDriver.sendData(); // send the LED data + LEDs = LEDs+1; + + increasing ? peakPoint++ : peakPoint--; + if (peakPoint == chainLen) { + increasing = false; + peakPoint = chainLen-2; + } + if (peakPoint == -1) { + setColour(&colour); + increasing = true; + peakPoint = 1; + } + + // update to the next trail ready for the next update. + setTrail (&colour, peakPoint, increasing, glowTail); // set the LED data + + // wait for the next update time. + while (updateRateTimer.read_ms() < updatePeriodMS) { + } + updateRateTimer.reset(); + } + +} \ No newline at end of file