library to modify and read program variable in runtime from a serial console. You can reset as well the mbed from the console without pushing buttons. Handy for debugging from the online compiler as you can change the behavior of the program without need to recompile each time.

Files at this revision

API Documentation at this revision

Comitter:
julmbed
Date:
Mon Aug 25 10:45:03 2014 +0000
Parent:
1:bbd6b84fc908
Child:
3:cf43e6de918e
Commit message:
worker como static, lanzandolo de manera independiente en el main. admite tres comandos, via ->DO que son set/get-dump/reset para carge de nuevo programa en el mbed sin darle al boton

Changed in this revision

VarItems.cpp Show annotated file Show diff for this revision Revisions of this file
VarItems.h Show annotated file Show diff for this revision Revisions of this file
VarStore.cpp Show annotated file Show diff for this revision Revisions of this file
VarStore.h Show annotated file Show diff for this revision Revisions of this file
--- a/VarItems.cpp	Mon Aug 25 09:17:32 2014 +0000
+++ b/VarItems.cpp	Mon Aug 25 10:45:03 2014 +0000
@@ -27,11 +27,6 @@
     char * Values;
     unsigned int Count=0;
 
-#if defined VARITEMS_DEBUG
-    puts(Val);
-#endif // defined
-
-
     Values=strtok(Val,",");
     if(Values) {
         do {
@@ -49,7 +44,7 @@
             Count++;
             Values=strtok(NULL,",");
         } while((Values !=NULL) && (Count < ArraySize));
-        return 0;
+        return 1;
     } else
         return ERR;
 
--- a/VarItems.h	Mon Aug 25 09:17:32 2014 +0000
+++ b/VarItems.h	Mon Aug 25 10:45:03 2014 +0000
@@ -18,7 +18,7 @@
 
     void SetVar(VarTypes VT,void* VarPtr);
     int SetVal(char *Val);
- //   void  SetVarType(VarTypes VarType);
+//   void  SetVarType(VarTypes VarType);
     void SetVarArraySize(int Size);
     void SetVarName(char *Name);
     char *Dump();
--- a/VarStore.cpp	Mon Aug 25 09:17:32 2014 +0000
+++ b/VarStore.cpp	Mon Aug 25 10:45:03 2014 +0000
@@ -4,6 +4,8 @@
 #include "mbed.h"
 #include "rtos.h"
 
+extern "C" void mbed_reset();
+
 #include "VarStore.h"
 
 #define CI_SZ 100
@@ -16,6 +18,8 @@
 {
     VarCounter=0;
     this->pc=ser;
+// doesn't work....  Thread thread(VarStore::Worker,this,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
+
 }
 
 /*******************************
@@ -33,7 +37,7 @@
 *
 ********************************/
 
-int VarStore::Set(char *Input)
+char *VarStore::Set(char *Input)
 {
     VarItem *V;
     char *Name;
@@ -42,9 +46,9 @@
         strtok(Input,":");
         Name=strtok(NULL,":");
         if((V=GetVar(Name)) != NULL)
-            return V->SetVal(strtok(NULL,":"));
+            return  V->SetVal(strtok(NULL,":"))!= ERR ? Input : NULL;
     }
-    return ERR;
+    return NULL;
 }
 
 /*******************************
@@ -112,13 +116,14 @@
 void VarStore::Worker(void const *args)
 {
     VarStore *MyThis=(VarStore *)args;
-    char c;
-    int ret=0,ci_counter=0;
+    char c, *ret="";// not NULL to start in no error state
+    
+    int ci_counter=0;
     char Cs[CI_SZ];
 
-    for(int i=0; i<CI_SZ; i++) Cs[i]='\0';
-    
-    MyThis->pc->printf(" hi worker\n");
+//    for(int i=0; i<CI_SZ; i++) Cs[i]='\0';
+
+//   MyThis->pc->printf(" hi worker\n");
     while (1) {
         if(MyThis->pc->readable()) {
             c=MyThis->pc->getc();
@@ -126,31 +131,31 @@
             if(ci_counter >= CI_SZ-1) {   // RESET
                 ci_counter=0;
                 Cs[0]='\0';
-                MyThis->pc->printf(" reset\n");
+//               MyThis->pc->printf(" reset\n");
 
             } else {
                 if(c=='\r') {
                     Cs[ci_counter]='\0';
                     MyThis->pc->printf(" CI -%s- \n",Cs);
-                    ret=MyThis->Set(Cs);
+                    ret=MyThis->Do(Cs,MyThis);
                     ci_counter=0;
                     Cs[0]='\0';
-                    MyThis->pc->printf(" set variable (%d)\n",ret);
-                    MyThis->pc->printf(" Get offset -%s- \n",MyThis->Get("offset"));
+                    //                 MyThis->pc->printf(" set variable (%d)\n",ret);
+                    //                  MyThis->pc->printf(" Get offset -%s- \n",MyThis->Get("offset"));
                 } else {
-                    MyThis->pc->printf(" CI -%s- -%c- -%d- \n",Cs,c,ci_counter);
-                    
+//                   MyThis->pc->printf(" CI -%s- -%c- -%d- \n",Cs,c,ci_counter);
+
                     Cs[ci_counter]=c;
                     ci_counter++;
-                    MyThis->pc->printf(" parcial CI -%s- -%c-(%c) -%d- \n",Cs,c,Cs[ci_counter],ci_counter);
+                    //                  MyThis->pc->printf(" parcial CI -%s- -%c-(%c) -%d- \n",Cs,c,Cs[ci_counter],ci_counter);
                 }
             }
         }
 
-        if(ret==ERR) {
+        if(ret==NULL) {
             Thread::wait(100);
-            MyThis->pc->printf(" error setting var \n");
-            ret=0;
+            MyThis->pc->printf(" error setting/getting var \n");
+            ret="";
         }
         Thread::wait(100);
     } // While
@@ -161,3 +166,27 @@
 *
 ********************************/
 
+char  *VarStore::Do(char *str, VarStore* MyThis)
+{
+    char *ret;
+    if(str != NULL) {
+
+        switch(*str) {
+            case 's':
+                return MyThis->Set(str);
+            case 'd':
+                strtok(str,":");
+                ret=MyThis->Get(strtok(NULL,":"));
+                if(ret!=NULL) {
+                    MyThis->pc->printf("%s (%s)\n",str,ret);
+                    return ret;
+                } else
+                    return NULL;
+            case 'r':
+                mbed_reset();
+                return NULL;
+        };
+    }
+    return NULL;
+}
+
--- a/VarStore.h	Mon Aug 25 09:17:32 2014 +0000
+++ b/VarStore.h	Mon Aug 25 10:45:03 2014 +0000
@@ -10,68 +10,81 @@
 class VarStore
 {
 public:
-/*******************************
-*
-*Constructor
-********************************/
+    /*******************************
+    *
+    *Constructor
+    ********************************/
 
     VarStore(Serial *ser);
-/*******************************
-*
-*Destructor
-********************************/
+    /*******************************
+    *
+    *Destructor
+    ********************************/
 
     virtual ~VarStore();
-/*******************************
-*
-*asigna valor a una variable/array o retorna ERR
-********************************/
+    /*******************************
+    *
+    *asigna valor a una variable/array o retorna ERR
+    ********************************/
 
-    int Set(char *Input);
+    char * Set(char *Input);
 
-/*******************************
-* 
-*devuelve en un string el contenido de una variable
-********************************/
+    /*******************************
+    *
+    *devuelve en un string el contenido de una variable
+    ********************************/
 
     char*  Get(char *Name);
 
-/*******************************
-*carga en el store una variable
-*
-********************************/
+    /*******************************
+    *carga en el store una variable
+    *
+    ********************************/
 
     int Load(char *Name, void *VarPtr,VarTypes VarType );
-    
-/*******************************
-*
-*carga en el store una variable/array
-********************************/
+
+    /*******************************
+    *
+    *carga en el store una variable/array
+    ********************************/
 
     int Load(char *Name, void *VarPtr,VarTypes VarType, int Size );
 
-/*******************************
-* Proceso que less del un serial para modificar
-* los valores de las variables.
-*
-********************************/
+    /*******************************
+    * Proceso que less del un serial para modificar
+    * los valores de las variables.
+    *
+    ********************************/
 
-static    void Worker(void const *args);
+    static    void Worker(void const *args);
+
 
 
 protected:
+
 private:
-/*******************************
-* Busca una variable con nombre name.
-*
-********************************/
+    /*******************************
+    * Busca una variable con nombre name.
+    *
+    ********************************/
 
     VarItem *GetVar(char *name);
 
+    /*******************************
+    * Llamado por worker para
+    * llevar a cabo la acción
+    *
+    ********************************/
+    char  *Do(char *str, VarStore *MyThis);
+    /*******************************
+    *
+    *
+    ********************************/
     VarItem Store[SZ];
     int VarCounter;
     Serial *pc;
 
+
 };
 
 #endif // VARSTORE_H