julian C / VarStore
Committer:
julmbed
Date:
Tue Aug 26 09:39:40 2014 +0000
Revision:
17:8c20a558d34f
Parent:
16:19ea694d96c0
fixed doc typos

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