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:
Tue Aug 26 09:09:01 2014 +0000
Parent:
12:e9c8d2d9ac71
Child:
14:6cbe8366d587
Commit message:
first version of documentation

Changed in this revision

VarItems.cpp 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	Tue Aug 26 08:20:02 2014 +0000
+++ b/VarItems.cpp	Tue Aug 26 09:09:01 2014 +0000
@@ -27,8 +27,6 @@
 {
     char * Values;
     unsigned int Count=0;
-
- //   pc.printf(" Set val:%s \n",Val);
     
     Values=strtok(Val,",");
     if(Values) {
@@ -42,7 +40,6 @@
                 case T_float:
                     *(ValFloat+Count) = atof(Values);
                     break;
-
             };
             Count++;
             Values=strtok(NULL,",");
@@ -103,30 +100,21 @@
                 sprintf(Tmp,"%f,",*(ValFloat+ArrayCounter));
                 break;
         };
-        if(DumpCounter+strlen(Tmp) >= DumpSize) {
-            /*
-            char *d;
-            DumpSize = DumpCounter+DMP_SZ;
-            d=(char *)malloc(DumpCounter+DMP_SZ);
-            strcpy(d,StrDump);
-            free(StrDump);
-            StrDump=d;
-            */
-            break;
-        }
+        
+        if(DumpCounter+strlen(Tmp) >= DumpSize) break;
+        
         strcat(StrDump+DumpCounter,Tmp);
         DumpCounter+=strlen(Tmp);
         ArrayCounter++;
     } while (ArrayCounter < ArraySize);
 
-    StrDump[strlen(StrDump)-1]='\0';
+    StrDump[DumpSize-1]='\0';
 
     return StrDump;
 
 }
 
 
-
 char *VarItem::GetVarName()
 {
     return VarName;
--- a/VarStore.cpp	Tue Aug 26 08:20:02 2014 +0000
+++ b/VarStore.cpp	Tue Aug 26 09:09:01 2014 +0000
@@ -11,34 +11,26 @@
 
 #define CI_SZ 100
 
-/*******************************
-*
-*
-********************************/
+/* Constructor
+*/
 VarStore::VarStore(     RawSerial *ser, int sz)
 {
     VarCounter=0;
     this->pc=ser;
     this->sz=sz;
-    //Store=(VarItem *) malloc( sizeof(VarItem)*sz);
+    //Store=(VarItem *) malloc( sizeof(VarItem)*sz); doesn't work
     Store=new VarItem[sz];
 }
 
-/*******************************
-*
-*
-********************************/
-
+/* destructor
+*/
 VarStore::~VarStore()
 {
     //dtor
 }
 
-/*******************************
-*
-*
-********************************/
-
+/* destructor
+*/
 char *VarStore::Set(char *Input)
 {
     VarItem *V;
@@ -55,46 +47,40 @@
     return NULL;
 }
 
-/*******************************
-*
-*
-********************************/
 
-
+/*
+************************
+*/
 int VarStore::Load(char *Name, void *VarPtr,VarTypes VarType )
 {
     return Load(Name, VarPtr,VarType,0 );
 }
 
-/*******************************
-*
-*
-********************************/
-
+/*
+************************
+*/
 int VarStore::Load(char *Name, void *VarPtr,VarTypes VarType, int Size )
 {
-    pc->puts("Entro en Load \n");
+   
     if(GetVar(Name) ==NULL) {
-        pc->puts("variable no esta \n");
+        
         if(VarCounter < sz) {
-            pc->puts("incorporo variable a Store \n");
+           
             Store[VarCounter].SetVar(VarType,VarPtr);
             Store[VarCounter].SetVarName(Name);
             Store[VarCounter].SetVarArraySize(Size);
             VarCounter++;
-            pc->puts("salgo por NULL \n");
+          
             return NULL;
         }
     }
-    pc->puts("salgo por error \n");
+  
     return ERR;
 }
 
-/*******************************
-*
-*
-********************************/
-
+/*
+************************
+*/
 VarItem *VarStore::GetVar(char *Name)
 {
     for (int i=0; i<sz; i++)
@@ -104,6 +90,9 @@
     return NULL;
 }
 
+/*
+************************
+*/
 char*  VarStore::Get(char *Name)
 {
     VarItem *V;
@@ -114,12 +103,9 @@
         return NULL;
 }
 
-/*******************************
-*
-*
-********************************/
-
-
+/*
+************************
+*/
 void VarStore::Worker2()
 {
 
@@ -130,64 +116,60 @@
     if(VarStore::MyThis->pc->readable()) {
         c=VarStore::MyThis->pc->getc();
 
-        if(ci_counter >= CI_SZ-1) {   // RESET
+        if(ci_counter >= CI_SZ-1) {   // RESET too much input
             ci_counter=0;
             Cs[0]='\0';
         } else {
-            if(c=='\r') {
+            if(c=='\r') {             // got a command lets see whan can be done
                 Cs[ci_counter]='\0';
                 ret=VarStore::MyThis->Do(Cs);
                 ci_counter=0;
                 Cs[0]='\0';
-            } else {
+            } else {                 // no command yet, let's keep recording.
                 Cs[ci_counter]=c;
                 ci_counter++;
             }
         }
     }
 
-    if(ret==NULL) {
+    if(ret==NULL) {  // ups.....
         VarStore::MyThis->pc->puts(" error setting/getting var \n");
         ret=STR_OK;
     }
 
 }
 
-/*******************************
-*
-*
-********************************/
-
+/*
+** nuisances for mixing threads/static members etc
+*/
 VarStore *VarStore::MyThis=NULL;   // used by the worker reading the terminal. Need to be initilized this way to avoid
 // compiling errors
+
+/*
+************************
+*/
 void VarStore::Worker(void const *args)
 {
 
     VarStore::MyThis=(VarStore *)args;
-
-//  cuidado puts VarStore::MyThis->pc->(" hi worker 1\n");
-
     VarStore::MyThis->pc->attach(&VarStore::Worker2);
-
     while(1) {
-        Thread::wait(1000);
+        Thread::wait(1000); // nothing to do here besides firing worker2 when input ready
     }
 
 }
-/*******************************
-*
-*
-********************************/
-
+/*
+************************
+*/
 char  *VarStore::Do(char *str)
 {
     char *ret;
     if(str != NULL) {
 
         switch(*str) {
-            case 's':
+            case 's':  /// command s  s:varname:value or s:arrayname:value1,value2,value3  assign values at runtime
                 return VarStore::MyThis->Set(str);
-            case 'd':
+            case 'd':  /// command d  d:variablename dumps content of variable to screen/console.
 
                 VarStore::MyThis->pc->puts(str);
                 VarStore::MyThis->pc->putc('\n');
@@ -203,10 +185,10 @@
                     return ret;
                 } else
                     return NULL;
-            case 'r':
+            case 'r':   /// command r resets the mbed. If you previously downloaded a new version fo code, it will run.
                 mbed_reset();
                 return NULL;
-            case 'w':
+            case 'w':   ///  command w   waits a number of miliseconds releasing the input console to be used for other stuff temporarily
                 strtok(str,":");
                 Thread::wait(atoi(strtok(NULL,":")));
                 return STR_OK;
--- a/VarStore.h	Tue Aug 26 08:20:02 2014 +0000
+++ b/VarStore.h	Tue Aug 26 09:09:01 2014 +0000
@@ -1,105 +1,151 @@
 #ifndef VARSTORE_H
 #define VARSTORE_H
-
 #include "mbed.h"
-
-
-
 #include "VarItems.h"
 
-#define SZ 20
 #define STR_OK ""
 
+/** VarStore Class
+*  Used for reading/modifying program variables
+*  from the console at runtime. Helpful for
+*  debugging.
+*  It has a facility as well to reset the mbed
+*  from the serial console without pressing  
+*  any button.
+*  It does not block the serial/input channel
+*  in case it is needed for other stuff
+*  
+* Example:
+* @code
+
+* #include "mbed.h"
+* #include "rtos.h"
+* #include "VarStore.h"
+*
+* #include <RawSerial.h>
+*
+* RawSerial  pc(USBTX,USBRX);  // Be aware !!!! need rawserial. No printf for you anymore !! 
+*                              // no malloc nither as this works in ISR
+*
+* VarStore Store(&pc,20);      // create storage for 20 variables/arrays attach to serial pc
+* 
+* DigitalOut led1(LED1);
+* DigitalOut led2(LED2);
+* 
+* void led2_thread(void const *args) {
+*    
+*    int wait2=1000;
+*    Store.Load("wait2",&wait2,T_int);   // load variable wait2 in Store
+*    
+*    while (true) {
+*        led2 = !led2;
+*        Thread::wait(wait2);  // remember, no WAIT, STOPS CPU FROM RUNNING WHOLE PROGRAM.
+                               // use Thread::wait to stop just your path of execution.
+*    }
+* }
+* 
+* int main() {
+*    
+*    int wait1=500;
+*    Store.Load("wait1",&wait1,T_int);  // Load variable wait1 in Store
+*    Thread VS_thread(VarStore::Worker,&Store,osPriorityNormal,DEFAULT_STACK_SIZE,NULL); // launch VarStore Thread
+*    
+*    Thread thread(led2_thread);
+*    
+*    while (true) {
+*        led1 = !led1;
+*        Thread::wait(wait1); // remember, no WAIT, STOPS CPU FROM RUNNING WHOLE PROGRAM.
+*                              // use Thread::wait to stop just your path of execution.
+*    }
+* }
+*
+* @endcode
+*
+*
+*
+*
+*/
 class VarStore
 {
     
  
 // friend    void Worker2();
 public:
-    /*******************************
-    *
-    *Constructor
-    ********************************/
-
+    /*
+    * Constructor        
+    */
     VarStore(   RawSerial *ser, int sz);
-    /*******************************
-    *
-    *Destructor
-    ********************************/
-
+    
+    /*
+    * destr        
+    */
     virtual ~VarStore();
-    /*******************************
-    *
-    *asigna valor a una variable/array o retorna ERR
-    ********************************/
-
+    
+    /* assigns (a) value(s) to a variable or array        
+    */
     char * Set(char *Input);
 
-    /*******************************
-    *
-    *devuelve en un string el contenido de una variable
-    ********************************/
-
+    /*Get contents of a variable as a string
+    */
     char*  Get(char *Name);
 
-    /*******************************
-    *carga en el store una variable
+    /** Load a variable on VarStore
     *
-    ********************************/
-
+    * @param Name string that will be used to query/set the value of the variable/array
+    * @param VarPtr pointer to variable 
+    * @param VarType enumerated type indicating int/float ( only supported currently) enum VarTypes {T_int,T_float};
+    *
+    * @returns ERR on error / NULL on success
+    */
     int Load(char *Name, void *VarPtr,VarTypes VarType );
 
-    /*******************************
+    /** Load an array on VarStore
     *
-    *carga en el store una variable/array
-    ********************************/
-
+    * @param Name string that will be used to query/set the value of the variable/array
+    * @param VarPtr pointer to base of array
+    * @param VarType enumerated type indicating int/float ( only supported currently) enum VarTypes {T_int,T_float};
+    * @param Size number of elements in the array
+    *
+    * @returns ERR on error / NULL on success
+    */
     int Load(char *Name, void *VarPtr,VarTypes VarType, int Size );
 
-    /*******************************
-    * Proceso que less del un serial para modificar
-    * los valores de las variables.
+
+
+   /** Thread that will manage the concole interaction. Main role is to remain sleeping 
+    * and only awakening to fire worker2 to deal with console input
     *
-    ********************************/
-
- //   static    void Worker(void const *args);
- static    void Worker(void const *args);
+    * @param args pointer to the VarStore Object that will be managing.
+    *
+    * @returns ERR on error / NULL on success
+    */
+    static  void Worker(void const *args);
    
 
 protected:
 
 private:
-    /*******************************
-    * Busca una variable con nombre name.
-    *
-    ********************************/
-
+    /* Get variable item by name
+    */
     VarItem *GetVar(char *name);
 
-    /*******************************
-    * Llamado por worker para
-    * llevar a cabo la acción
-    *
-    ********************************/
+    /* do stuff with console input
+    */
     char  *Do(char *str);
     
-    /*******************************
-    *Para hacer gestión via interrupciones.
-    *
-    ********************************/
+    /* manages console input
+    */
     static void Worker2();
     
-    /*******************************
-    *
-    *
-    ********************************/
+   
     VarItem *Store;
     int sz;         // number of varibales to store / array size
     int VarCounter;
     RawSerial  *pc;
 
     static VarStore *MyThis;   // used by the workers reading the terminal
-
+                               // instantiation via a static fucntion so need to record
+                               // who am I 
 };