Thanks for all the help folks! I did, indeed find what I needed when I found this code. I added some documentation to as I understand it.
It is the blessing/curse of Arduino that it is not all things to all people. The blessing is that we don't have to endure bloatware, but the curse is the one thing we want is not there and we have to write it ourselves. I'm good with it. As a note, the windows executable to output "hello, world" is well over a megabyte.
Thanks again!
OSD
/*
- This program tests the dateTimeCallback() function
- and the timestamp() function.
*/
#include <SPI.h>
#include <SdFat.h>
SdFat sd;
SdFile file;
// Default SD chip select is SS pin
const uint8_t chipSelect = SS;
// create Serial stream (in my code, I used Serial.print)
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt(F(s)) // I assume that the compiler has bunch of
// error messages
//------------------------------------------------------------------------------
/*
- date/time values for debug
- normally supplied by a real-time clock or GPS
*/
// date 1-Oct-14 (I used them as global variables and stuffed them with data
// extracted from my GPS)
uint16_t year = 2014;
uint8_t month = 10;
uint8_t day = 1;
// time 20:30:40
uint8_t hour = 20;
uint8_t minute = 30;
uint8_t second = 40;
//------------------------------------------------------------------------------
/*
- User provided date time callback function.
- See SdFile::dateTimeCallback() for usage.
This routine takes the information from the global time/date variables above,
formats into something the file.open (function of SDfat) can use and
puts the formated information in a place that the file.open can find it.
/
void dateTime(uint16_t date, uint16_t* time) {
// User gets date and time from GPS or real-time
// clock in real callback function
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(year, month, day);
// return time using FAT_TIME macro to format fields
time = FAT_TIME(hour, minute, second);
}
//------------------------------------------------------------------------------
/
- Function to print all timestamps.
*/
void printTimestamps(SdFile& f) {
dir_t d;
if (!f.dirEntry(&d)) {
error("f.dirEntry failed");
}
cout << F("Creation: ");
f.printFatDate(d.creationDate);
cout << ' ';
f.printFatTime(d.creationTime);
cout << endl;
cout << F("Modify: ");
f.printFatDate(d.lastWriteDate);
cout <<' ';
f.printFatTime(d.lastWriteTime);
cout << endl;
cout << F("Access: ");
f.printFatDate(d.lastAccessDate);
cout << endl;
}
//------------------------------------------------------------------------------
void setup(void) {
Serial.begin(9600);
while (!Serial) {} // wait for Leonardo
cout << F("Type any character to start\n");
while (!Serial.available());
delay(400); // catch Due reset problem
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
sd.initErrorHalt();
}
// remove files if they exist
sd.remove("callback.txt");
sd.remove("default.txt");
sd.remove("stamp.txt");
// create a new file with default timestamps
if (!file.open("default.txt", O_CREAT | O_WRITE)) {
error("open default.txt failed");
}
cout << F("\nOpen with default times\n");
printTimestamps(file);
// close file
file.close();
/*
- Test the date time callback function.
-
- dateTimeCallback() sets the function
- that is called when a file is created
- or when a file's directory entry is
- modified by sync().
-
- The callback can be disabled by the call
- SdFile::dateTimeCallbackCancel()
*/
// set date time callback function
SdFile::dateTimeCallback(dateTime); // This call tells file.open to call the
// dateTime() function coded above.
//
// This call only needs to happen once, thus,
// it can be in the setup() section.
//
// There seems to be a bug in that the
// last access time does not get written.
// The date does get written.
// create a new file with callback timestamps
if (!file.open("callback.txt", O_CREAT | O_WRITE)) { // the timedate stamp are
// automatically written.)
error("open callback.txt failed");
}
cout << ("\nOpen with callback times\n");
printTimestamps(file);
// change call back date
day += 1;
// must add two to see change since FAT second field is 5-bits
second += 2;
// modify file by writing a byte // Set up the data to be written on the card
file.write('t');
// force dir update // cause the data to be written.
file.sync();
cout << F("\nTimes after write\n");
printTimestamps(file);
// close file
file.close();
/*
- Test timestamp() function
-
- Cancel callback so sync will not
- change access/modify timestamp
*/
SdFile::dateTimeCallbackCancel(); // this call cancels the call to dateTime
// thus reverting to the default timestamp.
// create a new file with default timestamps
if (!file.open("stamp.txt", O_CREAT | O_WRITE)) {
error("open stamp.txt failed");
}
// set creation date time
if (!file.timestamp(T_CREATE, 2014, 11, 10, 1, 2, 3)) {
error("set create time failed");
}
// set write/modification date time
if (!file.timestamp(T_WRITE, 2014, 11, 11, 4, 5, 6)) {
error("set write time failed");
}
// set access date
if (!file.timestamp(T_ACCESS, 2014, 11, 12, 7, 8, 9)) {
error("set access time failed");
}
cout << F("\nTimes after timestamp() calls\n");
printTimestamps(file);
file.close();
cout << F("\nDone\n");
}
void loop(void) {}