Skip to content

Commit

Permalink
Merge pull request #8 from djuseeq/update
Browse files Browse the repository at this point in the history
Update, new functions
  • Loading branch information
djuseeq authored Sep 26, 2019
2 parents a2bef17 + 7d891a8 commit 017fbb1
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 82 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ Configure the jumpers on the module depending on which communication protocol yo
![Alt text](extras/JumperSelect.png?raw=true "Setting")

## Versioning
v1.4.0 Sep 26, 2019
- new functions
- getTotalSectors() - returns a unsigned long number, total sectors on the drive
- getFreeSectors() - returns a unsigned long number, free sectors on the drive
- getFileSystem() - returns a byte number, 0x01-FAT12, 0x02-FAT16, 0x03-FAT32
- updated example files with a new functions
- new example file, seraching for the oldest/newest file on the flash drive

v1.3.1 Sep 20, 2019
- rearrange the folder structure to be 1.5 library format compatible

v1.3 Sep 17, 2019
- bug fix for moveCursor issue #3 , minor changes

Expand Down
84 changes: 65 additions & 19 deletions examples/basicUsageHwSerial/basicUsageHwSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,59 @@ char adatBuffer[255];// max length 255 = 254 char + 1 NULL character
char adat[]="Vivamus nec nisl molestie, blandit diam vel, varius mi. Fusce luctus cursus sapien in vulputate.\n";
char adat2[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis efficitur ac est eu pharetra. \n";
//..............................................................................................................................
unsigned long totSect = 0;
unsigned long freeSect = 0;
byte percentg = 0;
byte tmpCommand; //used to store data coming from serial port
boolean readMore;


void setup() {
Serial.begin(115200);
flashDrive.init();
printInfo("h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n5:Modify date/time\n6:Delete\n7:List dir");
printInfo("h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n"
"5:Modify date/time\n6:Delete\n7:List dir\n8:Print free space");
}

void loop() {
if(Serial.available()){
tmpCommand = Serial.read(); //read incoming bytes from the serial monitor
if(((tmpCommand > 48)&&(tmpCommand < 58)) && !flashDrive.checkDrive()){ // if the data is ASCII 0 - 9 and no flash drive are attached
printInfo("Attach flash drive first!");
tmpCommand = 10; // change the command byte
tmpCommand = 10; // change the command byte
}
switch (tmpCommand) {

case 49: //1
printInfo("COMMAND1: Create and write data to file : TEST1.TXT"); // Create a file called TEST1.TXT
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.openFile(); //open the file

for(int a = 0; a < 20; a++){ //write text from string(adat) to flash drive 20 times
flashDrive.writeFile(adat, strlen(adat)); //string, string length
}
flashDrive.closeFile(); //at the end, close the file
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 50: //2
printInfo("COMMAND2: Append data to file: TEST1.TXT"); // Append data to the end of the file.
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.openFile(); //open the file
flashDrive.moveCursor(CURSOREND); //move the "virtual" cursor at end of the file, with CURSORBEGIN we actually rewrite our old file
//flashDrive.moveCursor(flashDrive.getFileSize()); // is almost the same as CURSOREND, because we put our cursor at end of the file

for(int a = 0; a < 20; a++){ //write text from string(adat) to flash drive 20 times
flashDrive.writeFile(adat2, strlen(adat2)); //string, string length
if(flashDrive.getFreeSectors()){ //check the free space on the drive
flashDrive.writeFile(adat2, strlen(adat2)); //string, string length
} else {
Serial.println("Disk full");
}
}
flashDrive.closeFile(); //at the end, close the file
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 51: //3
printInfo("COMMAND3: Read File: TEST1.TXT"); // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
flashDrive.setFileName("TEST1.TXT"); //set the file name
Expand All @@ -78,13 +86,13 @@ void loop() {
flashDrive.closeFile(); //at the end, close the file
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 52: //4
printInfo("COMMAND4: Read File date/time: TEST1.TXT"); // Read the date and time of file, default 2004.01.01 - 00:00:00
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.openFile(); //open the file
//print informations about the file
Serial.println(flashDrive.getFileName());
Serial.println(flashDrive.getFileName());
Serial.print(flashDrive.getYear());
Serial.print("y\t");
Serial.print(flashDrive.getMonth());
Expand All @@ -100,31 +108,31 @@ void loop() {
flashDrive.closeFile(); //at the end, close the file
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 53: //5
printInfo("COMMAND5: Modify File date/time: TEST1.TXT"); // Modify the file date/time and save
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.openFile(); //open the file

flashDrive.setYear(2019);
flashDrive.setMonth(2);
flashDrive.setDay(24);
flashDrive.setHour(15);
flashDrive.setMinute(47);
flashDrive.setSecond(26);

flashDrive.dirInfoSave(); //save the changed data
flashDrive.closeFile(); //and yes again, close the file after when you don`t use it
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 54: //6
printInfo("COMMAND6: Delete File: TEST1.TXT"); // Delete the file named TEST1.TXT
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.deleteFile(); //delete file
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 55: //7
printInfo("COMMAND7: List root directory"); //Print all file names in the current directory
while(flashDrive.listDir()){ // reading next file
Expand All @@ -136,9 +144,47 @@ void loop() {
}
printInfo("Done!");
break;
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
case 56: //8
totSect = flashDrive.getTotalSectors(); // get the total sector number
freeSect = flashDrive.getFreeSectors(); // get the available sector number
percentg = map(freeSect,totSect,0,0,100); // convert it to percentage (0-100)
Serial.print("Disk size in bytes: ");
/*if the sector number is more than 8388607 (8388607 * 512 = 4294966784 byte = 4Gb (fits in a 32bit variable) )
e.g. 8388608 * 512 = 4294967296 byte (32bit variable overflows) */
if(totSect > 8388607){
Serial.print(">4Gb");
} else {
Serial.print(totSect * SECTORSIZE);
}
Serial.print("\tFree space in bytes: ");
if(freeSect > 8388607){
Serial.print(">4Gb");
} else {
Serial.print(freeSect * SECTORSIZE);
}
Serial.print("\tDisk usage :");
Serial.print(percentg);
Serial.print("%");
switch (flashDrive.getFileSystem()) { //1-FAT12, 2-FAT16, 3-FAT32
case 1:
Serial.print("\tFAT12 partition");
break;
case 2:
Serial.print("\tFAT16 partition");
break;
case 3:
Serial.println("\tFAT32 partition");
break;
default:
Serial.print("\tNo valid partition");
break;
}
break;
//*****************************************************************************************************************************************************
case 104: //h
printInfo("h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n5:Modify date/time\n6:Delete\n7:List dir");
printInfo("h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n"
"5:Modify date/time\n6:Delete\n7:List dir\n8:Print free space");
break;
default:
break;
Expand All @@ -149,9 +195,9 @@ void loop() {
}//end loop

//Print information
void printInfo(char* info){
void printInfo(const char* info){
char * infoPtr = info;
int infoLength;
int infoLength = 0;
while(*infoPtr){
infoPtr++;
infoLength++;
Expand Down
Loading

0 comments on commit 017fbb1

Please sign in to comment.