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.
VarItems.cpp
- Committer:
- julmbed
- Date:
- 2014-08-25
- Revision:
- 1:bbd6b84fc908
- Parent:
- 0:85afbf3c9fad
- Child:
- 2:a59207652720
File content as of revision 1:bbd6b84fc908:
#include "VarItems.h" #include <string.h> #include <stdlib.h> #include <stdio.h> #define DMP_SZ 100 VarItem::VarItem() { VarName[0]='\0'; ValInt=NULL; ValFloat=NULL; ArraySize=0; } VarItem::~VarItem() { //dtor } int VarItem::SetVal(char *Val) { char * Values; unsigned int Count=0; #if defined VARITEMS_DEBUG puts(Val); #endif // defined Values=strtok(Val,","); if(Values) { do { switch(VarType) { case T_int: *(ValInt+Count) = atoi(Values); break; case T_float: *(ValFloat+Count) = atof(Values); break; }; Count++; Values=strtok(NULL,","); } while((Values !=NULL) && (Count < ArraySize)); return 0; } else return ERR; } void VarItem::SetVar(VarTypes VT,void* VarPtr) { VarType=VT; switch(VarType) { case T_int: ValInt = (int *) VarPtr; break; case T_float: ValFloat = (float*) VarPtr; break; }; } /****** void VarItem::SetVarType(VarTypes VarType) { this->VarType=VarType; } *******/ void VarItem::SetVarArraySize(int Size) { ArraySize=Size; } void VarItem::SetVarName(char *Name) { strncpy(VarName,Name,VAR_NAME_LEN); VarName[VAR_NAME_LEN-1]='\0'; } char *VarItem::Dump() { static char *StrDump=NULL; unsigned int DumpSize=0; unsigned int DumpCounter=0, ArrayCounter=0;; char Tmp[16]; if(StrDump!=NULL) free(StrDump); DumpSize=DMP_SZ; StrDump=(char *)malloc(DumpSize); memset(StrDump,0,DMP_SZ); 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) { char *d; DumpSize = DumpCounter+DMP_SZ; d=(char *)malloc(DumpCounter+DMP_SZ); strcpy(d,StrDump); free(StrDump); StrDump=d; } strcat(StrDump+DumpCounter,Tmp); DumpCounter+=strlen(Tmp); ArrayCounter++; } while (ArrayCounter < ArraySize); StrDump[strlen(StrDump)-1]='\0'; return StrDump; } char *VarItem::GetVarName() { return VarName; }