Rihards Balass / 4DGL-mbed-32PTU
Committer:
CaptainR
Date:
Wed Sep 28 12:41:32 2016 +0000
Revision:
26:c6a803706a42
Parent:
25:015631f9e875
Child:
27:dbf79d116497
File open File close File read File seek File index File tell File write File size ; Display image and screen capture not working yet???

Who changed what in which revision?

UserRevisionLine numberNew 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 26:c6a803706a42 73 // 26 File is empty (size 0)
CaptainR 21:ea68a8a3cea4 74 //**************************************************************************
CaptainR 21:ea68a8a3cea4 75 short PICASO_4DGL :: file_Error() {
CaptainR 21:ea68a8a3cea4 76
CaptainR 21:ea68a8a3cea4 77 char command[2] = "";
CaptainR 21:ea68a8a3cea4 78
CaptainR 21:ea68a8a3cea4 79 command[0] = (FILE_ERROR >> (8*1)) & 0xff;
CaptainR 21:ea68a8a3cea4 80 command[1] = (FILE_ERROR >> (8*0)) & 0xff;
CaptainR 21:ea68a8a3cea4 81
CaptainR 21:ea68a8a3cea4 82 writeCOMMAND(command, 2);
CaptainR 21:ea68a8a3cea4 83 short error = fileErrorResponse();
CaptainR 21:ea68a8a3cea4 84 #ifdef DEBUGMODE
CaptainR 21:ea68a8a3cea4 85 pc.printf("\n\r DEBUG: FAT16 Error: %i\n\r", error);
CaptainR 21:ea68a8a3cea4 86 #endif
CaptainR 21:ea68a8a3cea4 87 return error;
CaptainR 21:ea68a8a3cea4 88 }
CaptainR 21:ea68a8a3cea4 89
CaptainR 22:cea582ea74c1 90 //**************************************************************************
CaptainR 22:cea582ea74c1 91 // Returns number of files found that match the criteria.
CaptainR 22:cea582ea74c1 92 // The wild card character '*' matches up with any combination of allowable
CaptainR 22:cea582ea74c1 93 // characters and '?' matches up with any single allowable character.
CaptainR 22:cea582ea74c1 94 //**************************************************************************
CaptainR 22:cea582ea74c1 95 short PICASO_4DGL :: file_Count(char *filename) {
CaptainR 22:cea582ea74c1 96
CaptainR 22:cea582ea74c1 97 int size = 3 + strlen(filename);
CaptainR 22:cea582ea74c1 98 int i, k, j = 2;
CaptainR 22:cea582ea74c1 99 char *command;
CaptainR 22:cea582ea74c1 100 command = (char *)malloc(sizeof(char) * size);
CaptainR 22:cea582ea74c1 101 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 22:cea582ea74c1 102
CaptainR 22:cea582ea74c1 103 command[0] = (FILE_COUNT >> (8*1)) & 0xff;
CaptainR 22:cea582ea74c1 104 command[1] = (FILE_COUNT >> (8*0)) & 0xff;
CaptainR 22:cea582ea74c1 105 for (k = 0; k < size-1; k++) command[j++] = filename[k];
CaptainR 22:cea582ea74c1 106 command[j] = 0;
CaptainR 22:cea582ea74c1 107
CaptainR 22:cea582ea74c1 108 writeCOMMAND(command, size);
CaptainR 22:cea582ea74c1 109 short success = fileCountResponse();
CaptainR 22:cea582ea74c1 110 free(command);
CaptainR 22:cea582ea74c1 111 #ifdef DEBUGMODE
CaptainR 22:cea582ea74c1 112 pc.printf("\n\r DEBUG: %s count = %i\n\r", filename, success);
CaptainR 22:cea582ea74c1 113 #endif
CaptainR 22:cea582ea74c1 114 return success;
CaptainR 22:cea582ea74c1 115 }
CaptainR 22:cea582ea74c1 116
CaptainR 22:cea582ea74c1 117 //**************************************************************************
CaptainR 22:cea582ea74c1 118 // Lists the stream of file names that agree with the search key on the Display Screen.
CaptainR 22:cea582ea74c1 119 // Returns number of files found that match the criteria.
CaptainR 22:cea582ea74c1 120 // The wild card character '*' matches up with any combination of allowable characters
CaptainR 22:cea582ea74c1 121 // and '?' matches up with any single allowable character.
CaptainR 22:cea582ea74c1 122 //
CaptainR 22:cea582ea74c1 123 // Note: “Find First File and Report” and “Find Next File and Report” are
CaptainR 22:cea582ea74c1 124 // recommended alternatives in order to return the responses.
CaptainR 22:cea582ea74c1 125 //**************************************************************************
CaptainR 22:cea582ea74c1 126 short PICASO_4DGL :: file_Dir(char *filename) {
CaptainR 22:cea582ea74c1 127
CaptainR 22:cea582ea74c1 128 int size = 3 + strlen(filename);
CaptainR 22:cea582ea74c1 129 int i, k, j = 2;
CaptainR 22:cea582ea74c1 130 char *command;
CaptainR 22:cea582ea74c1 131 command = (char *)malloc(sizeof(char) * size);
CaptainR 22:cea582ea74c1 132 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 22:cea582ea74c1 133
CaptainR 22:cea582ea74c1 134 command[0] = (FILE_DIR >> (8*1)) & 0xff;
CaptainR 22:cea582ea74c1 135 command[1] = (FILE_DIR >> (8*0)) & 0xff;
CaptainR 22:cea582ea74c1 136 for (k = 0; k < size-1; k++) command[j++] = filename[k];
CaptainR 22:cea582ea74c1 137 command[j] = 0;
CaptainR 22:cea582ea74c1 138
CaptainR 22:cea582ea74c1 139 writeCOMMAND(command, size);
CaptainR 22:cea582ea74c1 140 short success = fileCountResponse();
CaptainR 22:cea582ea74c1 141 free(command);
CaptainR 22:cea582ea74c1 142 #ifdef DEBUGMODE
CaptainR 22:cea582ea74c1 143 pc.printf("\n\r DEBUG: %s count = %i\n\r", filename, success);
CaptainR 22:cea582ea74c1 144 #endif
CaptainR 22:cea582ea74c1 145 return success;
CaptainR 22:cea582ea74c1 146 }
CaptainR 22:cea582ea74c1 147
CaptainR 22:cea582ea74c1 148 //**************************************************************************
CaptainR 22:cea582ea74c1 149 // Returns true if at least 1 file exists that satisfies the file argument.
CaptainR 22:cea582ea74c1 150 // Wildcards are usually used so if the “Find First File” command returns true,
CaptainR 22:cea582ea74c1 151 // further tests can be made using the “Find Next File” command to find all
CaptainR 22:cea582ea74c1 152 // the files that match the wildcard class.
CaptainR 22:cea582ea74c1 153 // Note that the filename is printed on the screen.
CaptainR 22:cea582ea74c1 154 //
CaptainR 22:cea582ea74c1 155 // Note: “Find First File and Report” and “Find Next File and Report” are
CaptainR 22:cea582ea74c1 156 // recommended alternatives in order to return the responses.
CaptainR 22:cea582ea74c1 157 //**************************************************************************
CaptainR 22:cea582ea74c1 158 void PICASO_4DGL :: file_FindFirst(char *filename) {
CaptainR 22:cea582ea74c1 159
CaptainR 22:cea582ea74c1 160 int size = 3 + strlen(filename);
CaptainR 22:cea582ea74c1 161 int i, k, j = 2;
CaptainR 22:cea582ea74c1 162 char *command;
CaptainR 22:cea582ea74c1 163 command = (char *)malloc(sizeof(char) * size);
CaptainR 22:cea582ea74c1 164 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 22:cea582ea74c1 165
CaptainR 22:cea582ea74c1 166 command[0] = (FILE_FIRST >> (8*1)) & 0xff;
CaptainR 22:cea582ea74c1 167 command[1] = (FILE_FIRST >> (8*0)) & 0xff;
CaptainR 22:cea582ea74c1 168 for (k = 0; k < size-1; k++) command[j++] = filename[k];
CaptainR 22:cea582ea74c1 169 command[j] = 0;
CaptainR 22:cea582ea74c1 170
CaptainR 24:19c77967674e 171 puts("\n\rFirst file = ");
CaptainR 22:cea582ea74c1 172 writeCOMMAND(command, size);
CaptainR 22:cea582ea74c1 173 bool success = writeSectorResponse(3);
CaptainR 22:cea582ea74c1 174 free(command);
CaptainR 22:cea582ea74c1 175 #ifdef DEBUGMODE
CaptainR 24:19c77967674e 176 pc.printf("\n\r DEBUG: %s Found = %s\n\r", filename, success ? " true" : "false");
CaptainR 22:cea582ea74c1 177 #endif
CaptainR 22:cea582ea74c1 178 }
CaptainR 22:cea582ea74c1 179
CaptainR 22:cea582ea74c1 180 //**************************************************************************
CaptainR 22:cea582ea74c1 181 // The Find First File and Report command returns the length of the filename
CaptainR 22:cea582ea74c1 182 // and the filename if at least 1 file exists that matches the criteria.
CaptainR 22:cea582ea74c1 183 //
CaptainR 22:cea582ea74c1 184 // Wildcards are usually used so if Find First File and Report command
CaptainR 22:cea582ea74c1 185 // returns the stringlength and filename, further tests can be made using
CaptainR 22:cea582ea74c1 186 // “Find Next File” or “Find Next File and Report” commands to find all the files
CaptainR 22:cea582ea74c1 187 // that match the wildcard class.
CaptainR 23:dd2c28fa4dfd 188 //
CaptainR 23:dd2c28fa4dfd 189 // you have to give the function the output char array to put filename in
CaptainR 22:cea582ea74c1 190 //**************************************************************************
CaptainR 23:dd2c28fa4dfd 191 short PICASO_4DGL :: file_FindFirstRet(char *filename, char *outStr) {
CaptainR 22:cea582ea74c1 192
CaptainR 23:dd2c28fa4dfd 193 int size = 3 + strlen(filename);
CaptainR 22:cea582ea74c1 194 int i, k, j = 2;
CaptainR 22:cea582ea74c1 195 char *command;
CaptainR 22:cea582ea74c1 196 command = (char *)malloc(sizeof(char) * size);
CaptainR 22:cea582ea74c1 197 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 22:cea582ea74c1 198
CaptainR 22:cea582ea74c1 199 command[0] = (FILE_FIRST_RET >> (8*1)) & 0xff;
CaptainR 22:cea582ea74c1 200 command[1] = (FILE_FIRST_RET >> (8*0)) & 0xff;
CaptainR 23:dd2c28fa4dfd 201 for (k = 0; k < size-1; k++) command[j++] = filename[k];
CaptainR 22:cea582ea74c1 202 command[j] = 0;
CaptainR 22:cea582ea74c1 203
CaptainR 22:cea582ea74c1 204 writeCOMMAND(command, size);
CaptainR 23:dd2c28fa4dfd 205 short len = getFilenameResponse(outStr);
CaptainR 22:cea582ea74c1 206
CaptainR 22:cea582ea74c1 207 free(command);
CaptainR 22:cea582ea74c1 208 #ifdef DEBUGMODE
CaptainR 23:dd2c28fa4dfd 209 pc.printf("\n\r DEBUG: Searching for %s Found filename = %s\n\r", filename, outStr);
CaptainR 22:cea582ea74c1 210 #endif
CaptainR 23:dd2c28fa4dfd 211 return len;
CaptainR 22:cea582ea74c1 212 }
CaptainR 21:ea68a8a3cea4 213
CaptainR 24:19c77967674e 214 //**************************************************************************
CaptainR 24:19c77967674e 215 // The Find Next File command returns true if more file exists that satisfies
CaptainR 24:19c77967674e 216 // the file argument that was given for the “Find First File” or
CaptainR 24:19c77967674e 217 // “Find First File and Report” commands.
CaptainR 24:19c77967674e 218 // Wildcards must be used for the “Find First File” or “Find First File and Report”
CaptainR 24:19c77967674e 219 // commands else this function will always return zero as the only occurrence
CaptainR 24:19c77967674e 220 // will have already been found.
CaptainR 24:19c77967674e 221 //
CaptainR 24:19c77967674e 222 // Note that the filename is printed on the screen.
CaptainR 24:19c77967674e 223 //**************************************************************************
CaptainR 24:19c77967674e 224 void PICASO_4DGL :: file_FindNext() {
CaptainR 24:19c77967674e 225
CaptainR 24:19c77967674e 226 char command[2];
CaptainR 24:19c77967674e 227
CaptainR 24:19c77967674e 228 command[0] = (FILE_NEXT >> (8*1)) & 0xff;
CaptainR 24:19c77967674e 229 command[1] = (FILE_NEXT >> (8*0)) & 0xff;
CaptainR 24:19c77967674e 230
CaptainR 24:19c77967674e 231 puts("\n\rNext file = ");
CaptainR 24:19c77967674e 232 writeCOMMAND(command, 2);
CaptainR 24:19c77967674e 233 bool success = writeSectorResponse(3);
CaptainR 24:19c77967674e 234 #ifdef DEBUGMODE
CaptainR 24:19c77967674e 235 pc.printf("\n\r DEBUG: next file found = %s\n\r", success ? " true" : "false");
CaptainR 24:19c77967674e 236 #endif
CaptainR 24:19c77967674e 237 }
CaptainR 24:19c77967674e 238
CaptainR 24:19c77967674e 239 //**************************************************************************
CaptainR 24:19c77967674e 240 // Returns length of the filename and the filename if at least 1 file exists
CaptainR 24:19c77967674e 241 // that matches the criteria given for the “Find First File” or
CaptainR 24:19c77967674e 242 // “Find First File and Report” commands.
CaptainR 24:19c77967674e 243 // Wildcards must be used for the “Find First File” or “Find First File and Report”
CaptainR 24:19c77967674e 244 // commands else this function will always return zero as the only occurrence will have
CaptainR 24:19c77967674e 245 // already been found.
CaptainR 24:19c77967674e 246 //
CaptainR 24:19c77967674e 247 // Wildcards are usually used, so if the “Find First File” or “Find First File and Report”
CaptainR 24:19c77967674e 248 // commands return the stringlength and filename, further tests can be made using Find
CaptainR 24:19c77967674e 249 // Next File and Report command to find all the files that match the wildcard class.
CaptainR 24:19c77967674e 250 //**************************************************************************
CaptainR 24:19c77967674e 251 short PICASO_4DGL :: file_FindNextRet(char *outStr) {
CaptainR 24:19c77967674e 252
CaptainR 24:19c77967674e 253 char command[2];
CaptainR 24:19c77967674e 254
CaptainR 24:19c77967674e 255 command[0] = (FILE_NEXT_RET >> (8*1)) & 0xff;
CaptainR 24:19c77967674e 256 command[1] = (FILE_NEXT_RET >> (8*0)) & 0xff;
CaptainR 24:19c77967674e 257
CaptainR 24:19c77967674e 258 writeCOMMAND(command, 2);
CaptainR 24:19c77967674e 259 short len = getFilenameResponse(outStr);
CaptainR 24:19c77967674e 260
CaptainR 24:19c77967674e 261 #ifdef DEBUGMODE
CaptainR 24:19c77967674e 262 pc.printf("\n\r DEBUG: Found next filename = %s\n\r", outStr);
CaptainR 24:19c77967674e 263 #endif
CaptainR 24:19c77967674e 264 return len;
CaptainR 24:19c77967674e 265 }
CaptainR 21:ea68a8a3cea4 266
CaptainR 25:015631f9e875 267 //**************************************************************************
CaptainR 25:015631f9e875 268 // Tests for the existence of the file provided with the search key. Returns TRUE if found.
CaptainR 25:015631f9e875 269 //**************************************************************************
CaptainR 25:015631f9e875 270 bool PICASO_4DGL :: file_Exists(char *filename) {
CaptainR 25:015631f9e875 271
CaptainR 25:015631f9e875 272 int size = 3 + strlen(filename);
CaptainR 25:015631f9e875 273 int i, k, j = 2;
CaptainR 25:015631f9e875 274 char *command;
CaptainR 25:015631f9e875 275 command = (char *)malloc(sizeof(char) * size);
CaptainR 25:015631f9e875 276 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 25:015631f9e875 277
CaptainR 25:015631f9e875 278 command[0] = (FILE_EXISTS >> (8*1)) & 0xff;
CaptainR 25:015631f9e875 279 command[1] = (FILE_EXISTS >> (8*0)) & 0xff;
CaptainR 25:015631f9e875 280 for (k = 0; k < size-1; k++) command[j++] = filename[k];
CaptainR 25:015631f9e875 281 command[j] = 0;
CaptainR 25:015631f9e875 282
CaptainR 25:015631f9e875 283 writeCOMMAND(command, size);
CaptainR 25:015631f9e875 284 bool success = writeSectorResponse(3);
CaptainR 25:015631f9e875 285 free(command);
CaptainR 25:015631f9e875 286 #ifdef DEBUGMODE
CaptainR 25:015631f9e875 287 pc.printf("\n\r DEBUG: %s Found = %s\n\r", filename, success ? " true" : "false");
CaptainR 25:015631f9e875 288 #endif
CaptainR 25:015631f9e875 289 return success;
CaptainR 25:015631f9e875 290 }
CaptainR 21:ea68a8a3cea4 291
CaptainR 26:c6a803706a42 292 //**************************************************************************
CaptainR 26:c6a803706a42 293 // Returns handle if file exists. The file ‘handle’ that is created is now used as reference for ‘filename’
CaptainR 26:c6a803706a42 294 // for further file commands such as “File Close”, etc. For File Write and File Append modes ('w' and 'a')
CaptainR 26:c6a803706a42 295 // the file is created if it does not exist. If the file is opened for append and it already exists,
CaptainR 26:c6a803706a42 296 // the file pointer is set to the end of the file ready for appending, else the file pointer will be
CaptainR 26:c6a803706a42 297 // set to the start of the newly created file.
CaptainR 26:c6a803706a42 298 //
CaptainR 26:c6a803706a42 299 // If the file was opened successfully, the internal error number is set to 0 (i.e. no errors) and can be
CaptainR 26:c6a803706a42 300 // read with the “File Error” command. For File Read mode ('r') the file must exist else a null handle
CaptainR 26:c6a803706a42 301 // (0x00, 0x00) is returned and the 'file not found' error number is set which can be read with the “File Error” command.
CaptainR 26:c6a803706a42 302 //
CaptainR 26:c6a803706a42 303 // Note: If a file is opened for File Write mode 'w', and the file already exists, the operation will fail.
CaptainR 26:c6a803706a42 304 // Unlike C and some other languages where the file will be erased ready for re-writing when opened for writing,
CaptainR 26:c6a803706a42 305 // 4DGL offers a simple level of protection that ensures that a file must be purposely erased before being re-written.
CaptainR 26:c6a803706a42 306 // Note: Beginning with the v4.0 PmmC a file opened with FILE_APPEND may be randomly read and or written.
CaptainR 26:c6a803706a42 307 // Also any altered file will have the Archive bit set in the directory entry.
CaptainR 26:c6a803706a42 308 //**************************************************************************
CaptainR 26:c6a803706a42 309 short PICASO_4DGL :: file_Open(char *filename, char openMode) {
CaptainR 26:c6a803706a42 310
CaptainR 26:c6a803706a42 311 int size = 4 + strlen(filename);
CaptainR 26:c6a803706a42 312 int i, k, j = 2;
CaptainR 26:c6a803706a42 313 char *command;
CaptainR 26:c6a803706a42 314 command = (char *)malloc(sizeof(char) * size);
CaptainR 26:c6a803706a42 315 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 26:c6a803706a42 316
CaptainR 26:c6a803706a42 317 command[0] = (FILE_OPEN >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 318 command[1] = (FILE_OPEN >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 319 for (k = 0; k < size-2; k++) {
CaptainR 26:c6a803706a42 320 command[j++] = filename[k];
CaptainR 26:c6a803706a42 321 }
CaptainR 26:c6a803706a42 322 //command[j++] = 0; // null terminated string
CaptainR 26:c6a803706a42 323
CaptainR 26:c6a803706a42 324 command[size-1] = openMode; // open mode
CaptainR 26:c6a803706a42 325
CaptainR 26:c6a803706a42 326 writeCOMMAND(command, size);
CaptainR 26:c6a803706a42 327 short success = fileCountResponse();
CaptainR 26:c6a803706a42 328 free(command);
CaptainR 26:c6a803706a42 329 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 330 pc.printf("\n\r DEBUG: Opened new file %s handle = %i\n\r", filename, success);
CaptainR 26:c6a803706a42 331 #endif
CaptainR 26:c6a803706a42 332 return success;
CaptainR 26:c6a803706a42 333 }
CaptainR 26:c6a803706a42 334
CaptainR 26:c6a803706a42 335 //**************************************************************************
CaptainR 26:c6a803706a42 336 // The File Close command will close the previously opened file.
CaptainR 26:c6a803706a42 337 //**************************************************************************
CaptainR 26:c6a803706a42 338 bool PICASO_4DGL :: file_Close(short handle) {
CaptainR 26:c6a803706a42 339
CaptainR 26:c6a803706a42 340 char command[4] = "";
CaptainR 26:c6a803706a42 341
CaptainR 26:c6a803706a42 342 command[0] = (FILE_CLOSE >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 343 command[1] = (FILE_CLOSE >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 344 command[2] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 345 command[3] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 346 writeCOMMAND(command, 4);
CaptainR 26:c6a803706a42 347
CaptainR 26:c6a803706a42 348 bool success = writeSectorResponse(3);
CaptainR 26:c6a803706a42 349 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 350 pc.printf("\n\r DEBUG: Closing file Nr. %i = %s\n\r", handle, success ? " true" : "false");
CaptainR 26:c6a803706a42 351 #endif
CaptainR 26:c6a803706a42 352 return success;
CaptainR 26:c6a803706a42 353 }
CaptainR 26:c6a803706a42 354
CaptainR 26:c6a803706a42 355 //**************************************************************************
CaptainR 26:c6a803706a42 356 // Returns the number of bytes specified by ‘size’ from the file referenced by ‘handle’.
CaptainR 26:c6a803706a42 357 //**************************************************************************
CaptainR 26:c6a803706a42 358 short PICASO_4DGL :: file_Read(short size, short handle) {
CaptainR 26:c6a803706a42 359
CaptainR 26:c6a803706a42 360 char command[6] = "";
CaptainR 26:c6a803706a42 361
CaptainR 26:c6a803706a42 362 command[0] = (FILE_READ >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 363 command[1] = (FILE_READ >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 364 command[2] = (size >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 365 command[3] = (size >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 366 command[4] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 367 command[5] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 368 writeCOMMAND(command, 6);
CaptainR 26:c6a803706a42 369
CaptainR 26:c6a803706a42 370 short count = readFileResponse(size);
CaptainR 26:c6a803706a42 371 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 372 pc.printf("\n\r DEBUG: Read %i bytes from file Nr %i\n\r", count, handle);
CaptainR 26:c6a803706a42 373 #endif
CaptainR 26:c6a803706a42 374 return count;
CaptainR 26:c6a803706a42 375 }
CaptainR 26:c6a803706a42 376
CaptainR 26:c6a803706a42 377 //**************************************************************************
CaptainR 26:c6a803706a42 378 // The File Seek command places the file pointer at the required position in a file
CaptainR 26:c6a803706a42 379 // that has been opened in 'r' (read) or 'a' (append) mode. In append mode, File Seek
CaptainR 26:c6a803706a42 380 // does not expand a filesize, instead, the file pointer (handle) is set to the end
CaptainR 26:c6a803706a42 381 // position of the file, e.g. assuming the file size is 10000 bytes, the File Seek
CaptainR 26:c6a803706a42 382 // command with HiWord = 0x00 and LoWord = 0x1234 will set the file position to
CaptainR 26:c6a803706a42 383 // 0x00001234 (byte position 4660) for the file handle, so subsequent data may be
CaptainR 26:c6a803706a42 384 // read from that position onwards with “Read Character from the File”, “Read Word from the File”,
CaptainR 26:c6a803706a42 385 // “Read String from the File” commands, or an image can be displayed with the “Display Image (FAT)” command.
CaptainR 26:c6a803706a42 386 //
CaptainR 26:c6a803706a42 387 // Conversely, “Write Character to the File”, “Write Word to the File”, “Write String to the File”
CaptainR 26:c6a803706a42 388 // commands can write to the file at the position. A FE_EOF (end of file error) will occur if
CaptainR 26:c6a803706a42 389 // you try to write or read past the end of the file, visible from the “File Error” command.
CaptainR 26:c6a803706a42 390 //**************************************************************************
CaptainR 26:c6a803706a42 391 bool PICASO_4DGL :: file_Seek(short handle, short hi, short lo) {
CaptainR 26:c6a803706a42 392
CaptainR 26:c6a803706a42 393 char command[8] = "";
CaptainR 26:c6a803706a42 394
CaptainR 26:c6a803706a42 395 command[0] = (FILE_SEEK >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 396 command[1] = (FILE_SEEK >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 397 command[2] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 398 command[3] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 399 command[4] = (hi >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 400 command[5] = (hi >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 401 command[6] = (lo >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 402 command[7] = (lo >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 403 writeCOMMAND(command, 8);
CaptainR 26:c6a803706a42 404
CaptainR 26:c6a803706a42 405 bool success = writeSectorResponse(3);
CaptainR 26:c6a803706a42 406 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 407 pc.printf("\n\r DEBUG: File pointer set = %s\n\r", success ? "true" : "false");
CaptainR 26:c6a803706a42 408 #endif
CaptainR 26:c6a803706a42 409 return success;
CaptainR 26:c6a803706a42 410 }
CaptainR 26:c6a803706a42 411
CaptainR 26:c6a803706a42 412 //**************************************************************************
CaptainR 26:c6a803706a42 413 // Places the file pointer at the position in a file that has been opened in
CaptainR 26:c6a803706a42 414 // 'r' (read) or 'a' (append) mode. In append mode, File Index does not expand
CaptainR 26:c6a803706a42 415 // a filesize, instead, the file pointer (handle) is set to the end position
CaptainR 26:c6a803706a42 416 // of the file, e.g. assuming the record size is 100 bytes, the File Index
CaptainR 26:c6a803706a42 417 // command with HiSize = 0, LoSize = 100 and recordnum = 22 will set the file
CaptainR 26:c6a803706a42 418 // position to 2200 for the file handle, so subsequent data may be read from
CaptainR 26:c6a803706a42 419 // that position onwards with “Read Character from the File”, “Read Word from the File”,
CaptainR 26:c6a803706a42 420 // “Read String from the File” commands or an image can be displayed with the “Display Image (FAT)” command.
CaptainR 26:c6a803706a42 421 //
CaptainR 26:c6a803706a42 422 // Conversely, the “Write Character to the File”, “Write Word to the File”,
CaptainR 26:c6a803706a42 423 // “Write String to the File” commands can write to the file at the position.
CaptainR 26:c6a803706a42 424 // A FE_EOF (end of file error) will occur if you try to write or read past
CaptainR 26:c6a803706a42 425 // the end of the file, visible from the “File Error” command.
CaptainR 26:c6a803706a42 426 //**************************************************************************
CaptainR 26:c6a803706a42 427 bool PICASO_4DGL :: file_Index(short handle, short hi, short lo, short rec) {
CaptainR 26:c6a803706a42 428
CaptainR 26:c6a803706a42 429 char command[10] = "";
CaptainR 26:c6a803706a42 430
CaptainR 26:c6a803706a42 431 command[0] = (FILE_INDEX >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 432 command[1] = (FILE_INDEX >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 433 command[2] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 434 command[3] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 435 command[4] = (hi >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 436 command[5] = (hi >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 437 command[6] = (lo >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 438 command[7] = (lo >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 439 command[8] = (rec >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 440 command[9] = (rec >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 441 writeCOMMAND(command, 10);
CaptainR 26:c6a803706a42 442
CaptainR 26:c6a803706a42 443 bool success = writeSectorResponse(3);
CaptainR 26:c6a803706a42 444 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 445 pc.printf("\n\r DEBUG: File pointer set = %s\n\r", success ? "true" : "false");
CaptainR 26:c6a803706a42 446 #endif
CaptainR 26:c6a803706a42 447 return success;
CaptainR 26:c6a803706a42 448 }
CaptainR 26:c6a803706a42 449
CaptainR 26:c6a803706a42 450 //**************************************************************************
CaptainR 26:c6a803706a42 451 // The File Tell command returns the current value of the file pointer.
CaptainR 26:c6a803706a42 452 //**************************************************************************
CaptainR 26:c6a803706a42 453 int PICASO_4DGL :: file_Tell(short handle) {
CaptainR 26:c6a803706a42 454
CaptainR 26:c6a803706a42 455 char command[4] = "";
CaptainR 26:c6a803706a42 456
CaptainR 26:c6a803706a42 457 command[0] = (FILE_TELL >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 458 command[1] = (FILE_TELL >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 459 command[2] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 460 command[3] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 461 writeCOMMAND(command, 4);
CaptainR 26:c6a803706a42 462
CaptainR 26:c6a803706a42 463 int point = fileTellResponse();
CaptainR 26:c6a803706a42 464 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 465 pc.printf("\n\r DEBUG: Pointer value = %i\n\r", point);
CaptainR 26:c6a803706a42 466 #endif
CaptainR 26:c6a803706a42 467 return point;
CaptainR 26:c6a803706a42 468 }
CaptainR 24:19c77967674e 469
CaptainR 25:015631f9e875 470
CaptainR 26:c6a803706a42 471 //**************************************************************************
CaptainR 26:c6a803706a42 472 // The File Write command returns the current value of the file pointer.
CaptainR 26:c6a803706a42 473 // size - Number of bytes to be written. Maximum that can be written at one time is 512 bytes.
CaptainR 26:c6a803706a42 474 // source - String of Data without Null terminator.
CaptainR 26:c6a803706a42 475 //**************************************************************************
CaptainR 26:c6a803706a42 476 short PICASO_4DGL :: file_Write(short num, char *str, short handle) {
CaptainR 26:c6a803706a42 477
CaptainR 26:c6a803706a42 478 int size = 6 + strlen(str);
CaptainR 26:c6a803706a42 479 int i, k, j = 4;
CaptainR 26:c6a803706a42 480 char *command;
CaptainR 26:c6a803706a42 481 command = (char *)malloc(sizeof(char) * size);
CaptainR 26:c6a803706a42 482 for(i = 0; i < size; i++) command[i] = 0;
CaptainR 26:c6a803706a42 483
CaptainR 26:c6a803706a42 484 command[0] = (FILE_WRITE >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 485 command[1] = (FILE_WRITE >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 486 command[2] = (num >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 487 command[3] = (num >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 488
CaptainR 26:c6a803706a42 489 for (k = 0; k < strlen(str); k++) {
CaptainR 26:c6a803706a42 490 command[j++] = str[k];
CaptainR 26:c6a803706a42 491 }
CaptainR 26:c6a803706a42 492 command[j++] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 493 command[j] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 494 writeCOMMAND(command, size);
CaptainR 26:c6a803706a42 495
CaptainR 26:c6a803706a42 496 short count = fileCountResponse();
CaptainR 26:c6a803706a42 497 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 498 pc.printf("\n\r DEBUG: Pointer value = %i\n\r", count);
CaptainR 26:c6a803706a42 499 #endif
CaptainR 26:c6a803706a42 500 return count;
CaptainR 26:c6a803706a42 501 }
CaptainR 26:c6a803706a42 502
CaptainR 26:c6a803706a42 503 //**************************************************************************
CaptainR 26:c6a803706a42 504 // The File Size command reads the 32 bit file size.
CaptainR 26:c6a803706a42 505 //**************************************************************************
CaptainR 26:c6a803706a42 506 int PICASO_4DGL :: file_Size(short handle) {
CaptainR 26:c6a803706a42 507
CaptainR 26:c6a803706a42 508 char command[4] = "";
CaptainR 26:c6a803706a42 509
CaptainR 26:c6a803706a42 510 command[0] = (FILE_TELL >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 511 command[1] = (FILE_TELL >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 512 command[2] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 513 command[3] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 514 writeCOMMAND(command, 4);
CaptainR 26:c6a803706a42 515
CaptainR 26:c6a803706a42 516 int size = fileTellResponse();
CaptainR 26:c6a803706a42 517 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 518 pc.printf("\n\r DEBUG: File %i size = %i\n\r", handle, size);
CaptainR 26:c6a803706a42 519 #endif
CaptainR 26:c6a803706a42 520 return size;
CaptainR 26:c6a803706a42 521 }
CaptainR 26:c6a803706a42 522
CaptainR 26:c6a803706a42 523 //**************************************************************************
CaptainR 26:c6a803706a42 524 // Display an image from the file stream at screen location specified by x, y
CaptainR 26:c6a803706a42 525 // (top left corner). If there is more than 1 image in the file, it can be
CaptainR 26:c6a803706a42 526 // accessed with the “File Seek” command
CaptainR 26:c6a803706a42 527 //**************************************************************************
CaptainR 26:c6a803706a42 528 bool PICASO_4DGL :: file_Image(short x, short y, short handle) {
CaptainR 26:c6a803706a42 529
CaptainR 26:c6a803706a42 530 char command[8] = "";
CaptainR 26:c6a803706a42 531
CaptainR 26:c6a803706a42 532 command[0] = (FILE_IMAGE >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 533 command[1] = (FILE_IMAGE >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 534 command[2] = (x >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 535 command[3] = (x >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 536 command[4] = (y >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 537 command[5] = (y >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 538 command[6] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 539 command[7] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 540 writeCOMMAND(command, 8);
CaptainR 26:c6a803706a42 541
CaptainR 26:c6a803706a42 542 bool success = !writeSectorResponse(3);
CaptainR 26:c6a803706a42 543 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 544 pc.printf("\n\r DEBUG: Display image = %s\n\r", success ? "true" : "false");
CaptainR 26:c6a803706a42 545 #endif
CaptainR 26:c6a803706a42 546 return success;
CaptainR 26:c6a803706a42 547 }
CaptainR 26:c6a803706a42 548
CaptainR 26:c6a803706a42 549 //**************************************************************************
CaptainR 26:c6a803706a42 550 // The Screen Capture command saves an image of the screen shot to file at the current file position.
CaptainR 26:c6a803706a42 551 // The image can later be displayed with the “Display Image (FAT)” command.
CaptainR 26:c6a803706a42 552 // The file may be opened in append mode to accumulate multiple images.
CaptainR 26:c6a803706a42 553 // ater, the images can be displayed with the “File Seek” command.
CaptainR 26:c6a803706a42 554 // The image is saved from x, y (with respect to top left corner), and the capture
CaptainR 26:c6a803706a42 555 // area is determined by "width" and "height".
CaptainR 26:c6a803706a42 556 //**************************************************************************
CaptainR 26:c6a803706a42 557 bool PICASO_4DGL :: file_ScreenCapture(short x, short y, short width, short height, short handle) {
CaptainR 26:c6a803706a42 558
CaptainR 26:c6a803706a42 559 char command[12] = "";
CaptainR 26:c6a803706a42 560
CaptainR 26:c6a803706a42 561 command[0] = (FILE_S_CAPTURE >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 562 command[1] = (FILE_S_CAPTURE >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 563 command[2] = (x >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 564 command[3] = (x >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 565 command[4] = (y >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 566 command[5] = (y >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 567 command[6] = (width >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 568 command[7] = (width >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 569 command[8] = (height >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 570 command[9] = (height >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 571 command[10] = (handle >> (8*1)) & 0xff;
CaptainR 26:c6a803706a42 572 command[11] = (handle >> (8*0)) & 0xff;
CaptainR 26:c6a803706a42 573 writeCOMMAND(command, 12);
CaptainR 26:c6a803706a42 574
CaptainR 26:c6a803706a42 575 bool success = !writeSectorResponse(3);
CaptainR 26:c6a803706a42 576 #ifdef DEBUGMODE
CaptainR 26:c6a803706a42 577 pc.printf("\n\r DEBUG: Screen capture = %s\n\r", success ? "true" : "false");
CaptainR 26:c6a803706a42 578 #endif
CaptainR 26:c6a803706a42 579 return success;
CaptainR 26:c6a803706a42 580 }
CaptainR 26:c6a803706a42 581
CaptainR 26:c6a803706a42 582
CaptainR 26:c6a803706a42 583
CaptainR 26:c6a803706a42 584
CaptainR 26:c6a803706a42 585
CaptainR 26:c6a803706a42 586
CaptainR 26:c6a803706a42 587
CaptainR 26:c6a803706a42 588
CaptainR 26:c6a803706a42 589