John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers QSPIBitmap.cpp Source File

QSPIBitmap.cpp

00001 #include "QSPIBitmap.h"
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 
00006 #include <fstream>
00007 
00008 
00009 // These are in main.cpp
00010 extern void DebugPrint(char *stuffToPrint, GuiConst_INT16S X, GuiConst_INT16S Y);
00011 extern bool qspiAlreadyFormatted;
00012 extern GuiConst_INTCOLOR SixteenBitColorValue(GuiConst_INT8U red, GuiConst_INT8U green, GuiConst_INT8U blue); 
00013 
00014 // QSPI utility function (for debugging)
00015 void DisplayQSPIDirectory(GuiConst_INT16S X, GuiConst_INT16S Y)
00016 {
00017     DIR *dp;
00018     dp = opendir("/qspi/");
00019 
00020     if(dp != NULL) {
00021         struct dirent *dirp;
00022         
00023         DebugPrint("Start of QSPI directory", X, Y);
00024         Y += 30;
00025     
00026         while((dirp = readdir(dp)) != NULL) {
00027             DebugPrint(dirp->d_name, X, Y);
00028             Y += 30;
00029         }
00030         closedir(dp);
00031     
00032         DebugPrint("End of QSPI directory", X, Y);
00033     } else {
00034         DebugPrint("Failed to open QSPI directory", X, Y);
00035     }
00036 }
00037 // End of QSPI utility functions
00038 
00039 
00040 // The default constructor exists purely to satisfy the compiler - it is not intended to be used
00041 QSPIBitmap::QSPIBitmap()
00042 {
00043     name[0] = '\0';
00044     
00045     size = 0;
00046     debugVar1 = 0;
00047         
00048     //data = NULL;
00049     
00050     bitmapLoaded = false;
00051 }
00052 
00053 QSPIBitmap::QSPIBitmap(char* bitmapName)
00054 {
00055     strcpy(name, bitmapName);
00056     
00057     // Set these when we read the bitmap from the QSPI drive
00058     size = 0;
00059     debugVar1 = 0;
00060     data = NULL;
00061     
00062     bitmapLoaded = GetBitmapFromQSPIDrive();
00063 }
00064 
00065 QSPIBitmap::~QSPIBitmap()
00066 {   
00067     if(data) {
00068         delete [] data;
00069     }
00070 }
00071 
00072 bool QSPIBitmap::ReadBitmapSizeFile(void)
00073 {
00074     char bitmapSizeFilename[200];
00075     sprintf(bitmapSizeFilename, "/qspi/%s_BMP.size", name);
00076     
00077     char buff[50];
00078     
00079     FILE *fpsize = fopen(bitmapSizeFilename, "r");
00080     if (fpsize != NULL) {
00081         // We allow the size value to be up to 20 digits
00082         int numRead = fread(buff, 1, 20, fpsize);
00083         fclose(fpsize);
00084 
00085         buff[numRead] = '\0';
00086         
00087         sscanf(buff, "%d", &size);
00088         
00089         return true;
00090     }
00091     
00092     // 'else'...
00093     
00094     return false;
00095 }
00096 
00097 bool QSPIBitmap::ReadBitmapDataFile(void)
00098 {
00099     char bitmapDataFilename[200];
00100     sprintf(bitmapDataFilename, "/qspi/%s_BMP.data", name);
00101 
00102     FILE *fpdata = fopen(bitmapDataFilename, "rb");
00103     if (fpdata != NULL) {
00104         fread(data, size, 1, fpdata);
00105         fclose(fpdata);
00106         
00107         return true;
00108     }
00109         
00110     // 'else'...
00111     
00112     return false;
00113 }
00114 
00115 bool QSPIBitmap::GetBitmapFromQSPIDrive(void)
00116 {
00117     //TODO: Raise exceptions(?) if any of the below fails
00118     
00119     debugVar1 = 1;
00120     
00121     if(ReadBitmapSizeFile()) {
00122         debugVar1 = 3;
00123     } else {
00124         debugVar1 = 2;
00125         return false;
00126     }
00127     
00128     debugVar1 = 4;
00129     
00130     // Do not throw an exception if the allocation fails - just leave 'data' set to NULL
00131     data = new (nothrow) GuiConst_INT8U[size + 10]; // Leave a small margin 'just in case'
00132 // TEST - what if the 'new' fails??
00133 // Answer - looks same as what Andrew saw at PittCon 2017
00134     
00135     if(data == NULL) {
00136         return false;
00137     }
00138     
00139     if(ReadBitmapDataFile()) {
00140         debugVar1 = 5;
00141     } else {
00142         debugVar1 = 6;
00143         return false;
00144     }
00145 
00146     debugVar1 = 7;
00147     
00148     return true;
00149 }
00150 
00151 void QSPIBitmap::ShowAt(GuiConst_INT16S X, GuiConst_INT16S Y, GuiConst_INT32S transparentColour)
00152 {
00153     if(bitmapLoaded) {    
00154         GuiLib_ShowBitmapAt(data, X, Y, transparentColour);
00155     } else {
00156         char buff[100];
00157         sprintf(buff, "Bitmap not loaded - size %d, debugVar1: %d", size, debugVar1);
00158         DebugPrint(buff, X, Y);
00159         
00160         char bitmapSizeFilename[200];
00161         sprintf(bitmapSizeFilename, "/qspi/%s_BMP.size", name);
00162         DebugPrint(bitmapSizeFilename, X, Y+40);
00163     }
00164 }
00165 
00166 void QSPIBitmap::ShowAreaAt(GuiConst_INT16S X, GuiConst_INT16S Y, GuiConst_INT16S AX1, GuiConst_INT16S AY1, GuiConst_INT16S AX2, GuiConst_INT16S AY2, GuiConst_INT32S transparentColour)
00167 {
00168     if(bitmapLoaded) {    
00169         GuiLib_ShowBitmapAreaAt(data, X, Y, AX1, AY1, AX2, AY2, transparentColour);
00170     } else {
00171         char buff[100];
00172         sprintf(buff, "Bitmap not loaded - size %d, debugVar1: %d", size, debugVar1);
00173         DebugPrint(buff, X, Y);
00174         
00175         char bitmapSizeFilename[200];
00176         sprintf(bitmapSizeFilename, "/qspi/%s_BMP.size", name);
00177         DebugPrint(bitmapSizeFilename, X, Y+40);
00178     }
00179 }
00180 
00181 // QSPIBitmaps class members
00182 QSPIBitmaps::QSPIBitmaps()
00183 {
00184     qspibmArray[HOME_COLUMN_BITMAP] = NULL;
00185     qspibmArray[HOME_DETECTOR_BITMAP] = NULL;
00186     qspibmArray[HOME_INJECTOR_BITMAP] = NULL;
00187     qspibmArray[HOME_GAS_BITMAP] = NULL;
00188 
00189     qspibmArray[COMPONENT_COLUMN_BITMAP] = NULL;
00190     qspibmArray[COMPONENT_DETECTOR_BITMAP] = NULL;
00191     qspibmArray[COMPONENT_INJECTOR_BITMAP] = NULL;
00192     qspibmArray[COMPONENT_GAS_BITMAP] = NULL;
00193 
00194     qspibmArray[BOOT_SCREEN_BITMAP] = NULL;
00195     qspibmArray[BLANK_BACKGROUND_BITMAP] = NULL;
00196     qspibmArray[UP_ARROW_BITMAP] = NULL;
00197     qspibmArray[DOWN_ARROW_BITMAP] = NULL;
00198     
00199     arraySetUp = false;
00200 }
00201 
00202 /*
00203     Sets up each of the bitmaps in the array, by reading the corresponding files
00204     from the QSPI (memory) file system.
00205     
00206     *** Note that this is currently loading bitmaps specific to the Home page ***
00207     
00208     Returns true if all loaded successfully, false if not
00209 */
00210 void QSPIBitmaps::SetupArray(void)
00211 {
00212     if(!arraySetUp) {
00213         
00214         // Bitmap names manually copied from GuiStruct.c, and hard-coded here.
00215         // Must match those in the GC500_2_5inch_bitmap_loader.
00216         
00217         qspibmArray[HOME_COLUMN_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_ColumnButton");
00218         qspibmArray[HOME_DETECTOR_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_DetectorButton");
00219         qspibmArray[HOME_INJECTOR_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_InjectorButton");
00220         qspibmArray[HOME_GAS_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_GasButton");
00221         
00222         qspibmArray[COMPONENT_COLUMN_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_Column");
00223         qspibmArray[COMPONENT_DETECTOR_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_Detector");
00224         qspibmArray[COMPONENT_INJECTOR_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_Injector");
00225         qspibmArray[COMPONENT_GAS_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_Gas");
00226         
00227         qspibmArray[BOOT_SCREEN_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_BootScreen");
00228         qspibmArray[BLANK_BACKGROUND_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_BlankBackground");
00229         
00230         qspibmArray[UP_ARROW_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_UpArrow1point5");
00231         qspibmArray[DOWN_ARROW_BITMAP] = new QSPIBitmap("GuiStruct_Bitmap_DownArrow1point5");
00232         
00233         arraySetUp = true;
00234     }    
00235 }
00236 
00237 
00238 void QSPIBitmaps::DisplayBitmapAtIfLoaded(int bitmapIndex, GuiConst_INT16S X, GuiConst_INT16S Y, GuiConst_INT32S transparentColour)
00239 {
00240     if(qspibmArray[bitmapIndex] != NULL) {
00241         qspibmArray[bitmapIndex]->ShowAt(X,  Y, transparentColour);
00242     }
00243 }
00244 
00245 void QSPIBitmaps::DisplayAllHomePageBitmaps(void)
00246 {
00247     // Home page component bitmaps have white corners - make them transparent
00248     DisplayBitmapAtIfLoaded(HOME_INJECTOR_BITMAP, 10,  10, 0xFFFF);
00249     DisplayBitmapAtIfLoaded(HOME_DETECTOR_BITMAP, 410, 10, 0xFFFF);
00250     DisplayBitmapAtIfLoaded(HOME_COLUMN_BITMAP, 10,  215, 0xFFFF);
00251     DisplayBitmapAtIfLoaded(HOME_GAS_BITMAP, 410, 215, 0xFFFF);
00252 
00253     //DEBUG
00254     if(qspiAlreadyFormatted) {
00255         DebugPrint("QSPI formatted at start", 200, 100);
00256     } else {
00257         DebugPrint("QSPI *not* formatted at start", 200, 100);
00258     }
00259         
00260 }
00261     
00262 void QSPIBitmaps::DisplayColumnComponentBitmap(void)
00263 {
00264     // RGB(231,231,231) is the colour of the corners of the component bitmaps -
00265     // make them transparent
00266     //DisplayBitmapAtIfLoaded(COMPONENT_COLUMN_BITMAP, 85, 230, SixteenBitColorValue(231, 231, 231)); // Bottom left, next to left arrow
00267     DisplayBitmapAtIfLoaded(COMPONENT_COLUMN_BITMAP, 609, 19, SixteenBitColorValue(231, 231, 231)); // Top right, like the others
00268 }
00269 
00270 void QSPIBitmaps::DisplayDetectorComponentBitmap(void)
00271 {
00272     // RGB(231,231,231) is the colour of the corners of the component bitmaps -
00273     // make them transparent
00274     DisplayBitmapAtIfLoaded(COMPONENT_DETECTOR_BITMAP, 609, 19, SixteenBitColorValue(231, 231, 231));
00275 }
00276 
00277 void QSPIBitmaps::DisplayInjectorComponentBitmap(void)
00278 {
00279     // RGB(231,231,231) is the colour of the corners of the component bitmaps -
00280     // make them transparent
00281     DisplayBitmapAtIfLoaded(COMPONENT_INJECTOR_BITMAP, 609, 19, SixteenBitColorValue(231, 231, 231));
00282 }
00283 
00284 void QSPIBitmaps::DisplayGasComponentBitmap(void)
00285 {
00286     // RGB(231,231,231) is the colour of the corners of the component bitmaps -
00287     // make them transparent
00288     DisplayBitmapAtIfLoaded(COMPONENT_GAS_BITMAP, 609, 19, SixteenBitColorValue(231, 231, 231));
00289 }
00290 
00291 void QSPIBitmaps::DisplayBootScreenBitmap(void)
00292 {
00293     // No transparency on background bitmaps
00294     DisplayBitmapAtIfLoaded(BOOT_SCREEN_BITMAP, 0, 0, -1);
00295 }
00296 
00297 void QSPIBitmaps::DisplayBlankBackgroundBitmap(void)
00298 {
00299     // No transparency on background bitmaps
00300     DisplayBitmapAtIfLoaded(BLANK_BACKGROUND_BITMAP, 0, 0, -1);
00301 }
00302 
00303 void QSPIBitmaps::DisplayUpArrowBitmap(GuiConst_INT16S X, GuiConst_INT16S Y)
00304 {
00305     DisplayBitmapAtIfLoaded(UP_ARROW_BITMAP, X, Y, -1); // No transparency
00306 }
00307 
00308 void QSPIBitmaps::DisplayDownArrowBitmap(GuiConst_INT16S X, GuiConst_INT16S Y)
00309 {
00310     DisplayBitmapAtIfLoaded(DOWN_ARROW_BITMAP, X, Y, -1); // No transparency
00311 }
00312    
00313 void QSPIBitmaps::ClearArray(void)
00314 {
00315     for( int bitmapIndex = 0; bitmapIndex < QSPIBITMAP_COUNT; ++bitmapIndex) {
00316         if(qspibmArray[bitmapIndex] != NULL) {
00317             delete qspibmArray[bitmapIndex];
00318             qspibmArray[bitmapIndex] = NULL;
00319         }
00320     }
00321 }
00322 
00323 /*
00324     Tells the caller whether or not all of the QSPI bitmaps have been loaded,
00325     false if not
00326     
00327     Returns true if they are *all* loaded, false if at least one is not
00328 */
00329 bool QSPIBitmaps::AllBitmapsLoaded(void)
00330 {
00331     for(int index = 0; index < QSPIBITMAP_COUNT; ++index) {
00332         if(qspibmArray[index] == NULL) {
00333             return false;
00334         }
00335         
00336         if(!qspibmArray[index]->BitmapLoaded()) {
00337             return false;
00338         }
00339     }
00340     
00341     // 'else' - all loaded successfully
00342 //#define SIMULATE_ERROR_FOR_TESTING
00343 #ifdef SIMULATE_ERROR_FOR_TESTING
00344     // Pretend they did not load...
00345     return false;
00346 #else
00347     return true;
00348 #endif
00349 }
00350