Create a new log file every time on startup
[rmslog.git] / FAT16 / examples / FAT16_WriteExample / applet / FAT16_WriteExample.cpp
1 /*
2 FAT16 WriteFile Example Sketch
3 SparkFun Electronics
4 Written by Ryan Owens
5 3/16/2010
6
7 Code Description: Uses an Arduino Duemillanove or Arduino Pro to write a string to a file.
8
9 Circuit Description: Uses the SparkFun microSD shield. (http://www.sparkfun.com/commerce/product_info.php?products_id=9520)
10
11 Attributions: Special thanks to Roland Riegel for providing an open source FAT library 
12 for AVR microcontrollers. See more of his projects here:
13 http://www.roland-riegel.de/
14
15 This code is provided under the Creative Commons Attribution License. More information can be found here:
16 http://creativecommons.org/licenses/by/3.0/
17
18 (Use our code freely! Please just remember to give us credit where it's due. Thanks!)
19 */
20
21 //Add libraries to support FAT16 on the SD Card.
22 //(Note: If you already have these libraries installed in the directory, they'll have to remove in order to compile this.)
23 #include <byteordering.h>
24 #include <fat.h>
25 #include <fat_config.h>
26 #include <partition.h>
27 #include <partition_config.h>
28 #include <sd-reader_config.h>
29 #include <sd_raw.h>
30 #include <sd_raw_config.h>
31
32 //Define the pin numbers
33 #define CS    8
34 #define MOSI    11
35 #define MISO    12
36 #define SCK    13
37
38 //This is the amount of data to be fetched from the SD card for each read.
39 #define BUFFERSIZE      256
40
41 #include "WProgram.h"
42 void setup();
43 void loop();
44 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry);
45 struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name);
46 char init_filesystem(void);
47 char get_next_filename(struct fat_dir_struct* cur_dir, char * new_file);
48 char buffer[BUFFERSIZE]="Testing\n";
49 char file_name[30];
50
51 struct fat_dir_struct* dd;              //FAT16 directory
52 struct fat_dir_entry_struct dir_entry;  //FAT16 directory entry (A.K.A. a file)
53
54 struct fat_fs_struct* fs;               //FAT16 File System
55 struct partition_struct* partition;     //FAT16 Partition
56
57 struct fat_file_struct * file_handle;   //FAT16 File Handle
58
59 void setup()
60 {
61     //Set up the pins for the Serial communication
62     pinMode(0, INPUT);
63     pinMode(1, OUTPUT);
64     Serial.begin(9600);
65  
66     //Set up the pins for the microSD shield
67     pinMode(CS, OUTPUT);
68     pinMode(MOSI, OUTPUT);
69     pinMode(MISO, INPUT);
70     pinMode(SCK, OUTPUT);
71     pinMode(10, OUTPUT);
72 }
73
74 void loop()
75 {
76   int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card.
77   int count=0;
78
79   init_filesystem();    //Initialize the FAT16 file system on the SD card.
80   
81   //Create a file named Test.txt. If the file already exists, delete it and create a new one.
82   if(!fat_create_file(dd, "Test.txt", &dir_entry)){
83       fat_delete_file(fs, &dir_entry);
84       fat_create_file(dd, "Test.txt", &dir_entry);
85   }
86   //Open the file that's just been created
87   file_handle=open_file_in_dir(fs, dd, "Test.txt");
88   //Write some initial data to the file
89   fat_write_file(file_handle, (const uint8_t*)buffer, strlen(buffer));
90   sd_raw_sync();    //An SD sync must be performed after each write operation
91   fat_close_file(file_handle);    //Close the file.
92   while(1){
93       //Open the file (now we're at the beginning of the file again.
94       open_file_in_dir(fs, dd, "Test.txt");
95       //Read the contents of the file (up to 512 bytes)
96       bytes_read = fat_read_file(file_handle, (uint8_t*)buffer, BUFFERSIZE);
97       //Print the contents of the file
98       Serial.println((const char*)buffer);
99       //Now go to the end of the file to write some data.
100       fat_seek_file(file_handle, 0, FAT_SEEK_END);
101       sprintf(buffer, "%d", count++);
102       //Write the new 'buffer' string to the end of the file
103       fat_write_file(file_handle, (const uint8_t*)buffer, strlen(buffer));
104       sd_raw_sync();
105       //Close the file, we're finished!
106       fat_close_file(file_handle);
107       delay(1000);
108   }
109   
110   while(1);
111 }
112
113 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry)
114 {
115         fat_reset_dir(dd);      //Make sure to start from the beginning of the directory!
116     while(fat_read_dir(dd, dir_entry))
117     {
118         if(strcmp(dir_entry->long_name, name) == 0)
119         {
120             //fat_reset_dir(dd);
121             return 1;
122         }
123     }
124
125     return 0;
126 }
127
128 struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
129 {
130     struct fat_dir_entry_struct file_entry;
131     if(!find_file_in_dir(fs, dd, name, &file_entry))
132         return 0;
133
134     return fat_open_file(fs, &file_entry);
135 }
136
137 char init_filesystem(void)
138 {
139         //setup sd card slot 
140         if(!sd_raw_init())
141         {
142                 return 0;
143         }
144
145         //open first partition
146         partition = partition_open(sd_raw_read,
147                                                                         sd_raw_read_interval,
148 #if SD_RAW_WRITE_SUPPORT
149                                                                         sd_raw_write,
150                                                                         sd_raw_write_interval,
151 #else
152                                                                         0,
153                                                                         0,
154 #endif
155                                                                         0
156                                                            );
157
158         if(!partition)
159         {
160                 //If the partition did not open, assume the storage device
161                 //is a "superfloppy", i.e. has no MBR.
162                 partition = partition_open(sd_raw_read,
163                                                                    sd_raw_read_interval,
164 #if SD_RAW_WRITE_SUPPORT
165                                                                    sd_raw_write,
166                                                                    sd_raw_write_interval,
167 #else
168                                                                    0,
169                                                                    0,
170 #endif
171                                                                    -1
172                                                                   );
173                 if(!partition)
174                 {
175                         return 0;
176                 }
177         }
178
179         //Open file system
180         fs = fat_open(partition);
181         if(!fs)
182         {
183                 return 0;
184         }
185
186         //Open root directory
187         fat_get_dir_entry_of_path(fs, "/", &dir_entry);
188         dd=fat_open_dir(fs, &dir_entry);
189         
190         if(!dd)
191         {
192                 return 0;
193         }
194         return 1;
195 }
196
197 char get_next_filename(struct fat_dir_struct* cur_dir, char * new_file)
198 {
199     //'dir_entry' is a global variable of type directory_entry_struct
200
201     //Get the next file from the root directory
202     if(fat_read_dir(cur_dir, &dir_entry))
203     {
204         sprintf(new_file, "%s", dir_entry.long_name);
205         Serial.println((const char *)new_file);
206         return 1;
207     }
208     //If another file isn't found, return 0
209     return 0;
210 }
211
212
213
214 int main(void)
215 {
216         init();
217
218         setup();
219     
220         for (;;)
221                 loop();
222         
223         return 0;
224 }
225