]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/dir/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / mbed / dir / main.cpp
1 #include "mbed.h"
2
3 void led_blink(PinName led) {
4     DigitalOut myled(led);
5     while (1) {
6         myled = !myled;
7         wait(1.0);
8     }
9 }
10
11 void notify_completion(bool success) {
12     if (success) {
13         printf("{success}\n");
14     } else {
15         printf("{failure}\n");
16     }
17
18     printf("{end}\n");
19     led_blink(success ? LED1 : LED4);
20 }
21
22 #define TEST_STRING   "Hello World!"
23
24 FILE* test_open(char* path, const char* mode) {
25     FILE *f;
26     f = fopen(path, mode);
27     if (f == NULL) {
28         printf("Error opening file\n");
29         notify_completion(false);
30     }
31
32     return f;
33 }
34
35 void test_write(FILE* f, const char* str) {
36     int n = fprintf(f, str);
37     if (n != strlen(str)) {
38         printf("Error writing file\n");
39         notify_completion(false);
40     }
41 }
42
43 void test_close(FILE* f) {
44     int rc = fclose(f);
45     if (rc != 0) {
46         printf("Error closing file\n");
47         notify_completion(false);
48     }
49 }
50
51 int main() {
52     LocalFileSystem local("local");
53
54     FILE *f;
55     char* str = TEST_STRING;
56     char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING));
57     int str_len = strlen(TEST_STRING);
58
59     printf("Write files\n");
60     char filename[32];
61     for (int i=0; i<10; i++) {
62         sprintf(filename, "/local/test_%d.txt", i);
63         printf("Creating file: %s\n", filename);
64         f = test_open(filename, "w");
65         test_write(f, str);
66         test_close(f);
67     }
68
69     printf("List files:\n");
70     DIR *d = opendir("/local");
71     struct dirent *p;
72     while((p = readdir(d)) != NULL) {
73         printf("%s\n", p->d_name);
74     }
75     closedir(d);
76
77     notify_completion(true);
78 }