John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SettingsHandler.cpp Source File

SettingsHandler.cpp

00001 
00002 #include "SettingsHandler.h"
00003 
00004 /*
00005     As explained in the header file, these are really C-style functions. They are all static,
00006     and they simply perform self-contained operations using the values passed to them.
00007     Having them in a class like this, however, seems more C++ like, easier to understand,
00008     and more consistent with the remainder of the code in this application.
00009 
00010     This class has no member variables and cannot be instantiated. It is simply 
00011     a container for the functions below.
00012 */
00013 
00014 // Use QSPI file system - uncomment '#define DM_BOARD_USE_QSPI' and '#define DM_BOARD_USE_QSPI_FS' in dm_board_config.h, and:
00015 #include "QSPIFileSystem.h"
00016 static QSPIFileSystem shqspifs("qspi");
00017 // Can now use QSPI memory as file device '/qspi/'
00018 
00019 extern void EasyGUIDebugPrint(char *stuffToPrint, short X, short Y); // In main.cpp
00020 
00021 
00022 void SettingsHandler::FormatQSPIIfRequired(void)
00023 {
00024     if (!shqspifs.isformatted()) {
00025         shqspifs.format();
00026     }    
00027 }
00028 
00029 /*
00030     Gets a setting value as a string, by reading it 
00031     from the corresponding file in QSPI memory.
00032     
00033     Args: pointer to the setting name (as a string)
00034           pointer to a buffer to contain the value read (as a string)
00035           the length of the buffer
00036           
00037     Returns the amount of data read (this will be zero if the setting was not found)
00038 */
00039 int SettingsHandler::GetSettingValueFromQSPI(char *settingName, char *settingValueBuff, int valueBuffLen)
00040 {
00041     FILE * pFile;
00042     size_t result;
00043     
00044     char filename[100];
00045     sprintf(filename, "/qspi/%s.txt", settingName);
00046     
00047     FormatQSPIIfRequired();
00048 
00049 //    pFile = fopen ( filename , "rb" );
00050 // No - text mode
00051     pFile = fopen ( filename , "r" );
00052     if (pFile == NULL) {
00053         return 0;
00054     }
00055 
00056     // copy the file into the buffer:
00057     result = fread (settingValueBuff, 1, valueBuffLen, pFile);
00058 
00059     // terminate
00060     fclose (pFile);
00061     
00062     // Make sure we return a null-terminated string
00063     if(result < valueBuffLen) {
00064         settingValueBuff[result] = '\0';
00065     }
00066     
00067     return result;    
00068 }
00069 
00070 /*
00071     Settings values are stored in QSPI memory as strings. This function
00072     gets the setting specified, and returns its value as an integer.
00073     
00074     Args: the setting name
00075           a default value in case the setting is not found
00076 
00077     Return value: either the setting value, converted to an integer, or the default value
00078 */
00079 int SettingsHandler::GetIntegerValueFromQSPISettings(char *settingName, int defaultValue)
00080 {
00081     int retVal;
00082     
00083     char buff[100];
00084     
00085     if(GetSettingValueFromQSPI(settingName, buff, 100) > 0) {
00086         // We read a value for the specified setting from QSPI settings
00087         sscanf(buff, "%d", &retVal);
00088     } else {
00089         // Value not found in QSPI settings
00090         retVal = defaultValue;
00091     }
00092     
00093     return retVal;
00094 }
00095 
00096 /*
00097     Puts (i.e. writes) a setting value as a string,
00098     to the corresponding file in QSPI memory.
00099     
00100     Args: pointer to the setting name (as a string)
00101           pointer to a buffer containing the value to be written (as a string)
00102           the length of the buffer
00103           
00104     Returns the amount of data written
00105 */
00106 int SettingsHandler::PutSettingValueToQSPI(char *settingName, char *settingValueBuff, int valueBuffLen)
00107 {
00108     FILE * pFile;
00109     size_t result;
00110 
00111     char filename[100];
00112     sprintf(filename, "/qspi/%s.txt", settingName);
00113 
00114     FormatQSPIIfRequired();
00115 
00116 //    pFile = fopen (filename, "wb");
00117 // No - text mode
00118     pFile = fopen (filename, "w");
00119     if (pFile == NULL) {
00120         return 0;
00121     }
00122     
00123     result = fwrite (settingValueBuff, 1, valueBuffLen, pFile);
00124     
00125     fclose (pFile);
00126     
00127     return result;
00128 }
00129 
00130 /*
00131     Settings values are stored in QSPI memory as strings. This function
00132     takes an integer as the new value for the specified setting,
00133     and converts it to a string before storing it.
00134     
00135     Args: the setting name
00136           the new value for the setting
00137 
00138     No return value.
00139 */
00140 void SettingsHandler::PutIntegerValueToQSPISettings(char *settingName, int value)
00141 {
00142     char buff[100];
00143     
00144     sprintf(buff, "%d", value);
00145     
00146     PutSettingValueToQSPI(settingName, buff, strlen(buff));
00147 }
00148 
00149 /*
00150     Displays the directory of files in QSPI memory.
00151     
00152     Used for debugging/testing - not appropriate or required in the 'real' system.
00153     
00154     Args: the X and Y coordinates at which to display the directory on the screen
00155 */
00156 void SettingsHandler::DisplayQSPIDirectory(GuiConst_INT16S X, GuiConst_INT16S Y)
00157 {
00158     DIR *dp;
00159     dp = opendir("/qspi/");
00160 
00161     if(dp != NULL) {
00162         struct dirent *dirp;
00163         
00164         EasyGUIDebugPrint("SH::Start of QSPI directory", X, Y);
00165         Y += 30;
00166     
00167         while((dirp = readdir(dp)) != NULL) {
00168             EasyGUIDebugPrint(dirp->d_name, X, Y);
00169             Y += 30;
00170         }
00171         closedir(dp);
00172     
00173         EasyGUIDebugPrint("SH::End of QSPI directory", X, Y);
00174     } else {
00175         EasyGUIDebugPrint("SH::Failed to open QSPI directory", X, Y);
00176     }
00177 }