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:
Tue Aug 26 09:20:44 2014 +0000
Revision:
15:a794a7ef6170
Parent:
14:6cbe8366d587
Child:
16:19ea694d96c0
trying to fix documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
julmbed 0:85afbf3c9fad 1 #ifndef VARSTORE_H
julmbed 0:85afbf3c9fad 2 #define VARSTORE_H
julmbed 0:85afbf3c9fad 3 #include "mbed.h"
julmbed 0:85afbf3c9fad 4 #include "VarItems.h"
julmbed 0:85afbf3c9fad 5
julmbed 3:cf43e6de918e 6 #define STR_OK ""
julmbed 0:85afbf3c9fad 7
julmbed 14:6cbe8366d587 8 /** VarStore Class beta !!!!!!!
julmbed 15:a794a7ef6170 9 * Used for reading/modifying program variables
julmbed 15:a794a7ef6170 10 * from the console at runtime. Helpful for
julmbed 15:a794a7ef6170 11 * debugging.
julmbed 15:a794a7ef6170 12 * It has a facility as well to reset the mbed
julmbed 15:a794a7ef6170 13 * from the serial console without pressing
julmbed 15:a794a7ef6170 14 * any button.
julmbed 15:a794a7ef6170 15 * It does not block the serial/input channel
julmbed 15:a794a7ef6170 16 * in case it is needed for other stuff
julmbed 15:a794a7ef6170 17 *
julmbed 15:a794a7ef6170 18 * Example:
julmbed 15:a794a7ef6170 19 * @code
julmbed 15:a794a7ef6170 20 *
julmbed 15:a794a7ef6170 21 * #include "mbed.h"
julmbed 15:a794a7ef6170 22 * #include "rtos.h"
julmbed 15:a794a7ef6170 23 * #include "VarStore.h"
julmbed 15:a794a7ef6170 24 *
julmbed 15:a794a7ef6170 25 * #include <RawSerial.h>
julmbed 15:a794a7ef6170 26 *
julmbed 15:a794a7ef6170 27 * RawSerial pc(USBTX,USBRX); // Be aware !!!! need rawserial. No printf for you anymore !!
julmbed 15:a794a7ef6170 28 * // no malloc nither as this works in ISR
julmbed 15:a794a7ef6170 29 *
julmbed 15:a794a7ef6170 30 * VarStore Store(&pc,20); // create storage for 20 variables/arrays attach to serial pc
julmbed 15:a794a7ef6170 31 *
julmbed 15:a794a7ef6170 32 * DigitalOut led1(LED1);
julmbed 15:a794a7ef6170 33 * DigitalOut led2(LED2);
julmbed 15:a794a7ef6170 34 *
julmbed 15:a794a7ef6170 35 * void led2_thread(void const *args) {
julmbed 15:a794a7ef6170 36 *
julmbed 15:a794a7ef6170 37 * int wait2=1000;
julmbed 15:a794a7ef6170 38 * Store.Load("wait2",&wait2,T_int); // load variable wait2 in Store
julmbed 15:a794a7ef6170 39 *
julmbed 15:a794a7ef6170 40 * while (true) {
julmbed 15:a794a7ef6170 41 * led2 = !led2;
julmbed 15:a794a7ef6170 42 * Thread::wait(wait2); // remember, no WAIT, STOPS CPU FROM RUNNING WHOLE PROGRAM.
julmbed 15:a794a7ef6170 43 * // use Thread::wait to stop just your path of execution.
julmbed 15:a794a7ef6170 44 * }
julmbed 15:a794a7ef6170 45 * }
julmbed 15:a794a7ef6170 46 *
julmbed 15:a794a7ef6170 47 * int main() {
julmbed 15:a794a7ef6170 48 *
julmbed 15:a794a7ef6170 49 * int wait1=500;
julmbed 15:a794a7ef6170 50 * Store.Load("wait1",&wait1,T_int); // Load variable wait1 in Store
julmbed 15:a794a7ef6170 51 * Thread VS_thread(VarStore::Worker,&Store,osPriorityNormal,DEFAULT_STACK_SIZE,NULL); // launch VarStore Thread
julmbed 15:a794a7ef6170 52 *
julmbed 15:a794a7ef6170 53 * Thread thread(led2_thread);
julmbed 15:a794a7ef6170 54 *
julmbed 15:a794a7ef6170 55 * while (true) {
julmbed 15:a794a7ef6170 56 * led1 = !led1;
julmbed 15:a794a7ef6170 57 * Thread::wait(wait1); // remember, no WAIT, STOPS CPU FROM RUNNING WHOLE PROGRAM.
julmbed 15:a794a7ef6170 58 * // use Thread::wait to stop just your path of execution.
julmbed 15:a794a7ef6170 59 * }
julmbed 15:a794a7ef6170 60 * }
julmbed 15:a794a7ef6170 61 *
julmbed 15:a794a7ef6170 62 * @endcode
julmbed 15:a794a7ef6170 63 *
julmbed 15:a794a7ef6170 64 *
julmbed 15:a794a7ef6170 65 *
julmbed 15:a794a7ef6170 66 *
julmbed 15:a794a7ef6170 67 */
julmbed 0:85afbf3c9fad 68 class VarStore
julmbed 0:85afbf3c9fad 69 {
julmbed 7:fafe81a95c08 70
julmbed 7:fafe81a95c08 71
julmbed 10:34d368966675 72 // friend void Worker2();
julmbed 0:85afbf3c9fad 73 public:
julmbed 13:e1ba5bf9e51f 74 /*
julmbed 13:e1ba5bf9e51f 75 * Constructor
julmbed 13:e1ba5bf9e51f 76 */
julmbed 12:e9c8d2d9ac71 77 VarStore( RawSerial *ser, int sz);
julmbed 13:e1ba5bf9e51f 78
julmbed 13:e1ba5bf9e51f 79 /*
julmbed 13:e1ba5bf9e51f 80 * destr
julmbed 13:e1ba5bf9e51f 81 */
julmbed 0:85afbf3c9fad 82 virtual ~VarStore();
julmbed 13:e1ba5bf9e51f 83
julmbed 13:e1ba5bf9e51f 84 /* assigns (a) value(s) to a variable or array
julmbed 13:e1ba5bf9e51f 85 */
julmbed 2:a59207652720 86 char * Set(char *Input);
julmbed 0:85afbf3c9fad 87
julmbed 13:e1ba5bf9e51f 88 /*Get contents of a variable as a string
julmbed 13:e1ba5bf9e51f 89 */
julmbed 0:85afbf3c9fad 90 char* Get(char *Name);
julmbed 0:85afbf3c9fad 91
julmbed 13:e1ba5bf9e51f 92 /** Load a variable on VarStore
julmbed 2:a59207652720 93 *
julmbed 13:e1ba5bf9e51f 94 * @param Name string that will be used to query/set the value of the variable/array
julmbed 13:e1ba5bf9e51f 95 * @param VarPtr pointer to variable
julmbed 13:e1ba5bf9e51f 96 * @param VarType enumerated type indicating int/float ( only supported currently) enum VarTypes {T_int,T_float};
julmbed 13:e1ba5bf9e51f 97 *
julmbed 13:e1ba5bf9e51f 98 * @returns ERR on error / NULL on success
julmbed 13:e1ba5bf9e51f 99 */
julmbed 0:85afbf3c9fad 100 int Load(char *Name, void *VarPtr,VarTypes VarType );
julmbed 2:a59207652720 101
julmbed 13:e1ba5bf9e51f 102 /** Load an array on VarStore
julmbed 2:a59207652720 103 *
julmbed 13:e1ba5bf9e51f 104 * @param Name string that will be used to query/set the value of the variable/array
julmbed 13:e1ba5bf9e51f 105 * @param VarPtr pointer to base of array
julmbed 13:e1ba5bf9e51f 106 * @param VarType enumerated type indicating int/float ( only supported currently) enum VarTypes {T_int,T_float};
julmbed 13:e1ba5bf9e51f 107 * @param Size number of elements in the array
julmbed 13:e1ba5bf9e51f 108 *
julmbed 13:e1ba5bf9e51f 109 * @returns ERR on error / NULL on success
julmbed 13:e1ba5bf9e51f 110 */
julmbed 0:85afbf3c9fad 111 int Load(char *Name, void *VarPtr,VarTypes VarType, int Size );
julmbed 0:85afbf3c9fad 112
julmbed 13:e1ba5bf9e51f 113
julmbed 13:e1ba5bf9e51f 114
julmbed 13:e1ba5bf9e51f 115 /** Thread that will manage the concole interaction. Main role is to remain sleeping
julmbed 13:e1ba5bf9e51f 116 * and only awakening to fire worker2 to deal with console input
julmbed 2:a59207652720 117 *
julmbed 13:e1ba5bf9e51f 118 * @param args pointer to the VarStore Object that will be managing.
julmbed 13:e1ba5bf9e51f 119 *
julmbed 13:e1ba5bf9e51f 120 * @returns ERR on error / NULL on success
julmbed 13:e1ba5bf9e51f 121 */
julmbed 13:e1ba5bf9e51f 122 static void Worker(void const *args);
julmbed 6:9848fdaf2ad9 123
julmbed 0:85afbf3c9fad 124
julmbed 0:85afbf3c9fad 125 protected:
julmbed 2:a59207652720 126
julmbed 0:85afbf3c9fad 127 private:
julmbed 13:e1ba5bf9e51f 128 /* Get variable item by name
julmbed 13:e1ba5bf9e51f 129 */
julmbed 0:85afbf3c9fad 130 VarItem *GetVar(char *name);
julmbed 0:85afbf3c9fad 131
julmbed 13:e1ba5bf9e51f 132 /* do stuff with console input
julmbed 13:e1ba5bf9e51f 133 */
julmbed 7:fafe81a95c08 134 char *Do(char *str);
julmbed 5:47b67a7c0bb7 135
julmbed 13:e1ba5bf9e51f 136 /* manages console input
julmbed 13:e1ba5bf9e51f 137 */
julmbed 10:34d368966675 138 static void Worker2();
julmbed 5:47b67a7c0bb7 139
julmbed 13:e1ba5bf9e51f 140
julmbed 12:e9c8d2d9ac71 141 VarItem *Store;
julmbed 12:e9c8d2d9ac71 142 int sz; // number of varibales to store / array size
julmbed 0:85afbf3c9fad 143 int VarCounter;
julmbed 12:e9c8d2d9ac71 144 RawSerial *pc;
julmbed 12:e9c8d2d9ac71 145
julmbed 6:9848fdaf2ad9 146 static VarStore *MyThis; // used by the workers reading the terminal
julmbed 13:e1ba5bf9e51f 147 // instantiation via a static fucntion so need to record
julmbed 13:e1ba5bf9e51f 148 // who am I
julmbed 0:85afbf3c9fad 149 };
julmbed 0:85afbf3c9fad 150
julmbed 7:fafe81a95c08 151
julmbed 6:9848fdaf2ad9 152
julmbed 0:85afbf3c9fad 153 #endif // VARSTORE_H