4D systems Picaso uLCD 32PTU touch display library

Picaso_4DGL-32PTU_File.cpp

Committer:
CaptainR
Date:
2016-09-27
Revision:
24:19c77967674e
Parent:
23:dd2c28fa4dfd
Child:
25:015631f9e875

File content as of revision 24:19c77967674e:

//
//  Picaso_4DGL-32PTU is a class to drive 4D Systems TFT touch screens with PICASO processor
//  Tested with NUCLEO L152RE development board
//  Copyright (C) <2016> Rihards Balass <rihards.balass@gmail.com>
//
// Picaso_4DGL-32PTU is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Picaso_4DGL-32PTU is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You can see GNU General Public License at <http://www.gnu.org/licenses/>.
//

#include "mbed.h"
#include "Picaso_4DGL-32PTU.h"

//**************************************************************************
// Starts up the FAT16 disk file services and allocates a small 32 byte control block
// for subsequent use. When you open a file using the “File Open” command
// further 512 + 44 = 556 bytes are attached to the FAT16 file control block.
// When you close a file using the “File Close” command, the 556 byte allocation
// is released leaving the 32 byte file control block.
// The File Mount command must be called before any other FAT16 file related
// functions can be used. The control block and all FAT16 file resources are
// completely released with the “File Unmount” command.
//**************************************************************************
bool PICASO_4DGL :: file_Mount() {

    char command[2] = "";
    
    command[0] = (FILE_MOUNT >> (8*1)) & 0xff;
    command[1] = (FILE_MOUNT >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 2);
    bool success = fileMountResponse();
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: FAT16 Mount: %s\n\r", success ? "true" : "false");
#endif
    return success;
}

//**************************************************************************
// Returns the most recent error code or 0 if there were no errors.
// Returns Error Number.
// 1    IDE command execution error
// 2    CARD not present
// 3    WRONG partition type, not FAT16
// 4    MBR sector invalid signature
// 5    Boot Record invalid signature
// 6    Media not mounted
// 7    File not found in open for read
// 8    File not open
// 9    Fat attempt to read beyond EOF
// 10   Reached the end of file
// 11   Invalid cluster value > maxcls
// 12   All root dir entry are taken
// 13   All clusters in partition are taken
// 14   A file with same name exist already
// 15   Cannot init the CARD
// 16   Cannot read the MBR
// 17   Malloc could not allocate the FILE struct
// 18   Mode was not r.w.
// 19   Failure during FILE search
// 20   Invalid Filename
// 21   bad media
// 22   Sector Read fail
// 23   Sector write fail
//**************************************************************************
short PICASO_4DGL :: file_Error() {

    char command[2] = "";
    
    command[0] = (FILE_ERROR >> (8*1)) & 0xff;
    command[1] = (FILE_ERROR >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 2);
    short error = fileErrorResponse();
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: FAT16 Error: %i\n\r", error);
#endif
    return error;
}

//**************************************************************************
// Returns number of files found that match the criteria.
// The wild card character '*' matches up with any combination of allowable
// characters and '?' matches up with any single allowable character.
//**************************************************************************
short PICASO_4DGL :: file_Count(char *filename) {
    
    int size = 3 + 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_COUNT >> (8*1)) & 0xff;
    command[1] = (FILE_COUNT >> (8*0)) & 0xff;
    for (k = 0; k < size-1; k++) command[j++] = filename[k];
    command[j] = 0;
    
    writeCOMMAND(command, size);
    short success = fileCountResponse();
    free(command);
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: %s count = %i\n\r", filename, success);
#endif
    return success;
}

//**************************************************************************
// Lists the stream of file names that agree with the search key on the Display Screen.
// Returns number of files found that match the criteria.
// The wild card character '*' matches up with any combination of allowable characters
// and '?' matches up with any single allowable character.
// 
// Note: “Find First File and Report” and “Find Next File and Report” are
// recommended alternatives in order to return the responses.
//**************************************************************************
short PICASO_4DGL :: file_Dir(char *filename) {
    
    int size = 3 + 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_DIR >> (8*1)) & 0xff;
    command[1] = (FILE_DIR >> (8*0)) & 0xff;
    for (k = 0; k < size-1; k++) command[j++] = filename[k];
    command[j] = 0;
    
    writeCOMMAND(command, size);
    short success = fileCountResponse();
    free(command);
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: %s count = %i\n\r", filename, success);
#endif
    return success;
}

//**************************************************************************
// Returns true if at least 1 file exists that satisfies the file argument.
// Wildcards are usually used so if the “Find First File” command returns true,
// further tests can be made using the “Find Next File” command to find all 
// the files that match the wildcard class. 
// Note that the filename is printed on the screen.
// 
// Note: “Find First File and Report” and “Find Next File and Report” are
// recommended alternatives in order to return the responses.
//**************************************************************************
void PICASO_4DGL :: file_FindFirst(char *filename) {
    
    int size = 3 + 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_FIRST >> (8*1)) & 0xff;
    command[1] = (FILE_FIRST >> (8*0)) & 0xff;
    for (k = 0; k < size-1; k++) command[j++] = filename[k];
    command[j] = 0;
    
    puts("\n\rFirst file = ");
    writeCOMMAND(command, size);
    bool success = writeSectorResponse(3);
    free(command);
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: %s Found = %s\n\r", filename, success ? " true" : "false");
#endif
}

//**************************************************************************
// The Find First File and Report command returns the length of the filename
// and the filename if at least 1 file exists that matches the criteria.
// 
// Wildcards are usually used so if Find First File and Report command
// returns the stringlength and filename, further tests can be made using
// “Find Next File” or “Find Next File and Report” commands to find all the files
// that match the wildcard class.
//
// you have to give the function the output char array to put filename in
//**************************************************************************
short PICASO_4DGL :: file_FindFirstRet(char *filename, char *outStr) {
    
    int size = 3 + 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_FIRST_RET >> (8*1)) & 0xff;
    command[1] = (FILE_FIRST_RET >> (8*0)) & 0xff;
    for (k = 0; k < size-1; k++) command[j++] = filename[k];
    command[j] = 0;
    
    writeCOMMAND(command, size);
    short len = getFilenameResponse(outStr);
    
    free(command);
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: Searching for %s Found filename = %s\n\r", filename, outStr);
#endif
    return len;
}

//**************************************************************************
// The Find Next File command returns true if more file exists that satisfies
// the file argument that was given for the “Find First File” or 
// “Find First File and Report” commands.
// Wildcards must be used for the “Find First File” or “Find First File and Report”
// commands else this function will always return zero as the only occurrence
// will have already been found.
// 
// Note that the filename is printed on the screen.
//**************************************************************************
void PICASO_4DGL :: file_FindNext() {
    
    char command[2];
    
    command[0] = (FILE_NEXT >> (8*1)) & 0xff;
    command[1] = (FILE_NEXT >> (8*0)) & 0xff;
    
    puts("\n\rNext file = ");
    writeCOMMAND(command, 2);
    bool success = writeSectorResponse(3);
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: next file found = %s\n\r", success ? " true" : "false");
#endif
}

//**************************************************************************
// Returns length of the filename and the filename if at least 1 file exists
// that matches the criteria given for the “Find First File” or
// “Find First File and Report” commands.
// Wildcards must be used for the “Find First File” or “Find First File and Report”
// commands else this function will always return zero as the only occurrence will have
// already been found.
// 
// Wildcards are usually used, so if the “Find First File” or “Find First File and Report”
// commands return the stringlength and filename, further tests can be made using Find
// Next File and Report command to find all the files that match the wildcard class.
//**************************************************************************
short PICASO_4DGL :: file_FindNextRet(char *outStr) {
    
    char command[2];
    
    command[0] = (FILE_NEXT_RET >> (8*1)) & 0xff;
    command[1] = (FILE_NEXT_RET >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 2);
    short len = getFilenameResponse(outStr);
    
#ifdef DEBUGMODE
    pc.printf("\n\r DEBUG: Found next filename = %s\n\r", outStr);
#endif
    return len;
}