work fine

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SomeCartoon.cpp Source File

SomeCartoon.cpp

00001 #include "SomeCartoon.h"
00002 #include "mbed.h"
00003 #include "Microduino_Matrix.h"
00004 
00005 extern Matrix           display;
00006 extern AnalogIn         gAnalogIn;
00007 void randomSeed(unsigned long seed)
00008 {
00009     if (seed != 0) {
00010         //srandom(seed);
00011         srand(seed);
00012     }
00013 }
00014 
00015 long random(long howbig)
00016 {
00017     if (howbig == 0) {
00018         return 0;
00019     }
00020     return rand() % howbig;
00021 }
00022 
00023 long random(long howsmall, long howbig)
00024 {
00025     if (howsmall >= howbig) {
00026         return howsmall;
00027     }
00028     long diff = howbig - howsmall;
00029     return random(diff) + howsmall;
00030 }
00031 /*
00032 uint8_t donghua[128] = {
00033     0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0,
00034     7,1, 7,2, 7,3, 7,4, 7,5, 7,6, 7,7,
00035     6,7, 5,7, 4,7, 3,7, 2,7, 1,7, 0,7,
00036     0,6, 0,5, 0,4, 0,3, 0,2, 0,1,
00037     1,1, 2,1, 3,1, 4,1, 5,1, 6,1,
00038 };*/
00039 
00040 void drawCartoon01(void)
00041 {
00042     uint8_t r,g,b;
00043     randomSeed(gAnalogIn.read_u16());
00044     display.clearDisplay();
00045     int8_t x = 0;
00046     int8_t y = 0;
00047     int8_t xMax = 7;
00048     int8_t yMax = 7;
00049     int8_t xMin = 0;
00050     int8_t yMin = 0;
00051     uint8_t state = 0;
00052     r = random(0, 255);
00053     g = random(0, 255);
00054     b = random(0, 255);
00055     uint8_t times = 0;
00056     while (true) {
00057         display.setLedColor(x, y, r, g, b);   //x, y, r, g, b
00058         wait_ms(50);
00059         if (x == 3 && y == 4) {
00060             times++;
00061             if (times >=3) {
00062                 break;
00063             }
00064             x = 0;
00065             y = 0;
00066             xMax = 7;
00067             yMax = 7;
00068             xMin = 0;
00069             yMin = 0;
00070             display.clearDisplay();
00071             wait_ms(200);
00072             state = 0;
00073             r = random(0, 255);
00074             g = random(0, 255);
00075             b = random(0, 255);
00076             display.setLedColor(0, 0, r, g, b);   //x, y, r, g, b
00077             //continue;
00078         }
00079 
00080         if (state == 0) {
00081             x++;
00082             if (x > xMax) {
00083                 state = 1;
00084                 x = xMax;
00085                 y++;
00086             }
00087         } else if (state == 1) {
00088             y++;
00089             if (y > yMax) {
00090                 state = 2;
00091                 y = yMax;
00092                 x--;
00093             }
00094         } else if (state == 2) {
00095             x--;
00096             if (x < xMin) {
00097                 state = 3;
00098                 x = xMin;
00099                 y--;
00100             }
00101         } else if (state == 3) {
00102             y--;
00103             if (y == yMin) {
00104                 state = 0;
00105                 y = yMin+1;
00106                 x++;
00107                 xMax--;
00108                 yMax--;
00109                 xMin++;
00110                 yMin++;
00111             }
00112         }
00113     }
00114 }
00115 
00116 void drawCartoon02(void)
00117 {
00118     uint8_t times = 0;
00119     int8_t x,y,x1,y1;
00120 
00121 doCartoon:
00122     display.setColor(random(0, 255), random(0, 255), random(0, 255));
00123     x = 0;
00124     y = 0;
00125     x1 = 7;
00126     y1 = 7;
00127     while (x < 8) {
00128         display.drawLine(x, y, x1, y1);//x,y,x1,y1
00129         wait_ms(200);
00130         x++;
00131         y1--;
00132     }
00133     x = 0;
00134     y = 1;
00135     x1 = 6;
00136     y1 = 7;
00137     while (y < 8) {
00138         display.drawLine(x, y, x1, y1);//x,y,x1,y1
00139         wait_ms(200);
00140         y++;
00141         x1--;
00142     }
00143     times++;
00144     if (times >= 3) {
00145         return;
00146     }
00147     wait_us(2000000);
00148     display.clearDisplay();
00149     goto doCartoon;
00150 }
00151 
00152 void drawCartoon03(void)
00153 {
00154     uint8_t times = 0;
00155     int8_t x = 0;
00156     int8_t y = 0;
00157     int8_t w = 8;
00158     int8_t h = 8;
00159 doCartoon:
00160     display.setColor(random(0, 255), random(0, 255), random(0, 255));
00161     while (x < 4) {
00162         display.clearDisplay();
00163         display.drawBox(x, y, w, h);  //x,y,w,h
00164         wait_ms(500);
00165         x++;
00166         y++;
00167         w -= 2;
00168         h -= 2;
00169     }
00170     display.clearDisplay();
00171     times++;
00172     if (times >= 3) {
00173         return;
00174     }
00175     wait_ms(2000);
00176     x = y = 0;
00177     w = h = 8;
00178     goto doCartoon;
00179 }