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.
VarStore.cpp
- Committer:
- julmbed
- Date:
- 2014-08-26
- Revision:
- 12:e9c8d2d9ac71
- Parent:
- 10:34d368966675
- Child:
- 13:e1ba5bf9e51f
File content as of revision 12:e9c8d2d9ac71:
#include <string.h> #include <stdio.h> #include "mbed.h" #include "rtos.h" extern "C" void mbed_reset(); #include "VarStore.h" #define CI_SZ 100 /******************************* * * ********************************/ VarStore::VarStore( RawSerial *ser, int sz) { VarCounter=0; this->pc=ser; this->sz=sz; //Store=(VarItem *) malloc( sizeof(VarItem)*sz); Store=new VarItem[sz]; } /******************************* * * ********************************/ VarStore::~VarStore() { //dtor } /******************************* * * ********************************/ char *VarStore::Set(char *Input) { VarItem *V; char *Name; // beaware of puts pc->printf("VarStore Set %s\n",Input); if(Input [0] == 's') { strtok(Input,":"); Name=strtok(NULL,":"); if((V=GetVar(Name)) != NULL) return V->SetVal(strtok(NULL,":"))!= ERR ? Input : NULL; } 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++) if((strcmp(Name,Store[i].GetVarName()))==0) return &Store[i]; return NULL; } char* VarStore::Get(char *Name) { VarItem *V; V=GetVar(Name); if(V!=NULL) return V->Dump(); else return NULL; } /******************************* * * ********************************/ void VarStore::Worker2() { static char c, *ret=STR_OK;// not NULL to start in no error state static int ci_counter=0; static char Cs[CI_SZ]; if(VarStore::MyThis->pc->readable()) { c=VarStore::MyThis->pc->getc(); if(ci_counter >= CI_SZ-1) { // RESET ci_counter=0; Cs[0]='\0'; } else { if(c=='\r') { Cs[ci_counter]='\0'; ret=VarStore::MyThis->Do(Cs); ci_counter=0; Cs[0]='\0'; } else { Cs[ci_counter]=c; ci_counter++; } } } if(ret==NULL) { VarStore::MyThis->pc->puts(" error setting/getting var \n"); ret=STR_OK; } } /******************************* * * ********************************/ 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); } } /******************************* * * ********************************/ char *VarStore::Do(char *str) { char *ret; if(str != NULL) { switch(*str) { case 's': return VarStore::MyThis->Set(str); case 'd': VarStore::MyThis->pc->puts(str); VarStore::MyThis->pc->putc('\n'); ret=strtok(str,":"); ret=strtok(NULL,":"); ret=VarStore::MyThis->Get(ret); if(ret!=NULL) { VarStore::MyThis->pc->puts(ret); VarStore::MyThis->pc->putc('\n'); return ret; } else return NULL; case 'r': mbed_reset(); return NULL; case 'w': strtok(str,":"); Thread::wait(atoi(strtok(NULL,":"))); return STR_OK; }; } return NULL; }