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:
- 8:934ec53fe2c0
- Parent:
- 4:4be2eaf872df
- Child:
- 9:d081aa4e4418
File content as of revision 8:934ec53fe2c0:
#include "VarItems.h" #include <string.h> #include <stdlib.h> #include <stdio.h> #include "mbed.h" extern Serial pc; #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; // pc.printf(" Set val:%s \n",Val); 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 1; } 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::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() { pc.puts(" 0 \n"); 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); pc.puts(" 1 \n"); do { switch(VarType) { case T_int: sprintf(Tmp,"%d,",*(ValInt+ArrayCounter)); break; case T_float: sprintf(Tmp,"%f,",*(ValFloat+ArrayCounter)); break; }; pc.puts(" 2 \n"); if(DumpCounter+strlen(Tmp) >= DumpSize) { char *d; DumpSize = DumpCounter+DMP_SZ; d=(char *)malloc(DumpCounter+DMP_SZ); strcpy(d,StrDump); free(StrDump); StrDump=d; } pc.puts(" 3 \n"); strcat(StrDump+DumpCounter,Tmp); DumpCounter+=strlen(Tmp); ArrayCounter++; } while (ArrayCounter < ArraySize); StrDump[strlen(StrDump)-1]='\0'; return StrDump; } char *VarItem::GetVarName() { return VarName; }