File System mit einen Argument

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
schoeni_91
Date:
Mon Nov 14 17:07:21 2016 +0000
Commit message:
File System mit einen Argument

Changed in this revision

localFileSystem_interprater.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
parser.cpp Show annotated file Show diff for this revision Revisions of this file
parser.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/localFileSystem_interprater.cpp	Mon Nov 14 17:07:21 2016 +0000
@@ -0,0 +1,127 @@
+#include "mbed.h"
+#include "parser.h"
+
+#define LOCAL_FS "fs"
+#define INTERP_FILE "/fs/STR1.txt"
+#define NORM_FILE "/fs/interp1a.txt"
+
+// global vars/objects
+const string keywords[] = {"a-nix", "LEDS", "WAIT", "REPEAT", "GOTO", "END", "NOP","TONE" ,"z-nix"}; 
+int validKeyWords;
+uint8_t statusExecute;
+BusOut myLeds(LED1, LED2, LED3, LED4);
+LocalFileSystem fs(LOCAL_FS);
+PwmOut speaker(p26);
+
+uint8_t executeComLine(int16_t command[], uint16_t *lNo)
+{   
+    static int16_t repeatCounter = 0;
+    uint8_t retVal = 0;
+    printf("line %d: %s %d\r\n", *lNo, keywords[command[0]].c_str(), command[1]); 
+    switch (command[0]) {
+        case 1: // LEDS
+            if (command[1] < UNVALID_ARG)
+                retVal = ERROR_DEF;
+            else            
+                myLeds = command[1]; 
+            break;
+        case 2: // WAIT
+            if (command[1] <= 0) 
+                command[1] = 1234;
+            wait_ms(command[1]);  
+            break;  
+        case 3: // REPEAT
+            if (++repeatCounter < command[1])
+                retVal = REPEAT_DEF; 
+            break;
+        case 4: // GOTO line
+            if (command[1] < UNVALID_ARG)
+                retVal = ERROR_DEF;
+            else {
+                *lNo = command[1];
+                retVal = GOTO_DEF;
+            }
+            break;
+        case 5: // END
+            repeatCounter = 0; break;
+            
+            case 6: //NOP
+            break;
+            
+            case 7: // TONE
+                
+               speaker.period(1.0/(float)command[1]);
+                speaker= 0.5; 
+                 wait(1);
+                 speaker= 0.0;
+                 wait(1);
+                 break;
+        default:
+            printf("no valid command: %d = %s ... ERROR\r\n\r\n", command[0], keywords[command[0]].c_str());
+            retVal = ERROR_DEF;
+            break;
+    }
+    return retVal;
+}
+
+int main()
+{
+    char myArr [100];
+    char* myStr;
+    string argLine;
+    int16_t comArray[4];
+    uint16_t lineNo = 0, lineCnt;
+    FILE *fp2rw;
+    FILE *fp2read = fopen(INTERP_FILE,"r");
+
+    fp2rw = fopen(NORM_FILE, "w"); // file name is LIMITED to 8 chars!!!
+    validKeyWords = sizeof(keywords)/sizeof(*keywords) -2;
+    printf("0) validKeyWords = %i\r\n", validKeyWords);
+
+    if (fp2read != NULL) {
+        while (fgets(myArr, sizeof myArr, fp2read) != NULL) {
+            myStr = normalizeStr(myArr);
+//            printf("a) myStr = <%s>\r\n", myStr);
+            if (myStr[0] != 0)
+                fprintf(fp2rw, "%s\r\n", myStr);
+        }
+        fclose(fp2rw);
+        fp2rw = fopen(NORM_FILE, "r"); // because "w+" and "r+" don't work
+//        printf("\r\nb) -- interpretation of Commands --\r\n\r\n");
+        while (fgets(myArr, sizeof myArr, fp2rw) != NULL) { // while 1
+            // printf("c) --> myArr = %s", myArr);
+            argLine.assign(myArr);
+            lineNo++;   // count lines
+            if (parseLine(argLine, comArray)) { // parse the command line in normalized file interp1a.txt
+                statusExecute = executeComLine(comArray, &lineNo); // interpret the command line using the prepared command array
+                if (statusExecute == REPEAT_DEF) { 
+                    rewind(fp2rw);  // do rewind if keyword is REPEAT
+                    lineNo=0;
+                }
+                else if (statusExecute == GOTO_DEF) {
+                    rewind(fp2rw);
+                    lineCnt = 0; // start with line 1  
+                    lineNo--; 
+                    while (fgets(myArr, sizeof myArr, fp2rw) != NULL) { // while 2
+                        lineCnt++;
+                        if (lineCnt>=lineNo)
+                            break; // break while 2; line found --> continue
+                    }
+                }
+                else if (statusExecute == ERROR_DEF) {
+                    printf("--> ERROR_DEF (%d) occurred in line = %d\r\n", statusExecute, lineNo);
+                    break; // break while 1; stop executing   
+                }
+            } 
+            else {
+                printf("Interpreter Error in file interp1a.txt; line = %d: %s\r\n", lineNo, myArr);
+            }
+        }
+    } else
+        printf("Error opening file to read: %s\r\n", INTERP_FILE);
+
+    fclose(fp2read);
+    fclose(fp2rw);
+    myLeds = 0x0A;
+    while(1);   // endless loop; use reset to start programm again
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 14 17:07:21 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/parser.cpp	Mon Nov 14 17:07:21 2016 +0000
@@ -0,0 +1,96 @@
+#include "parser.h"
+#include <cctype>
+
+// functions
+char* normalizeStr(char* theArr) {
+    int i=0;
+    string myStr, bestStr;
+    myStr.assign(theArr);
+    while (myStr[i]) {  // replace all white spaces into spaces (0x20)
+        if (isspace(myStr[i])) 
+            myStr[i]=' ';
+        i++;
+    }
+    myStr.erase(0, myStr.find_first_not_of(" "));  // remove leading spaces 
+    myStr.erase(myStr.find_last_not_of(" ")+1);  // remove trailing spaces
+    strcpy(theArr, myStr.c_str());
+    int x = 0;
+    for (i=0; i<=myStr.length(); i++) {
+        theArr[x] = myStr[i]; 
+        if (x>=1) {
+            if ((theArr[x] == ' ') && (theArr[x-1] == ' ')) // reduce several spaces to one space
+                x--;
+            if ((theArr[x] == ';') && (theArr[x-1] == ' ')) // delete every space before ';'
+                theArr[--x] = ';';
+        } 
+        x++;    
+        if (theArr[x-1] == ';')             
+            break; // ignore everything after ';' 
+    }
+    theArr[x] = '\0';
+    if (x>1) {
+     if ((theArr[0] == '/') && (theArr[1] == '/'))
+        theArr[0] = 0;  // ignore comments 
+    }
+    return theArr;
+}
+
+bool getArg(int16_t myCom[], string myItem) { // keyword specific argument handling
+    bool done=true;
+    switch (myCom[0]) {
+        case 1: // LEDS
+            if (myItem[0] == 'E') myCom[1]=0x0F;
+            else if (myItem[0] == 'A') myCom[1]=0;
+            else myCom[1] = (int16_t)std::strtol(myItem.c_str(), NULL, 10);
+            break;       
+        case 2: // WAIT 
+        case 3: // REPEAT
+        case 4: // GOTO
+            myCom[1] = (int16_t)std::strtol(myItem.c_str(), NULL, 10); 
+            break;
+            
+            case 5: //END
+            break;
+            case 6: //NOP
+            break;
+            case 7:
+            myCom[1] = (int16_t)std::strtol(myItem.c_str(), NULL, 10);//TONE
+            break;  
+            
+       
+        default: myCom[1]= myCom[0]*-1; done=false; break;
+    }
+//  printf("... getArg: myItem=<%s>;   %s %d\r\n", myItem.c_str(), keywords[myCom[0]].c_str(), myCom[1]); 
+    return done;
+}
+
+bool parseLine(string line, int16_t command[]) { // parse a normalized command line
+    bool done = false;
+    int i=0;
+
+    size_t pos = line.find_first_of(" ;");  // find first ' ' or ';' in string
+    string theItem = line.substr(0, pos); 
+    for (i=1; i <= validKeyWords; i++) {
+        if (keywords[i].compare(theItem)==0) {  // compare keyword with keywords in list (array)
+            command[0] = i;             // store no representation of the found keyword in command[0]
+            if (line[pos] == ';') {
+                done = true;
+                command[1] = -101;  
+            }
+            else {
+                line.erase(0, line.find_first_of(" ")+1); // remove keyword from string
+                pos = line.find_first_of(";\r");    // error tolerant: if line is not completed with ';'; --> '\r\n' .. end of line
+                if (pos >0) {
+                    theItem = line.substr(0, pos); 
+                    done = getArg(command, theItem);    // handling of a second arument depending on the keyword
+                }
+                else {
+                    command[1] = -102;
+                    done = false;
+                }
+            }
+            break;  // keyword found
+        }
+    } // for
+    return done;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/parser.h	Mon Nov 14 17:07:21 2016 +0000
@@ -0,0 +1,18 @@
+#include "mbed.h"
+#include <string>
+
+// #define ValidKEYWORDS 5
+#define UNVALID_ARG -100
+#define ERROR_DEF 255
+#define REPEAT_DEF 1
+#define GOTO_DEF 2
+
+// global vars
+extern const string keywords[]; // defined in interpreter.cpp
+extern int validKeyWords;
+
+// functions
+char* normalizeStr(char* theArr);   // reduce whitespaces, etc.
+// bool getArg(int16_t myCom[], string myItem);  // keyword specific argument handling
+bool parseLine(string line, int16_t command[]);  // parse a normalized command line
+