control multi animation ledws2812 by usb serial com
Dependencies: PixelArray WS2812 mbed
Fork of WS2812_Example by
Revision 3:e59164341b18, committed 2017-12-23
- Comitter:
- exarkun
- Date:
- Sat Dec 23 18:52:47 2017 +0000
- Parent:
- 2:cb82a3dc4031
- Commit message:
- usb serial com for led ws2812
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r cb82a3dc4031 -r e59164341b18 main.cpp --- a/main.cpp Thu Feb 12 21:55:24 2015 +0000 +++ b/main.cpp Sat Dec 23 18:52:47 2017 +0000 @@ -1,7 +1,223 @@ #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 @@ -10,10 +226,8 @@ // 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); - -int main() -{ +//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 @@ -24,12 +238,14 @@ // 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))); } @@ -37,11 +253,93 @@ // Now the buffer is written, rotate it // by writing it out with an increasing offset - while (1) { - for (int z=WS2812_BUF; z >= 0 ; z--) { + 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); - } + 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 + +} + +} +