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.

Revision:
1:bbd6b84fc908
Parent:
0:85afbf3c9fad
Child:
2:a59207652720
--- a/VarItems.cpp	Mon Aug 25 08:43:23 2014 +0000
+++ b/VarItems.cpp	Mon Aug 25 09:17:32 2014 +0000
@@ -3,6 +3,11 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+
+
+#define DMP_SZ 100
+
+
 VarItem::VarItem()
 {
     VarName[0]='\0';
@@ -28,29 +33,24 @@
 
 
     Values=strtok(Val,",");
-    if(Values)
-    {
-        do
-        {
-            switch(VarType)
-            {
+    if(Values) {
+        do {
+            switch(VarType) {
 
-            case T_int:
-                *(ValInt+Count) = atoi(Values);
-                break;
+                case T_int:
+                    *(ValInt+Count) = atoi(Values);
+                    break;
 
-            case T_float:
-                *(ValFloat+Count) = atof(Values);
-                break;
+                case T_float:
+                    *(ValFloat+Count) = atof(Values);
+                    break;
 
             };
             Count++;
             Values=strtok(NULL,",");
-        }
-        while((Values !=NULL) && (Count < ArraySize));
+        } while((Values !=NULL) && (Count < ArraySize));
         return 0;
-    }
-    else
+    } else
         return ERR;
 
 }
@@ -59,16 +59,15 @@
 {
 
     VarType=VT;
-    switch(VarType)
-    {
+    switch(VarType) {
 
-    case T_int:
-        ValInt = (int *) VarPtr;
-        break;
+        case T_int:
+            ValInt = (int *) VarPtr;
+            break;
 
-    case T_float:
-        ValFloat = (float*) VarPtr;
-        break;
+        case T_float:
+            ValFloat = (float*) VarPtr;
+            break;
 
     };
 
@@ -96,54 +95,42 @@
 
 char *VarItem::Dump()
 {
-    static unsigned int DumpSize=0;
-    static char *StrDump;
+
+    static char *StrDump=NULL;
 
-    unsigned int counter=0, DumpCounter=0;
+    unsigned int DumpSize=0;
+    unsigned int  DumpCounter=0, ArrayCounter=0;;
     char Tmp[16];
 
-    if(DumpSize==0)
-    {
-        DumpSize=100;
-        StrDump=(char *)malloc(DumpSize);
-        memset(StrDump,0,100);
-    }
+    if(StrDump!=NULL) free(StrDump);
 
-    do
-    {
-
-        switch(VarType)
-        {
+    DumpSize=DMP_SZ;
+    StrDump=(char *)malloc(DumpSize);
+    memset(StrDump,0,DMP_SZ);
 
-        case T_int:
-            sprintf(Tmp,"%d,",*(ValInt+counter));
-            break;
-
-        case T_float:
-            sprintf(Tmp,"%f,",*(ValFloat+counter));
-            break;
-
+    do {
+        switch(VarType) {
+            case T_int:
+                sprintf(Tmp,"%d,",*(ValInt+ArrayCounter));
+                break;
+            case T_float:
+                sprintf(Tmp,"%f,",*(ValFloat+ArrayCounter));
+                break;
         };
-
-        if(DumpCounter+strlen(Tmp) >= DumpSize)
-        {
+        if(DumpCounter+strlen(Tmp) >= DumpSize) {
             char *d;
-            d=(char *)malloc(strlen(StrDump));
-            DumpSize += strlen(StrDump);
+            DumpSize = DumpCounter+DMP_SZ;
+            d=(char *)malloc(DumpCounter+DMP_SZ);
             strcpy(d,StrDump);
             free(StrDump);
             StrDump=d;
         }
-
-#if defined VARITEMS_DEBUG
-        puts(StrDump);
-#endif // defined
-
         strcat(StrDump+DumpCounter,Tmp);
         DumpCounter+=strlen(Tmp);
+        ArrayCounter++;
+    } while (ArrayCounter < ArraySize);
 
-    }
-    while (counter < ArraySize);
+    StrDump[strlen(StrDump)-1]='\0';
 
     return StrDump;