Forked para SNOCC

Dependencies:   GPS

Dependents:   SNOCC_V1 SNOCC_V2

Fork of RA8875 by SNOCC

Revision:
86:e86b355940f4
Parent:
85:022bba13c5c4
Child:
87:ee2240581aa7
--- a/RA8875.cpp	Tue Jan 13 12:31:44 2015 +0000
+++ b/RA8875.cpp	Sat Jan 17 20:59:51 2015 +0000
@@ -1016,12 +1016,17 @@
 RetCode_t RA8875::getPixelStream(color_t * p, uint32_t count, loc_t x, loc_t y)
 {
     color_t pixel;
+    RetCode_t ret = noerror;
 
+    INFO("getPixelStream(%p, %u, %d, %d)", p, count, x, y);
     PERFORMANCE_RESET;
     //WriteCommand(0x45,0x00);    // read left->right, top->bottom
-    WriteCommand(0x40,0x00);    // Graphics write mode
-    SetGraphicsCursorRead(x, y);
-    WriteCommand(0x02);
+    ret = WriteCommand(0x40,0x00);    // Graphics write mode
+    INFO("  r = %d", ret);
+    ret = SetGraphicsCursorRead(x, y);
+    INFO("  r = %d", ret);
+    ret = WriteCommand(0x02);
+    INFO("  r = %d", ret);
     _select(true);
     _spiwrite(0x40);         // Cmd: read data
     _spiwrite(0x00);         // dummy read
@@ -1032,7 +1037,7 @@
     }
     _select(false);
     REGISTERPERFORMANCE(PRF_READPIXELSTREAM);
-    return noerror;
+    return ret;
 }
 
 
@@ -1396,6 +1401,10 @@
     return noerror;
 }
 
+uint8_t RA8875::GetBacklight_u8(void)
+{
+    return ReadCommand(0x8b);
+}
 
 RetCode_t RA8875::Backlight(float brightness)
 {
@@ -1410,6 +1419,10 @@
     return Backlight_u8(b);
 }
 
+float RA8875::GetBacklight(void)
+{
+    return (float)(GetBacklight_u8())/255;
+}
 
 RetCode_t RA8875::set_font(const unsigned char * _font)
 {
@@ -1541,7 +1554,10 @@
 {
     BITMAPFILEHEADER BMP_Header;
     BITMAPINFOHEADER BMP_Info;
-
+    uint8_t * lineBuffer = NULL;
+    color_t * pixelBuffer = NULL;
+    color_t * pixelBuffer2 = NULL;
+    
     INFO("(%d,%d) - (%d,%d) %s", x,y,w,h,Name_BMP);
     if (x >= 0 && x < width()
             && y >= 0 && y < height()
@@ -1566,43 +1582,66 @@
         BMP_Info.biClrUsed = 0;
         BMP_Info.biClrImportant = 0;
 
-        INFO("Writing {%s}", Name_BMP);
+        // Allocate the memory we need to proceed
+        int lineBufSize = ((24 * w + 7)/8);
+        lineBuffer = (uint8_t *)malloc(lineBufSize);
+        if (lineBuffer == NULL) {
+            ERR("Not enough RAM for PrintScreen lineBuffer");
+            return(not_enough_ram);
+        }
+
+        #define DOUBLEBUF /* one larger buffer instead of two */
+        
+        #ifdef DOUBLEBUF
+        // In the "#else", pixelBuffer2 malloc returns a value, 
+        // but is actually causing a failure later. 
+        // This test helps determine if it is truly out of memory,
+        // or if malloc is broken.
+        pixelBuffer = (color_t *)malloc(2 * w * sizeof(color_t));
+        pixelBuffer2 = pixelBuffer + (w * sizeof(color_t));
+        #else
+        pixelBuffer = (color_t *)malloc(w * sizeof(color_t));
+        pixelBuffer2 = (color_t *)malloc(w * sizeof(color_t));
+        #endif
+        if (pixelBuffer == NULL || pixelBuffer2 == NULL) {
+            ERR("Not enough RAM for pixelBuffer");
+            #ifndef DOUBLEBUF
+            if (pixelBuffer2)
+                free(pixelBuffer2);
+            #endif
+            if (pixelBuffer)
+                free(pixelBuffer);
+            free(lineBuffer);
+            if (pixelBuffer)
+                free(pixelBuffer);
+            return(not_enough_ram);
+        }
+
         FILE *Image = fopen(Name_BMP, "wb");
         if (!Image) {
-            ERR("File not found");
+            ERR("Can't open file for write");
+            #ifndef DOUBLEBUF
+            if (pixelBuffer2)
+                free(pixelBuffer2);
+            #endif
+            if (pixelBuffer)
+                free(pixelBuffer);
+            free(lineBuffer);
+            if (pixelBuffer)
+                free(pixelBuffer);
             return(file_not_found);
         }
 
         // Be optimistic - don't check for errors.
         //HexDump("BMP_Header", (uint8_t *)&BMP_Header, sizeof(BMP_Header));
-        fwrite(&BMP_Header, sizeof(char), sizeof(BMP_Header), Image);
-        //INFO("fwrite returned %d", r);
+        size_t r = fwrite(&BMP_Header, sizeof(char), sizeof(BMP_Header), Image);
 
         //HexDump("BMP_Info", (uint8_t *)&BMP_Info, sizeof(BMP_Info));
-        fwrite(&BMP_Info, sizeof(char), sizeof(BMP_Info), Image);
-        //INFO("fwrite returned %d", r);
+        r = fwrite(&BMP_Info, sizeof(char), sizeof(BMP_Info), Image);
 
-        int lineBufSize = ((24 * w + 7)/8);
-        uint8_t * lineBuffer = (uint8_t *)malloc(lineBufSize);
-        if (lineBuffer == NULL) {
-            fclose(Image);
-            ERR("Not enough RAM for lineBuffer");
-            return(not_enough_ram);
-        }
-        color_t * pixelBuffer = (color_t *)malloc(w * sizeof(color_t));
-        color_t * pixelBuffer2 = (color_t *)malloc(w * sizeof(color_t));
         color_t transparency = GetBackgroundTransparencyColor();
         unsigned char ltpr0 = ReadCommand(0x52) & 0x7;
 
-        if (pixelBuffer == NULL || pixelBuffer2 == NULL) {
-            fclose(Image);
-            free(lineBuffer);
-            ERR("Not enough RAM for pixelBuffer");
-            if (pixelBuffer)
-                free(pixelBuffer);
-            return(not_enough_ram);
-        }
-
         uint16_t prevLayer = GetDrawingLayer();
         // If only one of the layers is visible, select that layer
         switch(ltpr0) {
@@ -1626,7 +1665,7 @@
                 ERR("getPixelStream error, and no recovery handler...");
             }
             if (ltpr0 >= 2) {           // Need to combine the layers...
-                SelectDrawingLayer(1);  // so read layer 0 first
+                SelectDrawingLayer(1);  // so read layer 1 next
                 if (getPixelStream(pixelBuffer2, w, x,y+j) != noerror) {
                     ERR("getPixelStream error, and no recovery handler...");
                 }
@@ -1666,9 +1705,15 @@
         }
         SelectDrawingLayer(prevLayer);
         fclose(Image);
-        free(pixelBuffer2);  // don't leak memory.
-        free(pixelBuffer);
+        #ifndef DOUBLEBUF
+        if (pixelBuffer2)
+            free(pixelBuffer2);
+        #endif
+        if (pixelBuffer)
+            free(pixelBuffer);
         free(lineBuffer);
+        if (pixelBuffer)
+            free(pixelBuffer);
         INFO("Image closed");
         return noerror;
     } else {