Repository for import to local machine
Dependencies: DMBasicGUI DMSupport
SettingsHandler.cpp@8:26e49e6955bd, 2017-07-31 (annotated)
- Committer:
- jmitc91516
- Date:
- Mon Jul 31 15:37:57 2017 +0000
- Revision:
- 8:26e49e6955bd
- Parent:
- 1:a5258871b33d
Method ramp scrolling improved, and more bitmaps moved to QSPI memory
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmitc91516 | 1:a5258871b33d | 1 | |
jmitc91516 | 1:a5258871b33d | 2 | #include "SettingsHandler.h" |
jmitc91516 | 1:a5258871b33d | 3 | |
jmitc91516 | 1:a5258871b33d | 4 | /* |
jmitc91516 | 1:a5258871b33d | 5 | As explained in the header file, these are really C-style functions. They are all static, |
jmitc91516 | 1:a5258871b33d | 6 | and they simply perform self-contained operations using the values passed to them. |
jmitc91516 | 1:a5258871b33d | 7 | Having them in a class like this, however, seems more C++ like, easier to understand, |
jmitc91516 | 1:a5258871b33d | 8 | and more consistent with the remainder of the code in this application. |
jmitc91516 | 1:a5258871b33d | 9 | |
jmitc91516 | 1:a5258871b33d | 10 | This class has no member variables and cannot be instantiated. It is simply |
jmitc91516 | 1:a5258871b33d | 11 | a container for the functions below. |
jmitc91516 | 1:a5258871b33d | 12 | */ |
jmitc91516 | 1:a5258871b33d | 13 | |
jmitc91516 | 1:a5258871b33d | 14 | // Use QSPI file system - uncomment '#define DM_BOARD_USE_QSPI' and '#define DM_BOARD_USE_QSPI_FS' in dm_board_config.h, and: |
jmitc91516 | 1:a5258871b33d | 15 | #include "QSPIFileSystem.h" |
jmitc91516 | 1:a5258871b33d | 16 | static QSPIFileSystem shqspifs("qspi"); |
jmitc91516 | 1:a5258871b33d | 17 | // Can now use QSPI memory as file device '/qspi/' |
jmitc91516 | 1:a5258871b33d | 18 | |
jmitc91516 | 1:a5258871b33d | 19 | extern void EasyGUIDebugPrint(char *stuffToPrint, short X, short Y); // In main.cpp |
jmitc91516 | 1:a5258871b33d | 20 | |
jmitc91516 | 1:a5258871b33d | 21 | |
jmitc91516 | 1:a5258871b33d | 22 | void SettingsHandler::FormatQSPIIfRequired(void) |
jmitc91516 | 1:a5258871b33d | 23 | { |
jmitc91516 | 1:a5258871b33d | 24 | if (!shqspifs.isformatted()) { |
jmitc91516 | 1:a5258871b33d | 25 | shqspifs.format(); |
jmitc91516 | 1:a5258871b33d | 26 | } |
jmitc91516 | 1:a5258871b33d | 27 | } |
jmitc91516 | 1:a5258871b33d | 28 | |
jmitc91516 | 1:a5258871b33d | 29 | /* |
jmitc91516 | 1:a5258871b33d | 30 | Gets a setting value as a string, by reading it |
jmitc91516 | 1:a5258871b33d | 31 | from the corresponding file in QSPI memory. |
jmitc91516 | 1:a5258871b33d | 32 | |
jmitc91516 | 1:a5258871b33d | 33 | Args: pointer to the setting name (as a string) |
jmitc91516 | 1:a5258871b33d | 34 | pointer to a buffer to contain the value read (as a string) |
jmitc91516 | 1:a5258871b33d | 35 | the length of the buffer |
jmitc91516 | 1:a5258871b33d | 36 | |
jmitc91516 | 1:a5258871b33d | 37 | Returns the amount of data read (this will be zero if the setting was not found) |
jmitc91516 | 1:a5258871b33d | 38 | */ |
jmitc91516 | 1:a5258871b33d | 39 | int SettingsHandler::GetSettingValueFromQSPI(char *settingName, char *settingValueBuff, int valueBuffLen) |
jmitc91516 | 1:a5258871b33d | 40 | { |
jmitc91516 | 1:a5258871b33d | 41 | FILE * pFile; |
jmitc91516 | 1:a5258871b33d | 42 | size_t result; |
jmitc91516 | 1:a5258871b33d | 43 | |
jmitc91516 | 1:a5258871b33d | 44 | char filename[100]; |
jmitc91516 | 1:a5258871b33d | 45 | sprintf(filename, "/qspi/%s.txt", settingName); |
jmitc91516 | 1:a5258871b33d | 46 | |
jmitc91516 | 1:a5258871b33d | 47 | FormatQSPIIfRequired(); |
jmitc91516 | 1:a5258871b33d | 48 | |
jmitc91516 | 1:a5258871b33d | 49 | // pFile = fopen ( filename , "rb" ); |
jmitc91516 | 1:a5258871b33d | 50 | // No - text mode |
jmitc91516 | 1:a5258871b33d | 51 | pFile = fopen ( filename , "r" ); |
jmitc91516 | 1:a5258871b33d | 52 | if (pFile == NULL) { |
jmitc91516 | 1:a5258871b33d | 53 | return 0; |
jmitc91516 | 1:a5258871b33d | 54 | } |
jmitc91516 | 1:a5258871b33d | 55 | |
jmitc91516 | 1:a5258871b33d | 56 | // copy the file into the buffer: |
jmitc91516 | 1:a5258871b33d | 57 | result = fread (settingValueBuff, 1, valueBuffLen, pFile); |
jmitc91516 | 1:a5258871b33d | 58 | |
jmitc91516 | 1:a5258871b33d | 59 | // terminate |
jmitc91516 | 1:a5258871b33d | 60 | fclose (pFile); |
jmitc91516 | 1:a5258871b33d | 61 | |
jmitc91516 | 1:a5258871b33d | 62 | // Make sure we return a null-terminated string |
jmitc91516 | 1:a5258871b33d | 63 | if(result < valueBuffLen) { |
jmitc91516 | 1:a5258871b33d | 64 | settingValueBuff[result] = '\0'; |
jmitc91516 | 1:a5258871b33d | 65 | } |
jmitc91516 | 1:a5258871b33d | 66 | |
jmitc91516 | 1:a5258871b33d | 67 | return result; |
jmitc91516 | 1:a5258871b33d | 68 | } |
jmitc91516 | 1:a5258871b33d | 69 | |
jmitc91516 | 1:a5258871b33d | 70 | /* |
jmitc91516 | 1:a5258871b33d | 71 | Settings values are stored in QSPI memory as strings. This function |
jmitc91516 | 1:a5258871b33d | 72 | gets the setting specified, and returns its value as an integer. |
jmitc91516 | 1:a5258871b33d | 73 | |
jmitc91516 | 1:a5258871b33d | 74 | Args: the setting name |
jmitc91516 | 1:a5258871b33d | 75 | a default value in case the setting is not found |
jmitc91516 | 1:a5258871b33d | 76 | |
jmitc91516 | 1:a5258871b33d | 77 | Return value: either the setting value, converted to an integer, or the default value |
jmitc91516 | 1:a5258871b33d | 78 | */ |
jmitc91516 | 1:a5258871b33d | 79 | int SettingsHandler::GetIntegerValueFromQSPISettings(char *settingName, int defaultValue) |
jmitc91516 | 1:a5258871b33d | 80 | { |
jmitc91516 | 1:a5258871b33d | 81 | int retVal; |
jmitc91516 | 1:a5258871b33d | 82 | |
jmitc91516 | 1:a5258871b33d | 83 | char buff[100]; |
jmitc91516 | 1:a5258871b33d | 84 | |
jmitc91516 | 1:a5258871b33d | 85 | if(GetSettingValueFromQSPI(settingName, buff, 100) > 0) { |
jmitc91516 | 1:a5258871b33d | 86 | // We read a value for the specified setting from QSPI settings |
jmitc91516 | 1:a5258871b33d | 87 | sscanf(buff, "%d", &retVal); |
jmitc91516 | 1:a5258871b33d | 88 | } else { |
jmitc91516 | 1:a5258871b33d | 89 | // Value not found in QSPI settings |
jmitc91516 | 1:a5258871b33d | 90 | retVal = defaultValue; |
jmitc91516 | 1:a5258871b33d | 91 | } |
jmitc91516 | 1:a5258871b33d | 92 | |
jmitc91516 | 1:a5258871b33d | 93 | return retVal; |
jmitc91516 | 1:a5258871b33d | 94 | } |
jmitc91516 | 1:a5258871b33d | 95 | |
jmitc91516 | 1:a5258871b33d | 96 | /* |
jmitc91516 | 1:a5258871b33d | 97 | Puts (i.e. writes) a setting value as a string, |
jmitc91516 | 1:a5258871b33d | 98 | to the corresponding file in QSPI memory. |
jmitc91516 | 1:a5258871b33d | 99 | |
jmitc91516 | 1:a5258871b33d | 100 | Args: pointer to the setting name (as a string) |
jmitc91516 | 1:a5258871b33d | 101 | pointer to a buffer containing the value to be written (as a string) |
jmitc91516 | 1:a5258871b33d | 102 | the length of the buffer |
jmitc91516 | 1:a5258871b33d | 103 | |
jmitc91516 | 1:a5258871b33d | 104 | Returns the amount of data written |
jmitc91516 | 1:a5258871b33d | 105 | */ |
jmitc91516 | 1:a5258871b33d | 106 | int SettingsHandler::PutSettingValueToQSPI(char *settingName, char *settingValueBuff, int valueBuffLen) |
jmitc91516 | 1:a5258871b33d | 107 | { |
jmitc91516 | 1:a5258871b33d | 108 | FILE * pFile; |
jmitc91516 | 1:a5258871b33d | 109 | size_t result; |
jmitc91516 | 1:a5258871b33d | 110 | |
jmitc91516 | 1:a5258871b33d | 111 | char filename[100]; |
jmitc91516 | 1:a5258871b33d | 112 | sprintf(filename, "/qspi/%s.txt", settingName); |
jmitc91516 | 1:a5258871b33d | 113 | |
jmitc91516 | 1:a5258871b33d | 114 | FormatQSPIIfRequired(); |
jmitc91516 | 1:a5258871b33d | 115 | |
jmitc91516 | 1:a5258871b33d | 116 | // pFile = fopen (filename, "wb"); |
jmitc91516 | 1:a5258871b33d | 117 | // No - text mode |
jmitc91516 | 1:a5258871b33d | 118 | pFile = fopen (filename, "w"); |
jmitc91516 | 1:a5258871b33d | 119 | if (pFile == NULL) { |
jmitc91516 | 1:a5258871b33d | 120 | return 0; |
jmitc91516 | 1:a5258871b33d | 121 | } |
jmitc91516 | 1:a5258871b33d | 122 | |
jmitc91516 | 1:a5258871b33d | 123 | result = fwrite (settingValueBuff, 1, valueBuffLen, pFile); |
jmitc91516 | 1:a5258871b33d | 124 | |
jmitc91516 | 1:a5258871b33d | 125 | fclose (pFile); |
jmitc91516 | 1:a5258871b33d | 126 | |
jmitc91516 | 1:a5258871b33d | 127 | return result; |
jmitc91516 | 1:a5258871b33d | 128 | } |
jmitc91516 | 1:a5258871b33d | 129 | |
jmitc91516 | 1:a5258871b33d | 130 | /* |
jmitc91516 | 1:a5258871b33d | 131 | Settings values are stored in QSPI memory as strings. This function |
jmitc91516 | 1:a5258871b33d | 132 | takes an integer as the new value for the specified setting, |
jmitc91516 | 1:a5258871b33d | 133 | and converts it to a string before storing it. |
jmitc91516 | 1:a5258871b33d | 134 | |
jmitc91516 | 1:a5258871b33d | 135 | Args: the setting name |
jmitc91516 | 1:a5258871b33d | 136 | the new value for the setting |
jmitc91516 | 1:a5258871b33d | 137 | |
jmitc91516 | 1:a5258871b33d | 138 | No return value. |
jmitc91516 | 1:a5258871b33d | 139 | */ |
jmitc91516 | 1:a5258871b33d | 140 | void SettingsHandler::PutIntegerValueToQSPISettings(char *settingName, int value) |
jmitc91516 | 1:a5258871b33d | 141 | { |
jmitc91516 | 1:a5258871b33d | 142 | char buff[100]; |
jmitc91516 | 1:a5258871b33d | 143 | |
jmitc91516 | 1:a5258871b33d | 144 | sprintf(buff, "%d", value); |
jmitc91516 | 1:a5258871b33d | 145 | |
jmitc91516 | 1:a5258871b33d | 146 | PutSettingValueToQSPI(settingName, buff, strlen(buff)); |
jmitc91516 | 1:a5258871b33d | 147 | } |
jmitc91516 | 1:a5258871b33d | 148 | |
jmitc91516 | 1:a5258871b33d | 149 | /* |
jmitc91516 | 1:a5258871b33d | 150 | Displays the directory of files in QSPI memory. |
jmitc91516 | 1:a5258871b33d | 151 | |
jmitc91516 | 1:a5258871b33d | 152 | Used for debugging/testing - not appropriate or required in the 'real' system. |
jmitc91516 | 1:a5258871b33d | 153 | |
jmitc91516 | 1:a5258871b33d | 154 | Args: the X and Y coordinates at which to display the directory on the screen |
jmitc91516 | 1:a5258871b33d | 155 | */ |
jmitc91516 | 1:a5258871b33d | 156 | void SettingsHandler::DisplayQSPIDirectory(GuiConst_INT16S X, GuiConst_INT16S Y) |
jmitc91516 | 1:a5258871b33d | 157 | { |
jmitc91516 | 1:a5258871b33d | 158 | DIR *dp; |
jmitc91516 | 1:a5258871b33d | 159 | dp = opendir("/qspi/"); |
jmitc91516 | 1:a5258871b33d | 160 | |
jmitc91516 | 1:a5258871b33d | 161 | if(dp != NULL) { |
jmitc91516 | 1:a5258871b33d | 162 | struct dirent *dirp; |
jmitc91516 | 1:a5258871b33d | 163 | |
jmitc91516 | 1:a5258871b33d | 164 | EasyGUIDebugPrint("SH::Start of QSPI directory", X, Y); |
jmitc91516 | 1:a5258871b33d | 165 | Y += 30; |
jmitc91516 | 1:a5258871b33d | 166 | |
jmitc91516 | 1:a5258871b33d | 167 | while((dirp = readdir(dp)) != NULL) { |
jmitc91516 | 1:a5258871b33d | 168 | EasyGUIDebugPrint(dirp->d_name, X, Y); |
jmitc91516 | 1:a5258871b33d | 169 | Y += 30; |
jmitc91516 | 1:a5258871b33d | 170 | } |
jmitc91516 | 1:a5258871b33d | 171 | closedir(dp); |
jmitc91516 | 1:a5258871b33d | 172 | |
jmitc91516 | 1:a5258871b33d | 173 | EasyGUIDebugPrint("SH::End of QSPI directory", X, Y); |
jmitc91516 | 1:a5258871b33d | 174 | } else { |
jmitc91516 | 1:a5258871b33d | 175 | EasyGUIDebugPrint("SH::Failed to open QSPI directory", X, Y); |
jmitc91516 | 1:a5258871b33d | 176 | } |
jmitc91516 | 1:a5258871b33d | 177 | } |