control multi animation ledws2812 by usb serial com

Dependencies:   PixelArray WS2812 mbed

Fork of WS2812_Example by Brian Daniels

Committer:
exarkun
Date:
Sat Dec 23 18:52:47 2017 +0000
Revision:
3:e59164341b18
Parent:
2:cb82a3dc4031
usb serial com for led ws2812

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bridadan 0:12cb6f0c2788 1 #include "mbed.h"
bridadan 0:12cb6f0c2788 2 #include "WS2812.h"
bridadan 0:12cb6f0c2788 3 #include "PixelArray.h"
exarkun 3:e59164341b18 4 #include <time.h>
bridadan 0:12cb6f0c2788 5
exarkun 3:e59164341b18 6 #define WS2812_BUF 30//numbers of led
exarkun 3:e59164341b18 7
exarkun 3:e59164341b18 8 // définition des sorties leds
exarkun 3:e59164341b18 9 DigitalOut led1(LED1);
exarkun 3:e59164341b18 10 DigitalOut led2(LED2);
exarkun 3:e59164341b18 11 DigitalOut led3(LED3);
exarkun 3:e59164341b18 12 DigitalOut led4(LED4);
exarkun 3:e59164341b18 13
exarkun 3:e59164341b18 14
exarkun 3:e59164341b18 15 //declaration des leds visuelle utiliser pour mesurer le temps des taches
exarkun 3:e59164341b18 16 DigitalOut myled1(LED1);
exarkun 3:e59164341b18 17 DigitalOut myled2(LED2);
exarkun 3:e59164341b18 18 DigitalOut myled3(LED3);
exarkun 3:e59164341b18 19 DigitalOut myled4(LED4);
exarkun 3:e59164341b18 20 // déclaration du hardware
exarkun 3:e59164341b18 21 Serial pc(USBTX, USBRX);//utilisation de la liaison usb
exarkun 3:e59164341b18 22
exarkun 3:e59164341b18 23 PixelArray px(WS2812_BUF);
exarkun 3:e59164341b18 24
exarkun 3:e59164341b18 25 WS2812 ws(D9, WS2812_BUF, 5, 10, 10, 15);
exarkun 3:e59164341b18 26
exarkun 3:e59164341b18 27 char c;
exarkun 3:e59164341b18 28
exarkun 3:e59164341b18 29 //declaration des differantes taches
exarkun 3:e59164341b18 30 void task1_switch(void);
exarkun 3:e59164341b18 31 void task2_switch(void);
exarkun 3:e59164341b18 32 void task3_switch(void);
exarkun 3:e59164341b18 33 void task4_switch(void);
exarkun 3:e59164341b18 34
exarkun 3:e59164341b18 35 //declaration des differantes interuption timer
exarkun 3:e59164341b18 36 Ticker time_up1; //definition du Ticker, avec le nom “time_up1”
exarkun 3:e59164341b18 37 Ticker time_up2; //definition du Ticker, avec le nom “time_up2”
exarkun 3:e59164341b18 38 Ticker time_up3; //definition du Ticker, avec le nom “time_up3”
exarkun 3:e59164341b18 39 Ticker time_up4; //definition du Ticker, avec le nom “time_up4”
exarkun 3:e59164341b18 40
exarkun 3:e59164341b18 41
exarkun 3:e59164341b18 42 int random_color_position()
exarkun 3:e59164341b18 43 {
exarkun 3:e59164341b18 44 #define WS2812_BUF 30//numbers of led
exarkun 3:e59164341b18 45 #define NUM_COLORS 1
exarkun 3:e59164341b18 46 #define NUM_LEDS_PER_COLOR 1
exarkun 3:e59164341b18 47
exarkun 3:e59164341b18 48 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
exarkun 3:e59164341b18 49
exarkun 3:e59164341b18 50 while (1)
exarkun 3:e59164341b18 51 {
exarkun 3:e59164341b18 52 if(pc.readable()){c = pc.getc();}
exarkun 3:e59164341b18 53 if(c=='0'){goto label;}
exarkun 3:e59164341b18 54 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random blue color
exarkun 3:e59164341b18 55 int colorbuf[NUM_COLORS] = {(rand()%16777215+0)};//random color
exarkun 3:e59164341b18 56 for (int i = 0; i < WS2812_BUF; i++)
exarkun 3:e59164341b18 57 {
exarkun 3:e59164341b18 58 px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
exarkun 3:e59164341b18 59 }
exarkun 3:e59164341b18 60
exarkun 3:e59164341b18 61 int k1=rand()%30+0;//random position1
exarkun 3:e59164341b18 62 int k2=rand()%30+0;//random position1
exarkun 3:e59164341b18 63 int k3=rand()%30+0;//random position1
exarkun 3:e59164341b18 64 int k4=rand()%30+0;//random position1
exarkun 3:e59164341b18 65 int k5=rand()%30+0;//random position1
exarkun 3:e59164341b18 66 int k6=rand()%30+0;//random position1
exarkun 3:e59164341b18 67 int k7=rand()%30+0;//random position1
exarkun 3:e59164341b18 68 int k8=rand()%30+0;//random position1
exarkun 3:e59164341b18 69 int k9=rand()%30+0;//random position1
exarkun 3:e59164341b18 70 int k10=rand()%30+0;//random position1
exarkun 3:e59164341b18 71 float time=rand()%1+0.001;//random time
exarkun 3:e59164341b18 72
exarkun 3:e59164341b18 73 px.SetI(k1,rand()%0xff+0x00);
exarkun 3:e59164341b18 74 px.SetI(k2,rand()%0xff+0x00);
exarkun 3:e59164341b18 75 px.SetI(k3,rand()%0xff+0x00);
exarkun 3:e59164341b18 76 px.SetI(k4,rand()%0xff+0x00);
exarkun 3:e59164341b18 77 px.SetI(k5,rand()%0xff+0x00);
exarkun 3:e59164341b18 78 px.SetI(k6,rand()%0xff+0x00);
exarkun 3:e59164341b18 79 px.SetI(k7,rand()%0xff+0x00);
exarkun 3:e59164341b18 80 px.SetI(k8,rand()%0xff+0x00);
exarkun 3:e59164341b18 81 px.SetI(k9,rand()%0xff+0x00);
exarkun 3:e59164341b18 82 px.SetI(k10,rand()%0xff+0x00);
exarkun 3:e59164341b18 83
exarkun 3:e59164341b18 84 // Now the buffer is written, rotate it
exarkun 3:e59164341b18 85 // by writing it out with an increasing offset
exarkun 3:e59164341b18 86
exarkun 3:e59164341b18 87 for (int z=WS2812_BUF; z >= 0 ; z--)
exarkun 3:e59164341b18 88 {
exarkun 3:e59164341b18 89 ws.write_offsets(px.getBuf(),z,z,z);
exarkun 3:e59164341b18 90 wait(time);
exarkun 3:e59164341b18 91 }
exarkun 3:e59164341b18 92 }
exarkun 3:e59164341b18 93 label:
exarkun 3:e59164341b18 94 }
exarkun 3:e59164341b18 95
exarkun 3:e59164341b18 96
exarkun 3:e59164341b18 97
exarkun 3:e59164341b18 98 int deux_point_rouge_k2000()
exarkun 3:e59164341b18 99 {
exarkun 3:e59164341b18 100
exarkun 3:e59164341b18 101 #define WS2812_BUF 30//numbers of led
exarkun 3:e59164341b18 102 #define NUM_COLORS 1
exarkun 3:e59164341b18 103 #define NUM_LEDS_PER_COLOR 1
exarkun 3:e59164341b18 104
exarkun 3:e59164341b18 105 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
exarkun 3:e59164341b18 106
exarkun 3:e59164341b18 107 while (1)
exarkun 3:e59164341b18 108 {
exarkun 3:e59164341b18 109 if(pc.readable()){c = pc.getc();}
exarkun 3:e59164341b18 110 if(c=='0'){goto label;}
exarkun 3:e59164341b18 111 // for (int x = 0; x < 256; x++)
exarkun 3:e59164341b18 112 {
exarkun 3:e59164341b18 113 for (int y = 0; y < 31; y++)
exarkun 3:e59164341b18 114 {
exarkun 3:e59164341b18 115 int colorbuf[NUM_COLORS] = {0xf0000f};//random color
exarkun 3:e59164341b18 116 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
exarkun 3:e59164341b18 117 // for each of the colours (j) write out 10 of them
exarkun 3:e59164341b18 118 // the pixels are written at the colour*10, plus the colour position
exarkun 3:e59164341b18 119 // all modulus 60 so it wraps around
exarkun 3:e59164341b18 120
exarkun 3:e59164341b18 121 for (int i = 0; i < WS2812_BUF; i++)
exarkun 3:e59164341b18 122 {
exarkun 3:e59164341b18 123 px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
exarkun 3:e59164341b18 124 }
exarkun 3:e59164341b18 125
exarkun 3:e59164341b18 126 // now all the colours are computed, add a fade effect using intensity scaling
exarkun 3:e59164341b18 127 // compute and write the II value for each pixel
exarkun 3:e59164341b18 128
exarkun 3:e59164341b18 129 int j=rand()%30+0;
exarkun 3:e59164341b18 130 px.SetI(y,0xf0000f);
exarkun 3:e59164341b18 131 px.SetI(30-y,0xf0000f);
exarkun 3:e59164341b18 132 // Now the buffer is written, rotate it
exarkun 3:e59164341b18 133 // by writing it out with an increasing offset
exarkun 3:e59164341b18 134
exarkun 3:e59164341b18 135 for (int z=WS2812_BUF; z >= 0 ; z--)
exarkun 3:e59164341b18 136 {
exarkun 3:e59164341b18 137 ws.write_offsets(px.getBuf(),z,z,z);
exarkun 3:e59164341b18 138 }
exarkun 3:e59164341b18 139 }
exarkun 3:e59164341b18 140 }
exarkun 3:e59164341b18 141 }
exarkun 3:e59164341b18 142 label:
exarkun 3:e59164341b18 143 }
exarkun 3:e59164341b18 144
exarkun 3:e59164341b18 145 int un_point_rouge_k2000()
exarkun 3:e59164341b18 146 {
exarkun 3:e59164341b18 147
exarkun 3:e59164341b18 148 #define WS2812_BUF 30//numbers of led
exarkun 3:e59164341b18 149 #define NUM_COLORS 1
exarkun 3:e59164341b18 150 #define NUM_LEDS_PER_COLOR 1
exarkun 3:e59164341b18 151 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
exarkun 3:e59164341b18 152
exarkun 3:e59164341b18 153 while (1)
exarkun 3:e59164341b18 154 {
exarkun 3:e59164341b18 155 if(pc.readable()){c = pc.getc();}
exarkun 3:e59164341b18 156 if(c=='0'){goto label;}
exarkun 3:e59164341b18 157 // for (int x = 0; x < 256; x++)
exarkun 3:e59164341b18 158 {
exarkun 3:e59164341b18 159 for (int y = 30; y > 0; y--)
exarkun 3:e59164341b18 160 {
exarkun 3:e59164341b18 161 int colorbuf[NUM_COLORS] = {0xff000f};//random color
exarkun 3:e59164341b18 162 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
exarkun 3:e59164341b18 163 // for each of the colours (j) write out 10 of them
exarkun 3:e59164341b18 164 // the pixels are written at the colour*10, plus the colour position
exarkun 3:e59164341b18 165 // all modulus 60 so it wraps around
exarkun 3:e59164341b18 166
exarkun 3:e59164341b18 167 for (int i = 0; i < WS2812_BUF; i++)
exarkun 3:e59164341b18 168 {
exarkun 3:e59164341b18 169 px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
exarkun 3:e59164341b18 170 }
exarkun 3:e59164341b18 171
exarkun 3:e59164341b18 172 // now all the colours are computed, add a fade effect using intensity scaling
exarkun 3:e59164341b18 173 // compute and write the II value for each pixel
exarkun 3:e59164341b18 174
exarkun 3:e59164341b18 175 int j=rand()%30+0;
exarkun 3:e59164341b18 176 px.SetI(y,0xff000f);
exarkun 3:e59164341b18 177
exarkun 3:e59164341b18 178 // Now the buffer is written, rotate it
exarkun 3:e59164341b18 179 // by writing it out with an increasing offset
exarkun 3:e59164341b18 180
exarkun 3:e59164341b18 181 for (int z=WS2812_BUF; z >= 0 ; z--)
exarkun 3:e59164341b18 182 {
exarkun 3:e59164341b18 183 ws.write_offsets(px.getBuf(),z,z,z);
exarkun 3:e59164341b18 184 }
exarkun 3:e59164341b18 185 }
exarkun 3:e59164341b18 186
exarkun 3:e59164341b18 187 for (int y = 0; y < 30; y++)
exarkun 3:e59164341b18 188 {
exarkun 3:e59164341b18 189 int colorbuf[NUM_COLORS] = {0xff000f};//random color
exarkun 3:e59164341b18 190 //int colorbuf[NUM_COLORS] = {(rand()%256+0)};//random bleu color
exarkun 3:e59164341b18 191 // for each of the colours (j) write out 10 of them
exarkun 3:e59164341b18 192 // the pixels are written at the colour*10, plus the colour position
exarkun 3:e59164341b18 193 // all modulus 60 so it wraps around
exarkun 3:e59164341b18 194
exarkun 3:e59164341b18 195 for (int i = 0; i < WS2812_BUF; i++)
exarkun 3:e59164341b18 196 {
exarkun 3:e59164341b18 197 px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
exarkun 3:e59164341b18 198 }
exarkun 3:e59164341b18 199
exarkun 3:e59164341b18 200 // now all the colours are computed, add a fade effect using intensity scaling
exarkun 3:e59164341b18 201 // compute and write the II value for each pixel
exarkun 3:e59164341b18 202
exarkun 3:e59164341b18 203 int j=rand()%30+0;
exarkun 3:e59164341b18 204 px.SetI(y,0xff000f);
exarkun 3:e59164341b18 205
exarkun 3:e59164341b18 206 // Now the buffer is written, rotate it
exarkun 3:e59164341b18 207 // by writing it out with an increasing offset
exarkun 3:e59164341b18 208
exarkun 3:e59164341b18 209 for (int z=WS2812_BUF; z >= 0 ; z--)
exarkun 3:e59164341b18 210 {
exarkun 3:e59164341b18 211 ws.write_offsets(px.getBuf(),z,z,z);
exarkun 3:e59164341b18 212 }
exarkun 3:e59164341b18 213 }
exarkun 3:e59164341b18 214 }
exarkun 3:e59164341b18 215 }
exarkun 3:e59164341b18 216 label:
exarkun 3:e59164341b18 217 }
exarkun 3:e59164341b18 218
exarkun 3:e59164341b18 219 int chainage_couleur()
exarkun 3:e59164341b18 220 {
bridadan 2:cb82a3dc4031 221 #define WS2812_BUF 150
bridadan 2:cb82a3dc4031 222 #define NUM_COLORS 6
bridadan 2:cb82a3dc4031 223 #define NUM_LEDS_PER_COLOR 10
bridadan 0:12cb6f0c2788 224
bridadan 0:12cb6f0c2788 225 PixelArray px(WS2812_BUF);
bridadan 0:12cb6f0c2788 226
bridadan 2:cb82a3dc4031 227 // See the program page for information on the timing numbers
bridadan 1:e04a0ecefa29 228 // The given numbers are for the K64F
exarkun 3:e59164341b18 229 //WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
exarkun 3:e59164341b18 230 WS2812 ws(D9, WS2812_BUF, 5, 10, 10, 15);
bridadan 0:12cb6f0c2788 231
bridadan 1:e04a0ecefa29 232 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
bridadan 0:12cb6f0c2788 233
bridadan 0:12cb6f0c2788 234 // set up the colours we want to draw with
bridadan 2:cb82a3dc4031 235 int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
bridadan 0:12cb6f0c2788 236
bridadan 0:12cb6f0c2788 237 // for each of the colours (j) write out 10 of them
bridadan 0:12cb6f0c2788 238 // the pixels are written at the colour*10, plus the colour position
bridadan 0:12cb6f0c2788 239 // all modulus 60 so it wraps around
bridadan 2:cb82a3dc4031 240 for (int i = 0; i < WS2812_BUF; i++) {
exarkun 3:e59164341b18 241 if(pc.readable()){c = pc.getc();}
bridadan 2:cb82a3dc4031 242 px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
bridadan 0:12cb6f0c2788 243 }
bridadan 0:12cb6f0c2788 244
bridadan 0:12cb6f0c2788 245 // now all the colours are computed, add a fade effect using intensity scaling
bridadan 0:12cb6f0c2788 246 // compute and write the II value for each pixel
bridadan 2:cb82a3dc4031 247 for (int j=0; j<WS2812_BUF; j++) {
exarkun 3:e59164341b18 248 if(pc.readable()){c = pc.getc();}
bridadan 0:12cb6f0c2788 249 // px.SetI(pixel position, II value)
bridadan 2:cb82a3dc4031 250 px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
bridadan 0:12cb6f0c2788 251 }
bridadan 0:12cb6f0c2788 252
bridadan 0:12cb6f0c2788 253
bridadan 0:12cb6f0c2788 254 // Now the buffer is written, rotate it
bridadan 0:12cb6f0c2788 255 // by writing it out with an increasing offset
exarkun 3:e59164341b18 256 while (1)
exarkun 3:e59164341b18 257 {
exarkun 3:e59164341b18 258 if(pc.readable()){c = pc.getc();}
exarkun 3:e59164341b18 259 if(c=='0'){goto label;}
exarkun 3:e59164341b18 260 for (int z=WS2812_BUF; z >= 0 ; z--)
exarkun 3:e59164341b18 261 {
bridadan 0:12cb6f0c2788 262 ws.write_offsets(px.getBuf(),z,z,z);
exarkun 3:e59164341b18 263 wait(0.075);
exarkun 3:e59164341b18 264 }
bridadan 0:12cb6f0c2788 265 }
exarkun 3:e59164341b18 266 label:
bridadan 0:12cb6f0c2788 267
bridadan 0:12cb6f0c2788 268 }
exarkun 3:e59164341b18 269
exarkun 3:e59164341b18 270
exarkun 3:e59164341b18 271 void task1_switch()
exarkun 3:e59164341b18 272 {
exarkun 3:e59164341b18 273 myled1=1;
exarkun 3:e59164341b18 274 if(pc.readable())
exarkun 3:e59164341b18 275 {
exarkun 3:e59164341b18 276 c = pc.getc();
exarkun 3:e59164341b18 277 }
exarkun 3:e59164341b18 278 myled1=0;
exarkun 3:e59164341b18 279 }
exarkun 3:e59164341b18 280
exarkun 3:e59164341b18 281
exarkun 3:e59164341b18 282
exarkun 3:e59164341b18 283 void task2_switch()
exarkun 3:e59164341b18 284 {
exarkun 3:e59164341b18 285 myled2=1;
exarkun 3:e59164341b18 286 switch(c)//chargement des wafers dans tete
exarkun 3:e59164341b18 287 {
exarkun 3:e59164341b18 288 case '1'://impossible y as pas encore la precision demander c faut y as un probleme .!!!!!!!!!!!!!!!!!!!!!!!!!!
exarkun 3:e59164341b18 289 random_color_position();
exarkun 3:e59164341b18 290 break;
exarkun 3:e59164341b18 291
exarkun 3:e59164341b18 292 case '2':
exarkun 3:e59164341b18 293 deux_point_rouge_k2000();
exarkun 3:e59164341b18 294 break;
exarkun 3:e59164341b18 295
exarkun 3:e59164341b18 296 case '3':
exarkun 3:e59164341b18 297 un_point_rouge_k2000();
exarkun 3:e59164341b18 298 break;
exarkun 3:e59164341b18 299
exarkun 3:e59164341b18 300 case '4':
exarkun 3:e59164341b18 301 chainage_couleur();
exarkun 3:e59164341b18 302 break;
exarkun 3:e59164341b18 303
exarkun 3:e59164341b18 304 }
exarkun 3:e59164341b18 305 myled2=0;
exarkun 3:e59164341b18 306
exarkun 3:e59164341b18 307 }
exarkun 3:e59164341b18 308
exarkun 3:e59164341b18 309 void task3_switch()
exarkun 3:e59164341b18 310 {
exarkun 3:e59164341b18 311 myled3=1;
exarkun 3:e59164341b18 312
exarkun 3:e59164341b18 313 myled3=0;
exarkun 3:e59164341b18 314 }
exarkun 3:e59164341b18 315
exarkun 3:e59164341b18 316
exarkun 3:e59164341b18 317
exarkun 3:e59164341b18 318 void task4_switch()
exarkun 3:e59164341b18 319 {
exarkun 3:e59164341b18 320 myled4=1;
exarkun 3:e59164341b18 321
exarkun 3:e59164341b18 322 myled4=0;
exarkun 3:e59164341b18 323 }
exarkun 3:e59164341b18 324
exarkun 3:e59164341b18 325
exarkun 3:e59164341b18 326 int main()
exarkun 3:e59164341b18 327 {
exarkun 3:e59164341b18 328
exarkun 3:e59164341b18 329
exarkun 3:e59164341b18 330 //lancement des tasks
exarkun 3:e59164341b18 331 time_up1.attach(&task1_switch, 0.5);
exarkun 3:e59164341b18 332 time_up2.attach(&task2_switch, 0.0005);
exarkun 3:e59164341b18 333 time_up3.attach(&task3_switch, 0.5);
exarkun 3:e59164341b18 334 time_up4.attach(&task4_switch, 0.5);
exarkun 3:e59164341b18 335 //while(1)
exarkun 3:e59164341b18 336 {
exarkun 3:e59164341b18 337 // pc.printf("press key...\r\n");
exarkun 3:e59164341b18 338
exarkun 3:e59164341b18 339
exarkun 3:e59164341b18 340 //c=rand()%4+1;//random value
exarkun 3:e59164341b18 341
exarkun 3:e59164341b18 342 }
exarkun 3:e59164341b18 343
exarkun 3:e59164341b18 344 }
exarkun 3:e59164341b18 345