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@8:934ec53fe2c0, 2014-08-25 (annotated)
- Committer:
- julmbed
- Date:
- Mon Aug 25 19:54:12 2014 +0000
- Revision:
- 8:934ec53fe2c0
- Parent:
- 7:fafe81a95c08
- Child:
- 9:d081aa4e4418
con rawserial y sin avanzar en dump de las variables
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
julmbed | 0:85afbf3c9fad | 1 | |
julmbed | 0:85afbf3c9fad | 2 | #include <string.h> |
julmbed | 0:85afbf3c9fad | 3 | #include <stdio.h> |
julmbed | 0:85afbf3c9fad | 4 | #include "mbed.h" |
julmbed | 0:85afbf3c9fad | 5 | #include "rtos.h" |
julmbed | 8:934ec53fe2c0 | 6 | |
julmbed | 0:85afbf3c9fad | 7 | |
julmbed | 2:a59207652720 | 8 | extern "C" void mbed_reset(); |
julmbed | 2:a59207652720 | 9 | |
julmbed | 0:85afbf3c9fad | 10 | #include "VarStore.h" |
julmbed | 0:85afbf3c9fad | 11 | |
julmbed | 0:85afbf3c9fad | 12 | #define CI_SZ 100 |
julmbed | 0:85afbf3c9fad | 13 | |
julmbed | 0:85afbf3c9fad | 14 | /******************************* |
julmbed | 0:85afbf3c9fad | 15 | * |
julmbed | 0:85afbf3c9fad | 16 | * |
julmbed | 0:85afbf3c9fad | 17 | ********************************/ |
julmbed | 8:934ec53fe2c0 | 18 | VarStore::VarStore( RawSerial *ser) |
julmbed | 0:85afbf3c9fad | 19 | { |
julmbed | 0:85afbf3c9fad | 20 | VarCounter=0; |
julmbed | 8:934ec53fe2c0 | 21 | this->pc=ser; |
julmbed | 0:85afbf3c9fad | 22 | } |
julmbed | 0:85afbf3c9fad | 23 | |
julmbed | 0:85afbf3c9fad | 24 | /******************************* |
julmbed | 0:85afbf3c9fad | 25 | * |
julmbed | 0:85afbf3c9fad | 26 | * |
julmbed | 0:85afbf3c9fad | 27 | ********************************/ |
julmbed | 0:85afbf3c9fad | 28 | |
julmbed | 0:85afbf3c9fad | 29 | VarStore::~VarStore() |
julmbed | 0:85afbf3c9fad | 30 | { |
julmbed | 0:85afbf3c9fad | 31 | //dtor |
julmbed | 0:85afbf3c9fad | 32 | } |
julmbed | 0:85afbf3c9fad | 33 | |
julmbed | 0:85afbf3c9fad | 34 | /******************************* |
julmbed | 0:85afbf3c9fad | 35 | * |
julmbed | 0:85afbf3c9fad | 36 | * |
julmbed | 0:85afbf3c9fad | 37 | ********************************/ |
julmbed | 0:85afbf3c9fad | 38 | |
julmbed | 2:a59207652720 | 39 | char *VarStore::Set(char *Input) |
julmbed | 0:85afbf3c9fad | 40 | { |
julmbed | 0:85afbf3c9fad | 41 | VarItem *V; |
julmbed | 0:85afbf3c9fad | 42 | char *Name; |
julmbed | 0:85afbf3c9fad | 43 | |
julmbed | 8:934ec53fe2c0 | 44 | // beaware of puts pc->printf("VarStore Set %s\n",Input); |
julmbed | 8:934ec53fe2c0 | 45 | |
julmbed | 0:85afbf3c9fad | 46 | if(Input [0] == 's') { |
julmbed | 0:85afbf3c9fad | 47 | strtok(Input,":"); |
julmbed | 0:85afbf3c9fad | 48 | Name=strtok(NULL,":"); |
julmbed | 0:85afbf3c9fad | 49 | if((V=GetVar(Name)) != NULL) |
julmbed | 2:a59207652720 | 50 | return V->SetVal(strtok(NULL,":"))!= ERR ? Input : NULL; |
julmbed | 0:85afbf3c9fad | 51 | } |
julmbed | 2:a59207652720 | 52 | return NULL; |
julmbed | 0:85afbf3c9fad | 53 | } |
julmbed | 0:85afbf3c9fad | 54 | |
julmbed | 0:85afbf3c9fad | 55 | /******************************* |
julmbed | 0:85afbf3c9fad | 56 | * |
julmbed | 0:85afbf3c9fad | 57 | * |
julmbed | 0:85afbf3c9fad | 58 | ********************************/ |
julmbed | 0:85afbf3c9fad | 59 | |
julmbed | 0:85afbf3c9fad | 60 | |
julmbed | 0:85afbf3c9fad | 61 | int VarStore::Load(char *Name, void *VarPtr,VarTypes VarType ) |
julmbed | 0:85afbf3c9fad | 62 | { |
julmbed | 0:85afbf3c9fad | 63 | return Load(Name, VarPtr,VarType,0 ); |
julmbed | 0:85afbf3c9fad | 64 | } |
julmbed | 0:85afbf3c9fad | 65 | |
julmbed | 0:85afbf3c9fad | 66 | /******************************* |
julmbed | 0:85afbf3c9fad | 67 | * |
julmbed | 0:85afbf3c9fad | 68 | * |
julmbed | 0:85afbf3c9fad | 69 | ********************************/ |
julmbed | 0:85afbf3c9fad | 70 | |
julmbed | 0:85afbf3c9fad | 71 | int VarStore::Load(char *Name, void *VarPtr,VarTypes VarType, int Size ) |
julmbed | 0:85afbf3c9fad | 72 | { |
julmbed | 0:85afbf3c9fad | 73 | |
julmbed | 0:85afbf3c9fad | 74 | if(GetVar(Name) ==NULL) { |
julmbed | 0:85afbf3c9fad | 75 | if(VarCounter < SZ) { |
julmbed | 0:85afbf3c9fad | 76 | Store[VarCounter].SetVar(VarType,VarPtr); |
julmbed | 0:85afbf3c9fad | 77 | Store[VarCounter].SetVarName(Name); |
julmbed | 0:85afbf3c9fad | 78 | Store[VarCounter].SetVarArraySize(Size); |
julmbed | 0:85afbf3c9fad | 79 | VarCounter++; |
julmbed | 0:85afbf3c9fad | 80 | return 0; |
julmbed | 0:85afbf3c9fad | 81 | } |
julmbed | 0:85afbf3c9fad | 82 | } |
julmbed | 0:85afbf3c9fad | 83 | return ERR; |
julmbed | 0:85afbf3c9fad | 84 | } |
julmbed | 0:85afbf3c9fad | 85 | |
julmbed | 0:85afbf3c9fad | 86 | /******************************* |
julmbed | 0:85afbf3c9fad | 87 | * |
julmbed | 0:85afbf3c9fad | 88 | * |
julmbed | 0:85afbf3c9fad | 89 | ********************************/ |
julmbed | 0:85afbf3c9fad | 90 | |
julmbed | 0:85afbf3c9fad | 91 | VarItem *VarStore::GetVar(char *Name) |
julmbed | 0:85afbf3c9fad | 92 | { |
julmbed | 0:85afbf3c9fad | 93 | for (int i=0; i<SZ; i++) |
julmbed | 0:85afbf3c9fad | 94 | if((strcmp(Name,Store[i].GetVarName()))==0) |
julmbed | 0:85afbf3c9fad | 95 | return &Store[i]; |
julmbed | 0:85afbf3c9fad | 96 | |
julmbed | 0:85afbf3c9fad | 97 | return NULL; |
julmbed | 0:85afbf3c9fad | 98 | } |
julmbed | 0:85afbf3c9fad | 99 | |
julmbed | 0:85afbf3c9fad | 100 | char* VarStore::Get(char *Name) |
julmbed | 0:85afbf3c9fad | 101 | { |
julmbed | 8:934ec53fe2c0 | 102 | char b[100]; |
julmbed | 0:85afbf3c9fad | 103 | VarItem *V; |
julmbed | 8:934ec53fe2c0 | 104 | V=GetVar(Name); |
julmbed | 8:934ec53fe2c0 | 105 | VarStore::MyThis->pc->puts(" antes de dump "); |
julmbed | 8:934ec53fe2c0 | 106 | sprintf(b," %s %d %d %d \n", |
julmbed | 8:934ec53fe2c0 | 107 | V->VarName,*V->ValInt,V->VarType,V->ArraySize); |
julmbed | 8:934ec53fe2c0 | 108 | |
julmbed | 8:934ec53fe2c0 | 109 | VarStore::MyThis->pc->puts(b); |
julmbed | 8:934ec53fe2c0 | 110 | V->Dump(); |
julmbed | 8:934ec53fe2c0 | 111 | |
julmbed | 8:934ec53fe2c0 | 112 | // char VarName[VAR_NAME_LEN]; |
julmbed | 0:85afbf3c9fad | 113 | |
julmbed | 8:934ec53fe2c0 | 114 | // int *ValInt; |
julmbed | 8:934ec53fe2c0 | 115 | // float *ValFloat; |
julmbed | 8:934ec53fe2c0 | 116 | // VarTypes VarType; |
julmbed | 8:934ec53fe2c0 | 117 | // unsigned int ArraySize; |
julmbed | 8:934ec53fe2c0 | 118 | // if(V!=NULL) return V->Dump(); |
julmbed | 8:934ec53fe2c0 | 119 | // else |
julmbed | 8:934ec53fe2c0 | 120 | VarStore::MyThis->pc->puts(" despues de dump "); |
julmbed | 0:85afbf3c9fad | 121 | return NULL; |
julmbed | 0:85afbf3c9fad | 122 | } |
julmbed | 0:85afbf3c9fad | 123 | |
julmbed | 0:85afbf3c9fad | 124 | /******************************* |
julmbed | 0:85afbf3c9fad | 125 | * |
julmbed | 0:85afbf3c9fad | 126 | * |
julmbed | 0:85afbf3c9fad | 127 | ********************************/ |
julmbed | 0:85afbf3c9fad | 128 | |
julmbed | 7:fafe81a95c08 | 129 | void Worker2(); |
julmbed | 7:fafe81a95c08 | 130 | |
julmbed | 7:fafe81a95c08 | 131 | void Worker2() |
julmbed | 0:85afbf3c9fad | 132 | { |
julmbed | 8:934ec53fe2c0 | 133 | |
julmbed | 6:9848fdaf2ad9 | 134 | static char c, *ret=STR_OK;// not NULL to start in no error state |
julmbed | 8:934ec53fe2c0 | 135 | |
julmbed | 6:9848fdaf2ad9 | 136 | static int ci_counter=0; |
julmbed | 6:9848fdaf2ad9 | 137 | static char Cs[CI_SZ]; |
julmbed | 8:934ec53fe2c0 | 138 | if(VarStore::MyThis->pc->readable()) { |
julmbed | 8:934ec53fe2c0 | 139 | c=VarStore::MyThis->pc->getc(); |
julmbed | 2:a59207652720 | 140 | |
julmbed | 8:934ec53fe2c0 | 141 | if(ci_counter >= CI_SZ-1) { // RESET |
julmbed | 8:934ec53fe2c0 | 142 | ci_counter=0; |
julmbed | 8:934ec53fe2c0 | 143 | Cs[0]='\0'; |
julmbed | 8:934ec53fe2c0 | 144 | } else { |
julmbed | 8:934ec53fe2c0 | 145 | if(c=='\r') { |
julmbed | 8:934ec53fe2c0 | 146 | Cs[ci_counter]='\0'; |
julmbed | 8:934ec53fe2c0 | 147 | ret=VarStore::MyThis->Do(Cs); |
julmbed | 0:85afbf3c9fad | 148 | ci_counter=0; |
julmbed | 0:85afbf3c9fad | 149 | Cs[0]='\0'; |
julmbed | 0:85afbf3c9fad | 150 | } else { |
julmbed | 8:934ec53fe2c0 | 151 | Cs[ci_counter]=c; |
julmbed | 8:934ec53fe2c0 | 152 | ci_counter++; |
julmbed | 0:85afbf3c9fad | 153 | } |
julmbed | 0:85afbf3c9fad | 154 | } |
julmbed | 8:934ec53fe2c0 | 155 | } |
julmbed | 0:85afbf3c9fad | 156 | |
julmbed | 8:934ec53fe2c0 | 157 | if(ret==NULL) { |
julmbed | 8:934ec53fe2c0 | 158 | VarStore::MyThis->pc->puts(" error setting/getting var \n"); |
julmbed | 8:934ec53fe2c0 | 159 | ret=STR_OK; |
julmbed | 8:934ec53fe2c0 | 160 | } |
julmbed | 8:934ec53fe2c0 | 161 | |
julmbed | 0:85afbf3c9fad | 162 | } |
julmbed | 0:85afbf3c9fad | 163 | |
julmbed | 0:85afbf3c9fad | 164 | /******************************* |
julmbed | 0:85afbf3c9fad | 165 | * |
julmbed | 0:85afbf3c9fad | 166 | * |
julmbed | 0:85afbf3c9fad | 167 | ********************************/ |
julmbed | 6:9848fdaf2ad9 | 168 | |
julmbed | 8:934ec53fe2c0 | 169 | VarStore *VarStore::MyThis=NULL; // used by the worker reading the terminal. Need to be initilized this way to avoid |
julmbed | 8:934ec53fe2c0 | 170 | // compiling errors |
julmbed | 5:47b67a7c0bb7 | 171 | void VarStore::Worker(void const *args) |
julmbed | 5:47b67a7c0bb7 | 172 | { |
julmbed | 5:47b67a7c0bb7 | 173 | |
julmbed | 5:47b67a7c0bb7 | 174 | VarStore::MyThis=(VarStore *)args; |
julmbed | 5:47b67a7c0bb7 | 175 | |
julmbed | 8:934ec53fe2c0 | 176 | // cuidado puts VarStore::MyThis->pc->(" hi worker 1\n"); |
julmbed | 8:934ec53fe2c0 | 177 | |
julmbed | 7:fafe81a95c08 | 178 | VarStore::MyThis->pc->attach(&Worker2); |
julmbed | 5:47b67a7c0bb7 | 179 | |
julmbed | 5:47b67a7c0bb7 | 180 | while(1) { |
julmbed | 6:9848fdaf2ad9 | 181 | Thread::wait(1000); |
julmbed | 5:47b67a7c0bb7 | 182 | } |
julmbed | 5:47b67a7c0bb7 | 183 | |
julmbed | 5:47b67a7c0bb7 | 184 | } |
julmbed | 5:47b67a7c0bb7 | 185 | /******************************* |
julmbed | 5:47b67a7c0bb7 | 186 | * |
julmbed | 5:47b67a7c0bb7 | 187 | * |
julmbed | 5:47b67a7c0bb7 | 188 | ********************************/ |
julmbed | 0:85afbf3c9fad | 189 | |
julmbed | 7:fafe81a95c08 | 190 | char *VarStore::Do(char *str) |
julmbed | 2:a59207652720 | 191 | { |
julmbed | 2:a59207652720 | 192 | char *ret; |
julmbed | 2:a59207652720 | 193 | if(str != NULL) { |
julmbed | 2:a59207652720 | 194 | |
julmbed | 2:a59207652720 | 195 | switch(*str) { |
julmbed | 2:a59207652720 | 196 | case 's': |
julmbed | 6:9848fdaf2ad9 | 197 | return VarStore::MyThis->Set(str); |
julmbed | 2:a59207652720 | 198 | case 'd': |
julmbed | 8:934ec53fe2c0 | 199 | |
julmbed | 8:934ec53fe2c0 | 200 | ret=strtok(str,":"); |
julmbed | 8:934ec53fe2c0 | 201 | ret=strtok(NULL,":"); |
julmbed | 8:934ec53fe2c0 | 202 | |
julmbed | 8:934ec53fe2c0 | 203 | ret=VarStore::MyThis->Get(ret); |
julmbed | 8:934ec53fe2c0 | 204 | |
julmbed | 2:a59207652720 | 205 | if(ret!=NULL) { |
julmbed | 8:934ec53fe2c0 | 206 | // cuidado puts VarStore::MyThis->pc->printf("%s (%s)\n",str,ret); |
julmbed | 8:934ec53fe2c0 | 207 | VarStore::MyThis->pc->puts("el del mostrar variable"); |
julmbed | 2:a59207652720 | 208 | return ret; |
julmbed | 2:a59207652720 | 209 | } else |
julmbed | 2:a59207652720 | 210 | return NULL; |
julmbed | 2:a59207652720 | 211 | case 'r': |
julmbed | 2:a59207652720 | 212 | mbed_reset(); |
julmbed | 2:a59207652720 | 213 | return NULL; |
julmbed | 3:cf43e6de918e | 214 | case 'w': |
julmbed | 3:cf43e6de918e | 215 | strtok(str,":"); |
julmbed | 3:cf43e6de918e | 216 | Thread::wait(atoi(strtok(NULL,":"))); |
julmbed | 3:cf43e6de918e | 217 | return STR_OK; |
julmbed | 2:a59207652720 | 218 | }; |
julmbed | 2:a59207652720 | 219 | } |
julmbed | 2:a59207652720 | 220 | return NULL; |
julmbed | 2:a59207652720 | 221 | } |
julmbed | 2:a59207652720 | 222 |