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:
9:d081aa4e4418
Parent:
8:934ec53fe2c0
Child:
13:e1ba5bf9e51f

File content as of revision 9:d081aa4e4418:

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

extern RawSerial pc;

#define DMP_SZ 132


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()
{
    static char StrDump[DMP_SZ];
    unsigned int DumpSize=DMP_SZ;
    unsigned int  DumpCounter=0, ArrayCounter=0;;
    char Tmp[16];
    
    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;
            */
            break;
        }
        strcat(StrDump+DumpCounter,Tmp);
        DumpCounter+=strlen(Tmp);
        ArrayCounter++;
    } while (ArrayCounter < ArraySize);

    StrDump[strlen(StrDump)-1]='\0';

    return StrDump;

}



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