major bug fixes, way too many to enumerate
[clinton/Smoothieware.git] / gcc4mbed / samples / SDFileSystem / main.cpp
1 /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15 /* LocalFileSystem test modified to use SD cards instead. */
16 #include "mbed.h"
17 #include "SDFileSystem.h"
18
19
20 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
21
22
23 int main()
24 {
25 int Result = -1;
26 char Buffer[32];
27
28 printf("\r\n\r\nGCC4MBED Test Suite\r\n");
29 printf("sdFileSystem Unit Tests\r\n");
30
31 printf("Test 1: fopen() for write\r\n");
32 FILE *fp = fopen("/sd/out.txt", "w"); // Open "out.txt" on the sd file system for writing
33 if (NULL == fp)
34 {
35 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);
36 }
37
38 printf("Test 2: fprintf()\r\n");
39 Result = fprintf(fp, "Hello World!");
40 if (Result < 0)
41 {
42 error("%s(%d) fprintf() failed\r\n", __FILE__, __LINE__);
43 }
44
45 printf("Test 3: fclose() on written file\r\n");
46 Result = fclose(fp);
47 if (0 != Result)
48 {
49 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);
50 }
51
52
53
54 printf("Test 4: fopen() for read\r\n");
55 fp = fopen("/sd/out.txt", "r");
56 if (NULL == fp)
57 {
58 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);
59 }
60
61 printf("Test 5: fscanf()\r\n");
62 Result = fscanf(fp, "%31s", Buffer);
63 if (EOF == Result)
64 {
65 error("%s(%d) fscanf() failed\r\n", __FILE__, __LINE__);
66 }
67 printf("Contents of /sd/out.txt: %s\r\n", Buffer);
68
69 printf("Test 6: fclose() on read file\r\n");
70 Result = fclose(fp);
71 if (0 != Result)
72 {
73 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);
74 }
75
76
77
78 printf("Test 7: remove()\r\n");
79 Result = remove("/sd/out.txt"); // Removes the file "out.txt" from the sd file system
80 if (0 != Result)
81 {
82 error("%s(%d) remove() failed\r\n", __FILE__, __LINE__);
83 }
84
85
86 printf("Test 8: opendir()\r\n");
87 DIR *d = opendir("/sd"); // Opens the root directory of the sd file system
88 printf("test\r\n");
89 if (NULL == d)
90 {
91 error("%s(%d) opendir() failed\r\n", __FILE__, __LINE__);
92 }
93 struct dirent *p;
94
95 printf("Test 9: readir() for all entries\r\n");
96 while((p = readdir(d)) != NULL)
97 { // Print the names of the files in the sd file system
98 printf("%s\r\n", p->d_name); // to stdout.
99 }
100
101 printf("Test 10: closedir\r\n");
102 closedir(d);
103
104 printf("\r\nTest completed\r\n");
105 }