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-25
Revision:
0:85afbf3c9fad
Child:
1:bbd6b84fc908

File content as of revision 0:85afbf3c9fad:


#include <string.h>
#include  <stdio.h>
#include "mbed.h"
#include "rtos.h"

#include "VarStore.h"

#define CI_SZ 100

/*******************************
*
*
********************************/
VarStore::VarStore(Serial *ser)
{
    VarCounter=0;
    this->pc=ser;
}

/*******************************
*
*
********************************/

VarStore::~VarStore()
{
    //dtor
}

/*******************************
*
*
********************************/

int VarStore::Set(char *Input)
{
    VarItem *V;
    char *Name;

    if(Input [0] == 's') {
        strtok(Input,":");
        Name=strtok(NULL,":");
        if((V=GetVar(Name)) != NULL)
            return V->SetVal(strtok(NULL,":"));
    }
    return ERR;
}

/*******************************
*
*
********************************/


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 )
{

    if(GetVar(Name) ==NULL) {
        if(VarCounter < SZ) {
            Store[VarCounter].SetVar(VarType,VarPtr);
            Store[VarCounter].SetVarName(Name);
            Store[VarCounter].SetVarArraySize(Size);
            VarCounter++;
            return 0;
        }
    }
    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::Worker(void const *args)
{
    VarStore *MyThis=(VarStore *)args;
    char c;
    int ret=0,ci_counter=0;
    char Cs[CI_SZ];

    for(int i=0; i<CI_SZ; i++) Cs[i]='\0';
    
    MyThis->pc->printf(" hi worker\n");
    while (1) {
        if(MyThis->pc->readable()) {
            c=MyThis->pc->getc();
//           MyThis->pc->printf(" leo %c\n",c);
            if(ci_counter >= CI_SZ-1) {   // RESET
                ci_counter=0;
                Cs[0]='\0';
                MyThis->pc->printf(" reset\n");

            } else {
                if(c=='\r') {
                    Cs[ci_counter]='\0';
                    MyThis->pc->printf(" CI -%s- \n",Cs);
                    ret=MyThis->Set(Cs);
                    ci_counter=0;
                    Cs[0]='\0';
                    MyThis->pc->printf(" set variable (%d)\n",ret);
                    MyThis->pc->printf(" offset -%s- \n",MyThis->Get("offset"));
                } else {
                    MyThis->pc->printf(" CI -%s- -%c- -%d- \n",Cs,c,ci_counter);
                    
                    Cs[ci_counter]=c;
                    ci_counter++;
                    MyThis->pc->printf(" parcial CI -%s- -%c-(%c) -%d- \n",Cs,c,Cs[ci_counter],ci_counter);
                }
            }
        }

        if(ret==ERR) {
            Thread::wait(100);
            MyThis->pc->printf(" error setting var \n");
            ret=0;
        }
        Thread::wait(100);
    } // While
}

/*******************************
*
*
********************************/