Preliminary main mbed library for nexpaq development
libraries/tests/mbed/dir/main.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | |
nexpaq | 0:6c56fb4bc5f0 | 3 | #if !DEVICE_LOCALFILESYSTEM |
nexpaq | 0:6c56fb4bc5f0 | 4 | #error [NOT_SUPPORTED] LocalFileSystem not supported |
nexpaq | 0:6c56fb4bc5f0 | 5 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 6 | |
nexpaq | 0:6c56fb4bc5f0 | 7 | void led_blink(PinName led) { |
nexpaq | 0:6c56fb4bc5f0 | 8 | DigitalOut myled(led); |
nexpaq | 0:6c56fb4bc5f0 | 9 | while (1) { |
nexpaq | 0:6c56fb4bc5f0 | 10 | myled = !myled; |
nexpaq | 0:6c56fb4bc5f0 | 11 | wait(1.0); |
nexpaq | 0:6c56fb4bc5f0 | 12 | } |
nexpaq | 0:6c56fb4bc5f0 | 13 | } |
nexpaq | 0:6c56fb4bc5f0 | 14 | |
nexpaq | 0:6c56fb4bc5f0 | 15 | void notify_completion(bool success) { |
nexpaq | 0:6c56fb4bc5f0 | 16 | if (success) { |
nexpaq | 0:6c56fb4bc5f0 | 17 | printf("{success}\n"); |
nexpaq | 0:6c56fb4bc5f0 | 18 | } else { |
nexpaq | 0:6c56fb4bc5f0 | 19 | printf("{failure}\n"); |
nexpaq | 0:6c56fb4bc5f0 | 20 | } |
nexpaq | 0:6c56fb4bc5f0 | 21 | |
nexpaq | 0:6c56fb4bc5f0 | 22 | printf("{end}\n"); |
nexpaq | 0:6c56fb4bc5f0 | 23 | led_blink(success ? LED1 : LED4); |
nexpaq | 0:6c56fb4bc5f0 | 24 | } |
nexpaq | 0:6c56fb4bc5f0 | 25 | |
nexpaq | 0:6c56fb4bc5f0 | 26 | #define TEST_STRING "Hello World!" |
nexpaq | 0:6c56fb4bc5f0 | 27 | |
nexpaq | 0:6c56fb4bc5f0 | 28 | FILE* test_open(char* path, const char* mode) { |
nexpaq | 0:6c56fb4bc5f0 | 29 | FILE *f; |
nexpaq | 0:6c56fb4bc5f0 | 30 | f = fopen(path, mode); |
nexpaq | 0:6c56fb4bc5f0 | 31 | if (f == NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 32 | printf("Error opening file\n"); |
nexpaq | 0:6c56fb4bc5f0 | 33 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 34 | } |
nexpaq | 0:6c56fb4bc5f0 | 35 | |
nexpaq | 0:6c56fb4bc5f0 | 36 | return f; |
nexpaq | 0:6c56fb4bc5f0 | 37 | } |
nexpaq | 0:6c56fb4bc5f0 | 38 | |
nexpaq | 0:6c56fb4bc5f0 | 39 | void test_write(FILE* f, const char* str) { |
nexpaq | 0:6c56fb4bc5f0 | 40 | int n = fprintf(f, str); |
nexpaq | 0:6c56fb4bc5f0 | 41 | if (n != strlen(str)) { |
nexpaq | 0:6c56fb4bc5f0 | 42 | printf("Error writing file\n"); |
nexpaq | 0:6c56fb4bc5f0 | 43 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 44 | } |
nexpaq | 0:6c56fb4bc5f0 | 45 | } |
nexpaq | 0:6c56fb4bc5f0 | 46 | |
nexpaq | 0:6c56fb4bc5f0 | 47 | void test_close(FILE* f) { |
nexpaq | 0:6c56fb4bc5f0 | 48 | int rc = fclose(f); |
nexpaq | 0:6c56fb4bc5f0 | 49 | if (rc != 0) { |
nexpaq | 0:6c56fb4bc5f0 | 50 | printf("Error closing file\n"); |
nexpaq | 0:6c56fb4bc5f0 | 51 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 52 | } |
nexpaq | 0:6c56fb4bc5f0 | 53 | } |
nexpaq | 0:6c56fb4bc5f0 | 54 | |
nexpaq | 0:6c56fb4bc5f0 | 55 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 56 | LocalFileSystem local("local"); |
nexpaq | 0:6c56fb4bc5f0 | 57 | |
nexpaq | 0:6c56fb4bc5f0 | 58 | FILE *f; |
nexpaq | 0:6c56fb4bc5f0 | 59 | char* str = TEST_STRING; |
nexpaq | 0:6c56fb4bc5f0 | 60 | char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING)); |
nexpaq | 0:6c56fb4bc5f0 | 61 | int str_len = strlen(TEST_STRING); |
nexpaq | 0:6c56fb4bc5f0 | 62 | |
nexpaq | 0:6c56fb4bc5f0 | 63 | printf("Write files\n"); |
nexpaq | 0:6c56fb4bc5f0 | 64 | char filename[32]; |
nexpaq | 0:6c56fb4bc5f0 | 65 | for (int i=0; i<10; i++) { |
nexpaq | 0:6c56fb4bc5f0 | 66 | sprintf(filename, "/local/test_%d.txt", i); |
nexpaq | 0:6c56fb4bc5f0 | 67 | printf("Creating file: %s\n", filename); |
nexpaq | 0:6c56fb4bc5f0 | 68 | f = test_open(filename, "w"); |
nexpaq | 0:6c56fb4bc5f0 | 69 | test_write(f, str); |
nexpaq | 0:6c56fb4bc5f0 | 70 | test_close(f); |
nexpaq | 0:6c56fb4bc5f0 | 71 | } |
nexpaq | 0:6c56fb4bc5f0 | 72 | |
nexpaq | 0:6c56fb4bc5f0 | 73 | printf("List files:\n"); |
nexpaq | 0:6c56fb4bc5f0 | 74 | DIR *d = opendir("/local"); |
nexpaq | 0:6c56fb4bc5f0 | 75 | struct dirent *p; |
nexpaq | 0:6c56fb4bc5f0 | 76 | while((p = readdir(d)) != NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 77 | printf("%s\n", p->d_name); |
nexpaq | 0:6c56fb4bc5f0 | 78 | } |
nexpaq | 0:6c56fb4bc5f0 | 79 | closedir(d); |
nexpaq | 0:6c56fb4bc5f0 | 80 | |
nexpaq | 0:6c56fb4bc5f0 | 81 | notify_completion(true); |
nexpaq | 0:6c56fb4bc5f0 | 82 | } |