Reinhold Schaefer / monitor

Dependents:   09_PT1000 10_PT1000 11_PT1000

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers monitor.cpp Source File

monitor.cpp

00001 
00002 #include <stdarg.h>
00003 #include <ctype.h>
00004 #include "monitor.h"
00005 #include "ConfigFile.h"
00006 #include "SDFileSystem.h"
00007 #include "FATDirHandle.h"
00008 
00009 extern SDFileSystem sd;                 // definiert in main
00010 extern Serial pc;                       // definiert in main
00011 extern Buffer <char> buf;               // definiert in main
00012 extern ConfigFile cfg;                  // definiert in main
00013 extern char value[BUFSIZ];              // definiert in main
00014 
00015 #define COMMAND_MAX    9
00016 #define COMMAND_LEN    7
00017 
00018 const char command[COMMAND_MAX][COMMAND_LEN] = {"DATE","DEL","DIR","DUMP","FILL","HELP","LIST","SET","TIME"};
00019 
00020 char buffer[100]; 
00021 
00022 // globale Variablen
00023 
00024 char com_line[COM_LINE_LEN];        // Aktuelle Eingabezeile 
00025 uint8_t ComLinePtr, ComLineTop;     // Cursor Position und Zeilenlänge
00026 
00027 uint8_t cr_flag;
00028 
00029 // uint16_t status[8];
00030 
00031 //-----------------------------------------------------------------------------
00032 // initialisierung
00033 
00034 void mon_init(void)
00035 {
00036 
00037       uint8_t i;
00038       
00039       for (i = 0; i < COM_LINE_LEN; com_line[i++] = 0);
00040       ComLinePtr = ComLineTop = 0;
00041       cr_flag = false;
00042       
00043       // 0 Kanal ist daktiv
00044       // 1 Rohdaten ausgeben
00045       // 2 Temperaturwert in Float Darstellung
00046       
00047       /*
00048       status[0] = 2;  // Temperaturwerte ausgeben 
00049       status[1] = 0;
00050       status[2] = 0;
00051       status[3] = 0;
00052       status[4] = 0;
00053       status[5] = 0;
00054       status[6] = 0;
00055       status[7] = 0;
00056       */
00057 }
00058 
00059 //-----------------------------------------------------------------------------
00060 // destructor
00061 
00062 void parser (void)
00063 {
00064   uint8_t i, ch, tch, top, bottom, len;
00065   int8_t res;
00066 
00067   // Zuerst wird der erste Buchstabe aus dem Eingabestring mit den ersten 
00068   // Buchstaben aus der Befehlstabelle verglichen                         
00069 
00070   bottom = 0;                                        // untere Suchgrenze 
00071   top = COMMAND_MAX;                                 // obere Suchgerenze 
00072   ch = com_line[0];                            // hole erstes Suchzeichen
00073   
00074   do 
00075   {
00076         i = (top + bottom) >> 1;    // suche in der Mitte des Feldes beginnen
00077         tch = command [i][0];                     // Vergleichszeichen laden 
00078         if (tch == ch) break;                             // Zeichen gefunden 
00079         if (tch > ch) top = i;                           // nach unten suchen 
00080         else bottom = i;                                  // nach oben suchen 
00081         if (bottom != 0 && top == bottom + 1) break;   // kein Buchstabe gef.
00082   
00083   } while (i > 0 && i < COMMAND_MAX - 1);
00084 
00085   if (tch != ch)
00086   {
00087         pc.printf("\nParser Kommando nicht gefunden\n");
00088         return;                              // Kommando nicht gefunden wurde 
00089   }
00090   
00091   // das erst Wort soll von den Übergabeparametern isoliert werden        
00092   
00093   for (i = 0; com_line[i] != ' ' &&  com_line[i] != 0; i++);
00094   len = i;
00095 
00096   
00097   if (i == 0) return;
00098 
00099   // die Übergabparameter ermitteln und in als Indexzeiger in            
00100   // 'ComLinePtr' abspeichern                                            
00101 
00102   for ( ; com_line[i] == ' ' && com_line[i] != 0; i++);
00103   ComLinePtr = i;
00104   
00105   // die binäre Suche nach den restlichen Zeichen wird hier fortgesetzt 
00106 
00107   do
00108   {
00109         i = (top + bottom) >> 1;                 // berechnen des Suchplatzes
00110         //printf_P (PSTR("\n\rVergleich 1 com_line = [%s] und Länge = [%d]"),com_line,len);
00111         //strcpy_P (temp, &command[i][0]);
00112         //printf_P (PSTR("\n\rVergleich 2 command[i] = [%s] und Index = [%d]"),temp,i);
00113         res = strncmp(com_line, &command[i][0], len);
00114         //printf_P (PSTR("\n\rVergleich 3 res = [%d]"),res);
00115         if (res == 0) break;                       // Zeichen gefunden 
00116         if (res > 0) 
00117             bottom = i;                            // nach unten suchen
00118         else 
00119             top = i;                               // nach oben suchen
00120         if (bottom != 0 && top == bottom + 1) break;
00121   
00122   } while (i > 0 && i < COMMAND_MAX - 1);
00123 
00124   
00125   if (res) 
00126   {
00127         pc.printf("\nParser Kommando nicht gefunden.\n");
00128   }
00129   else 
00130   {
00131         pc.printf("\nAufruf von Funktion %d",i);
00132         
00133         switch(i)                       // Programmaufruf
00134         {
00135           case  0: date(); break;
00136           case  1: del();  break;
00137           case  2: dir();  break;
00138           case  3: dump(); break;
00139           case  4: fill(); break;
00140           case  5: help(); break;
00141           case  6: list(); break;
00142           case  7: set();  break;
00143           case  8: time(); break;
00144           
00145         }                                   
00146   }
00147 }
00148 
00149 
00150 
00151 //-----------------------------------------------------------------------------
00152 // eine Zeile aus dem Eingangsbuffer lesen 
00153   
00154 void get_line(void)
00155 {
00156     char ch;
00157     uint8_t i;
00158      
00159     get_ch();
00160     if (cr_flag)                            // Neue Eingabezeile  
00161     {
00162         if (com_line[0] != 0)
00163         {
00164             // uart_puts(com_line);         // zum Testen => später wird der parcer aufgerufen
00165             // Zeichenkette in Großbuchstaben umwandeln
00166 
00167             // printf_P (PSTR("\n monitor "));
00168             for (i = 0; com_line[i] != 0; i++)
00169             {
00170                 ch = com_line[i];
00171                 ch = toupper(ch);                          // Nur Großbuchstaben 
00172                 com_line[i] = ch;
00173                 // pc.printf(("\n[%02x] %c"),ch,ch);                
00174                 if (ch == ' ') i = COM_LINE_LEN;
00175             }
00176             // pc.printf(("\n"));
00177             parser();                                   // Parcer wird aufgerufen
00178         }
00179  
00180         for (i=0; i < COM_LINE_LEN; com_line[i++] = 0);
00181         ComLinePtr = ComLineTop = 0;                         
00182         cr_flag = 0;
00183     }     
00184 }
00185 
00186 //-----------------------------------------------------------------------------
00187 // eine Zeichen aus dem Eingangsbuffer lesen 
00188 
00189 void get_ch (void)
00190 {
00191     char ch;
00192   
00193     if (!buf.available()) return;               // kein Zeichen vorhanden 
00194      
00195     ch = buf.get();                             // hole das Zeichen  
00196     // printf("mon_line: %c %02x\n",ch,ch);     // nur zum Test
00197   
00198     switch(ch) 
00199     {
00200         case '\r':     // CARRIAGE RETURN  
00201             cr_flag = true;
00202             break;
00203 
00204         case '\n':      // LF empfangen
00205             cr_flag = true;
00206             break;
00207 
00208         default:                                 // Normales Zeichen  
00209             if (~iscntrl(ch)) 
00210             {
00211                 com_line[ComLinePtr] = ch;       // Zeichen einfuegen 
00212                 ComLinePtr++;
00213             }
00214             break;
00215 
00216     }   // Ende SWITCH
00217     
00218     if (ComLinePtr >= 80) cr_flag = 1;           // Zeilenüberlauf ?  
00219     
00220 }
00221 
00222 //-----------------------------------------------------------------------------
00223 // monitor Aufruf
00224 
00225 void date(void)
00226 {
00227   pc.printf("\nin date");
00228 }
00229 
00230 //-----------------------------------------------------------------------------
00231 // monitor Aufruf
00232 
00233 void dump(void)
00234 {
00235   pc.printf("\nin dump");
00236 }
00237 
00238 //-----------------------------------------------------------------------------
00239 // monitor Aufruf
00240 
00241 void help(void)
00242 {
00243   pc.printf("\n -- help ------------------------");
00244   pc.printf("\n set index [val] Sensor auswaehlen");
00245   pc.printf("\n");
00246   pc.printf("\n -- files ------------------------");
00247   pc.printf("\n dir  >> Dateien anzeigen");  
00248   pc.printf("\n list name >> den Inhalte einer Datei anzeigen"); 
00249   pc.printf("\n del name >> eine Datei loeschen"); 
00250   pc.printf("\n fill name [zeilen] >> eine Datei anlegen und fuellen");        
00251   pc.printf("\n");
00252 }
00253 
00254 //-----------------------------------------------------------------------------
00255 // monitor Aufruf
00256 
00257 void set(void)
00258 {
00259     
00260     int n;
00261     char name[20], wert[20];
00262 
00263     n = sscanf(&com_line[ComLinePtr],"%s %s",name,wert);
00264     
00265     switch (n)
00266     {
00267       case -1: pc.printf("\n list config file");
00268               
00269               cfg.read("/sd/input.cfg");
00270               
00271               pc.printf("\n Index >> Sonsor 0 bis 7");
00272               pc.printf("\n wert  >> 0 = inaktiv, 1 = Rohwerte, 2 = errechneter Wert");
00273               break;
00274       
00275       case 1: pc.printf("\n list config value");
00276               
00277               cfg.read("/sd/input.cfg");
00278               
00279               if (cfg.getValue(name, &value[0], sizeof(value))) 
00280               {
00281                 pc.printf("\n'%s'='%s'", name, value);
00282               }
00283               break;
00284               
00285       case 2: pc.printf("\n'%s' = '%s'",name,wert);
00286                
00287               cfg.setValue(name, wert);
00288               cfg.write("/sd/input.cfg");
00289               
00290               break;
00291      } // end switch
00292     
00293 }
00294 
00295 //-----------------------------------------------------------------------------
00296 // monitor Aufruf
00297 
00298 void time(void)
00299 {
00300   pc.printf("\nin timer");
00301 }
00302 
00303 //-----------------------------------------------------------------------------
00304 // monitor Aufruf dir
00305 //
00306 // weiter Infos siehe auch
00307 // http://elm-chan.org/fsw/ff/00index_e.html
00308 //
00309 void dir(void)
00310 {
00311     long size;
00312 
00313     DIR *d;
00314     struct dirent *p;
00315 
00316     d = opendir("/sd");
00317     if (d != NULL) 
00318     {
00319         while ((p = readdir(d)) != NULL) 
00320         {
00321             sprintf(buffer,"/sd/%s",p->d_name);
00322             pc.printf("\n %s", p->d_name);
00323             FILE * f = fopen(buffer, "r");
00324             fseek(f, 0, SEEK_END); // seek to end of file
00325             size = ftell(f);       // get current file pointer
00326             pc.printf("    %ld bytes ",size);            
00327 
00328             time_t fattime = get_fattime();
00329             strftime(buffer, 40, "%a,%d %m %Y.%H:%M:%S", localtime(&fattime));
00330             pc.printf(" %s ", buffer);
00331 
00332             fclose(f);
00333         }
00334         
00335     }
00336     else 
00337     {
00338         pc.printf("\nCould not open directory!\n");
00339     }
00340     closedir(d);
00341     
00342  
00343     FATFS* fs;
00344     DWORD fre_clust;
00345     f_getfree("0:",&fre_clust,&fs);
00346     const float frs = float(fs->csize)*float(fs->free_clust)
00347     #if _MAX_SS != 512
00348         *(fs->ssize);
00349     #else
00350         *512;
00351     #endif
00352     pc.printf("\n\nfree space = %f GB",frs/1073741824.0);
00353 
00354     pc.printf("\ndone\n");
00355 }
00356 
00357 //-----------------------------------------------------------------------------
00358 // monitor Aufruf list
00359 
00360 void list(void)
00361 {
00362     char dname[25];
00363     int ch;
00364       
00365     sscanf(&com_line[ComLinePtr],"%s",dname);
00366     sprintf(buffer,"/sd/%s",dname);
00367     pc.printf("\nlist, file %s \n",buffer);
00368     FILE * fp = fopen(buffer, "r");
00369     if(fp == NULL) 
00370     {
00371         pc.printf("\nCould not open file for read\n\r");
00372         return;
00373     }
00374     
00375     // mit fgets werden die Zeile einzeln gelesen, wenn die Länge < 64 Zeichen ist
00376 
00377     while (1)                     // list src to pc  
00378     {
00379         ch = fgetc(fp);           // until src EOF read. 
00380         if (ch == EOF) break;
00381         pc.putc(ch);  
00382     }
00383     
00384     fclose(fp);
00385             
00386     pc.printf("\ndone\n");
00387 }
00388 
00389 //-----------------------------------------------------------------------------
00390 // monitor Aufruf del
00391 
00392 void del(void)
00393 {
00394     char dname[25];
00395     char buffer[40];
00396     
00397     sscanf(&com_line[ComLinePtr],"%s",dname);
00398     sprintf(buffer,"/sd/%s",dname);
00399     pc.printf("\ndelete file %s",buffer);
00400     remove(buffer);
00401     pc.printf("\ndone");
00402 }
00403 
00404 //-----------------------------------------------------------------------------
00405 // monitor Aufruf fill
00406 
00407 void fill(void)
00408 {
00409     char dname[25];
00410     char buffer[40];
00411     int n = 20;
00412     
00413     sscanf(&com_line[ComLinePtr],"%s %d",dname,&n);
00414     sprintf(buffer,"/sd/%s",dname);
00415     pc.printf("\nfill file %s \n",buffer);
00416     FILE *fp = fopen(buffer, "w");
00417     if(fp == NULL) 
00418     {
00419         pc.printf("Could not open file for write\n");
00420     }
00421     for (int i = 0; i<n; i++)
00422     {
00423         fprintf(fp, "\nschreibe eine Zeile %d ",i);
00424     }
00425     fclose(fp); 
00426     pc.printf("\ndone\n");
00427 }
00428 
00429