Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Picaso_4DGL-32PTU_File.cpp
- Revision:
- 26:c6a803706a42
- Parent:
- 25:015631f9e875
- Child:
- 27:dbf79d116497
--- a/Picaso_4DGL-32PTU_File.cpp Tue Sep 27 11:49:34 2016 +0000 +++ b/Picaso_4DGL-32PTU_File.cpp Wed Sep 28 12:41:32 2016 +0000 @@ -70,6 +70,7 @@ // 21 bad media // 22 Sector Read fail // 23 Sector write fail +// 26 File is empty (size 0) //************************************************************************** short PICASO_4DGL :: file_Error() { @@ -288,5 +289,301 @@ return success; } +//************************************************************************** +// Returns handle if file exists. The file ‘handle’ that is created is now used as reference for ‘filename’ +// for further file commands such as “File Close”, etc. For File Write and File Append modes ('w' and 'a') +// the file is created if it does not exist. If the file is opened for append and it already exists, +// the file pointer is set to the end of the file ready for appending, else the file pointer will be +// set to the start of the newly created file. +// +// If the file was opened successfully, the internal error number is set to 0 (i.e. no errors) and can be +// read with the “File Error” command. For File Read mode ('r') the file must exist else a null handle +// (0x00, 0x00) is returned and the 'file not found' error number is set which can be read with the “File Error” command. +// +// Note: If a file is opened for File Write mode 'w', and the file already exists, the operation will fail. +// Unlike C and some other languages where the file will be erased ready for re-writing when opened for writing, +// 4DGL offers a simple level of protection that ensures that a file must be purposely erased before being re-written. +// Note: Beginning with the v4.0 PmmC a file opened with FILE_APPEND may be randomly read and or written. +// Also any altered file will have the Archive bit set in the directory entry. +//************************************************************************** +short PICASO_4DGL :: file_Open(char *filename, char openMode) { + + int size = 4 + strlen(filename); + int i, k, j = 2; + char *command; + command = (char *)malloc(sizeof(char) * size); + for(i = 0; i < size; i++) command[i] = 0; + + command[0] = (FILE_OPEN >> (8*1)) & 0xff; + command[1] = (FILE_OPEN >> (8*0)) & 0xff; + for (k = 0; k < size-2; k++) { + command[j++] = filename[k]; + } + //command[j++] = 0; // null terminated string + + command[size-1] = openMode; // open mode + + writeCOMMAND(command, size); + short success = fileCountResponse(); + free(command); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Opened new file %s handle = %i\n\r", filename, success); +#endif + return success; +} + +//************************************************************************** +// The File Close command will close the previously opened file. +//************************************************************************** +bool PICASO_4DGL :: file_Close(short handle) { + + char command[4] = ""; + + command[0] = (FILE_CLOSE >> (8*1)) & 0xff; + command[1] = (FILE_CLOSE >> (8*0)) & 0xff; + command[2] = (handle >> (8*1)) & 0xff; + command[3] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, 4); + + bool success = writeSectorResponse(3); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Closing file Nr. %i = %s\n\r", handle, success ? " true" : "false"); +#endif + return success; +} + +//************************************************************************** +// Returns the number of bytes specified by ‘size’ from the file referenced by ‘handle’. +//************************************************************************** +short PICASO_4DGL :: file_Read(short size, short handle) { + + char command[6] = ""; + + command[0] = (FILE_READ >> (8*1)) & 0xff; + command[1] = (FILE_READ >> (8*0)) & 0xff; + command[2] = (size >> (8*1)) & 0xff; + command[3] = (size >> (8*0)) & 0xff; + command[4] = (handle >> (8*1)) & 0xff; + command[5] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, 6); + + short count = readFileResponse(size); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Read %i bytes from file Nr %i\n\r", count, handle); +#endif + return count; +} + +//************************************************************************** +// The File Seek command places the file pointer at the required position in a file +// that has been opened in 'r' (read) or 'a' (append) mode. In append mode, File Seek +// does not expand a filesize, instead, the file pointer (handle) is set to the end +// position of the file, e.g. assuming the file size is 10000 bytes, the File Seek +// command with HiWord = 0x00 and LoWord = 0x1234 will set the file position to +// 0x00001234 (byte position 4660) for the file handle, so subsequent data may be +// read from that position onwards with “Read Character from the File”, “Read Word from the File”, +// “Read String from the File” commands, or an image can be displayed with the “Display Image (FAT)” command. +// +// Conversely, “Write Character to the File”, “Write Word to the File”, “Write String to the File” +// commands can write to the file at the position. A FE_EOF (end of file error) will occur if +// you try to write or read past the end of the file, visible from the “File Error” command. +//************************************************************************** +bool PICASO_4DGL :: file_Seek(short handle, short hi, short lo) { + + char command[8] = ""; + + command[0] = (FILE_SEEK >> (8*1)) & 0xff; + command[1] = (FILE_SEEK >> (8*0)) & 0xff; + command[2] = (handle >> (8*1)) & 0xff; + command[3] = (handle >> (8*0)) & 0xff; + command[4] = (hi >> (8*1)) & 0xff; + command[5] = (hi >> (8*0)) & 0xff; + command[6] = (lo >> (8*1)) & 0xff; + command[7] = (lo >> (8*0)) & 0xff; + writeCOMMAND(command, 8); + + bool success = writeSectorResponse(3); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: File pointer set = %s\n\r", success ? "true" : "false"); +#endif + return success; +} + +//************************************************************************** +// Places the file pointer at the position in a file that has been opened in +// 'r' (read) or 'a' (append) mode. In append mode, File Index does not expand +// a filesize, instead, the file pointer (handle) is set to the end position +// of the file, e.g. assuming the record size is 100 bytes, the File Index +// command with HiSize = 0, LoSize = 100 and recordnum = 22 will set the file +// position to 2200 for the file handle, so subsequent data may be read from +// that position onwards with “Read Character from the File”, “Read Word from the File”, +// “Read String from the File” commands or an image can be displayed with the “Display Image (FAT)” command. +// +// Conversely, the “Write Character to the File”, “Write Word to the File”, +// “Write String to the File” commands can write to the file at the position. +// A FE_EOF (end of file error) will occur if you try to write or read past +// the end of the file, visible from the “File Error” command. +//************************************************************************** +bool PICASO_4DGL :: file_Index(short handle, short hi, short lo, short rec) { + + char command[10] = ""; + + command[0] = (FILE_INDEX >> (8*1)) & 0xff; + command[1] = (FILE_INDEX >> (8*0)) & 0xff; + command[2] = (handle >> (8*1)) & 0xff; + command[3] = (handle >> (8*0)) & 0xff; + command[4] = (hi >> (8*1)) & 0xff; + command[5] = (hi >> (8*0)) & 0xff; + command[6] = (lo >> (8*1)) & 0xff; + command[7] = (lo >> (8*0)) & 0xff; + command[8] = (rec >> (8*1)) & 0xff; + command[9] = (rec >> (8*0)) & 0xff; + writeCOMMAND(command, 10); + + bool success = writeSectorResponse(3); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: File pointer set = %s\n\r", success ? "true" : "false"); +#endif + return success; +} + +//************************************************************************** +// The File Tell command returns the current value of the file pointer. +//************************************************************************** +int PICASO_4DGL :: file_Tell(short handle) { + + char command[4] = ""; + + command[0] = (FILE_TELL >> (8*1)) & 0xff; + command[1] = (FILE_TELL >> (8*0)) & 0xff; + command[2] = (handle >> (8*1)) & 0xff; + command[3] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, 4); + + int point = fileTellResponse(); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Pointer value = %i\n\r", point); +#endif + return point; +} +//************************************************************************** +// The File Write command returns the current value of the file pointer. +// size - Number of bytes to be written. Maximum that can be written at one time is 512 bytes. +// source - String of Data without Null terminator. +//************************************************************************** +short PICASO_4DGL :: file_Write(short num, char *str, short handle) { + + int size = 6 + strlen(str); + int i, k, j = 4; + char *command; + command = (char *)malloc(sizeof(char) * size); + for(i = 0; i < size; i++) command[i] = 0; + + command[0] = (FILE_WRITE >> (8*1)) & 0xff; + command[1] = (FILE_WRITE >> (8*0)) & 0xff; + command[2] = (num >> (8*1)) & 0xff; + command[3] = (num >> (8*0)) & 0xff; + + for (k = 0; k < strlen(str); k++) { + command[j++] = str[k]; + } + command[j++] = (handle >> (8*1)) & 0xff; + command[j] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, size); + + short count = fileCountResponse(); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Pointer value = %i\n\r", count); +#endif + return count; +} + +//************************************************************************** +// The File Size command reads the 32 bit file size. +//************************************************************************** +int PICASO_4DGL :: file_Size(short handle) { + + char command[4] = ""; + + command[0] = (FILE_TELL >> (8*1)) & 0xff; + command[1] = (FILE_TELL >> (8*0)) & 0xff; + command[2] = (handle >> (8*1)) & 0xff; + command[3] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, 4); + + int size = fileTellResponse(); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: File %i size = %i\n\r", handle, size); +#endif + return size; +} + +//************************************************************************** +// Display an image from the file stream at screen location specified by x, y +// (top left corner). If there is more than 1 image in the file, it can be +// accessed with the “File Seek” command +//************************************************************************** +bool PICASO_4DGL :: file_Image(short x, short y, short handle) { + + char command[8] = ""; + + command[0] = (FILE_IMAGE >> (8*1)) & 0xff; + command[1] = (FILE_IMAGE >> (8*0)) & 0xff; + command[2] = (x >> (8*1)) & 0xff; + command[3] = (x >> (8*0)) & 0xff; + command[4] = (y >> (8*1)) & 0xff; + command[5] = (y >> (8*0)) & 0xff; + command[6] = (handle >> (8*1)) & 0xff; + command[7] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, 8); + + bool success = !writeSectorResponse(3); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Display image = %s\n\r", success ? "true" : "false"); +#endif + return success; +} + +//************************************************************************** +// The Screen Capture command saves an image of the screen shot to file at the current file position. +// The image can later be displayed with the “Display Image (FAT)” command. +// The file may be opened in append mode to accumulate multiple images. +// ater, the images can be displayed with the “File Seek” command. +// The image is saved from x, y (with respect to top left corner), and the capture +// area is determined by "width" and "height". +//************************************************************************** +bool PICASO_4DGL :: file_ScreenCapture(short x, short y, short width, short height, short handle) { + + char command[12] = ""; + + command[0] = (FILE_S_CAPTURE >> (8*1)) & 0xff; + command[1] = (FILE_S_CAPTURE >> (8*0)) & 0xff; + command[2] = (x >> (8*1)) & 0xff; + command[3] = (x >> (8*0)) & 0xff; + command[4] = (y >> (8*1)) & 0xff; + command[5] = (y >> (8*0)) & 0xff; + command[6] = (width >> (8*1)) & 0xff; + command[7] = (width >> (8*0)) & 0xff; + command[8] = (height >> (8*1)) & 0xff; + command[9] = (height >> (8*0)) & 0xff; + command[10] = (handle >> (8*1)) & 0xff; + command[11] = (handle >> (8*0)) & 0xff; + writeCOMMAND(command, 12); + + bool success = !writeSectorResponse(3); +#ifdef DEBUGMODE + pc.printf("\n\r DEBUG: Screen capture = %s\n\r", success ? "true" : "false"); +#endif + return success; +} + + + + + + + + +