File System mit einen Argument
Dependencies: mbed
localFileSystem_interprater.cpp@0:6791a518728e, 2016-11-14 (annotated)
- Committer:
- schoeni_91
- Date:
- Mon Nov 14 17:07:21 2016 +0000
- Revision:
- 0:6791a518728e
File System mit einen Argument
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
schoeni_91 | 0:6791a518728e | 1 | #include "mbed.h" |
schoeni_91 | 0:6791a518728e | 2 | #include "parser.h" |
schoeni_91 | 0:6791a518728e | 3 | |
schoeni_91 | 0:6791a518728e | 4 | #define LOCAL_FS "fs" |
schoeni_91 | 0:6791a518728e | 5 | #define INTERP_FILE "/fs/STR1.txt" |
schoeni_91 | 0:6791a518728e | 6 | #define NORM_FILE "/fs/interp1a.txt" |
schoeni_91 | 0:6791a518728e | 7 | |
schoeni_91 | 0:6791a518728e | 8 | // global vars/objects |
schoeni_91 | 0:6791a518728e | 9 | const string keywords[] = {"a-nix", "LEDS", "WAIT", "REPEAT", "GOTO", "END", "NOP","TONE" ,"z-nix"}; |
schoeni_91 | 0:6791a518728e | 10 | int validKeyWords; |
schoeni_91 | 0:6791a518728e | 11 | uint8_t statusExecute; |
schoeni_91 | 0:6791a518728e | 12 | BusOut myLeds(LED1, LED2, LED3, LED4); |
schoeni_91 | 0:6791a518728e | 13 | LocalFileSystem fs(LOCAL_FS); |
schoeni_91 | 0:6791a518728e | 14 | PwmOut speaker(p26); |
schoeni_91 | 0:6791a518728e | 15 | |
schoeni_91 | 0:6791a518728e | 16 | uint8_t executeComLine(int16_t command[], uint16_t *lNo) |
schoeni_91 | 0:6791a518728e | 17 | { |
schoeni_91 | 0:6791a518728e | 18 | static int16_t repeatCounter = 0; |
schoeni_91 | 0:6791a518728e | 19 | uint8_t retVal = 0; |
schoeni_91 | 0:6791a518728e | 20 | printf("line %d: %s %d\r\n", *lNo, keywords[command[0]].c_str(), command[1]); |
schoeni_91 | 0:6791a518728e | 21 | switch (command[0]) { |
schoeni_91 | 0:6791a518728e | 22 | case 1: // LEDS |
schoeni_91 | 0:6791a518728e | 23 | if (command[1] < UNVALID_ARG) |
schoeni_91 | 0:6791a518728e | 24 | retVal = ERROR_DEF; |
schoeni_91 | 0:6791a518728e | 25 | else |
schoeni_91 | 0:6791a518728e | 26 | myLeds = command[1]; |
schoeni_91 | 0:6791a518728e | 27 | break; |
schoeni_91 | 0:6791a518728e | 28 | case 2: // WAIT |
schoeni_91 | 0:6791a518728e | 29 | if (command[1] <= 0) |
schoeni_91 | 0:6791a518728e | 30 | command[1] = 1234; |
schoeni_91 | 0:6791a518728e | 31 | wait_ms(command[1]); |
schoeni_91 | 0:6791a518728e | 32 | break; |
schoeni_91 | 0:6791a518728e | 33 | case 3: // REPEAT |
schoeni_91 | 0:6791a518728e | 34 | if (++repeatCounter < command[1]) |
schoeni_91 | 0:6791a518728e | 35 | retVal = REPEAT_DEF; |
schoeni_91 | 0:6791a518728e | 36 | break; |
schoeni_91 | 0:6791a518728e | 37 | case 4: // GOTO line |
schoeni_91 | 0:6791a518728e | 38 | if (command[1] < UNVALID_ARG) |
schoeni_91 | 0:6791a518728e | 39 | retVal = ERROR_DEF; |
schoeni_91 | 0:6791a518728e | 40 | else { |
schoeni_91 | 0:6791a518728e | 41 | *lNo = command[1]; |
schoeni_91 | 0:6791a518728e | 42 | retVal = GOTO_DEF; |
schoeni_91 | 0:6791a518728e | 43 | } |
schoeni_91 | 0:6791a518728e | 44 | break; |
schoeni_91 | 0:6791a518728e | 45 | case 5: // END |
schoeni_91 | 0:6791a518728e | 46 | repeatCounter = 0; break; |
schoeni_91 | 0:6791a518728e | 47 | |
schoeni_91 | 0:6791a518728e | 48 | case 6: //NOP |
schoeni_91 | 0:6791a518728e | 49 | break; |
schoeni_91 | 0:6791a518728e | 50 | |
schoeni_91 | 0:6791a518728e | 51 | case 7: // TONE |
schoeni_91 | 0:6791a518728e | 52 | |
schoeni_91 | 0:6791a518728e | 53 | speaker.period(1.0/(float)command[1]); |
schoeni_91 | 0:6791a518728e | 54 | speaker= 0.5; |
schoeni_91 | 0:6791a518728e | 55 | wait(1); |
schoeni_91 | 0:6791a518728e | 56 | speaker= 0.0; |
schoeni_91 | 0:6791a518728e | 57 | wait(1); |
schoeni_91 | 0:6791a518728e | 58 | break; |
schoeni_91 | 0:6791a518728e | 59 | default: |
schoeni_91 | 0:6791a518728e | 60 | printf("no valid command: %d = %s ... ERROR\r\n\r\n", command[0], keywords[command[0]].c_str()); |
schoeni_91 | 0:6791a518728e | 61 | retVal = ERROR_DEF; |
schoeni_91 | 0:6791a518728e | 62 | break; |
schoeni_91 | 0:6791a518728e | 63 | } |
schoeni_91 | 0:6791a518728e | 64 | return retVal; |
schoeni_91 | 0:6791a518728e | 65 | } |
schoeni_91 | 0:6791a518728e | 66 | |
schoeni_91 | 0:6791a518728e | 67 | int main() |
schoeni_91 | 0:6791a518728e | 68 | { |
schoeni_91 | 0:6791a518728e | 69 | char myArr [100]; |
schoeni_91 | 0:6791a518728e | 70 | char* myStr; |
schoeni_91 | 0:6791a518728e | 71 | string argLine; |
schoeni_91 | 0:6791a518728e | 72 | int16_t comArray[4]; |
schoeni_91 | 0:6791a518728e | 73 | uint16_t lineNo = 0, lineCnt; |
schoeni_91 | 0:6791a518728e | 74 | FILE *fp2rw; |
schoeni_91 | 0:6791a518728e | 75 | FILE *fp2read = fopen(INTERP_FILE,"r"); |
schoeni_91 | 0:6791a518728e | 76 | |
schoeni_91 | 0:6791a518728e | 77 | fp2rw = fopen(NORM_FILE, "w"); // file name is LIMITED to 8 chars!!! |
schoeni_91 | 0:6791a518728e | 78 | validKeyWords = sizeof(keywords)/sizeof(*keywords) -2; |
schoeni_91 | 0:6791a518728e | 79 | printf("0) validKeyWords = %i\r\n", validKeyWords); |
schoeni_91 | 0:6791a518728e | 80 | |
schoeni_91 | 0:6791a518728e | 81 | if (fp2read != NULL) { |
schoeni_91 | 0:6791a518728e | 82 | while (fgets(myArr, sizeof myArr, fp2read) != NULL) { |
schoeni_91 | 0:6791a518728e | 83 | myStr = normalizeStr(myArr); |
schoeni_91 | 0:6791a518728e | 84 | // printf("a) myStr = <%s>\r\n", myStr); |
schoeni_91 | 0:6791a518728e | 85 | if (myStr[0] != 0) |
schoeni_91 | 0:6791a518728e | 86 | fprintf(fp2rw, "%s\r\n", myStr); |
schoeni_91 | 0:6791a518728e | 87 | } |
schoeni_91 | 0:6791a518728e | 88 | fclose(fp2rw); |
schoeni_91 | 0:6791a518728e | 89 | fp2rw = fopen(NORM_FILE, "r"); // because "w+" and "r+" don't work |
schoeni_91 | 0:6791a518728e | 90 | // printf("\r\nb) -- interpretation of Commands --\r\n\r\n"); |
schoeni_91 | 0:6791a518728e | 91 | while (fgets(myArr, sizeof myArr, fp2rw) != NULL) { // while 1 |
schoeni_91 | 0:6791a518728e | 92 | // printf("c) --> myArr = %s", myArr); |
schoeni_91 | 0:6791a518728e | 93 | argLine.assign(myArr); |
schoeni_91 | 0:6791a518728e | 94 | lineNo++; // count lines |
schoeni_91 | 0:6791a518728e | 95 | if (parseLine(argLine, comArray)) { // parse the command line in normalized file interp1a.txt |
schoeni_91 | 0:6791a518728e | 96 | statusExecute = executeComLine(comArray, &lineNo); // interpret the command line using the prepared command array |
schoeni_91 | 0:6791a518728e | 97 | if (statusExecute == REPEAT_DEF) { |
schoeni_91 | 0:6791a518728e | 98 | rewind(fp2rw); // do rewind if keyword is REPEAT |
schoeni_91 | 0:6791a518728e | 99 | lineNo=0; |
schoeni_91 | 0:6791a518728e | 100 | } |
schoeni_91 | 0:6791a518728e | 101 | else if (statusExecute == GOTO_DEF) { |
schoeni_91 | 0:6791a518728e | 102 | rewind(fp2rw); |
schoeni_91 | 0:6791a518728e | 103 | lineCnt = 0; // start with line 1 |
schoeni_91 | 0:6791a518728e | 104 | lineNo--; |
schoeni_91 | 0:6791a518728e | 105 | while (fgets(myArr, sizeof myArr, fp2rw) != NULL) { // while 2 |
schoeni_91 | 0:6791a518728e | 106 | lineCnt++; |
schoeni_91 | 0:6791a518728e | 107 | if (lineCnt>=lineNo) |
schoeni_91 | 0:6791a518728e | 108 | break; // break while 2; line found --> continue |
schoeni_91 | 0:6791a518728e | 109 | } |
schoeni_91 | 0:6791a518728e | 110 | } |
schoeni_91 | 0:6791a518728e | 111 | else if (statusExecute == ERROR_DEF) { |
schoeni_91 | 0:6791a518728e | 112 | printf("--> ERROR_DEF (%d) occurred in line = %d\r\n", statusExecute, lineNo); |
schoeni_91 | 0:6791a518728e | 113 | break; // break while 1; stop executing |
schoeni_91 | 0:6791a518728e | 114 | } |
schoeni_91 | 0:6791a518728e | 115 | } |
schoeni_91 | 0:6791a518728e | 116 | else { |
schoeni_91 | 0:6791a518728e | 117 | printf("Interpreter Error in file interp1a.txt; line = %d: %s\r\n", lineNo, myArr); |
schoeni_91 | 0:6791a518728e | 118 | } |
schoeni_91 | 0:6791a518728e | 119 | } |
schoeni_91 | 0:6791a518728e | 120 | } else |
schoeni_91 | 0:6791a518728e | 121 | printf("Error opening file to read: %s\r\n", INTERP_FILE); |
schoeni_91 | 0:6791a518728e | 122 | |
schoeni_91 | 0:6791a518728e | 123 | fclose(fp2read); |
schoeni_91 | 0:6791a518728e | 124 | fclose(fp2rw); |
schoeni_91 | 0:6791a518728e | 125 | myLeds = 0x0A; |
schoeni_91 | 0:6791a518728e | 126 | while(1); // endless loop; use reset to start programm again |
schoeni_91 | 0:6791a518728e | 127 | } |