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.

Committer:
julmbed
Date:
Mon Aug 25 20:44:39 2014 +0000
Revision:
9:d081aa4e4418
Parent:
8:934ec53fe2c0
Child:
13:e1ba5bf9e51f
workin with rawserial and without malloc.  dump string limited to 132 chars

Who changed what in which revision?

UserRevisionLine numberNew contents of line
julmbed 0:85afbf3c9fad 1 #include "VarItems.h"
julmbed 0:85afbf3c9fad 2 #include <string.h>
julmbed 0:85afbf3c9fad 3 #include <stdlib.h>
julmbed 0:85afbf3c9fad 4 #include <stdio.h>
julmbed 4:4be2eaf872df 5 #include "mbed.h"
julmbed 0:85afbf3c9fad 6
julmbed 9:d081aa4e4418 7 extern RawSerial pc;
julmbed 1:bbd6b84fc908 8
julmbed 9:d081aa4e4418 9 #define DMP_SZ 132
julmbed 1:bbd6b84fc908 10
julmbed 1:bbd6b84fc908 11
julmbed 0:85afbf3c9fad 12 VarItem::VarItem()
julmbed 0:85afbf3c9fad 13 {
julmbed 0:85afbf3c9fad 14 VarName[0]='\0';
julmbed 0:85afbf3c9fad 15
julmbed 0:85afbf3c9fad 16 ValInt=NULL;
julmbed 0:85afbf3c9fad 17 ValFloat=NULL;
julmbed 0:85afbf3c9fad 18 ArraySize=0;
julmbed 0:85afbf3c9fad 19 }
julmbed 0:85afbf3c9fad 20
julmbed 0:85afbf3c9fad 21 VarItem::~VarItem()
julmbed 0:85afbf3c9fad 22 {
julmbed 0:85afbf3c9fad 23 //dtor
julmbed 0:85afbf3c9fad 24 }
julmbed 0:85afbf3c9fad 25
julmbed 0:85afbf3c9fad 26 int VarItem::SetVal(char *Val)
julmbed 0:85afbf3c9fad 27 {
julmbed 0:85afbf3c9fad 28 char * Values;
julmbed 0:85afbf3c9fad 29 unsigned int Count=0;
julmbed 0:85afbf3c9fad 30
julmbed 4:4be2eaf872df 31 // pc.printf(" Set val:%s \n",Val);
julmbed 4:4be2eaf872df 32
julmbed 0:85afbf3c9fad 33 Values=strtok(Val,",");
julmbed 1:bbd6b84fc908 34 if(Values) {
julmbed 1:bbd6b84fc908 35 do {
julmbed 1:bbd6b84fc908 36 switch(VarType) {
julmbed 0:85afbf3c9fad 37
julmbed 1:bbd6b84fc908 38 case T_int:
julmbed 1:bbd6b84fc908 39 *(ValInt+Count) = atoi(Values);
julmbed 1:bbd6b84fc908 40 break;
julmbed 0:85afbf3c9fad 41
julmbed 1:bbd6b84fc908 42 case T_float:
julmbed 1:bbd6b84fc908 43 *(ValFloat+Count) = atof(Values);
julmbed 1:bbd6b84fc908 44 break;
julmbed 0:85afbf3c9fad 45
julmbed 0:85afbf3c9fad 46 };
julmbed 0:85afbf3c9fad 47 Count++;
julmbed 0:85afbf3c9fad 48 Values=strtok(NULL,",");
julmbed 1:bbd6b84fc908 49 } while((Values !=NULL) && (Count < ArraySize));
julmbed 2:a59207652720 50 return 1;
julmbed 1:bbd6b84fc908 51 } else
julmbed 0:85afbf3c9fad 52 return ERR;
julmbed 0:85afbf3c9fad 53
julmbed 0:85afbf3c9fad 54 }
julmbed 0:85afbf3c9fad 55
julmbed 0:85afbf3c9fad 56 void VarItem::SetVar(VarTypes VT,void* VarPtr)
julmbed 0:85afbf3c9fad 57 {
julmbed 0:85afbf3c9fad 58
julmbed 0:85afbf3c9fad 59 VarType=VT;
julmbed 1:bbd6b84fc908 60 switch(VarType) {
julmbed 0:85afbf3c9fad 61
julmbed 1:bbd6b84fc908 62 case T_int:
julmbed 1:bbd6b84fc908 63 ValInt = (int *) VarPtr;
julmbed 1:bbd6b84fc908 64 break;
julmbed 0:85afbf3c9fad 65
julmbed 1:bbd6b84fc908 66 case T_float:
julmbed 1:bbd6b84fc908 67 ValFloat = (float*) VarPtr;
julmbed 1:bbd6b84fc908 68 break;
julmbed 0:85afbf3c9fad 69
julmbed 0:85afbf3c9fad 70 };
julmbed 0:85afbf3c9fad 71
julmbed 0:85afbf3c9fad 72 }
julmbed 0:85afbf3c9fad 73
julmbed 0:85afbf3c9fad 74
julmbed 0:85afbf3c9fad 75 void VarItem::SetVarArraySize(int Size)
julmbed 0:85afbf3c9fad 76 {
julmbed 0:85afbf3c9fad 77 ArraySize=Size;
julmbed 0:85afbf3c9fad 78 }
julmbed 0:85afbf3c9fad 79
julmbed 0:85afbf3c9fad 80
julmbed 0:85afbf3c9fad 81 void VarItem::SetVarName(char *Name)
julmbed 0:85afbf3c9fad 82 {
julmbed 0:85afbf3c9fad 83 strncpy(VarName,Name,VAR_NAME_LEN);
julmbed 0:85afbf3c9fad 84 VarName[VAR_NAME_LEN-1]='\0';
julmbed 0:85afbf3c9fad 85 }
julmbed 0:85afbf3c9fad 86
julmbed 0:85afbf3c9fad 87
julmbed 0:85afbf3c9fad 88 char *VarItem::Dump()
julmbed 0:85afbf3c9fad 89 {
julmbed 9:d081aa4e4418 90 static char StrDump[DMP_SZ];
julmbed 9:d081aa4e4418 91 unsigned int DumpSize=DMP_SZ;
julmbed 1:bbd6b84fc908 92 unsigned int DumpCounter=0, ArrayCounter=0;;
julmbed 0:85afbf3c9fad 93 char Tmp[16];
julmbed 9:d081aa4e4418 94
julmbed 1:bbd6b84fc908 95 memset(StrDump,0,DMP_SZ);
julmbed 0:85afbf3c9fad 96
julmbed 1:bbd6b84fc908 97 do {
julmbed 1:bbd6b84fc908 98 switch(VarType) {
julmbed 1:bbd6b84fc908 99 case T_int:
julmbed 1:bbd6b84fc908 100 sprintf(Tmp,"%d,",*(ValInt+ArrayCounter));
julmbed 1:bbd6b84fc908 101 break;
julmbed 1:bbd6b84fc908 102 case T_float:
julmbed 1:bbd6b84fc908 103 sprintf(Tmp,"%f,",*(ValFloat+ArrayCounter));
julmbed 1:bbd6b84fc908 104 break;
julmbed 0:85afbf3c9fad 105 };
julmbed 1:bbd6b84fc908 106 if(DumpCounter+strlen(Tmp) >= DumpSize) {
julmbed 9:d081aa4e4418 107 /*
julmbed 0:85afbf3c9fad 108 char *d;
julmbed 1:bbd6b84fc908 109 DumpSize = DumpCounter+DMP_SZ;
julmbed 1:bbd6b84fc908 110 d=(char *)malloc(DumpCounter+DMP_SZ);
julmbed 0:85afbf3c9fad 111 strcpy(d,StrDump);
julmbed 0:85afbf3c9fad 112 free(StrDump);
julmbed 0:85afbf3c9fad 113 StrDump=d;
julmbed 9:d081aa4e4418 114 */
julmbed 9:d081aa4e4418 115 break;
julmbed 0:85afbf3c9fad 116 }
julmbed 0:85afbf3c9fad 117 strcat(StrDump+DumpCounter,Tmp);
julmbed 0:85afbf3c9fad 118 DumpCounter+=strlen(Tmp);
julmbed 1:bbd6b84fc908 119 ArrayCounter++;
julmbed 1:bbd6b84fc908 120 } while (ArrayCounter < ArraySize);
julmbed 0:85afbf3c9fad 121
julmbed 1:bbd6b84fc908 122 StrDump[strlen(StrDump)-1]='\0';
julmbed 0:85afbf3c9fad 123
julmbed 0:85afbf3c9fad 124 return StrDump;
julmbed 0:85afbf3c9fad 125
julmbed 0:85afbf3c9fad 126 }
julmbed 0:85afbf3c9fad 127
julmbed 0:85afbf3c9fad 128
julmbed 0:85afbf3c9fad 129
julmbed 0:85afbf3c9fad 130 char *VarItem::GetVarName()
julmbed 0:85afbf3c9fad 131 {
julmbed 0:85afbf3c9fad 132 return VarName;
julmbed 0:85afbf3c9fad 133 }
julmbed 0:85afbf3c9fad 134
julmbed 0:85afbf3c9fad 135