Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 Serial pc(USBTX, USBRX); 00004 LocalFileSystem local("local"); 00005 00006 void get_from_pc(char* buffer) { 00007 char c; 00008 int i; 00009 for (i=0; i<64; i++) buffer[i] = '\0'; 00010 i=0; 00011 pc.printf(": "); 00012 while ((i<64) && ((c = pc.getc()) != '\n')) { 00013 if (c == 0x7f) { 00014 if (i>0) { 00015 i--; 00016 buffer[i] = '\0'; 00017 pc.putc(8); 00018 pc.putc(' '); 00019 pc.putc(8); 00020 } 00021 } else { 00022 pc.putc(c); 00023 buffer[i] = c; 00024 i++; 00025 } 00026 } 00027 pc.printf("\n\r"); 00028 } 00029 00030 void ls() { 00031 DIR* d = opendir("/local"); 00032 struct dirent *p; 00033 while ((p=readdir(d)) != NULL) pc.printf("%s\n\r", p->d_name); 00034 closedir(d); 00035 } 00036 00037 void cat(char* buffer) { 00038 int i; 00039 signed char c; 00040 char filepath[19] = "/local/"; 00041 for (i=0;i<60;i++) buffer[i] = buffer[i+4]; 00042 for (i=7;i<19;i++) filepath[i] = buffer[i-7]; 00043 FILE* file = fopen(filepath, "r"); 00044 while ((c=fgetc(file)) != EOF) { 00045 pc.putc(c); 00046 if (c == '\n') pc.putc('\r'); 00047 } 00048 fclose(file); 00049 pc.printf("\r"); 00050 } 00051 00052 void ed(char* buffer) { 00053 int i; 00054 signed char c; 00055 char filepath[19] = "/local/"; 00056 for (i=0;i<61;i++) buffer[i] = buffer[i+3]; 00057 for (i=7;i<19;i++) filepath[i] = buffer[i-7]; 00058 FILE* file = fopen(filepath, "w"); 00059 c = pc.getc(); 00060 while ((c != 4) && (c != EOF)) { 00061 if (c == 0x7F) { 00062 pc.putc(8); 00063 pc.putc(' '); 00064 pc.putc(8); 00065 fseek(file, -1, SEEK_CUR); 00066 } else { 00067 pc.putc(c); 00068 fputc(c, file); 00069 if (c == '\n') pc.putc('\r'); 00070 } 00071 c = pc.getc(); 00072 } 00073 pc.printf("\n\r"); 00074 fputc('\n', file); 00075 fflush(file); 00076 fclose(file); 00077 } 00078 00079 int main() { 00080 char buffer[64]; 00081 pc.baud(115200); 00082 pc.printf("Local Flash Filesystem Test\n\r"); 00083 pc.printf("Nathan Lasseter 2010\n\n\r"); 00084 pc.printf("Up to 64 characters input is allowed,\n\r\tup to 8 character filenames (excluding extension)\n\r"); 00085 pc.printf("Commands: ls, cat filename, ed filename\n\n\r"); 00086 while(1) { 00087 get_from_pc(buffer); 00088 if (strncmp(buffer, "ls", 2) == 0) { 00089 ls(); 00090 } else if (strncmp(buffer, "cat", 3) == 0) { 00091 cat(buffer); 00092 } else if (strncmp(buffer, "ed", 2) == 0) { 00093 ed(buffer); 00094 } else if (strcmp(buffer, "") == 0) { 00095 pc.printf("! No command\n\r"); 00096 } else { 00097 pc.printf("! Invalid Command <%s>\n\r", buffer); 00098 } 00099 } 00100 }
Generated on Tue Jul 19 2022 02:11:05 by
1.7.2