1
1
#include " mbed.h"
2
2
#include " SDFileSystem.h"
3
3
4
- void led_blink (PinName led) {
4
+ void led_blink (PinName led)
5
+ {
5
6
DigitalOut myled (led);
7
+
6
8
while (1 ) {
7
9
myled = !myled;
8
10
wait (1.0 );
9
11
}
10
12
}
11
13
12
- void notify_completion (bool success) {
13
- if (success) {
14
+ void notify_completion (bool success)
15
+ {
16
+ if (success)
14
17
printf (" {success}\n " );
15
- } else {
18
+ else
16
19
printf (" {failure}\n " );
17
- }
18
-
20
+
19
21
printf (" {end}\n " );
20
22
led_blink (success ? LED1 : LED4);
21
23
}
22
24
23
25
#define TEST_STRING " Hello World!"
24
26
25
- FILE* test_open (char * path, const char * mode) {
27
+ FILE *test_open (char *path, const char *mode)
28
+ {
26
29
FILE *f;
30
+
27
31
f = fopen (path, mode);
28
32
if (f == NULL ) {
29
33
printf (" Error opening file\n " );
30
34
notify_completion (false );
31
35
}
32
-
36
+
33
37
return f;
34
38
}
35
39
36
- void test_write (FILE* f, const char * str) {
40
+ void test_write (FILE *f, const char *str)
41
+ {
37
42
int n = fprintf (f, str);
43
+
38
44
if (n != strlen (str)) {
39
45
printf (" Error writing file\n " );
40
46
notify_completion (false );
41
47
}
42
48
}
43
49
44
- void test_close (FILE* f) {
50
+ void test_close (FILE *f)
51
+ {
45
52
int rc = fclose (f);
53
+
46
54
if (rc != 0 ) {
47
55
printf (" Error closing file\n " );
48
56
notify_completion (false );
@@ -51,52 +59,52 @@ void test_close(FILE* f) {
51
59
52
60
DigitalOut led2 (LED2);
53
61
54
- int main () {
62
+ int main ()
63
+ {
55
64
#if defined(TARGET_KL25Z)
56
65
SDFileSystem sd (PTD2, PTD3, PTD1, PTD0, " sd" );
57
66
#elif defined(TARGET_nRF51822)
58
67
// SDFileSystem sd(p20, p22, p25, p24, "sd");
59
- SDFileSystem sd (p12, p13, p15, p14, " sd" );
68
+ SDFileSystem sd (p12, p13, p15, p14, " sd" );
60
69
#elif defined(TARGET_NUCLEO_F103RB) || \
61
70
defined (TARGET_NUCLEO_L152RE) || \
62
71
defined (TARGET_NUCLEO_F302R8) || \
63
72
defined (TARGET_NUCLEO_F030R8) || \
64
73
defined (TARGET_NUCLEO_F401RE) || \
65
74
defined (TARGET_NUCLEO_L053R8)
66
- SDFileSystem sd (D11, D12, D13, D10, " sd" );
75
+ SDFileSystem sd (D11, D12, D13, D10, " sd" );
67
76
#else
68
77
SDFileSystem sd (p11, p12, p13, p14, " sd" );
69
78
#endif
70
- led2= 1 ;
71
- wait (0.5 );
79
+ led2 = 1 ;
80
+ wait (0.5 );
72
81
FILE *f;
73
- char * str = TEST_STRING;
74
- char * buffer = (char *) malloc (sizeof (unsigned char )* strlen (TEST_STRING));
82
+ char * str = TEST_STRING;
83
+ char * buffer = (char *) malloc (sizeof (unsigned char ) * strlen (TEST_STRING));
75
84
int str_len = strlen (TEST_STRING);
76
-
85
+
77
86
printf (" Write files\n " );
78
87
char filename[32 ];
79
- for (int i= 0 ; i< 10 ; i++) {
88
+ for (int i = 0 ; i < 10 ; i++) {
80
89
sprintf (filename, " /sd/test_%d.txt" , i);
81
90
printf (" Creating file: %s\n " , filename);
82
91
f = test_open (filename, " w" );
83
- led2= 0 ;
92
+ led2 = 0 ;
84
93
test_write (f, str);
85
94
test_close (f);
86
95
}
87
-
96
+
88
97
printf (" List files:\n " );
89
98
DIR *d = opendir (" /sd" );
90
99
if (d == NULL ) {
91
100
printf (" Error opening directory\n " );
92
101
notify_completion (false );
93
102
}
94
-
103
+
95
104
struct dirent *p;
96
- while ((p = readdir (d)) != NULL ) {
105
+ while ((p = readdir (d)) != NULL )
97
106
printf (" %s\n " , p->d_name );
98
- }
99
107
closedir (d);
100
-
108
+
101
109
notify_completion (true );
102
110
}
0 commit comments