4D systems Picaso uLCD 32PTU touch display library
Picaso_4DGL-32PTU_File.cpp@22:cea582ea74c1, 2016-09-27 (annotated)
- Committer:
- CaptainR
- Date:
- Tue Sep 27 08:48:08 2016 +0000
- Revision:
- 22:cea582ea74c1
- Parent:
- 21:ea68a8a3cea4
- Child:
- 23:dd2c28fa4dfd
fileCount, listFilenames, find first, find first and report (started)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CaptainR | 20:88e137b9ea46 | 1 | // |
CaptainR | 20:88e137b9ea46 | 2 | // Picaso_4DGL-32PTU is a class to drive 4D Systems TFT touch screens with PICASO processor |
CaptainR | 20:88e137b9ea46 | 3 | // Tested with NUCLEO L152RE development board |
CaptainR | 20:88e137b9ea46 | 4 | // Copyright (C) <2016> Rihards Balass <rihards.balass@gmail.com> |
CaptainR | 20:88e137b9ea46 | 5 | // |
CaptainR | 20:88e137b9ea46 | 6 | // Picaso_4DGL-32PTU is free software: you can redistribute it and/or modify |
CaptainR | 20:88e137b9ea46 | 7 | // it under the terms of the GNU General Public License as published by |
CaptainR | 20:88e137b9ea46 | 8 | // the Free Software Foundation, either version 3 of the License, or |
CaptainR | 20:88e137b9ea46 | 9 | // (at your option) any later version. |
CaptainR | 20:88e137b9ea46 | 10 | // |
CaptainR | 20:88e137b9ea46 | 11 | // Picaso_4DGL-32PTU is distributed in the hope that it will be useful, |
CaptainR | 20:88e137b9ea46 | 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
CaptainR | 20:88e137b9ea46 | 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
CaptainR | 20:88e137b9ea46 | 14 | // GNU General Public License for more details. |
CaptainR | 20:88e137b9ea46 | 15 | // |
CaptainR | 20:88e137b9ea46 | 16 | // You can see GNU General Public License at <http://www.gnu.org/licenses/>. |
CaptainR | 20:88e137b9ea46 | 17 | // |
CaptainR | 20:88e137b9ea46 | 18 | |
CaptainR | 20:88e137b9ea46 | 19 | #include "mbed.h" |
CaptainR | 20:88e137b9ea46 | 20 | #include "Picaso_4DGL-32PTU.h" |
CaptainR | 20:88e137b9ea46 | 21 | |
CaptainR | 20:88e137b9ea46 | 22 | //************************************************************************** |
CaptainR | 20:88e137b9ea46 | 23 | // Starts up the FAT16 disk file services and allocates a small 32 byte control block |
CaptainR | 20:88e137b9ea46 | 24 | // for subsequent use. When you open a file using the “File Open” command |
CaptainR | 20:88e137b9ea46 | 25 | // further 512 + 44 = 556 bytes are attached to the FAT16 file control block. |
CaptainR | 20:88e137b9ea46 | 26 | // When you close a file using the “File Close” command, the 556 byte allocation |
CaptainR | 20:88e137b9ea46 | 27 | // is released leaving the 32 byte file control block. |
CaptainR | 20:88e137b9ea46 | 28 | // The File Mount command must be called before any other FAT16 file related |
CaptainR | 20:88e137b9ea46 | 29 | // functions can be used. The control block and all FAT16 file resources are |
CaptainR | 20:88e137b9ea46 | 30 | // completely released with the “File Unmount” command. |
CaptainR | 20:88e137b9ea46 | 31 | //************************************************************************** |
CaptainR | 20:88e137b9ea46 | 32 | bool PICASO_4DGL :: file_Mount() { |
CaptainR | 20:88e137b9ea46 | 33 | |
CaptainR | 20:88e137b9ea46 | 34 | char command[2] = ""; |
CaptainR | 20:88e137b9ea46 | 35 | |
CaptainR | 20:88e137b9ea46 | 36 | command[0] = (FILE_MOUNT >> (8*1)) & 0xff; |
CaptainR | 20:88e137b9ea46 | 37 | command[1] = (FILE_MOUNT >> (8*0)) & 0xff; |
CaptainR | 20:88e137b9ea46 | 38 | |
CaptainR | 20:88e137b9ea46 | 39 | writeCOMMAND(command, 2); |
CaptainR | 20:88e137b9ea46 | 40 | bool success = fileMountResponse(); |
CaptainR | 20:88e137b9ea46 | 41 | #ifdef DEBUGMODE |
CaptainR | 20:88e137b9ea46 | 42 | pc.printf("\n\r DEBUG: FAT16 Mount: %s\n\r", success ? "true" : "false"); |
CaptainR | 20:88e137b9ea46 | 43 | #endif |
CaptainR | 20:88e137b9ea46 | 44 | return success; |
CaptainR | 20:88e137b9ea46 | 45 | } |
CaptainR | 21:ea68a8a3cea4 | 46 | |
CaptainR | 21:ea68a8a3cea4 | 47 | //************************************************************************** |
CaptainR | 21:ea68a8a3cea4 | 48 | // Returns the most recent error code or 0 if there were no errors. |
CaptainR | 21:ea68a8a3cea4 | 49 | // Returns Error Number. |
CaptainR | 21:ea68a8a3cea4 | 50 | // 1 IDE command execution error |
CaptainR | 21:ea68a8a3cea4 | 51 | // 2 CARD not present |
CaptainR | 21:ea68a8a3cea4 | 52 | // 3 WRONG partition type, not FAT16 |
CaptainR | 21:ea68a8a3cea4 | 53 | // 4 MBR sector invalid signature |
CaptainR | 21:ea68a8a3cea4 | 54 | // 5 Boot Record invalid signature |
CaptainR | 21:ea68a8a3cea4 | 55 | // 6 Media not mounted |
CaptainR | 21:ea68a8a3cea4 | 56 | // 7 File not found in open for read |
CaptainR | 21:ea68a8a3cea4 | 57 | // 8 File not open |
CaptainR | 21:ea68a8a3cea4 | 58 | // 9 Fat attempt to read beyond EOF |
CaptainR | 21:ea68a8a3cea4 | 59 | // 10 Reached the end of file |
CaptainR | 21:ea68a8a3cea4 | 60 | // 11 Invalid cluster value > maxcls |
CaptainR | 21:ea68a8a3cea4 | 61 | // 12 All root dir entry are taken |
CaptainR | 21:ea68a8a3cea4 | 62 | // 13 All clusters in partition are taken |
CaptainR | 21:ea68a8a3cea4 | 63 | // 14 A file with same name exist already |
CaptainR | 21:ea68a8a3cea4 | 64 | // 15 Cannot init the CARD |
CaptainR | 21:ea68a8a3cea4 | 65 | // 16 Cannot read the MBR |
CaptainR | 21:ea68a8a3cea4 | 66 | // 17 Malloc could not allocate the FILE struct |
CaptainR | 21:ea68a8a3cea4 | 67 | // 18 Mode was not r.w. |
CaptainR | 21:ea68a8a3cea4 | 68 | // 19 Failure during FILE search |
CaptainR | 21:ea68a8a3cea4 | 69 | // 20 Invalid Filename |
CaptainR | 21:ea68a8a3cea4 | 70 | // 21 bad media |
CaptainR | 21:ea68a8a3cea4 | 71 | // 22 Sector Read fail |
CaptainR | 21:ea68a8a3cea4 | 72 | // 23 Sector write fail |
CaptainR | 21:ea68a8a3cea4 | 73 | //************************************************************************** |
CaptainR | 21:ea68a8a3cea4 | 74 | short PICASO_4DGL :: file_Error() { |
CaptainR | 21:ea68a8a3cea4 | 75 | |
CaptainR | 21:ea68a8a3cea4 | 76 | char command[2] = ""; |
CaptainR | 21:ea68a8a3cea4 | 77 | |
CaptainR | 21:ea68a8a3cea4 | 78 | command[0] = (FILE_ERROR >> (8*1)) & 0xff; |
CaptainR | 21:ea68a8a3cea4 | 79 | command[1] = (FILE_ERROR >> (8*0)) & 0xff; |
CaptainR | 21:ea68a8a3cea4 | 80 | |
CaptainR | 21:ea68a8a3cea4 | 81 | writeCOMMAND(command, 2); |
CaptainR | 21:ea68a8a3cea4 | 82 | short error = fileErrorResponse(); |
CaptainR | 21:ea68a8a3cea4 | 83 | #ifdef DEBUGMODE |
CaptainR | 21:ea68a8a3cea4 | 84 | pc.printf("\n\r DEBUG: FAT16 Error: %i\n\r", error); |
CaptainR | 21:ea68a8a3cea4 | 85 | #endif |
CaptainR | 21:ea68a8a3cea4 | 86 | return error; |
CaptainR | 21:ea68a8a3cea4 | 87 | } |
CaptainR | 21:ea68a8a3cea4 | 88 | |
CaptainR | 22:cea582ea74c1 | 89 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 90 | // Returns number of files found that match the criteria. |
CaptainR | 22:cea582ea74c1 | 91 | // The wild card character '*' matches up with any combination of allowable |
CaptainR | 22:cea582ea74c1 | 92 | // characters and '?' matches up with any single allowable character. |
CaptainR | 22:cea582ea74c1 | 93 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 94 | short PICASO_4DGL :: file_Count(char *filename) { |
CaptainR | 22:cea582ea74c1 | 95 | |
CaptainR | 22:cea582ea74c1 | 96 | int size = 3 + strlen(filename); |
CaptainR | 22:cea582ea74c1 | 97 | int i, k, j = 2; |
CaptainR | 22:cea582ea74c1 | 98 | char *command; |
CaptainR | 22:cea582ea74c1 | 99 | command = (char *)malloc(sizeof(char) * size); |
CaptainR | 22:cea582ea74c1 | 100 | for(i = 0; i < size; i++) command[i] = 0; |
CaptainR | 22:cea582ea74c1 | 101 | |
CaptainR | 22:cea582ea74c1 | 102 | command[0] = (FILE_COUNT >> (8*1)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 103 | command[1] = (FILE_COUNT >> (8*0)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 104 | for (k = 0; k < size-1; k++) command[j++] = filename[k]; |
CaptainR | 22:cea582ea74c1 | 105 | command[j] = 0; |
CaptainR | 22:cea582ea74c1 | 106 | |
CaptainR | 22:cea582ea74c1 | 107 | writeCOMMAND(command, size); |
CaptainR | 22:cea582ea74c1 | 108 | short success = fileCountResponse(); |
CaptainR | 22:cea582ea74c1 | 109 | free(command); |
CaptainR | 22:cea582ea74c1 | 110 | #ifdef DEBUGMODE |
CaptainR | 22:cea582ea74c1 | 111 | pc.printf("\n\r DEBUG: %s count = %i\n\r", filename, success); |
CaptainR | 22:cea582ea74c1 | 112 | #endif |
CaptainR | 22:cea582ea74c1 | 113 | return success; |
CaptainR | 22:cea582ea74c1 | 114 | } |
CaptainR | 22:cea582ea74c1 | 115 | |
CaptainR | 22:cea582ea74c1 | 116 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 117 | // Lists the stream of file names that agree with the search key on the Display Screen. |
CaptainR | 22:cea582ea74c1 | 118 | // Returns number of files found that match the criteria. |
CaptainR | 22:cea582ea74c1 | 119 | // The wild card character '*' matches up with any combination of allowable characters |
CaptainR | 22:cea582ea74c1 | 120 | // and '?' matches up with any single allowable character. |
CaptainR | 22:cea582ea74c1 | 121 | // |
CaptainR | 22:cea582ea74c1 | 122 | // Note: “Find First File and Report” and “Find Next File and Report” are |
CaptainR | 22:cea582ea74c1 | 123 | // recommended alternatives in order to return the responses. |
CaptainR | 22:cea582ea74c1 | 124 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 125 | short PICASO_4DGL :: file_Dir(char *filename) { |
CaptainR | 22:cea582ea74c1 | 126 | |
CaptainR | 22:cea582ea74c1 | 127 | int size = 3 + strlen(filename); |
CaptainR | 22:cea582ea74c1 | 128 | int i, k, j = 2; |
CaptainR | 22:cea582ea74c1 | 129 | char *command; |
CaptainR | 22:cea582ea74c1 | 130 | command = (char *)malloc(sizeof(char) * size); |
CaptainR | 22:cea582ea74c1 | 131 | for(i = 0; i < size; i++) command[i] = 0; |
CaptainR | 22:cea582ea74c1 | 132 | |
CaptainR | 22:cea582ea74c1 | 133 | command[0] = (FILE_DIR >> (8*1)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 134 | command[1] = (FILE_DIR >> (8*0)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 135 | for (k = 0; k < size-1; k++) command[j++] = filename[k]; |
CaptainR | 22:cea582ea74c1 | 136 | command[j] = 0; |
CaptainR | 22:cea582ea74c1 | 137 | |
CaptainR | 22:cea582ea74c1 | 138 | writeCOMMAND(command, size); |
CaptainR | 22:cea582ea74c1 | 139 | short success = fileCountResponse(); |
CaptainR | 22:cea582ea74c1 | 140 | free(command); |
CaptainR | 22:cea582ea74c1 | 141 | #ifdef DEBUGMODE |
CaptainR | 22:cea582ea74c1 | 142 | pc.printf("\n\r DEBUG: %s count = %i\n\r", filename, success); |
CaptainR | 22:cea582ea74c1 | 143 | #endif |
CaptainR | 22:cea582ea74c1 | 144 | return success; |
CaptainR | 22:cea582ea74c1 | 145 | } |
CaptainR | 22:cea582ea74c1 | 146 | |
CaptainR | 22:cea582ea74c1 | 147 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 148 | // Returns true if at least 1 file exists that satisfies the file argument. |
CaptainR | 22:cea582ea74c1 | 149 | // Wildcards are usually used so if the “Find First File” command returns true, |
CaptainR | 22:cea582ea74c1 | 150 | // further tests can be made using the “Find Next File” command to find all |
CaptainR | 22:cea582ea74c1 | 151 | // the files that match the wildcard class. |
CaptainR | 22:cea582ea74c1 | 152 | // Note that the filename is printed on the screen. |
CaptainR | 22:cea582ea74c1 | 153 | // |
CaptainR | 22:cea582ea74c1 | 154 | // Note: “Find First File and Report” and “Find Next File and Report” are |
CaptainR | 22:cea582ea74c1 | 155 | // recommended alternatives in order to return the responses. |
CaptainR | 22:cea582ea74c1 | 156 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 157 | void PICASO_4DGL :: file_FindFirst(char *filename) { |
CaptainR | 22:cea582ea74c1 | 158 | |
CaptainR | 22:cea582ea74c1 | 159 | int size = 3 + strlen(filename); |
CaptainR | 22:cea582ea74c1 | 160 | int i, k, j = 2; |
CaptainR | 22:cea582ea74c1 | 161 | char *command; |
CaptainR | 22:cea582ea74c1 | 162 | command = (char *)malloc(sizeof(char) * size); |
CaptainR | 22:cea582ea74c1 | 163 | for(i = 0; i < size; i++) command[i] = 0; |
CaptainR | 22:cea582ea74c1 | 164 | |
CaptainR | 22:cea582ea74c1 | 165 | command[0] = (FILE_FIRST >> (8*1)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 166 | command[1] = (FILE_FIRST >> (8*0)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 167 | for (k = 0; k < size-1; k++) command[j++] = filename[k]; |
CaptainR | 22:cea582ea74c1 | 168 | command[j] = 0; |
CaptainR | 22:cea582ea74c1 | 169 | |
CaptainR | 22:cea582ea74c1 | 170 | writeCOMMAND(command, size); |
CaptainR | 22:cea582ea74c1 | 171 | bool success = writeSectorResponse(3); |
CaptainR | 22:cea582ea74c1 | 172 | free(command); |
CaptainR | 22:cea582ea74c1 | 173 | #ifdef DEBUGMODE |
CaptainR | 22:cea582ea74c1 | 174 | pc.printf("\n\r DEBUG: %s count = %s\n\r", filename, success ? " true" : "false"); |
CaptainR | 22:cea582ea74c1 | 175 | #endif |
CaptainR | 22:cea582ea74c1 | 176 | } |
CaptainR | 22:cea582ea74c1 | 177 | |
CaptainR | 22:cea582ea74c1 | 178 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 179 | // The Find First File and Report command returns the length of the filename |
CaptainR | 22:cea582ea74c1 | 180 | // and the filename if at least 1 file exists that matches the criteria. |
CaptainR | 22:cea582ea74c1 | 181 | // |
CaptainR | 22:cea582ea74c1 | 182 | // Wildcards are usually used so if Find First File and Report command |
CaptainR | 22:cea582ea74c1 | 183 | // returns the stringlength and filename, further tests can be made using |
CaptainR | 22:cea582ea74c1 | 184 | // “Find Next File” or “Find Next File and Report” commands to find all the files |
CaptainR | 22:cea582ea74c1 | 185 | // that match the wildcard class. |
CaptainR | 22:cea582ea74c1 | 186 | //************************************************************************** |
CaptainR | 22:cea582ea74c1 | 187 | char *PICASO_4DGL :: file_FindFirstRet(char *name) { |
CaptainR | 22:cea582ea74c1 | 188 | |
CaptainR | 22:cea582ea74c1 | 189 | int size = 3 + strlen(name); |
CaptainR | 22:cea582ea74c1 | 190 | int i, k, j = 2; |
CaptainR | 22:cea582ea74c1 | 191 | char *command; |
CaptainR | 22:cea582ea74c1 | 192 | command = (char *)malloc(sizeof(char) * size); |
CaptainR | 22:cea582ea74c1 | 193 | for(i = 0; i < size; i++) command[i] = 0; |
CaptainR | 22:cea582ea74c1 | 194 | |
CaptainR | 22:cea582ea74c1 | 195 | command[0] = (FILE_FIRST_RET >> (8*1)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 196 | command[1] = (FILE_FIRST_RET >> (8*0)) & 0xff; |
CaptainR | 22:cea582ea74c1 | 197 | for (k = 0; k < size-1; k++) command[j++] = name[k]; |
CaptainR | 22:cea582ea74c1 | 198 | command[j] = 0; |
CaptainR | 22:cea582ea74c1 | 199 | |
CaptainR | 22:cea582ea74c1 | 200 | writeCOMMAND(command, size); |
CaptainR | 22:cea582ea74c1 | 201 | char filename[FILENAME_LENGTH]; // create filename array |
CaptainR | 22:cea582ea74c1 | 202 | |
CaptainR | 22:cea582ea74c1 | 203 | free(command); |
CaptainR | 22:cea582ea74c1 | 204 | #ifdef DEBUGMODE |
CaptainR | 22:cea582ea74c1 | 205 | pc.printf("\n\r DEBUG: %s count = %s\n\r", filename, success ? " true" : "false"); |
CaptainR | 22:cea582ea74c1 | 206 | #endif |
CaptainR | 22:cea582ea74c1 | 207 | return filename; |
CaptainR | 22:cea582ea74c1 | 208 | } |
CaptainR | 21:ea68a8a3cea4 | 209 | |
CaptainR | 21:ea68a8a3cea4 | 210 | |
CaptainR | 21:ea68a8a3cea4 | 211 |