cleaned up verison
Dependencies: mbed C12832_lcd LCD_fonts NeoStrip
Diff: main.cpp
- Revision:
- 3:d8620e883b77
- Parent:
- 2:a04589b1a088
--- a/main.cpp Tue Apr 20 11:02:36 2021 +0000 +++ b/main.cpp Tue Apr 20 13:11:03 2021 +0000 @@ -14,60 +14,62 @@ #define N 12 //number of leds - - #define PATTERNS 3 -Serial pc(USBTX, USBRX); -//int hueToRGB(float h); -//void pattern0(); -//void pattern1(); -//void pattern2(); - -// array of function pointers to the various patterns -//void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; - +Serial pc(USBTX, USBRX); NeoStrip strip(p25, N); // DIN PIN with number of LEDs NeoStrip led1(p25, 0); -C12832_LCD LCD("LCD"); -//NeoStrip leds[] = {(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)}; + +//////////////////////////////////// +//////note colours value below////// +//////////////////////////////////// + +float bright = 0.20; // 20% is plenty for indoor use +int red = 0xFF0000; +int orange = 0xFF8000; +int yellow = 0xFFFF00; +int green = 0x00FF44; +int blue = 0x00BFFF; +int indigo = 0x0F0082; +int violet = 0x8000B4; +int white = 0xFFFFFF; +int i = 0; +int hueToRGB(float h); -//DigitalIn b1(p20); // brightness up -//DigitalIn b2(p19); // brightness down -//DigitalIn b3(p22); // next pattern + +//C12832_LCD LCD("LCD"); +//C12832_LCD lcd(p5, p7, p6, p8, p11); //jtb + +//variable that hold 1ms timer count, this can used in main to time stuff +int play_led_1ms_timer = 0; + +//set up a ticker funtion +////////////////////////////////////////////////////////////////////////// + +Ticker play_led_1ms_ticker; -// timer used for debugging -Timer timer; +//this funtion runs every ms and increments lay_led_1ms_timer by 1 +void play_led_1ms(void) +{ +play_led_1ms_timer = play_led_1ms_timer+1; + +//stop overflow +if(play_led_1ms_timer> 1000000) + { + play_led_1ms_timer = 0; + } +} int main() { -// b1.mode(PullDown); -// b2.mode(PullDown); -// b3.mode(PullDown); - - //int pattern = 0; - - //bool b3o = b3; // old copy of button 3 to poll for changes - //int p = 0; - - //////////////////////////////////// - //////note colours value below////// - //////////////////////////////////// - - float bright = 0.05; // 20% is plenty for indoor use - int red = 0xFF0000; - int orange = 0xFF8000; - int yellow = 0xFFFF00; - int green = 0x00FF44; - int blue = 0x00BFFF; - int indigo = 0x0F0082; - int violet = 0x8000B4; - int white = 0xFFFFFF; - int i = 0; - int hueToRGB(float h); + // set up a function to play every 1.5 sec + play_led_1ms_ticker.attach(&play_led_1ms, 0.001); + +///////////////////////////////////////////////////////////////////////////// - strip.setBrightness(bright); // set default brightness - + strip.setBrightness(bright); // set default brightness + strip.clear(); + strip.write(); pc.printf("1 - Bootup\n\r"); pc.printf("2 - Bootdown\n\r"); @@ -76,416 +78,206 @@ pc.printf("5 - Sad\n\r"); while(true) + { + if (pc.readable()) + { + switch(pc.getc()) + { + case '1'://bootup sequence + + //LCD.cls(); + strip.clear(); + strip.write(); + wait_ms(200); - {if (pc.readable()) { - switch(pc.getc()) { - case '1'://bootup sequence -//strip.setPixels(0, N, test_img); - LCD.cls(); - { - strip.clear(); - strip.write(); - wait_ms(200); - strip.setPixel(0,red); - strip.write(); - wait_ms(200); - - strip.setPixel(1,orange); - strip.write(); - wait_ms(200); - - strip.setPixel(2,yellow); - strip.write(); - wait_ms(200); - - strip.setPixel(3,green); - strip.write(); - wait_ms(200); - - strip.setPixel(4,blue); - strip.write(); - wait_ms(200); - - strip.setPixel(5,indigo); - strip.write(); - wait_ms(200); - - strip.setPixel(6,violet); - strip.write(); - wait_ms(200); - - strip.setPixel(7,indigo); - strip.write(); - wait_ms(200); - - strip.setPixel(8,blue); - strip.write(); - wait_ms(200); - - strip.setPixel(9,green); - strip.write(); - wait_ms(200); - - strip.setPixel(10,yellow); - strip.write(); - wait_ms(200); - - strip.setPixel(11,orange); - strip.write(); - wait_ms(200); -} - + strip.setPixel(0,red); + strip.write(); + wait_ms(200); + + strip.setPixel(1,orange); + strip.write(); + wait_ms(200); + + strip.setPixel(2,yellow); + strip.write(); + wait_ms(200); + + strip.setPixel(3,green); + strip.write(); + wait_ms(200); + + strip.setPixel(4,blue); + strip.write(); + wait_ms(200); + + strip.setPixel(5,indigo); + strip.write(); + wait_ms(200); + + strip.setPixel(6,violet); + strip.write(); + wait_ms(200); + + strip.setPixel(7,indigo); + strip.write(); + wait_ms(200); + + strip.setPixel(8,blue); + strip.write(); + wait_ms(200); - - break; - case '2'://bootdown sequence - - LCD.cls(); - - strip.setPixel(11,orange); - strip.write(); - wait_ms(1); + strip.setPixel(9,green); + strip.write(); + wait_ms(200); + + strip.setPixel(10,yellow); + strip.write(); + wait_ms(200); - - - strip.setPixel(10,yellow); - strip.write(); - wait_ms(1); - - - strip.setPixel(9,green); - strip.write(); - wait_ms(1); + strip.setPixel(11,orange); + strip.write(); + wait_ms(200); + + break; + + case '2'://bootdown sequence + + //LCD.cls(); - - strip.setPixel(8,blue); - strip.write(); - wait_ms(1); + strip.setPixel(11,orange); + strip.write(); + wait_ms(1); - - strip.setPixel(7,indigo); - strip.write(); - wait_ms(1); + strip.setPixel(10,yellow); + strip.write(); + wait_ms(1); - strip.setPixel(6,violet); - strip.write(); - wait_ms(1); + strip.setPixel(9,green); + strip.write(); + wait_ms(1); - strip.setPixel(5,indigo); - strip.write(); - wait_ms(1); + strip.setPixel(8,blue); + strip.write(); + wait_ms(1); - strip.setPixel(4,blue); - strip.write(); - wait_ms(1); + strip.setPixel(7,indigo); + strip.write(); + wait_ms(1); - strip.setPixel(3,green); - strip.write(); - wait_ms(1); - - strip.setPixel(2,yellow); - strip.write(); - wait_ms(1); + strip.setPixel(6,violet); + strip.write(); + wait_ms(1); - strip.setPixel(1,orange); - strip.write(); - wait_ms(1); + strip.setPixel(5,indigo); + strip.write(); + wait_ms(1); - strip.setPixel(0,red); - strip.write(); - wait_ms(1); + strip.setPixel(4,blue); + strip.write(); + wait_ms(1); -{ - for (int i = 11; i >= 0; i--) - {strip.setPixel(i,0,0,0); - strip.write(); - wait_ms(500); - } -} - - - - + strip.setPixel(3,green); + strip.write(); + wait_ms(1); - break; - case '3'://clear - LCD.cls(); - strip.clear(); - strip.write(); - - break; - case '4': + strip.setPixel(2,yellow); + strip.write(); + wait_ms(1); - LCD.claim(stdout); - LCD.locate(10,0); - LCD.set_font((unsigned char*) Arial_9); - printf("Happy Bot"); - LCD.copy_to_lcd(); - LCD.set_font((unsigned char*) Small_7); - + strip.setPixel(1,orange); + strip.write(); + wait_ms(1); -while(true) -{ -{ - for (int i = 0; i < N; i++) - { - strip.setPixel(i,white); - strip.write(); - wait_ms(15); - } + strip.setPixel(0,red); + strip.write(); + wait_ms(1); + + for (int i = 11; i >= 0; i--) + { + strip.setPixel(i,0,0,0); + strip.write(); + wait_ms(500); + } + + break; + + case '3'://clear + //LCD.cls(); + strip.clear(); + strip.write(); + + break; + + case '4': -//if(white) -{ - for (int i = 0; i < N; i++) - { - strip.setPixel(i,yellow); - strip.write(); - wait_ms(50); - } -//else(yellow) -{ - for (int i =0; i < N; i++) - { - strip.setPixel(i,orange); - strip.write(); - wait_ms(50); - } + //LCD.claim(stdout); + //LCD.locate(10,0); + //LCD.set_font((unsigned char*) Arial_9); + //printf("Happy Bot"); + //LCD.copy_to_lcd(); + //LCD.set_font((unsigned char*) Small_7); -} -//return 1; - - - - -//for (int i = 0; i < 4; i++) -//strip.clear(); -//wait_ms(100); - + //reset timer value to 0 + ////////////////////////////////////// + play_led_1ms_timer= 0; + + //repeat do while for 2000 ms see line 247 for while part + // this code will repeat untill play_led_1ms_timer > 2000 + do + { + for (int i = 0; i < N; i++) + { + strip.setPixel(i,white); + strip.write(); + wait_ms(30); + } - break; - - case '5': -{ - for (int i = 0; i < 12; i++) - strip.setPixel(i,blue); - strip.write(); - wait_ms(1); - } - -{ - for (int i = 0; i<0.2; i+0.01) - strip.setBrightness(bright+i); - wait_ms(20); - } - - - - - + for (int i = 0; i < N; i++) + { + strip.setPixel(i,yellow); + strip.write(); + wait_ms(30); + } + + for (int i =0; i < N; i++) + { + strip.setPixel(i,orange); + strip.write(); + wait_ms(30); + } + } + while(play_led_1ms_timer < 2000); //timer value you want here i.e 2000 2s, 3500 3.5s etc + + strip.clear(); + strip.write(); + + break; - break; - - - // case '5': - -{ - static float dh = 360.0 / N; - static float x = 0; - - for (int i = 0; i < N; i++) - strip.setPixel(i, hueToRGB((dh * i) - x)); - - x += 1; - if (x > 360) - x = 0; -} - -// break; -//led1 = 1; -//wait(0.5); -//strip.write(); -//led1 = !1; -//wait(0.5); -//strip.write(); - - - // PwmOut strip(); - // strip.setPixel.write(0,red,brightness)); - - - - - // wait_ms(30); - // brightness = brightness + fadeAmount; - - //if((int)brightness == 1.0) - // brightness = 0; - //{ - - - - -//} - - - - - - - - - - - - - - - - - - - - - - - - - + case '5': - /*while (true) - { - timer.reset(); // use a timer to measure loop execution time for debugging purposes - timer.start(); // for this application, the main loop takes approximately 3ms to run - - // button 1 increases brightness - if(0) //(b1 && bright < 1) - { - bright += 0.01; - if (bright > 1) - bright = 1; - strip.setBrightness(bright); - } - - // button 2 decreases brightness - if (0)//(b2 && bright > 0) - { - bright -= 0.01; - if (bright < 0) - bright = 0; - strip.setBrightness(bright); - } - - // button 3 changes the pattern, only do stuff when its state has changed - if (0)//(b3 != b3o) - { - if (b3 && ++pattern == PATTERNS) - pattern = 0; - b3o = b3; - } - - // run the pattern update function which sets the strip's pixels - patterns[pattern](); - strip.write(); - - timer.stop(); - // print loop time if b3 is pressed - if (b3) - printf("Loop Time: %dus\n", timer.read_us()); - - wait_ms(10); - } -}*/ - -// pattern0 displays a static image -/*void pattern0() -{ - strip.setPixels(0, N, gt_img); -} - -// display a shifting rainbow, all colors have maximum -// saturation and value, with evenly spaced hue -void pattern1() -{ - static float dh = 360.0 / N; - static float x = 0; - - for (int i = 0; i < N; i++) - strip.setPixel(i, hueToRGB((dh * i) - x)); - - x += 1; - if (x > 360) - x = 0; -} - -// display a shifting gradient between red and blue -void pattern2() -{ - // offset for each pixel to allow the pattern to move - static float x = 0; - - float r, b, y; - - for (int i = 0; i < N; i++) - { - // y is a scaled position between 0 (red) and 1.0 (blue) - y = 1.0 * i / (N - 1) + x; - if (y > 1) - y -= 1; - - // if on the left half, red is decreasing and blue is increasng - if (y < 0.5) - { - b = 2 * y; - r = 1 - b; - } - - // else red is increasing and blue is decreasing - else - { - r = 2 * (y - 0.5); - b = 1 - r; - } - - // scale to integers and set the pixel - strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200)); - } - - x += 0.003; - if (x > 1) - x = 0; -} - -// Converts HSV to RGB with the given hue, assuming -// maximum saturation and value -int hueToRGB(float h) -{ - // lots of floating point magic from the internet and scratching my head - float r, g, b; - if (h > 360) - h -= 360; - if (h < 0) - h += 360; - int i = (int)(h / 60.0); - float f = (h / 60.0) - i; - float q = 1 - f; - - switch (i % 6) - { - case 0: r = 1; g = f; b = 0; break; - case 1: r = q; g = 1; b = 0; break; - case 2: r = 0; g = 1; b = f; break; - case 3: r = 0; g = q; b = 1; break; - case 4: r = f; g = 0; b = 1; break; - case 5: r = 1; g = 0; b = q; break; - default: r = 0; g = 0; b = 0; break; - } - - // scale to integers and return the packed value - uint8_t R = (uint8_t)(r * 255); - uint8_t G = (uint8_t)(g * 255); - uint8_t B = (uint8_t)(b * 255); - - return (R << 16) | (G << 8) | B; -} -*/}} -}}}}} + for (float j = 0; j < 0.3; j=j+0.01) //float jack float i :-) , i = i+0.01 + { + //pc.printf("%f ",j); debug + strip.setBrightness(j); + + //need to write to pixcel to change brightness + for (int i = 0; i < 12; i++) + { + strip.setPixel(i,blue); + strip.write(); + wait_ms(1); + } + wait_ms(200); + } + + strip.clear(); + strip.write(); + + break; + }//End of switch + + }//end of if readable + + }//end of while(1) + +}//end of main()