cleaned up verison

Dependencies:   mbed C12832_lcd LCD_fonts NeoStrip

Committer:
slackerjacker
Date:
Tue Apr 20 11:02:36 2021 +0000
Revision:
2:a04589b1a088
Parent:
1:a9f32c0c835d
Child:
3:d8620e883b77
Jack's code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aswild 0:f38492690f0e 1 /*
jasonberry 1:a9f32c0c835d 2 * Adafruit NeoPixel example
aswild 0:f38492690f0e 3 *
aswild 0:f38492690f0e 4 * This program displays a couple simple patterns on an 8x8 NeoPixel matrix.
aswild 0:f38492690f0e 5 *
aswild 0:f38492690f0e 6 */
aswild 0:f38492690f0e 7
aswild 0:f38492690f0e 8 #include "mbed.h"
aswild 0:f38492690f0e 9 #include "NeoStrip.h"
aswild 0:f38492690f0e 10 #include "gt.h"
slackerjacker 2:a04589b1a088 11 #include "C12832_lcd.h"
slackerjacker 2:a04589b1a088 12 #include "Arial_9.h"
slackerjacker 2:a04589b1a088 13 #include "Small_7.h"
aswild 0:f38492690f0e 14
slackerjacker 2:a04589b1a088 15
slackerjacker 2:a04589b1a088 16 #define N 12 //number of leds
jasonberry 1:a9f32c0c835d 17
jasonberry 1:a9f32c0c835d 18
aswild 0:f38492690f0e 19 #define PATTERNS 3
slackerjacker 2:a04589b1a088 20 Serial pc(USBTX, USBRX);
aswild 0:f38492690f0e 21
jasonberry 1:a9f32c0c835d 22 //int hueToRGB(float h);
jasonberry 1:a9f32c0c835d 23 //void pattern0();
slackerjacker 2:a04589b1a088 24 //void pattern1();
jasonberry 1:a9f32c0c835d 25 //void pattern2();
aswild 0:f38492690f0e 26
aswild 0:f38492690f0e 27 // array of function pointers to the various patterns
jasonberry 1:a9f32c0c835d 28 //void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2};
aswild 0:f38492690f0e 29
slackerjacker 2:a04589b1a088 30 NeoStrip strip(p25, N); // DIN PIN with number of LEDs
slackerjacker 2:a04589b1a088 31 NeoStrip led1(p25, 0);
slackerjacker 2:a04589b1a088 32 C12832_LCD LCD("LCD");
slackerjacker 2:a04589b1a088 33 //NeoStrip leds[] = {(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)};
slackerjacker 2:a04589b1a088 34
jasonberry 1:a9f32c0c835d 35 //DigitalIn b1(p20); // brightness up
jasonberry 1:a9f32c0c835d 36 //DigitalIn b2(p19); // brightness down
jasonberry 1:a9f32c0c835d 37 //DigitalIn b3(p22); // next pattern
aswild 0:f38492690f0e 38
aswild 0:f38492690f0e 39 // timer used for debugging
aswild 0:f38492690f0e 40 Timer timer;
aswild 0:f38492690f0e 41
aswild 0:f38492690f0e 42 int main()
aswild 0:f38492690f0e 43 {
jasonberry 1:a9f32c0c835d 44 // b1.mode(PullDown);
jasonberry 1:a9f32c0c835d 45 // b2.mode(PullDown);
jasonberry 1:a9f32c0c835d 46 // b3.mode(PullDown);
aswild 0:f38492690f0e 47
jasonberry 1:a9f32c0c835d 48 //int pattern = 0;
aswild 0:f38492690f0e 49
jasonberry 1:a9f32c0c835d 50 //bool b3o = b3; // old copy of button 3 to poll for changes
jasonberry 1:a9f32c0c835d 51 //int p = 0;
slackerjacker 2:a04589b1a088 52
slackerjacker 2:a04589b1a088 53 ////////////////////////////////////
slackerjacker 2:a04589b1a088 54 //////note colours value below//////
slackerjacker 2:a04589b1a088 55 ////////////////////////////////////
slackerjacker 2:a04589b1a088 56
slackerjacker 2:a04589b1a088 57 float bright = 0.05; // 20% is plenty for indoor use
slackerjacker 2:a04589b1a088 58 int red = 0xFF0000;
slackerjacker 2:a04589b1a088 59 int orange = 0xFF8000;
slackerjacker 2:a04589b1a088 60 int yellow = 0xFFFF00;
slackerjacker 2:a04589b1a088 61 int green = 0x00FF44;
slackerjacker 2:a04589b1a088 62 int blue = 0x00BFFF;
slackerjacker 2:a04589b1a088 63 int indigo = 0x0F0082;
slackerjacker 2:a04589b1a088 64 int violet = 0x8000B4;
slackerjacker 2:a04589b1a088 65 int white = 0xFFFFFF;
slackerjacker 2:a04589b1a088 66 int i = 0;
slackerjacker 2:a04589b1a088 67 int hueToRGB(float h);
slackerjacker 2:a04589b1a088 68
aswild 0:f38492690f0e 69 strip.setBrightness(bright); // set default brightness
slackerjacker 2:a04589b1a088 70
slackerjacker 2:a04589b1a088 71
slackerjacker 2:a04589b1a088 72 pc.printf("1 - Bootup\n\r");
slackerjacker 2:a04589b1a088 73 pc.printf("2 - Bootdown\n\r");
slackerjacker 2:a04589b1a088 74 pc.printf("3 - Clear\n\r");
slackerjacker 2:a04589b1a088 75 pc.printf("4 - Happy\n\r");
slackerjacker 2:a04589b1a088 76 pc.printf("5 - Sad\n\r");
slackerjacker 2:a04589b1a088 77
jasonberry 1:a9f32c0c835d 78 while(true)
slackerjacker 2:a04589b1a088 79
slackerjacker 2:a04589b1a088 80 {if (pc.readable()) {
slackerjacker 2:a04589b1a088 81 switch(pc.getc()) {
slackerjacker 2:a04589b1a088 82 case '1'://bootup sequence
slackerjacker 2:a04589b1a088 83 //strip.setPixels(0, N, test_img);
slackerjacker 2:a04589b1a088 84 LCD.cls();
slackerjacker 2:a04589b1a088 85 {
jasonberry 1:a9f32c0c835d 86 strip.clear();
jasonberry 1:a9f32c0c835d 87 strip.write();
slackerjacker 2:a04589b1a088 88 wait_ms(200);
slackerjacker 2:a04589b1a088 89 strip.setPixel(0,red);
slackerjacker 2:a04589b1a088 90 strip.write();
slackerjacker 2:a04589b1a088 91 wait_ms(200);
slackerjacker 2:a04589b1a088 92
slackerjacker 2:a04589b1a088 93 strip.setPixel(1,orange);
jasonberry 1:a9f32c0c835d 94 strip.write();
slackerjacker 2:a04589b1a088 95 wait_ms(200);
slackerjacker 2:a04589b1a088 96
slackerjacker 2:a04589b1a088 97 strip.setPixel(2,yellow);
slackerjacker 2:a04589b1a088 98 strip.write();
slackerjacker 2:a04589b1a088 99 wait_ms(200);
slackerjacker 2:a04589b1a088 100
slackerjacker 2:a04589b1a088 101 strip.setPixel(3,green);
slackerjacker 2:a04589b1a088 102 strip.write();
slackerjacker 2:a04589b1a088 103 wait_ms(200);
slackerjacker 2:a04589b1a088 104
slackerjacker 2:a04589b1a088 105 strip.setPixel(4,blue);
slackerjacker 2:a04589b1a088 106 strip.write();
slackerjacker 2:a04589b1a088 107 wait_ms(200);
jasonberry 1:a9f32c0c835d 108
slackerjacker 2:a04589b1a088 109 strip.setPixel(5,indigo);
slackerjacker 2:a04589b1a088 110 strip.write();
slackerjacker 2:a04589b1a088 111 wait_ms(200);
slackerjacker 2:a04589b1a088 112
slackerjacker 2:a04589b1a088 113 strip.setPixel(6,violet);
slackerjacker 2:a04589b1a088 114 strip.write();
slackerjacker 2:a04589b1a088 115 wait_ms(200);
slackerjacker 2:a04589b1a088 116
slackerjacker 2:a04589b1a088 117 strip.setPixel(7,indigo);
jasonberry 1:a9f32c0c835d 118 strip.write();
slackerjacker 2:a04589b1a088 119 wait_ms(200);
slackerjacker 2:a04589b1a088 120
slackerjacker 2:a04589b1a088 121 strip.setPixel(8,blue);
slackerjacker 2:a04589b1a088 122 strip.write();
slackerjacker 2:a04589b1a088 123 wait_ms(200);
jasonberry 1:a9f32c0c835d 124
slackerjacker 2:a04589b1a088 125 strip.setPixel(9,green);
slackerjacker 2:a04589b1a088 126 strip.write();
slackerjacker 2:a04589b1a088 127 wait_ms(200);
slackerjacker 2:a04589b1a088 128
slackerjacker 2:a04589b1a088 129 strip.setPixel(10,yellow);
slackerjacker 2:a04589b1a088 130 strip.write();
slackerjacker 2:a04589b1a088 131 wait_ms(200);
slackerjacker 2:a04589b1a088 132
slackerjacker 2:a04589b1a088 133 strip.setPixel(11,orange);
jasonberry 1:a9f32c0c835d 134 strip.write();
slackerjacker 2:a04589b1a088 135 wait_ms(200);
slackerjacker 2:a04589b1a088 136 }
slackerjacker 2:a04589b1a088 137
slackerjacker 2:a04589b1a088 138
slackerjacker 2:a04589b1a088 139
slackerjacker 2:a04589b1a088 140 break;
slackerjacker 2:a04589b1a088 141 case '2'://bootdown sequence
slackerjacker 2:a04589b1a088 142
slackerjacker 2:a04589b1a088 143 LCD.cls();
slackerjacker 2:a04589b1a088 144
slackerjacker 2:a04589b1a088 145 strip.setPixel(11,orange);
slackerjacker 2:a04589b1a088 146 strip.write();
slackerjacker 2:a04589b1a088 147 wait_ms(1);
slackerjacker 2:a04589b1a088 148
slackerjacker 2:a04589b1a088 149
jasonberry 1:a9f32c0c835d 150
slackerjacker 2:a04589b1a088 151 strip.setPixel(10,yellow);
slackerjacker 2:a04589b1a088 152 strip.write();
slackerjacker 2:a04589b1a088 153 wait_ms(1);
slackerjacker 2:a04589b1a088 154
slackerjacker 2:a04589b1a088 155
slackerjacker 2:a04589b1a088 156 strip.setPixel(9,green);
slackerjacker 2:a04589b1a088 157 strip.write();
slackerjacker 2:a04589b1a088 158 wait_ms(1);
slackerjacker 2:a04589b1a088 159
slackerjacker 2:a04589b1a088 160
slackerjacker 2:a04589b1a088 161 strip.setPixel(8,blue);
slackerjacker 2:a04589b1a088 162 strip.write();
slackerjacker 2:a04589b1a088 163 wait_ms(1);
slackerjacker 2:a04589b1a088 164
slackerjacker 2:a04589b1a088 165
slackerjacker 2:a04589b1a088 166 strip.setPixel(7,indigo);
jasonberry 1:a9f32c0c835d 167 strip.write();
slackerjacker 2:a04589b1a088 168 wait_ms(1);
slackerjacker 2:a04589b1a088 169
slackerjacker 2:a04589b1a088 170 strip.setPixel(6,violet);
slackerjacker 2:a04589b1a088 171 strip.write();
slackerjacker 2:a04589b1a088 172 wait_ms(1);
slackerjacker 2:a04589b1a088 173
slackerjacker 2:a04589b1a088 174 strip.setPixel(5,indigo);
slackerjacker 2:a04589b1a088 175 strip.write();
slackerjacker 2:a04589b1a088 176 wait_ms(1);
slackerjacker 2:a04589b1a088 177
slackerjacker 2:a04589b1a088 178 strip.setPixel(4,blue);
slackerjacker 2:a04589b1a088 179 strip.write();
slackerjacker 2:a04589b1a088 180 wait_ms(1);
slackerjacker 2:a04589b1a088 181
slackerjacker 2:a04589b1a088 182 strip.setPixel(3,green);
jasonberry 1:a9f32c0c835d 183 strip.write();
slackerjacker 2:a04589b1a088 184 wait_ms(1);
slackerjacker 2:a04589b1a088 185
slackerjacker 2:a04589b1a088 186 strip.setPixel(2,yellow);
slackerjacker 2:a04589b1a088 187 strip.write();
slackerjacker 2:a04589b1a088 188 wait_ms(1);
slackerjacker 2:a04589b1a088 189
slackerjacker 2:a04589b1a088 190 strip.setPixel(1,orange);
slackerjacker 2:a04589b1a088 191 strip.write();
slackerjacker 2:a04589b1a088 192 wait_ms(1);
slackerjacker 2:a04589b1a088 193
slackerjacker 2:a04589b1a088 194 strip.setPixel(0,red);
slackerjacker 2:a04589b1a088 195 strip.write();
slackerjacker 2:a04589b1a088 196 wait_ms(1);
slackerjacker 2:a04589b1a088 197
slackerjacker 2:a04589b1a088 198 {
slackerjacker 2:a04589b1a088 199 for (int i = 11; i >= 0; i--)
slackerjacker 2:a04589b1a088 200 {strip.setPixel(i,0,0,0);
slackerjacker 2:a04589b1a088 201 strip.write();
slackerjacker 2:a04589b1a088 202 wait_ms(500);
jasonberry 1:a9f32c0c835d 203 }
jasonberry 1:a9f32c0c835d 204 }
slackerjacker 2:a04589b1a088 205
slackerjacker 2:a04589b1a088 206
slackerjacker 2:a04589b1a088 207
slackerjacker 2:a04589b1a088 208
slackerjacker 2:a04589b1a088 209
slackerjacker 2:a04589b1a088 210 break;
slackerjacker 2:a04589b1a088 211 case '3'://clear
slackerjacker 2:a04589b1a088 212 LCD.cls();
slackerjacker 2:a04589b1a088 213 strip.clear();
slackerjacker 2:a04589b1a088 214 strip.write();
slackerjacker 2:a04589b1a088 215
slackerjacker 2:a04589b1a088 216 break;
slackerjacker 2:a04589b1a088 217 case '4':
slackerjacker 2:a04589b1a088 218
slackerjacker 2:a04589b1a088 219 LCD.claim(stdout);
slackerjacker 2:a04589b1a088 220 LCD.locate(10,0);
slackerjacker 2:a04589b1a088 221 LCD.set_font((unsigned char*) Arial_9);
slackerjacker 2:a04589b1a088 222 printf("Happy Bot");
slackerjacker 2:a04589b1a088 223 LCD.copy_to_lcd();
slackerjacker 2:a04589b1a088 224 LCD.set_font((unsigned char*) Small_7);
slackerjacker 2:a04589b1a088 225
slackerjacker 2:a04589b1a088 226
slackerjacker 2:a04589b1a088 227 while(true)
slackerjacker 2:a04589b1a088 228 {
slackerjacker 2:a04589b1a088 229 {
slackerjacker 2:a04589b1a088 230 for (int i = 0; i < N; i++)
slackerjacker 2:a04589b1a088 231 {
slackerjacker 2:a04589b1a088 232 strip.setPixel(i,white);
slackerjacker 2:a04589b1a088 233 strip.write();
slackerjacker 2:a04589b1a088 234 wait_ms(15);
slackerjacker 2:a04589b1a088 235 }
slackerjacker 2:a04589b1a088 236
slackerjacker 2:a04589b1a088 237 //if(white)
slackerjacker 2:a04589b1a088 238 {
slackerjacker 2:a04589b1a088 239 for (int i = 0; i < N; i++)
slackerjacker 2:a04589b1a088 240 {
slackerjacker 2:a04589b1a088 241 strip.setPixel(i,yellow);
slackerjacker 2:a04589b1a088 242 strip.write();
slackerjacker 2:a04589b1a088 243 wait_ms(50);
slackerjacker 2:a04589b1a088 244 }
slackerjacker 2:a04589b1a088 245 //else(yellow)
slackerjacker 2:a04589b1a088 246 {
slackerjacker 2:a04589b1a088 247 for (int i =0; i < N; i++)
slackerjacker 2:a04589b1a088 248 {
slackerjacker 2:a04589b1a088 249 strip.setPixel(i,orange);
slackerjacker 2:a04589b1a088 250 strip.write();
slackerjacker 2:a04589b1a088 251 wait_ms(50);
slackerjacker 2:a04589b1a088 252 }
slackerjacker 2:a04589b1a088 253
slackerjacker 2:a04589b1a088 254 }
slackerjacker 2:a04589b1a088 255 //return 1;
slackerjacker 2:a04589b1a088 256
slackerjacker 2:a04589b1a088 257
slackerjacker 2:a04589b1a088 258
slackerjacker 2:a04589b1a088 259
slackerjacker 2:a04589b1a088 260 //for (int i = 0; i < 4; i++)
slackerjacker 2:a04589b1a088 261 //strip.clear();
slackerjacker 2:a04589b1a088 262 //wait_ms(100);
slackerjacker 2:a04589b1a088 263
slackerjacker 2:a04589b1a088 264
slackerjacker 2:a04589b1a088 265 break;
slackerjacker 2:a04589b1a088 266
slackerjacker 2:a04589b1a088 267 case '5':
slackerjacker 2:a04589b1a088 268 {
slackerjacker 2:a04589b1a088 269 for (int i = 0; i < 12; i++)
slackerjacker 2:a04589b1a088 270 strip.setPixel(i,blue);
slackerjacker 2:a04589b1a088 271 strip.write();
slackerjacker 2:a04589b1a088 272 wait_ms(1);
slackerjacker 2:a04589b1a088 273 }
slackerjacker 2:a04589b1a088 274
slackerjacker 2:a04589b1a088 275 {
slackerjacker 2:a04589b1a088 276 for (int i = 0; i<0.2; i+0.01)
slackerjacker 2:a04589b1a088 277 strip.setBrightness(bright+i);
slackerjacker 2:a04589b1a088 278 wait_ms(20);
slackerjacker 2:a04589b1a088 279 }
slackerjacker 2:a04589b1a088 280
slackerjacker 2:a04589b1a088 281
slackerjacker 2:a04589b1a088 282
slackerjacker 2:a04589b1a088 283
slackerjacker 2:a04589b1a088 284
slackerjacker 2:a04589b1a088 285
slackerjacker 2:a04589b1a088 286 break;
slackerjacker 2:a04589b1a088 287
slackerjacker 2:a04589b1a088 288
slackerjacker 2:a04589b1a088 289 // case '5':
slackerjacker 2:a04589b1a088 290
slackerjacker 2:a04589b1a088 291 {
slackerjacker 2:a04589b1a088 292 static float dh = 360.0 / N;
slackerjacker 2:a04589b1a088 293 static float x = 0;
slackerjacker 2:a04589b1a088 294
slackerjacker 2:a04589b1a088 295 for (int i = 0; i < N; i++)
slackerjacker 2:a04589b1a088 296 strip.setPixel(i, hueToRGB((dh * i) - x));
slackerjacker 2:a04589b1a088 297
slackerjacker 2:a04589b1a088 298 x += 1;
slackerjacker 2:a04589b1a088 299 if (x > 360)
slackerjacker 2:a04589b1a088 300 x = 0;
slackerjacker 2:a04589b1a088 301 }
slackerjacker 2:a04589b1a088 302
slackerjacker 2:a04589b1a088 303 // break;
slackerjacker 2:a04589b1a088 304 //led1 = 1;
slackerjacker 2:a04589b1a088 305 //wait(0.5);
slackerjacker 2:a04589b1a088 306 //strip.write();
slackerjacker 2:a04589b1a088 307 //led1 = !1;
slackerjacker 2:a04589b1a088 308 //wait(0.5);
slackerjacker 2:a04589b1a088 309 //strip.write();
slackerjacker 2:a04589b1a088 310
slackerjacker 2:a04589b1a088 311
slackerjacker 2:a04589b1a088 312 // PwmOut strip();
slackerjacker 2:a04589b1a088 313 // strip.setPixel.write(0,red,brightness));
slackerjacker 2:a04589b1a088 314
slackerjacker 2:a04589b1a088 315
slackerjacker 2:a04589b1a088 316
slackerjacker 2:a04589b1a088 317
slackerjacker 2:a04589b1a088 318 // wait_ms(30);
slackerjacker 2:a04589b1a088 319 // brightness = brightness + fadeAmount;
slackerjacker 2:a04589b1a088 320
slackerjacker 2:a04589b1a088 321 //if((int)brightness == 1.0)
slackerjacker 2:a04589b1a088 322 // brightness = 0;
slackerjacker 2:a04589b1a088 323 //{
slackerjacker 2:a04589b1a088 324
slackerjacker 2:a04589b1a088 325
slackerjacker 2:a04589b1a088 326
slackerjacker 2:a04589b1a088 327
slackerjacker 2:a04589b1a088 328 //}
jasonberry 1:a9f32c0c835d 329
jasonberry 1:a9f32c0c835d 330
jasonberry 1:a9f32c0c835d 331
jasonberry 1:a9f32c0c835d 332
jasonberry 1:a9f32c0c835d 333
jasonberry 1:a9f32c0c835d 334
jasonberry 1:a9f32c0c835d 335
jasonberry 1:a9f32c0c835d 336
jasonberry 1:a9f32c0c835d 337
jasonberry 1:a9f32c0c835d 338
jasonberry 1:a9f32c0c835d 339
jasonberry 1:a9f32c0c835d 340
jasonberry 1:a9f32c0c835d 341
jasonberry 1:a9f32c0c835d 342
jasonberry 1:a9f32c0c835d 343
jasonberry 1:a9f32c0c835d 344
jasonberry 1:a9f32c0c835d 345
jasonberry 1:a9f32c0c835d 346
jasonberry 1:a9f32c0c835d 347
jasonberry 1:a9f32c0c835d 348
jasonberry 1:a9f32c0c835d 349
jasonberry 1:a9f32c0c835d 350
jasonberry 1:a9f32c0c835d 351
jasonberry 1:a9f32c0c835d 352
jasonberry 1:a9f32c0c835d 353
jasonberry 1:a9f32c0c835d 354
jasonberry 1:a9f32c0c835d 355 /*while (true)
aswild 0:f38492690f0e 356 {
aswild 0:f38492690f0e 357 timer.reset(); // use a timer to measure loop execution time for debugging purposes
aswild 0:f38492690f0e 358 timer.start(); // for this application, the main loop takes approximately 3ms to run
aswild 0:f38492690f0e 359
aswild 0:f38492690f0e 360 // button 1 increases brightness
jasonberry 1:a9f32c0c835d 361 if(0) //(b1 && bright < 1)
aswild 0:f38492690f0e 362 {
aswild 0:f38492690f0e 363 bright += 0.01;
aswild 0:f38492690f0e 364 if (bright > 1)
aswild 0:f38492690f0e 365 bright = 1;
aswild 0:f38492690f0e 366 strip.setBrightness(bright);
aswild 0:f38492690f0e 367 }
aswild 0:f38492690f0e 368
aswild 0:f38492690f0e 369 // button 2 decreases brightness
jasonberry 1:a9f32c0c835d 370 if (0)//(b2 && bright > 0)
aswild 0:f38492690f0e 371 {
aswild 0:f38492690f0e 372 bright -= 0.01;
aswild 0:f38492690f0e 373 if (bright < 0)
aswild 0:f38492690f0e 374 bright = 0;
aswild 0:f38492690f0e 375 strip.setBrightness(bright);
aswild 0:f38492690f0e 376 }
aswild 0:f38492690f0e 377
aswild 0:f38492690f0e 378 // button 3 changes the pattern, only do stuff when its state has changed
jasonberry 1:a9f32c0c835d 379 if (0)//(b3 != b3o)
aswild 0:f38492690f0e 380 {
aswild 0:f38492690f0e 381 if (b3 && ++pattern == PATTERNS)
aswild 0:f38492690f0e 382 pattern = 0;
aswild 0:f38492690f0e 383 b3o = b3;
aswild 0:f38492690f0e 384 }
aswild 0:f38492690f0e 385
aswild 0:f38492690f0e 386 // run the pattern update function which sets the strip's pixels
aswild 0:f38492690f0e 387 patterns[pattern]();
aswild 0:f38492690f0e 388 strip.write();
aswild 0:f38492690f0e 389
aswild 0:f38492690f0e 390 timer.stop();
aswild 0:f38492690f0e 391 // print loop time if b3 is pressed
aswild 0:f38492690f0e 392 if (b3)
aswild 0:f38492690f0e 393 printf("Loop Time: %dus\n", timer.read_us());
aswild 0:f38492690f0e 394
aswild 0:f38492690f0e 395 wait_ms(10);
aswild 0:f38492690f0e 396 }
jasonberry 1:a9f32c0c835d 397 }*/
aswild 0:f38492690f0e 398
aswild 0:f38492690f0e 399 // pattern0 displays a static image
jasonberry 1:a9f32c0c835d 400 /*void pattern0()
aswild 0:f38492690f0e 401 {
aswild 0:f38492690f0e 402 strip.setPixels(0, N, gt_img);
aswild 0:f38492690f0e 403 }
aswild 0:f38492690f0e 404
aswild 0:f38492690f0e 405 // display a shifting rainbow, all colors have maximum
aswild 0:f38492690f0e 406 // saturation and value, with evenly spaced hue
aswild 0:f38492690f0e 407 void pattern1()
aswild 0:f38492690f0e 408 {
aswild 0:f38492690f0e 409 static float dh = 360.0 / N;
aswild 0:f38492690f0e 410 static float x = 0;
aswild 0:f38492690f0e 411
aswild 0:f38492690f0e 412 for (int i = 0; i < N; i++)
aswild 0:f38492690f0e 413 strip.setPixel(i, hueToRGB((dh * i) - x));
aswild 0:f38492690f0e 414
aswild 0:f38492690f0e 415 x += 1;
aswild 0:f38492690f0e 416 if (x > 360)
aswild 0:f38492690f0e 417 x = 0;
aswild 0:f38492690f0e 418 }
aswild 0:f38492690f0e 419
aswild 0:f38492690f0e 420 // display a shifting gradient between red and blue
aswild 0:f38492690f0e 421 void pattern2()
aswild 0:f38492690f0e 422 {
aswild 0:f38492690f0e 423 // offset for each pixel to allow the pattern to move
aswild 0:f38492690f0e 424 static float x = 0;
aswild 0:f38492690f0e 425
aswild 0:f38492690f0e 426 float r, b, y;
aswild 0:f38492690f0e 427
aswild 0:f38492690f0e 428 for (int i = 0; i < N; i++)
aswild 0:f38492690f0e 429 {
aswild 0:f38492690f0e 430 // y is a scaled position between 0 (red) and 1.0 (blue)
aswild 0:f38492690f0e 431 y = 1.0 * i / (N - 1) + x;
aswild 0:f38492690f0e 432 if (y > 1)
aswild 0:f38492690f0e 433 y -= 1;
aswild 0:f38492690f0e 434
aswild 0:f38492690f0e 435 // if on the left half, red is decreasing and blue is increasng
aswild 0:f38492690f0e 436 if (y < 0.5)
aswild 0:f38492690f0e 437 {
aswild 0:f38492690f0e 438 b = 2 * y;
aswild 0:f38492690f0e 439 r = 1 - b;
aswild 0:f38492690f0e 440 }
aswild 0:f38492690f0e 441
aswild 0:f38492690f0e 442 // else red is increasing and blue is decreasing
aswild 0:f38492690f0e 443 else
aswild 0:f38492690f0e 444 {
aswild 0:f38492690f0e 445 r = 2 * (y - 0.5);
aswild 0:f38492690f0e 446 b = 1 - r;
aswild 0:f38492690f0e 447 }
aswild 0:f38492690f0e 448
aswild 0:f38492690f0e 449 // scale to integers and set the pixel
aswild 0:f38492690f0e 450 strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200));
aswild 0:f38492690f0e 451 }
aswild 0:f38492690f0e 452
aswild 0:f38492690f0e 453 x += 0.003;
aswild 0:f38492690f0e 454 if (x > 1)
aswild 0:f38492690f0e 455 x = 0;
aswild 0:f38492690f0e 456 }
aswild 0:f38492690f0e 457
aswild 0:f38492690f0e 458 // Converts HSV to RGB with the given hue, assuming
aswild 0:f38492690f0e 459 // maximum saturation and value
aswild 0:f38492690f0e 460 int hueToRGB(float h)
aswild 0:f38492690f0e 461 {
aswild 0:f38492690f0e 462 // lots of floating point magic from the internet and scratching my head
aswild 0:f38492690f0e 463 float r, g, b;
aswild 0:f38492690f0e 464 if (h > 360)
aswild 0:f38492690f0e 465 h -= 360;
aswild 0:f38492690f0e 466 if (h < 0)
aswild 0:f38492690f0e 467 h += 360;
aswild 0:f38492690f0e 468 int i = (int)(h / 60.0);
aswild 0:f38492690f0e 469 float f = (h / 60.0) - i;
aswild 0:f38492690f0e 470 float q = 1 - f;
aswild 0:f38492690f0e 471
aswild 0:f38492690f0e 472 switch (i % 6)
aswild 0:f38492690f0e 473 {
aswild 0:f38492690f0e 474 case 0: r = 1; g = f; b = 0; break;
aswild 0:f38492690f0e 475 case 1: r = q; g = 1; b = 0; break;
aswild 0:f38492690f0e 476 case 2: r = 0; g = 1; b = f; break;
aswild 0:f38492690f0e 477 case 3: r = 0; g = q; b = 1; break;
aswild 0:f38492690f0e 478 case 4: r = f; g = 0; b = 1; break;
aswild 0:f38492690f0e 479 case 5: r = 1; g = 0; b = q; break;
aswild 0:f38492690f0e 480 default: r = 0; g = 0; b = 0; break;
aswild 0:f38492690f0e 481 }
aswild 0:f38492690f0e 482
aswild 0:f38492690f0e 483 // scale to integers and return the packed value
aswild 0:f38492690f0e 484 uint8_t R = (uint8_t)(r * 255);
aswild 0:f38492690f0e 485 uint8_t G = (uint8_t)(g * 255);
aswild 0:f38492690f0e 486 uint8_t B = (uint8_t)(b * 255);
aswild 0:f38492690f0e 487
aswild 0:f38492690f0e 488 return (R << 16) | (G << 8) | B;
aswild 0:f38492690f0e 489 }
slackerjacker 2:a04589b1a088 490 */}}
slackerjacker 2:a04589b1a088 491 }}}}}