control multi animation ledws2812 by usb serial com

Dependencies:   PixelArray WS2812 mbed

Fork of WS2812_Example by Brian Daniels

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WS2812.h"
00003 #include "PixelArray.h"
00004 #include <time.h>
00005 
00006 #define WS2812_BUF 30//numbers of led 
00007 
00008 // définition des sorties leds
00009 DigitalOut led1(LED1);
00010 DigitalOut led2(LED2);
00011 DigitalOut led3(LED3);
00012 DigitalOut led4(LED4);
00013 
00014 
00015 //declaration des leds visuelle utiliser pour mesurer le temps des taches 
00016 DigitalOut myled1(LED1);
00017 DigitalOut myled2(LED2);
00018 DigitalOut myled3(LED3);
00019 DigitalOut myled4(LED4);
00020 // déclaration du hardware
00021 Serial pc(USBTX, USBRX);//utilisation de la liaison usb
00022 
00023 PixelArray px(WS2812_BUF);
00024 
00025 WS2812 ws(D9, WS2812_BUF, 5, 10, 10, 15);
00026 
00027 char c;
00028 
00029 //declaration des differantes taches
00030 void task1_switch(void);
00031 void task2_switch(void);
00032 void task3_switch(void);
00033 void task4_switch(void);
00034 
00035 //declaration des differantes interuption timer
00036 Ticker time_up1; //definition du Ticker, avec le nom “time_up1”
00037 Ticker time_up2; //definition du Ticker, avec le nom “time_up2”
00038 Ticker time_up3; //definition du Ticker, avec le nom “time_up3”
00039 Ticker time_up4; //definition du Ticker, avec le nom “time_up4”
00040 
00041 
00042 int random_color_position()
00043 {   
00044 #define WS2812_BUF 30//numbers of led 
00045 #define NUM_COLORS 1
00046 #define NUM_LEDS_PER_COLOR 1
00047 
00048     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00049     
00050     while (1) 
00051     { 
00052     if(pc.readable()){c = pc.getc();}
00053     if(c=='0'){goto label;}
00054                 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random blue color
00055                 int colorbuf[NUM_COLORS] = {(rand()%16777215+0)};//random color
00056                 for (int i = 0; i < WS2812_BUF; i++) 
00057                 {
00058                     px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
00059                 }
00060 
00061                 int k1=rand()%30+0;//random position1
00062                 int k2=rand()%30+0;//random position1
00063                 int k3=rand()%30+0;//random position1
00064                 int k4=rand()%30+0;//random position1
00065                 int k5=rand()%30+0;//random position1
00066                 int k6=rand()%30+0;//random position1
00067                 int k7=rand()%30+0;//random position1
00068                 int k8=rand()%30+0;//random position1
00069                 int k9=rand()%30+0;//random position1
00070                 int k10=rand()%30+0;//random position1
00071                 float time=rand()%1+0.001;//random time                              
00072 
00073                 px.SetI(k1,rand()%0xff+0x00);
00074                 px.SetI(k2,rand()%0xff+0x00);
00075                 px.SetI(k3,rand()%0xff+0x00);
00076                 px.SetI(k4,rand()%0xff+0x00);                
00077                 px.SetI(k5,rand()%0xff+0x00);
00078                 px.SetI(k6,rand()%0xff+0x00);
00079                 px.SetI(k7,rand()%0xff+0x00);
00080                 px.SetI(k8,rand()%0xff+0x00);
00081                 px.SetI(k9,rand()%0xff+0x00);
00082                 px.SetI(k10,rand()%0xff+0x00);                                
00083 
00084                 // Now the buffer is written, rotate it
00085                 // by writing it out with an increasing offset
00086   
00087                 for (int z=WS2812_BUF; z >= 0 ; z--) 
00088                 {
00089                 ws.write_offsets(px.getBuf(),z,z,z);
00090                 wait(time);
00091                 }                      
00092   }
00093   label:
00094 }
00095 
00096 
00097 
00098 int deux_point_rouge_k2000()
00099 {
00100 
00101 #define WS2812_BUF 30//numbers of led 
00102 #define NUM_COLORS 1
00103 #define NUM_LEDS_PER_COLOR 1
00104 
00105     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00106     
00107     while (1) 
00108     {
00109     if(pc.readable()){c = pc.getc();}     
00110     if(c=='0'){goto label;}
00111      //   for (int x = 0; x < 256; x++)
00112         { 
00113             for (int y = 0; y < 31; y++)
00114             { 
00115                 int colorbuf[NUM_COLORS] = {0xf0000f};//random color
00116                 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
00117                 // for each of the colours (j) write out 10 of them
00118                 // the pixels are written at the colour*10, plus the colour position
00119                 // all modulus 60 so it wraps around
00120  
00121                 for (int i = 0; i < WS2812_BUF; i++) 
00122                 {
00123                     px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
00124                 }
00125  
00126                 // now all the colours are computed, add a fade effect using intensity scaling
00127                 // compute and write the II value for each pixel
00128      
00129                 int j=rand()%30+0;               
00130                 px.SetI(y,0xf0000f);
00131                 px.SetI(30-y,0xf0000f);
00132                 // Now the buffer is written, rotate it
00133                 // by writing it out with an increasing offset
00134   
00135                 for (int z=WS2812_BUF; z >= 0 ; z--) 
00136                 {
00137                 ws.write_offsets(px.getBuf(),z,z,z);
00138                 }    
00139             }               
00140         }      
00141     }
00142 label:
00143 }
00144 
00145 int un_point_rouge_k2000()
00146 {
00147 
00148 #define WS2812_BUF 30//numbers of led 
00149 #define NUM_COLORS 1
00150 #define NUM_LEDS_PER_COLOR 1
00151     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00152     
00153     while (1) 
00154     {
00155     if(pc.readable()){c = pc.getc();} 
00156     if(c=='0'){goto label;}
00157      //   for (int x = 0; x < 256; x++)
00158         { 
00159             for (int y = 30; y > 0; y--)
00160             { 
00161                 int colorbuf[NUM_COLORS] = {0xff000f};//random color
00162                 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
00163                 // for each of the colours (j) write out 10 of them
00164                 // the pixels are written at the colour*10, plus the colour position
00165                 // all modulus 60 so it wraps around
00166  
00167                 for (int i = 0; i < WS2812_BUF; i++) 
00168                 {
00169                     px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
00170                 }
00171  
00172                 // now all the colours are computed, add a fade effect using intensity scaling
00173                 // compute and write the II value for each pixel
00174      
00175                 int j=rand()%30+0;               
00176                  px.SetI(y,0xff000f);
00177     
00178                 // Now the buffer is written, rotate it
00179                 // by writing it out with an increasing offset
00180   
00181                 for (int z=WS2812_BUF; z >= 0 ; z--) 
00182                 {
00183                 ws.write_offsets(px.getBuf(),z,z,z);
00184                 }     
00185             } 
00186    
00187             for (int y = 0; y < 30; y++)
00188             { 
00189                 int colorbuf[NUM_COLORS] = {0xff000f};//random color
00190                 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
00191                 // for each of the colours (j) write out 10 of them
00192                 // the pixels are written at the colour*10, plus the colour position
00193                 // all modulus 60 so it wraps around
00194  
00195                 for (int i = 0; i < WS2812_BUF; i++) 
00196                 {
00197                     px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
00198                 }
00199  
00200                 // now all the colours are computed, add a fade effect using intensity scaling
00201                 // compute and write the II value for each pixel
00202      
00203                 int j=rand()%30+0;               
00204                 px.SetI(y,0xff000f);
00205     
00206                 // Now the buffer is written, rotate it
00207                 // by writing it out with an increasing offset
00208   
00209                 for (int z=WS2812_BUF; z >= 0 ; z--) 
00210                 {
00211                      ws.write_offsets(px.getBuf(),z,z,z);
00212                 }  
00213             }                
00214         }
00215     }
00216     label:
00217 }
00218 
00219 int chainage_couleur()
00220 {
00221 #define WS2812_BUF 150
00222 #define NUM_COLORS 6
00223 #define NUM_LEDS_PER_COLOR 10
00224 
00225 PixelArray px(WS2812_BUF);
00226 
00227 // See the program page for information on the timing numbers
00228 // The given numbers are for the K64F
00229 //WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
00230 WS2812 ws(D9, WS2812_BUF, 5, 10, 10, 15);
00231 
00232     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00233     
00234     // set up the colours we want to draw with
00235     int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
00236 
00237     // for each of the colours (j) write out 10 of them
00238     // the pixels are written at the colour*10, plus the colour position
00239     // all modulus 60 so it wraps around
00240     for (int i = 0; i < WS2812_BUF; i++) {
00241         if(pc.readable()){c = pc.getc();}
00242         px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
00243     }
00244 
00245     // now all the colours are computed, add a fade effect using intensity scaling
00246     // compute and write the II value for each pixel
00247     for (int j=0; j<WS2812_BUF; j++) {
00248         if(pc.readable()){c = pc.getc();}
00249         // px.SetI(pixel position, II value)
00250         px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
00251     }
00252 
00253 
00254     // Now the buffer is written, rotate it
00255     // by writing it out with an increasing offset
00256     while (1) 
00257     {
00258         if(pc.readable()){c = pc.getc();}
00259         if(c=='0'){goto label;}
00260         for (int z=WS2812_BUF; z >= 0 ; z--) 
00261         {
00262             ws.write_offsets(px.getBuf(),z,z,z);
00263             wait(0.075); 
00264         } 
00265     }
00266   label:
00267 
00268 }
00269 
00270 
00271 void task1_switch()
00272 { 
00273 myled1=1;
00274  if(pc.readable()) 
00275  {
00276     c = pc.getc();
00277  }   
00278 myled1=0;
00279 }
00280 
00281 
00282 
00283 void task2_switch()
00284 { 
00285 myled2=1;
00286         switch(c)//chargement des wafers dans tete                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
00287                 {
00288                     case '1'://impossible y as pas encore la precision demander c faut y as un probleme .!!!!!!!!!!!!!!!!!!!!!!!!!!
00289                         random_color_position();
00290                     break;
00291                     
00292                     case '2':
00293                         deux_point_rouge_k2000();
00294                     break;
00295                     
00296                     case '3':
00297                         un_point_rouge_k2000();
00298                     break;
00299                     
00300                     case '4':    
00301                         chainage_couleur();
00302                     break;
00303                         
00304                 }
00305 myled2=0;
00306 
00307 }
00308 
00309 void task3_switch()
00310 {
00311 myled3=1; 
00312 
00313 myled3=0;
00314 }
00315 
00316 
00317 
00318 void task4_switch()
00319 {
00320 myled4=1; 
00321 
00322 myled4=0;
00323 }
00324 
00325 
00326 int main()
00327 {
00328 
00329     
00330 //lancement des tasks               
00331             time_up1.attach(&task1_switch, 0.5);    
00332             time_up2.attach(&task2_switch, 0.0005);    
00333             time_up3.attach(&task3_switch, 0.5);
00334             time_up4.attach(&task4_switch, 0.5);     
00335 //while(1)
00336 {                
00337                // pc.printf("press key...\r\n");
00338 
00339  
00340                 //c=rand()%4+1;//random value
00341                 
00342 }
00343                 
00344 }
00345