Added 'disk' checking in the read example sketch.

Related to issue 158.
This commit is contained in:
Nathan Seidle 2013-09-22 20:22:31 -06:00
parent 2b37b0ea50
commit 0df7f4c164

View File

@ -1,15 +1,15 @@
/*
2-22-2012
SparkFun Electronics 2011
Nathan Seidle
Example of reading the disk properties on OpenLog
By: Nathan Seidle
SparkFun Electronics
Date: September 22nd, 2013
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
This is an exampe of writing to a file then read from a file contain OpenLog
This is an example of issuing the 'disk' command and seeing how big the current SD card is.
Connect the following OpenLog to Arduino:
TXO of OpenLog to pin 3 on the Arduino
RXI to 3
RXI of OpenLog to pin 2 on the Arduino
TXO to 3
GRN to 4
VCC to 5V
GND to GND
@ -79,6 +79,9 @@ void loop() {
gotoCommandMode(); //Puts OpenLog in command mode
readFile(fileName); //This dumps the contents of a given file to the serial terminal
//Now let's read back
readDisk(); //Check the size and stats of the SD card
Serial.println();
Serial.println("File read complete");
@ -167,10 +170,66 @@ void readFile(char *fileName) {
//above 38400bps.
//This loop will stop listening after 1 second of no characters received
for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
if(OpenLog.available()) {
Serial.write(OpenLog.read()); //Take the character from OpenLog and push it to the Arduino terminal
while(OpenLog.available()) {
char tempString[100];
int spot = 0;
while(OpenLog.available()) {
tempString[spot++] = OpenLog.read();
if(spot > 98) break;
}
tempString[spot] = '\0';
Serial.write(tempString); //Take the string from OpenLog and push it to the Arduino terminal
timeOut = 0;
}
delay(1);
}
//This is not perfect. The above loop will print the '.'s from the log file. These are the two escape characters
//recorded before the third escape character is seen.
//It will also print the '>' character. This is the OpenLog telling us it is done reading the file.
//This function leaves OpenLog in command mode
}
//Check the stats of the SD card via 'disk' command
//This function assumes the OpenLog is in command mode
void readDisk() {
//Old way
OpenLog.print("disk");
OpenLog.write(13); //This is \r
//New way
//OpenLog.print("read ");
//OpenLog.println(filename); //regular println works with OpenLog v2.51 and above
//The OpenLog echos the commands we send it by default so we have 'disk\r' sitting
//in the RX buffer. Let's try to not print this.
while(1) {
if(OpenLog.available())
if(OpenLog.read() == '\r') break;
}
//This will listen for characters coming from OpenLog and print them to the terminal
//This relies heavily on the SoftSerial buffer not overrunning. This will probably not work
//above 38400bps.
//This loop will stop listening after 1 second of no characters received
for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
while(OpenLog.available()) {
char tempString[100];
int spot = 0;
while(OpenLog.available()) {
tempString[spot++] = OpenLog.read();
if(spot > 98) break;
}
tempString[spot] = '\0';
Serial.write(tempString); //Take the string from OpenLog and push it to the Arduino terminal
timeOut = 0;
}
delay(1);
}