Files
qmk-keychron-q3-colemak-dh/tool/mbed/mbed-sdk/libraries/tests/mbed/dir/main.cpp
Jun Wako 1fe4406f37 Squashed 'tmk_core/' changes from 7967731..b9e0ea0
b9e0ea0 Merge commit '7fa9d8bdea3773d1195b04d98fcf27cf48ddd81d' as 'tool/mbed/mbed-sdk'
7fa9d8b Squashed 'tool/mbed/mbed-sdk/' content from commit 7c21ce5

git-subtree-dir: tmk_core
git-subtree-split: b9e0ea08cb940de20b3610ecdda18e9d8cd7c552
2015-04-24 16:26:14 +09:00

79 lines
1.6 KiB
C++

#include "mbed.h"
void led_blink(PinName led) {
DigitalOut myled(led);
while (1) {
myled = !myled;
wait(1.0);
}
}
void notify_completion(bool success) {
if (success) {
printf("{success}\n");
} else {
printf("{failure}\n");
}
printf("{end}\n");
led_blink(success ? LED1 : LED4);
}
#define TEST_STRING "Hello World!"
FILE* test_open(char* path, const char* mode) {
FILE *f;
f = fopen(path, mode);
if (f == NULL) {
printf("Error opening file\n");
notify_completion(false);
}
return f;
}
void test_write(FILE* f, const char* str) {
int n = fprintf(f, str);
if (n != strlen(str)) {
printf("Error writing file\n");
notify_completion(false);
}
}
void test_close(FILE* f) {
int rc = fclose(f);
if (rc != 0) {
printf("Error closing file\n");
notify_completion(false);
}
}
int main() {
LocalFileSystem local("local");
FILE *f;
char* str = TEST_STRING;
char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING));
int str_len = strlen(TEST_STRING);
printf("Write files\n");
char filename[32];
for (int i=0; i<10; i++) {
sprintf(filename, "/local/test_%d.txt", i);
printf("Creating file: %s\n", filename);
f = test_open(filename, "w");
test_write(f, str);
test_close(f);
}
printf("List files:\n");
DIR *d = opendir("/local");
struct dirent *p;
while((p = readdir(d)) != NULL) {
printf("%s\n", p->d_name);
}
closedir(d);
notify_completion(true);
}