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

File content as of revision 0:85afbf3c9fad:

#include "VarItems.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

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 unsigned int DumpSize=0;
    static char *StrDump;

    unsigned int counter=0, DumpCounter=0;
    char Tmp[16];

    if(DumpSize==0)
    {
        DumpSize=100;
        StrDump=(char *)malloc(DumpSize);
        memset(StrDump,0,100);
    }

    do
    {

        switch(VarType)
        {

        case T_int:
            sprintf(Tmp,"%d,",*(ValInt+counter));
            break;

        case T_float:
            sprintf(Tmp,"%f,",*(ValFloat+counter));
            break;

        };

        if(DumpCounter+strlen(Tmp) >= DumpSize)
        {
            char *d;
            d=(char *)malloc(strlen(StrDump));
            DumpSize += strlen(StrDump);
            strcpy(d,StrDump);
            free(StrDump);
            StrDump=d;
        }

#if defined VARITEMS_DEBUG
        puts(StrDump);
#endif // defined

        strcat(StrDump+DumpCounter,Tmp);
        DumpCounter+=strlen(Tmp);

    }
    while (counter < ArraySize);

    return StrDump;

}



char *VarItem::GetVarName()
{
    return VarName;
}