Fork of AndyA/cylon, with support for STM32F4 and LPC8xx

Dependencies:   mbed wsDrive

Fork of cylon by Andy A

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "wsDrive.h"
00003 
00004 DigitalOut led1(LED1);
00005 
00006 // time period between each movement
00007 #define updatePeriodMS 10
00008 
00009 // number of LEDs in chain
00010 #define chainLen 300
00011 
00012 // set the pulldown and then create the driver
00013 //wsDrive ledDriver(P1_22, P0_22, P1_15);
00014 wsDrive ledDriver(PC_12, NC, NC);
00015 
00016 Timer updateRateTimer;
00017 
00018 // pixel storage buffer
00019 pixelInfo16 pixelData[chainLen];
00020 
00021 const uint8_t trailCount = 3;
00022 
00023 // info for each trail
00024 struct trailInfo {
00025     float start;  // location of the trail from 0 to chainLen-1
00026     int length;   // length of the trail
00027     float speed;  // speed in moves at in LEDs per update
00028     int backgroundRatio; // background glow level
00029     pixelInfo colour;  // colour (and brightness) at the start of the chain
00030     bool dir;       // direction of travel - true = increasing location
00031 };
00032 
00033 
00034 struct trailInfo lines[trailCount];
00035 
00036 
00037 void blankBuffer(pixelInfo *Ptr)
00038 {
00039     memset( (void *)Ptr, 0, chainLen*sizeof(pixelInfo) );
00040 }
00041 
00042 void blankBuffer(pixelInfo16 *Ptr)
00043 {
00044     memset( (void *)Ptr, 0, chainLen*sizeof(pixelInfo16) );
00045 }
00046 
00047 void setPixel (pixelInfo *pixel, pixelInfo *colour, float level)
00048 {
00049     pixel->R = (colour->R * level);
00050     pixel->G = (colour->G * level);
00051     pixel->B = (colour->B * level);
00052 }
00053 
00054 void addPixel (pixelInfo16 *pixel, pixelInfo *colour, float level)
00055 {
00056     pixel->R = pixel->R + (int)(colour->R * level);
00057     pixel->G = pixel->G + (int)(colour->G * level);
00058     pixel->B = pixel->B + (int)(colour->B * level);
00059 }
00060 
00061 void subtractPixel (pixelInfo16 *pixel, pixelInfo *colour, float level)
00062 {
00063     pixel->R = pixel->R - (int)(colour->R * level);
00064     pixel->G = pixel->G - (int)(colour->G * level);
00065     pixel->B = pixel->B - (int)(colour->B * level);
00066 }
00067 
00068 
00069 void setTrail(bool add, pixelInfo16* buffer, pixelInfo *colour, int peakPoint, bool increasing, int len)
00070 {
00071     int pixelToUpdate = peakPoint;
00072     for (int pixel = 0; pixel < len; pixel++) {
00073 
00074         if (add)
00075             addPixel((buffer+pixelToUpdate), colour, 1.0f - (float)pixel/(float)len);
00076         else
00077             subtractPixel((buffer+pixelToUpdate), colour, 1.0f - (float)pixel/(float)len);
00078 
00079         increasing ? pixelToUpdate-- : pixelToUpdate++;
00080 
00081         if (pixelToUpdate == chainLen) {
00082             increasing = false;
00083             pixelToUpdate = chainLen-2;
00084         }
00085         if (pixelToUpdate == -1) {
00086             increasing = true;
00087             pixelToUpdate = 1;
00088         }
00089     }
00090 }
00091 
00092 void removeTrail (pixelInfo16* buffer, pixelInfo *colour, int peakPoint, bool increasing, int len)
00093 {
00094     setTrail (false, buffer, colour, peakPoint, increasing, len);
00095 }
00096 
00097 void addTrail (pixelInfo16* buffer, pixelInfo *colour, int peakPoint, bool increasing, int len)
00098 {
00099     setTrail (true, buffer, colour, peakPoint, increasing, len);
00100 }
00101 
00102 
00103 int main ()
00104 {
00105     //LEDs = 0;
00106 
00107 
00108     // set up the lights.
00109     lines[0].start = 0;
00110     lines[0].speed = 1.6;
00111     lines[0].length = 30;
00112     lines[0].backgroundRatio = 40;
00113     lines[0].colour.R = 200;
00114     lines[0].colour.G = 0;
00115     lines[0].colour.B = 0;
00116     lines[0].dir = true;
00117 
00118 #if 1
00119     lines[1].start = 150;
00120     lines[1].speed = 1.4;
00121     lines[1].length = 30;
00122     lines[1].backgroundRatio = 40;
00123     lines[1].colour.R = 0;
00124     lines[1].colour.G = 0;
00125     lines[1].colour.B = 200;
00126     lines[1].dir = true;
00127 
00128     lines[2].start = 299;
00129     lines[2].speed = 1.1;
00130     lines[2].length = 30;
00131     lines[2].backgroundRatio = 40;
00132     lines[2].colour.R = 0;
00133     lines[2].colour.G = 200;
00134     lines[2].colour.B = 0;
00135     lines[2].dir = false;
00136 #endif
00137 
00138     // clear the buffer
00139     blankBuffer(pixelData);
00140 
00141     // add the optional background
00142     /*
00143         for (int i = 0; i< chainLen; i++) {
00144             for (int j = 0; j <trailCount; j++) {
00145                 addPixel((pixelData+i), &(lines[j].colour), 1.0/lines[j].backgroundRatio);
00146             }
00147         }
00148     */
00149 
00150 // add the initial lines
00151     for (int j = 0; j <trailCount; j++) {
00152         addTrail (pixelData, &(lines[j].colour), lines[j].start, lines[j].dir, lines[j].length); // set the LED data
00153     }
00154     // give the LED driver the buffer to use.
00155     ledDriver.setData(pixelData, chainLen);
00156 
00157     //LEDs = 1;
00158 
00159     updateRateTimer.start();
00160     while (true) {
00161         ledDriver.sendData(); // send the LED data
00162 
00163         led1 = !led1;
00164         //LEDs = LEDs+1;
00165 
00166         // subtract the current trail locations and then add the new locations.
00167         for (int j = 0; j <trailCount; j++) {
00168             removeTrail (pixelData, &(lines[j].colour), lines[j].start, lines[j].dir, lines[j].length); // set the LED data
00169 
00170             lines[j].dir ? lines[j].start+=lines[j].speed : lines[j].start-=lines[j].speed;
00171             if ((int)lines[j].start >= chainLen) {
00172                 lines[j].dir = false;
00173                 lines[j].start = chainLen-1 - lines[j].speed;
00174             }
00175             if ((int)lines[j].start <= -1) {
00176                 lines[j].dir = true;
00177                 lines[j].start = lines[j].speed;
00178             }
00179             addTrail (pixelData, &(lines[j].colour), lines[j].start, lines[j].dir, lines[j].length); // set the LED data
00180         }
00181         // wait for the next update time.
00182         while (updateRateTimer.read_ms() < updatePeriodMS) {
00183         }
00184         updateRateTimer.reset();
00185     }
00186 
00187 }