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