Microduino

Dependencies:   mbed

Fork of LED_DZ by Li Weiyi

Revision:
2:487a727d6181
Parent:
1:14b7c3a3ec60
--- 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)
 {