work fine

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
lixianyu
Date:
Thu Jun 02 01:07:50 2016 +0000
Parent:
1:14b7c3a3ec60
Commit message:
drawBMP work.

Changed in this revision

Microduino_Matrix.cpp Show annotated file Show diff for this revision Revisions of this file
Microduino_MatrixBase.cpp Show annotated file Show diff for this revision Revisions of this file
SomeCartoon.cpp Show annotated file Show diff for this revision Revisions of this file
SomeCartoon.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 14b7c3a3ec60 -r 487a727d6181 Microduino_Matrix.cpp
--- a/Microduino_Matrix.cpp	Wed Jun 01 13:38:30 2016 +0000
+++ b/Microduino_Matrix.cpp	Thu Jun 02 01:07:50 2016 +0000
@@ -30,6 +30,8 @@
 #include "Microduino_Matrix.h"
 #include "Fonts.h"
 
+extern Serial pc;
+
 #define read16(Y,Z)  (uint16_t)((uint8_t)pgm_read_byte((Y) + (Z++)) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 8))
 #define read32(Y,Z) (uint32_t)((uint8_t)pgm_read_byte((Y) + (Z++)) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 8) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 16) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 24))
 #define BUFFPIXEL (MatrixPix_X * 8 * 3)
@@ -523,6 +525,7 @@
     }
 }
 
+#if 0
 bool Matrix::drawBMP(int16_t x, int16_t y, const uint8_t *bitmap)
 {
     uint32_t _dataNum = 0;
@@ -590,6 +593,96 @@
     }
     return true;
 }
+#else
+uint8_t  _dataBuffer[BUFFPIXEL]; //pixel buffer (R+G+B per pixel)
+bool Matrix::drawBMP(int16_t x, int16_t y, const uint8_t *bitmap)
+{
+    uint32_t _dataNum = 0;
+    uint32_t tempData = 0;
+    
+    pc.printf("Enter drawBMP()\r\n");
+    //Parse BMP header
+    //if (read16((uint8_t*)bitmap, _dataNum) == 0x4D42) { //BMP signature
+    if ((bitmap[_dataNum++] == 0x42) && (bitmap[_dataNum++] == 0x4D)) { //BMP signature
+        pc.printf("Enter if\r\n");
+        //(void)read32((uint8_t*)bitmap, _dataNum); //File size
+        _dataNum += 4;  //File size
+        //(void)read32((uint8_t*)bitmap, _dataNum); //Read & ignore creator bytes
+        _dataNum += 4; //Read & ignore creator bytes
+        //uint32_t bmpImageoffset = read32((uint8_t*)bitmap, _dataNum); //Start of image data in file
+        uint32_t bmpImageoffset = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24; //Start of image data in file
+        _dataNum += 4;
+        pc.printf("bmpImageoffset = %u\r\n", bmpImageoffset);
+        //Read DIB header
+        //(void)read32((uint8_t*)bitmap, _dataNum);  //Header size
+        _dataNum += 4;  //Header size
+        //int bmpWidth  = read32((uint8_t*)bitmap, _dataNum);
+        int bmpWidth  = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24;
+        _dataNum += 4;
+        //int bmpHeight = read32((uint8_t*)bitmap, _dataNum);
+        int bmpHeight = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24;
+        _dataNum += 4;
+
+        bool  flip = true;      //BMP is stored bottom-to-top
+        //If bmpHeight is negative, image is in top-down order.
+        if (bmpHeight < 0) {
+            bmpHeight = -bmpHeight;
+            flip = false;
+        }
+
+        //if (read16((uint8_t*)bitmap, _dataNum) == 1) { //# planes -- must be '1'
+        if ((bitmap[_dataNum] | bitmap[_dataNum+1]<<8) == 1) { //# planes -- must be '1'
+            _dataNum += 2;
+            //uint8_t bmpDepth = read16((uint8_t*)bitmap, _dataNum); //Bit depth (currently must be 24)
+            uint8_t bmpDepth = bitmap[_dataNum] | bitmap[_dataNum+1]<<8; //Bit depth (currently must be 24)
+            _dataNum += 2;
+            tempData = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24;
+            _dataNum += 4;
+            //if ((bmpDepth == 24) && (read32((uint8_t*)bitmap, _dataNum) == 0)) { //0 = uncompressed
+            if ((bmpDepth == 24) && (tempData == 0)) { //0 = uncompressed
+                //BMP rows are padded (if needed) to 4-byte boundary
+                uint32_t rowSize = (bmpWidth * 3 + 3) & ~3; //Not always = bmpWidth; may have padding
+
+                //Crop area to be loaded
+                int w = bmpWidth,
+                    h = bmpHeight;
+
+                if ((x + w - 1) >= (getWidth() * 8)) w = (getWidth() * 8)  - x;
+                if ((y + h - 1) >= (getHeight() * 8)) h = (getHeight() * 8) - y;
+
+                for (int row = 0; row < h; row++) { //For each scanline...
+                    uint32_t pos = bmpImageoffset + (flip ? (bmpHeight - 1 - row) : row) * rowSize ;
+                    uint8_t  buffidx = sizeof(_dataBuffer); //Current position in _dataBuffer
+                    for (int col = 0; col < w; col++) { //For each pixel...
+                        //Time to read more pixel data?
+                        if (buffidx >= sizeof(_dataBuffer)) { //Indeed
+                            buffidx = 0; //Set index to beginning
+                            for (int a = 0; a < BUFFPIXEL; a++) {
+                                _dataBuffer[a] = pgm_read_byte((uint8_t*)bitmap + (pos + a));
+                            }
+                        }
+
+                        uint8_t _b = _dataBuffer[buffidx++],
+                                _g = _dataBuffer[buffidx++],
+                                _r = _dataBuffer[buffidx++];
+                        setLedColor(col + x, row + y, _r, _g, _b);
+                    } //end pixel
+                } //end scanline
+            } //end goodBmp
+            else {
+                return false;
+            }
+        }//end planes
+        else {
+            return false;
+        }
+    }//end sianatrue
+    else {
+        return false;
+    }
+    return true;
+}
+#endif
 
 void Matrix::writeString(char* _c, bool _m, uint16_t _t, int16_t _xy)
 {
diff -r 14b7c3a3ec60 -r 487a727d6181 Microduino_MatrixBase.cpp
--- a/Microduino_MatrixBase.cpp	Wed Jun 01 13:38:30 2016 +0000
+++ b/Microduino_MatrixBase.cpp	Thu Jun 02 01:07:50 2016 +0000
@@ -154,8 +154,6 @@
             this->setLedColor(_row, _col, this->value_color[0], this->value_color[1], this->value_color[2]);
     } else
         this->setLedColorFast(_row, _col, 0, 0, 0);
-        
-    wait_ms(1);
 }
 
 void LedControl::setRow(uint8_t _row, uint8_t _value)
diff -r 14b7c3a3ec60 -r 487a727d6181 SomeCartoon.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SomeCartoon.cpp	Thu Jun 02 01:07:50 2016 +0000
@@ -0,0 +1,179 @@
+#include "SomeCartoon.h"
+#include "mbed.h"
+#include "Microduino_Matrix.h"
+
+extern Matrix           display;
+extern AnalogIn         gAnalogIn;
+void randomSeed(unsigned long seed)
+{
+    if (seed != 0) {
+        //srandom(seed);
+        srand(seed);
+    }
+}
+
+long random(long howbig)
+{
+    if (howbig == 0) {
+        return 0;
+    }
+    return rand() % howbig;
+}
+
+long random(long howsmall, long howbig)
+{
+    if (howsmall >= howbig) {
+        return howsmall;
+    }
+    long diff = howbig - howsmall;
+    return random(diff) + howsmall;
+}
+/*
+uint8_t donghua[128] = {
+    0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0,
+    7,1, 7,2, 7,3, 7,4, 7,5, 7,6, 7,7,
+    6,7, 5,7, 4,7, 3,7, 2,7, 1,7, 0,7,
+    0,6, 0,5, 0,4, 0,3, 0,2, 0,1,
+    1,1, 2,1, 3,1, 4,1, 5,1, 6,1,
+};*/
+
+void drawCartoon01(void)
+{
+    uint8_t r,g,b;
+    randomSeed(gAnalogIn.read_u16());
+    display.clearDisplay();
+    int8_t x = 0;
+    int8_t y = 0;
+    int8_t xMax = 7;
+    int8_t yMax = 7;
+    int8_t xMin = 0;
+    int8_t yMin = 0;
+    uint8_t state = 0;
+    r = random(0, 255);
+    g = random(0, 255);
+    b = random(0, 255);
+    uint8_t times = 0;
+    while (true) {
+        display.setLedColor(x, y, r, g, b);   //x, y, r, g, b
+        wait_ms(50);
+        if (x == 3 && y == 4) {
+            times++;
+            if (times >=3) {
+                break;
+            }
+            x = 0;
+            y = 0;
+            xMax = 7;
+            yMax = 7;
+            xMin = 0;
+            yMin = 0;
+            display.clearDisplay();
+            wait_ms(200);
+            state = 0;
+            r = random(0, 255);
+            g = random(0, 255);
+            b = random(0, 255);
+            display.setLedColor(0, 0, r, g, b);   //x, y, r, g, b
+            //continue;
+        }
+
+        if (state == 0) {
+            x++;
+            if (x > xMax) {
+                state = 1;
+                x = xMax;
+                y++;
+            }
+        } else if (state == 1) {
+            y++;
+            if (y > yMax) {
+                state = 2;
+                y = yMax;
+                x--;
+            }
+        } else if (state == 2) {
+            x--;
+            if (x < xMin) {
+                state = 3;
+                x = xMin;
+                y--;
+            }
+        } else if (state == 3) {
+            y--;
+            if (y == yMin) {
+                state = 0;
+                y = yMin+1;
+                x++;
+                xMax--;
+                yMax--;
+                xMin++;
+                yMin++;
+            }
+        }
+    }
+}
+
+void drawCartoon02(void)
+{
+    uint8_t times = 0;
+    int8_t x,y,x1,y1;
+
+doCartoon:
+    display.setColor(random(0, 255), random(0, 255), random(0, 255));
+    x = 0;
+    y = 0;
+    x1 = 7;
+    y1 = 7;
+    while (x < 8) {
+        display.drawLine(x, y, x1, y1);//x,y,x1,y1
+        wait_ms(200);
+        x++;
+        y1--;
+    }
+    x = 0;
+    y = 1;
+    x1 = 6;
+    y1 = 7;
+    while (y < 8) {
+        display.drawLine(x, y, x1, y1);//x,y,x1,y1
+        wait_ms(200);
+        y++;
+        x1--;
+    }
+    times++;
+    if (times >= 3) {
+        return;
+    }
+    wait_us(2000000);
+    display.clearDisplay();
+    goto doCartoon;
+}
+
+void drawCartoon03(void)
+{
+    uint8_t times = 0;
+    int8_t x = 0;
+    int8_t y = 0;
+    int8_t w = 8;
+    int8_t h = 8;
+doCartoon:
+    display.setColor(random(0, 255), random(0, 255), random(0, 255));
+    while (x < 4) {
+        display.clearDisplay();
+        display.drawBox(x, y, w, h);  //x,y,w,h
+        wait_ms(500);
+        x++;
+        y++;
+        w -= 2;
+        h -= 2;
+    }
+    display.clearDisplay();
+    times++;
+    if (times >= 3) {
+        return;
+    }
+    wait_ms(2000);
+    x = y = 0;
+    w = h = 8;
+    goto doCartoon;
+}
\ No newline at end of file
diff -r 14b7c3a3ec60 -r 487a727d6181 SomeCartoon.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SomeCartoon.h	Thu Jun 02 01:07:50 2016 +0000
@@ -0,0 +1,10 @@
+#ifndef SOME_CARTOON_H
+#define SOME_CARTOON_H
+extern void drawCartoon01(void);
+extern void drawCartoon02(void);
+extern void drawCartoon03(void);
+extern void randomSeed(unsigned long seed);
+extern long random(long howbig);
+extern long random(long howsmall, long howbig);
+
+#endif //SOME_CARTOON_H
\ No newline at end of file
diff -r 14b7c3a3ec60 -r 487a727d6181 main.cpp
--- a/main.cpp	Wed Jun 01 13:38:30 2016 +0000
+++ b/main.cpp	Thu Jun 02 01:07:50 2016 +0000
@@ -28,24 +28,20 @@
 // @老潘orz
 
 #define _MAIN_CPP
-
 #include "mbed.h"
 #include "MicroduinoPinNames.h"
 #include "Microduino_Matrix.h"
+#include "SomeCartoon.h"
 
 uint8_t Addr[MatrixPix_X][MatrixPix_Y] = {
     { 64}
 };
 
-Matrix display = Matrix(Addr);
-AnalogIn gAnalogIn(A0);
-Timer g_Timer;
-
-static const uint8_t logoA[] = {   //低位在前 逐行
+static uint8_t logoA[] = {   //低位在前 逐行
     0x00, 0x66, 0x66, 0xDB, 0xDB, 0xDB, 0xDB, 0x00
 };
 
-static const uint8_t logoB[] = {  //BMP File
+static uint8_t logoB[] = {  //BMP File
     0x42, 0x4D, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
     0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
     0x00, 0x00, 0xC2, 0x00, 0x00, 0x00, 0x20, 0x2E, 0x00, 0x00, 0x20, 0x2E, 0x00, 0x00, 0x00, 0x00,
@@ -64,35 +60,13 @@
     0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xDD, 0x00, 0x00,
 };
 
-DigitalOut myled(D13);
 #define delay wait_ms
-void randomSeed(unsigned long seed)
-{
-    if (seed != 0) {
-        //srandom(seed);
-        srand(seed);
-    }
-}
+DigitalOut       myled(D13);
+Matrix           display(Addr);
+AnalogIn         gAnalogIn(A0);
+Timer            g_Timer;
+Serial           pc(D1, D0); // tx, rx
 
-long random(long howbig)
-{
-    if (howbig == 0) {
-        return 0;
-    }
-    return rand() % howbig;
-}
-
-long random(long howsmall, long howbig)
-{
-    if (howsmall >= howbig) {
-        return howsmall;
-    }
-    long diff = howbig - howsmall;
-    return random(diff) + howsmall;
-}
-
-Serial pc(P0_4,P0_0);
-#if 0
 int main()
 {
     int i;
@@ -100,14 +74,13 @@
     myled = 0;
     pc.baud(115200);
     g_Timer.start();
-#if 1
-    //getDeviceAddr
+
     for (int a = 0; a < display.getMatrixNum(); a++) {
         pc.printf("device add = %d", display.getDeviceAddr(a));
         pc.printf(" ");
     }
     pc.printf("\r\n");
-#endif
+
     myled = !myled;
     display.clearDisplay();
     //setLedColor
@@ -115,7 +88,19 @@
         for (int x = 0; x < display.getWidth() * 8; x++) {
             randomSeed(x);
             display.setLedColor(x, y, random(0, 255), random(0, 255), random(0, 255));   //x, y, r, g, b
-            delay(1000);
+            delay(20);
+        }
+    }
+    delay(1000);
+    myled = !myled;
+
+    //setLed
+    display.clearDisplay();
+    display.setColor(255, 0, 0);
+    for (int y = 0; y < display.getHeight() * 8; y++) {
+        for (int x = 0; x < display.getWidth() * 8; x++) {
+            display.setLed(x, y, true);   //x, y, sta
+            delay(5);
         }
     }
     delay(1000);
@@ -132,10 +117,10 @@
     }
     delay(1000);
     myled = !myled;
-
+    
     //setLed
     display.clearDisplay();
-    display.setColor(255, 0, 0);
+    display.setColor(0, 0, 255);
     for (int y = 0; y < display.getHeight() * 8; y++) {
         for (int x = 0; x < display.getWidth() * 8; x++) {
             display.setLed(x, y, true);   //x, y, sta
@@ -167,8 +152,14 @@
     display.clearDisplay();
     myled = !myled;
     while(1) {
+        display.clearDisplay();
+        drawCartoon01();
+        display.clearDisplay();
+        drawCartoon02();
+        display.clearDisplay();
+        drawCartoon03();
+        
         display.setColor(random(0, 255), random(0, 255), random(0, 255));
-
         display.drawBox(0, 0, 8, 8);  //x,y,w,h
         delay(2000);
         display.clearDisplay();
@@ -248,183 +239,4 @@
         display.writeString(buffer_data, MODE_H, 50, 1);
         myled = !myled;
     }
-}
-#elif 0
-int main()
-{
-    int i;
-    char buf[32];
-    myled = 0;
-    pc.baud(115200);
-    g_Timer.start();
-    pc.printf("rand max = %d\r\n", RAND_MAX);
-#if 1
-    //getDeviceAddr
-    for (int a = 0; a < display.getMatrixNum(); a++) {
-        pc.printf("device add = %d", display.getDeviceAddr(a));
-        pc.printf(" ");
-    }
-    pc.printf("\r\n");
-    //wait(5.0);
-    pc.printf("Let us go into while\r\n");
-#endif
-    while (1) {
-        myled = 1;
-        //wait(2.0);
-        //pc.printf("clearDisplay....\r\n");
-        display.clearDisplay();
-        int16_t height = display.getHeight();
-        int16_t width = display.getWidth();
-        //pc.printf("height = %d, width = %d\r\n", height, width);
-        for (int y = 0; y < display.getHeight() * 8; y++) {
-            for (int x = 0; x < display.getWidth() * 8; x++) {
-                randomSeed(23);
-                //display.setLedColor(x, y, random(0, 255), random(0, 255), random(0, 255));   //x, y, r, g, b
-                display.setLedColor(x, y, 156, 200, 56);   //x, y, r, g, b
-                wait_ms(5);
-            }
-        }
-        myled = 0;
-        wait(1);
-    }
-}
-#elif 0
-I2C i2c(P0_11, P0_10);
-void main()
-{
-    pc.baud(115200);
-    pc.printf("Hello Microduino!\r\n");
-    wait(3.5);
-    i2c.frequency(400000);
-    //i2c.start();
-    uint8_t address = 64;
-    char cmd = 0x60;
-    int ret = i2c.write(address<<1, &cmd, 1, false);
-    //int ret = i2c.write(0x60);
-    pc.printf("clear led ret = %d\r\n", ret);
-    //i2c.stop();
-
-    uint8_t _value_r = random(0, 255);
-    uint8_t _value_g = random(0, 255);
-    uint8_t _value_b = random(0, 255);
-    uint8_t temp[4];
-    //temp[0] = 0x80 | (_row << 3) | _col;
-    temp[1] = _value_b / 8;
-    temp[2] = 0x20 | _value_g / 8;
-    temp[3] = 0x40 | _value_r / 8;
-    randomSeed(23);
-    while (1) {
-        i2c.write(address<<1, &cmd, 1, false);
-        wait_ms(900);
-        myled = !myled;
-        for (int y = 0; y < display.getHeight() * 8; y++) {
-            for (int x = 0; x < display.getWidth() * 8; x++) {
-                //randomSeed(23);
-                //display.setLedColor(x, y, random(0, 255), random(0, 255), random(0, 255));   //x, y, r, g, b
-                //display.setLedColor(x, y, 156, 200, 56);   //x, y, r, g, b
-                _value_r = random(0, 255);
-                _value_g = random(0, 255);
-                _value_b = random(0, 255);
-                temp[0] = 0x80 | (x << 3) | y;
-                temp[1] = _value_b / 8;
-                temp[2] = 0x20 | _value_g / 8;
-                temp[3] = 0x40 | _value_r / 8;
-                i2c.write(address<<1, (char*)temp, 4, false);
-                wait_ms(5);
-            }
-        }
-        wait(1.9);
-    }
-}
-#elif 0
-void main()
-{
-    randomSeed(gAnalogIn.read_u16());
-    while (1) {
-        display.setColor(random(0, 255), random(0, 255), random(0, 255));
-        display.drawBox(0, 0, 8, 8);  //x,y,w,h
-        delay(2000);
-        display.clearDisplay();
-        delay(599);
-        //randomSeed(gAnalogIn.read_u16());
-    }
-}
-#else
-uint8_t donghua[128] = {
-    0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0,
-    7,1, 7,2, 7,3, 7,4, 7,5, 7,6, 7,7,
-    6,7, 5,7, 4,7, 3,7, 2,7, 1,7, 0,7,
-    0,6, 0,5, 0,4, 0,3, 0,2, 0,1,
-    1,1, 2,1, 3,1, 4,1, 5,1, 6,1,
-};
-void main()
-{
-    uint8_t r,g,b;
-    randomSeed(gAnalogIn.read_u16());
-    display.clearDisplay();
-    int8_t x = 0;
-    int8_t y = 0;
-    int8_t xMax = 7;
-    int8_t yMax = 7;
-    int8_t xMin = 0;
-    int8_t yMin = 0;
-    uint8_t state = 0;
-    r = random(0, 255);
-    g = random(0, 255);
-    b = random(0, 255);
-    while (true) {
-        display.setLedColor(x, y, r, g, b);   //x, y, r, g, b
-        delay(50);
-        if (x == 3 && y == 4) {
-            x = 0;
-            y = 0;
-            xMax = 7;
-            yMax = 7;
-            xMin = 0;
-            yMin = 0;
-            display.clearDisplay();
-            delay(500);
-            state = 0;
-            r = random(0, 255);
-            g = random(0, 255);
-            b = random(0, 255);
-            display.setLedColor(0, 0, r, g, b);   //x, y, r, g, b
-            //continue;
-        }
-
-        if (state == 0) {
-            x++;
-            if (x > xMax) {
-                state = 1;
-                x = xMax;
-                y++;
-            }
-        } else if (state == 1) {
-            y++;
-            if (y > yMax) {
-                state = 2;
-                y = yMax;
-                x--;
-            }
-        } else if (state == 2) {
-            x--;
-            if (x < xMin) {
-                state = 3;
-                x = xMin;
-                y--;
-            }
-        } else if (state == 3) {
-            y--;
-            if (y == yMin) {
-                state = 0;
-                y = yMin+1;
-                x++;
-                xMax--;
-                yMax--;
-                xMin++;
-                yMin++;
-            }
-        }
-    }
-}
-#endif
\ No newline at end of file
+}
\ No newline at end of file