The problem with this code is that the info for it is in the actual header file. You can open the header file with WordPad, and go to the function that you want to look at by searching using ctrl+f.
To enter a new line, just put \n in your [edit]code string[/edit]. It doesn't print in Notepad, but it works in WordPad.
It is also a problem for people who don't have a lot of coding experience, as it isn't an object based class (which people who have solely used the Arduino are comfortable with)
(I know how you feel about posting code, PaulS, but this is quite a problem)
I found problems with the examples, and have came up with my own subroutines.
I was having trouble creating a few new files at once, so I added this to my create file:
void create_new_file(char *file_save_name) {
if(!fat_create_file(dd, file_save_name, &dir_entry)){
fat_delete_file(fs, &dir_entry);
fat_create_file(dd, file_save_name, &dir_entry);
}
sd_raw_sync();
}
This is a subroutine that writes what is in buffer to your file. I was having trouble creating a default value to _offset, so I just decided to keep it as an entry and write 0 there when I don't want to offset where I am writing to (you may want to overwrite the last 2 characters: To do this, just change _offset to -2).
void AddBufferToFile(char *FileWritingTo, int32_t _offset){
int32_t OffSet = _offset;
//Open the file at the beginning of the file
file_handle = open_file_in_dir(fs, dd, FileWritingTo);
if(file_handle)
{
//Now go to the end of the file to write some data.
if(fat_seek_file(file_handle, &OffSet, FAT_SEEK_END))
{
//Write the new 'buffer' string to the end of the file
fat_write_file(file_handle, (const uint8_t*)buffer, strlen(buffer));
//sychronise SD (Required)
sd_raw_sync();
}
//Close the file
fat_close_file(file_handle);
}
}
This next on gets info from a file (name is in a variable "char file_name[30];"
Note that you can change file_name to an entry to the function. However, I do not change the file that I am writing to, so I was just saving some code by taking the name from the same place each time.
int AddFileToBuffer(int32_t OffSet){
int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card.
//Open the file
file_handle = open_file_in_dir(fs, dd, file_name);
//Read up to 512 bytes from the file
if(file_handle) {
fat_seek_file(file_handle, &OffSet, FAT_SEEK_SET);
bytes_read = fat_read_file(file_handle, (uint8_t*)buffer, BUFFERSIZE);
//Close the file before moving on to the next one.
fat_close_file(file_handle);
return(bytes_read);
}
}
Here is an example on how to use it. It once again uses the variable file_name as a file destination, and an input file called "_xml_file_name[]". The idea of this function is to save data in a separate file, and at the end of recording data, all the different records are put together in an XML format.
void printDataFromFile(char _xml_file_name[]) {
int bytes_read = 0;
int32_t OffSet = 0;
do {
OffSet += (int32_t)bytes_read;
bytes_read = AddFileToBuffer(OffSet);
buffer[bytes_read] = '\0';
AddBufferToFile(_xml_file_name,0);
}
while(bytes_read == BUFFERSIZE);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table[13])));
bytes_read > 2? OffSet = -2: OffSet = 0;
AddBufferToFile(_xml_file_name,OffSet);
}
You need to be careful that you don't omit the '\0' (EOT) character to the string you are entering.
If you have any questions, please feel free to send me a message
