control multi animation ledws2812 by usb serial com

Dependencies:   PixelArray WS2812 mbed

Fork of WS2812_Example by Brian Daniels

main.cpp

Committer:
exarkun
Date:
2017-12-23
Revision:
3:e59164341b18
Parent:
2:cb82a3dc4031

File content as of revision 3:e59164341b18:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"
#include <time.h>

#define WS2812_BUF 30//numbers of led 

// définition des sorties leds
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);


//declaration des leds visuelle utiliser pour mesurer le temps des taches 
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
// déclaration du hardware
Serial pc(USBTX, USBRX);//utilisation de la liaison usb

PixelArray px(WS2812_BUF);

WS2812 ws(D9, WS2812_BUF, 5, 10, 10, 15);

char c;

//declaration des differantes taches
void task1_switch(void);
void task2_switch(void);
void task3_switch(void);
void task4_switch(void);

//declaration des differantes interuption timer
Ticker time_up1; //definition du Ticker, avec le nom “time_up1”
Ticker time_up2; //definition du Ticker, avec le nom “time_up2”
Ticker time_up3; //definition du Ticker, avec le nom “time_up3”
Ticker time_up4; //definition du Ticker, avec le nom “time_up4”


int random_color_position()
{   
#define WS2812_BUF 30//numbers of led 
#define NUM_COLORS 1
#define NUM_LEDS_PER_COLOR 1

    ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    
    while (1) 
    { 
    if(pc.readable()){c = pc.getc();}
    if(c=='0'){goto label;}
                //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random blue color
                int colorbuf[NUM_COLORS] = {(rand()%16777215+0)};//random color
                for (int i = 0; i < WS2812_BUF; i++) 
                {
                    px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
                }

                int k1=rand()%30+0;//random position1
                int k2=rand()%30+0;//random position1
                int k3=rand()%30+0;//random position1
                int k4=rand()%30+0;//random position1
                int k5=rand()%30+0;//random position1
                int k6=rand()%30+0;//random position1
                int k7=rand()%30+0;//random position1
                int k8=rand()%30+0;//random position1
                int k9=rand()%30+0;//random position1
                int k10=rand()%30+0;//random position1
                float time=rand()%1+0.001;//random time                              

                px.SetI(k1,rand()%0xff+0x00);
                px.SetI(k2,rand()%0xff+0x00);
                px.SetI(k3,rand()%0xff+0x00);
                px.SetI(k4,rand()%0xff+0x00);                
                px.SetI(k5,rand()%0xff+0x00);
                px.SetI(k6,rand()%0xff+0x00);
                px.SetI(k7,rand()%0xff+0x00);
                px.SetI(k8,rand()%0xff+0x00);
                px.SetI(k9,rand()%0xff+0x00);
                px.SetI(k10,rand()%0xff+0x00);                                

                // Now the buffer is written, rotate it
                // by writing it out with an increasing offset
  
                for (int z=WS2812_BUF; z >= 0 ; z--) 
                {
                ws.write_offsets(px.getBuf(),z,z,z);
                wait(time);
                }                      
  }
  label:
}



int deux_point_rouge_k2000()
{

#define WS2812_BUF 30//numbers of led 
#define NUM_COLORS 1
#define NUM_LEDS_PER_COLOR 1

    ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    
    while (1) 
    {
    if(pc.readable()){c = pc.getc();}     
    if(c=='0'){goto label;}
     //   for (int x = 0; x < 256; x++)
        { 
            for (int y = 0; y < 31; y++)
            { 
                int colorbuf[NUM_COLORS] = {0xf0000f};//random color
                //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
                // for each of the colours (j) write out 10 of them
                // the pixels are written at the colour*10, plus the colour position
                // all modulus 60 so it wraps around
 
                for (int i = 0; i < WS2812_BUF; i++) 
                {
                    px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
                }
 
                // now all the colours are computed, add a fade effect using intensity scaling
                // compute and write the II value for each pixel
     
                int j=rand()%30+0;               
                px.SetI(y,0xf0000f);
                px.SetI(30-y,0xf0000f);
                // Now the buffer is written, rotate it
                // by writing it out with an increasing offset
  
                for (int z=WS2812_BUF; z >= 0 ; z--) 
                {
                ws.write_offsets(px.getBuf(),z,z,z);
                }    
            }               
        }      
    }
label:
}

int un_point_rouge_k2000()
{

#define WS2812_BUF 30//numbers of led 
#define NUM_COLORS 1
#define NUM_LEDS_PER_COLOR 1
    ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    
    while (1) 
    {
    if(pc.readable()){c = pc.getc();} 
    if(c=='0'){goto label;}
     //   for (int x = 0; x < 256; x++)
        { 
            for (int y = 30; y > 0; y--)
            { 
                int colorbuf[NUM_COLORS] = {0xff000f};//random color
                //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
                // for each of the colours (j) write out 10 of them
                // the pixels are written at the colour*10, plus the colour position
                // all modulus 60 so it wraps around
 
                for (int i = 0; i < WS2812_BUF; i++) 
                {
                    px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
                }
 
                // now all the colours are computed, add a fade effect using intensity scaling
                // compute and write the II value for each pixel
     
                int j=rand()%30+0;               
                 px.SetI(y,0xff000f);
    
                // Now the buffer is written, rotate it
                // by writing it out with an increasing offset
  
                for (int z=WS2812_BUF; z >= 0 ; z--) 
                {
                ws.write_offsets(px.getBuf(),z,z,z);
                }     
            } 
   
            for (int y = 0; y < 30; y++)
            { 
                int colorbuf[NUM_COLORS] = {0xff000f};//random color
                //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
                // for each of the colours (j) write out 10 of them
                // the pixels are written at the colour*10, plus the colour position
                // all modulus 60 so it wraps around
 
                for (int i = 0; i < WS2812_BUF; i++) 
                {
                    px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
                }
 
                // now all the colours are computed, add a fade effect using intensity scaling
                // compute and write the II value for each pixel
     
                int j=rand()%30+0;               
                px.SetI(y,0xff000f);
    
                // Now the buffer is written, rotate it
                // by writing it out with an increasing offset
  
                for (int z=WS2812_BUF; z >= 0 ; z--) 
                {
                     ws.write_offsets(px.getBuf(),z,z,z);
                }  
            }                
        }
    }
    label:
}

int chainage_couleur()
{
#define WS2812_BUF 150
#define NUM_COLORS 6
#define NUM_LEDS_PER_COLOR 10

PixelArray px(WS2812_BUF);

// See the program page for information on the timing numbers
// The given numbers are for the K64F
//WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
WS2812 ws(D9, WS2812_BUF, 5, 10, 10, 15);

    ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    
    // set up the colours we want to draw with
    int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};

    // for each of the colours (j) write out 10 of them
    // the pixels are written at the colour*10, plus the colour position
    // all modulus 60 so it wraps around
    for (int i = 0; i < WS2812_BUF; i++) {
        if(pc.readable()){c = pc.getc();}
        px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
    }

    // now all the colours are computed, add a fade effect using intensity scaling
    // compute and write the II value for each pixel
    for (int j=0; j<WS2812_BUF; j++) {
        if(pc.readable()){c = pc.getc();}
        // px.SetI(pixel position, II value)
        px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
    }


    // Now the buffer is written, rotate it
    // by writing it out with an increasing offset
    while (1) 
    {
        if(pc.readable()){c = pc.getc();}
        if(c=='0'){goto label;}
        for (int z=WS2812_BUF; z >= 0 ; z--) 
        {
            ws.write_offsets(px.getBuf(),z,z,z);
            wait(0.075); 
        } 
    }
  label:

}


void task1_switch()
{ 
myled1=1;
 if(pc.readable()) 
 {
    c = pc.getc();
 }   
myled1=0;
}



void task2_switch()
{ 
myled2=1;
        switch(c)//chargement des wafers dans tete                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                {
                    case '1'://impossible y as pas encore la precision demander c faut y as un probleme .!!!!!!!!!!!!!!!!!!!!!!!!!!
                        random_color_position();
                    break;
                    
                    case '2':
                        deux_point_rouge_k2000();
                    break;
                    
                    case '3':
                        un_point_rouge_k2000();
                    break;
                    
                    case '4':    
                        chainage_couleur();
                    break;
                        
                }
myled2=0;

}

void task3_switch()
{
myled3=1; 

myled3=0;
}



void task4_switch()
{
myled4=1; 

myled4=0;
}


int main()
{

    
//lancement des tasks               
            time_up1.attach(&task1_switch, 0.5);    
            time_up2.attach(&task2_switch, 0.0005);    
            time_up3.attach(&task3_switch, 0.5);
            time_up4.attach(&task4_switch, 0.5);     
//while(1)
{                
               // pc.printf("press key...\r\n");

 
                //c=rand()%4+1;//random value
                
}
                
}