Touch screen drivers control dashboard for miniature locomotive. Features meters for speed, volts, power. Switches for lights, horns. Drives multiple STM3_ESC brushless motor controllers for complete brushless loco system as used in "The Brute" - www.jons-workshop.com
Dependencies: TS_DISCO_F746NG mbed Servo LCD_DISCO_F746NG BSP_DISCO_F746NG QSPI_DISCO_F746NG AsyncSerial FastPWM
cli_TS_nortos.cpp@10:0bdfd342f393, 2018-06-22 (annotated)
- Committer:
- JonFreeman
- Date:
- Fri Jun 22 14:35:00 2018 +0000
- Revision:
- 10:0bdfd342f393
- Parent:
- cli_nortos.cpp@7:3b1f44cd4735
seem to have broken comms a bit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonFreeman | 5:21a8ac83142c | 1 | //Loco_TS_2018 |
JonFreeman | 4:67478861c670 | 2 | #include "mbed.h" |
JonFreeman | 4:67478861c670 | 3 | //#include "BufferedSerial.h" |
JonFreeman | 5:21a8ac83142c | 4 | #include "AsyncSerial.hpp" |
JonFreeman | 4:67478861c670 | 5 | #include <cctype> |
JonFreeman | 4:67478861c670 | 6 | #include "Electric_Loco.h" |
JonFreeman | 4:67478861c670 | 7 | using namespace std; |
JonFreeman | 4:67478861c670 | 8 | |
JonFreeman | 4:67478861c670 | 9 | //extern int I_Am () ; // Returns boards id number as ASCII char '0', '1' etc. Code for Broadcast = '\r' |
JonFreeman | 5:21a8ac83142c | 10 | //typedef double fl_typ; // |
JonFreeman | 4:67478861c670 | 11 | |
JonFreeman | 10:0bdfd342f393 | 12 | struct parameters pccom, lococom; |
JonFreeman | 10:0bdfd342f393 | 13 | void clicore (struct parameters & a) ; |
JonFreeman | 4:67478861c670 | 14 | const int BROADCAST = '\r'; |
JonFreeman | 4:67478861c670 | 15 | |
JonFreeman | 4:67478861c670 | 16 | // WithOUT RTOS |
JonFreeman | 4:67478861c670 | 17 | extern Serial pc; |
JonFreeman | 5:21a8ac83142c | 18 | extern AsyncSerial com; |
JonFreeman | 4:67478861c670 | 19 | extern void send_test () ; |
JonFreeman | 4:67478861c670 | 20 | |
JonFreeman | 5:21a8ac83142c | 21 | void null_cmd (struct parameters & a) |
JonFreeman | 5:21a8ac83142c | 22 | { |
JonFreeman | 4:67478861c670 | 23 | pc.printf ("At null_cmd, parameters : First %.3f, second %.3f\r\n", a.dbl[0], a.dbl[1]); |
JonFreeman | 4:67478861c670 | 24 | } |
JonFreeman | 4:67478861c670 | 25 | |
JonFreeman | 10:0bdfd342f393 | 26 | extern struct multi_bogie_options detected_bogie_options; |
JonFreeman | 10:0bdfd342f393 | 27 | |
JonFreeman | 10:0bdfd342f393 | 28 | /* |
JonFreeman | 10:0bdfd342f393 | 29 | struct single_bogie_options { |
JonFreeman | 10:0bdfd342f393 | 30 | char motoradir, motorbdir, gang, svo1, svo2, comm_src, id, wheeldia, motpin, wheelgear, spare; |
JonFreeman | 10:0bdfd342f393 | 31 | } ; |
JonFreeman | 10:0bdfd342f393 | 32 | */ |
JonFreeman | 10:0bdfd342f393 | 33 | bool validate_mode_bytes (char * bytes) { |
JonFreeman | 10:0bdfd342f393 | 34 | int q; |
JonFreeman | 10:0bdfd342f393 | 35 | for (int i = 0; i < numof_eeprom_options; i++) { |
JonFreeman | 10:0bdfd342f393 | 36 | q = (int)bytes[i]; |
JonFreeman | 10:0bdfd342f393 | 37 | if (i == ID) |
JonFreeman | 10:0bdfd342f393 | 38 | q |= '0'; |
JonFreeman | 10:0bdfd342f393 | 39 | if (q < option_list[i].min || q > option_list[i].max) |
JonFreeman | 10:0bdfd342f393 | 40 | return false; |
JonFreeman | 10:0bdfd342f393 | 41 | } |
JonFreeman | 10:0bdfd342f393 | 42 | return true; |
JonFreeman | 10:0bdfd342f393 | 43 | } |
JonFreeman | 10:0bdfd342f393 | 44 | |
JonFreeman | 10:0bdfd342f393 | 45 | extern uint32_t sys_timer_32Hz; |
JonFreeman | 10:0bdfd342f393 | 46 | char * globdest = 0; // findable by code here responding to gbb |
JonFreeman | 10:0bdfd342f393 | 47 | bool globflag; // findable by code here responding to gbb |
JonFreeman | 10:0bdfd342f393 | 48 | |
JonFreeman | 10:0bdfd342f393 | 49 | bool get_bogie_bytes (int bogie_id, char * dest) { // bogie_id could be e.g. 3, or '3' NOTE 22/06/2018 yet to write matching fn in bogie code |
JonFreeman | 10:0bdfd342f393 | 50 | uint32_t localtime = sys_timer_32Hz + 50; // for timeout |
JonFreeman | 10:0bdfd342f393 | 51 | bogie_id |= '0'; // ***** NOTE ***** THIS IS A BLOCKING FUNCTION |
JonFreeman | 10:0bdfd342f393 | 52 | globdest = dest; |
JonFreeman | 10:0bdfd342f393 | 53 | globflag = false; |
JonFreeman | 10:0bdfd342f393 | 54 | com.printf ("%cgbb\r", bogie_id); // issue gbb command to get bogie bytes to be coded into bogie |
JonFreeman | 10:0bdfd342f393 | 55 | pc.printf ("Issued %cgbb\r\n", bogie_id); |
JonFreeman | 10:0bdfd342f393 | 56 | while (!globflag && (localtime > sys_timer_32Hz)) { |
JonFreeman | 10:0bdfd342f393 | 57 | clicore (pccom); |
JonFreeman | 10:0bdfd342f393 | 58 | clicore (lococom); |
JonFreeman | 10:0bdfd342f393 | 59 | } |
JonFreeman | 10:0bdfd342f393 | 60 | return globflag; // Returns false on timeout |
JonFreeman | 10:0bdfd342f393 | 61 | } |
JonFreeman | 10:0bdfd342f393 | 62 | |
JonFreeman | 10:0bdfd342f393 | 63 | void make_option_bytes_from_dbls (double * src, char * dest) { // cli has sorted text string into array of doubles |
JonFreeman | 10:0bdfd342f393 | 64 | for (int i = 0; i < numof_eeprom_options; i++) { // Turn this to string of mode bytes as stored in eeprom |
JonFreeman | 10:0bdfd342f393 | 65 | dest[i] = (char)src[i]; |
JonFreeman | 10:0bdfd342f393 | 66 | } |
JonFreeman | 10:0bdfd342f393 | 67 | } |
JonFreeman | 10:0bdfd342f393 | 68 | |
JonFreeman | 10:0bdfd342f393 | 69 | char * make_str_from_option_bytes (char * src, char * dest) { // Use for easy readback to pc |
JonFreeman | 10:0bdfd342f393 | 70 | dest[0] = 0; // string null terminator |
JonFreeman | 10:0bdfd342f393 | 71 | for (int i = 0; i < numof_eeprom_options; i++) { |
JonFreeman | 10:0bdfd342f393 | 72 | if (i) |
JonFreeman | 10:0bdfd342f393 | 73 | strcat (dest, " "); // single space between outputs, no leading or trailing space |
JonFreeman | 10:0bdfd342f393 | 74 | sprintf (dest + strlen(dest), "%d", (int)src[i]); |
JonFreeman | 10:0bdfd342f393 | 75 | } |
JonFreeman | 10:0bdfd342f393 | 76 | return dest; |
JonFreeman | 10:0bdfd342f393 | 77 | } |
JonFreeman | 10:0bdfd342f393 | 78 | |
JonFreeman | 10:0bdfd342f393 | 79 | void buggertest (char * src) { // src is string of option bytes read from eeprom |
JonFreeman | 10:0bdfd342f393 | 80 | |
JonFreeman | 10:0bdfd342f393 | 81 | } |
JonFreeman | 10:0bdfd342f393 | 82 | void mode_cmd (struct parameters & a) |
JonFreeman | 10:0bdfd342f393 | 83 | { |
JonFreeman | 10:0bdfd342f393 | 84 | int error = 0, q; |
JonFreeman | 10:0bdfd342f393 | 85 | // bool valid_params = false; |
JonFreeman | 10:0bdfd342f393 | 86 | pc.printf ("At mode_cmd, numof parameters found=%d : numof parameters required=%d\r\n", a.numof_dbls, numof_eeprom_options); |
JonFreeman | 10:0bdfd342f393 | 87 | for (int i = 0; i < numof_eeprom_options; i++) { |
JonFreeman | 10:0bdfd342f393 | 88 | q = (int)a.dbl[i]; |
JonFreeman | 10:0bdfd342f393 | 89 | if (i == ID) |
JonFreeman | 10:0bdfd342f393 | 90 | q |= '0'; |
JonFreeman | 10:0bdfd342f393 | 91 | // badf = false; |
JonFreeman | 10:0bdfd342f393 | 92 | if (q < option_list[i].min || q > option_list[i].max) { |
JonFreeman | 10:0bdfd342f393 | 93 | // badf = true; |
JonFreeman | 10:0bdfd342f393 | 94 | error++; |
JonFreeman | 10:0bdfd342f393 | 95 | } |
JonFreeman | 10:0bdfd342f393 | 96 | // pc.printf ("Parameter %s, entered %d, %s\r\n",option_list[i].t , q, badf ? "Bad":"Good"); |
JonFreeman | 10:0bdfd342f393 | 97 | } |
JonFreeman | 10:0bdfd342f393 | 98 | if (a.numof_dbls == numof_eeprom_options && !error) { // correct num of options presented, proceed to blow eeproms |
JonFreeman | 10:0bdfd342f393 | 99 | pc.printf ("Congrats - looks like valid input!!\r\n"); |
JonFreeman | 10:0bdfd342f393 | 100 | // sprintf (txt, "%cmode",(int)a.dbl[ID]); |
JonFreeman | 10:0bdfd342f393 | 101 | com.putc ((int)a.dbl[ID]); // have already checked is valid in range |
JonFreeman | 10:0bdfd342f393 | 102 | com.printf ("mode"); |
JonFreeman | 10:0bdfd342f393 | 103 | for (int i = 0; i < numof_eeprom_options; i++) |
JonFreeman | 10:0bdfd342f393 | 104 | com.printf (" %d", (int)a.dbl[i]); |
JonFreeman | 10:0bdfd342f393 | 105 | com.putc ('\r'); |
JonFreeman | 10:0bdfd342f393 | 106 | pc.printf ("Have sent mode parameters down to %d\r\n", (int)a.dbl[ID]); |
JonFreeman | 10:0bdfd342f393 | 107 | } |
JonFreeman | 10:0bdfd342f393 | 108 | else { // report only |
JonFreeman | 10:0bdfd342f393 | 109 | } |
JonFreeman | 10:0bdfd342f393 | 110 | } |
JonFreeman | 10:0bdfd342f393 | 111 | |
JonFreeman | 4:67478861c670 | 112 | |
JonFreeman | 4:67478861c670 | 113 | void menucmd (struct parameters & a); |
JonFreeman | 4:67478861c670 | 114 | |
JonFreeman | 4:67478861c670 | 115 | void kd_cmd (struct parameters & a) // kick the watchdog |
JonFreeman | 4:67478861c670 | 116 | { |
JonFreeman | 4:67478861c670 | 117 | } |
JonFreeman | 4:67478861c670 | 118 | |
JonFreeman | 10:0bdfd342f393 | 119 | void uptem_cmd (struct parameters & a) // get temperature reading from bogie |
JonFreeman | 10:0bdfd342f393 | 120 | { |
JonFreeman | 10:0bdfd342f393 | 121 | int bog_id = (int)a.dbl[0] | '0'; |
JonFreeman | 10:0bdfd342f393 | 122 | int temp = (int)a.dbl[1]; |
JonFreeman | 10:0bdfd342f393 | 123 | pc.printf ("Read Temp %d from bogie id %d\r\n", temp, bog_id); |
JonFreeman | 10:0bdfd342f393 | 124 | } |
JonFreeman | 10:0bdfd342f393 | 125 | |
JonFreeman | 10:0bdfd342f393 | 126 | void gbb_cmd (struct parameters & a) // handling bogie response to get bogie constants - bogie returns as string of nums in hex format |
JonFreeman | 10:0bdfd342f393 | 127 | { // decode bogie bytes and place in global array 'globdest', setting global 'globflag' when done. Code issuing 'gbb' deals with timeout |
JonFreeman | 10:0bdfd342f393 | 128 | pc.printf ("Arrived at gbb_cmd\r\n"); |
JonFreeman | 10:0bdfd342f393 | 129 | if (globdest == 0) { |
JonFreeman | 10:0bdfd342f393 | 130 | pc.printf ("Fatal null ptr in gbb_cmd\r\n"); |
JonFreeman | 10:0bdfd342f393 | 131 | return; |
JonFreeman | 10:0bdfd342f393 | 132 | } |
JonFreeman | 10:0bdfd342f393 | 133 | pc.printf ("Responding to gbb back from bogie\r\n"); |
JonFreeman | 10:0bdfd342f393 | 134 | for (int i = 0; i < numof_eeprom_options; i++) { |
JonFreeman | 10:0bdfd342f393 | 135 | globdest[i] = (char)a.dbl[i]; |
JonFreeman | 10:0bdfd342f393 | 136 | } |
JonFreeman | 10:0bdfd342f393 | 137 | globflag = true; |
JonFreeman | 10:0bdfd342f393 | 138 | } |
JonFreeman | 10:0bdfd342f393 | 139 | |
JonFreeman | 10:0bdfd342f393 | 140 | void upbc_cmd (struct parameters & a) // get bogie constants - wheel dia, motor pinion, wheel gear ** SUPERCEDED BY 'gbb' |
JonFreeman | 10:0bdfd342f393 | 141 | { |
JonFreeman | 10:0bdfd342f393 | 142 | int bog_id = (int)a.dbl[0] | '0'; |
JonFreeman | 10:0bdfd342f393 | 143 | int wheeldia = (int)a.dbl[1]; |
JonFreeman | 10:0bdfd342f393 | 144 | int motorpin = (int)a.dbl[2]; |
JonFreeman | 10:0bdfd342f393 | 145 | int wheelgear = (int)a.dbl[3]; |
JonFreeman | 10:0bdfd342f393 | 146 | pc.printf ("Read bogie constants from bogie id %d,wheel dia %d, motorpin %d, wheel gear %d\r\n", bog_id, wheeldia, motorpin, wheelgear); |
JonFreeman | 10:0bdfd342f393 | 147 | for (int i = 0; i < MAX_BOGIES; i++) { |
JonFreeman | 10:0bdfd342f393 | 148 | if (detected_bogie_options.bogie[i].p[ID] == bog_id) { |
JonFreeman | 10:0bdfd342f393 | 149 | detected_bogie_options.bogie[i].p[WHEELDIA] = wheeldia; |
JonFreeman | 10:0bdfd342f393 | 150 | detected_bogie_options.bogie[i].p[MOTPIN] = motorpin; |
JonFreeman | 10:0bdfd342f393 | 151 | detected_bogie_options.bogie[i].p[WHEELGEAR] = wheelgear; |
JonFreeman | 10:0bdfd342f393 | 152 | pc.printf ("Stored in detected_bogie_options [%d]\r\n", i); |
JonFreeman | 10:0bdfd342f393 | 153 | i = MAX_BOGIES; |
JonFreeman | 10:0bdfd342f393 | 154 | } |
JonFreeman | 10:0bdfd342f393 | 155 | } |
JonFreeman | 10:0bdfd342f393 | 156 | } |
JonFreeman | 10:0bdfd342f393 | 157 | |
JonFreeman | 5:21a8ac83142c | 158 | extern void rpm_push (struct parameters & a) ; |
JonFreeman | 5:21a8ac83142c | 159 | void uprpm_cmd (struct parameters & a) // controller rec'd rpm from driver boards |
JonFreeman | 5:21a8ac83142c | 160 | { |
JonFreeman | 5:21a8ac83142c | 161 | rpm_push (a); |
JonFreeman | 7:3b1f44cd4735 | 162 | // a.gp_i ^= 1; // toggle lsb |
JonFreeman | 5:21a8ac83142c | 163 | } |
JonFreeman | 5:21a8ac83142c | 164 | |
JonFreeman | 5:21a8ac83142c | 165 | void uprdi_cmd (struct parameters & a) // controller rec'd rpm from driver boards |
JonFreeman | 5:21a8ac83142c | 166 | { |
JonFreeman | 5:21a8ac83142c | 167 | // pc.printf ("RPM %d %d\r\n", (int)a.dbl[0], (int)a.dbl[1]); |
JonFreeman | 5:21a8ac83142c | 168 | } |
JonFreeman | 5:21a8ac83142c | 169 | |
JonFreeman | 4:67478861c670 | 170 | |
JonFreeman | 4:67478861c670 | 171 | /*void who_cmd (struct parameters & a) |
JonFreeman | 4:67478861c670 | 172 | { |
JonFreeman | 4:67478861c670 | 173 | int i = I_Am (); |
JonFreeman | 4:67478861c670 | 174 | if (I_Am() == a.target_unit) |
JonFreeman | 4:67478861c670 | 175 | pc.printf ("Hi there, I am %c\r\n", a.target_unit); |
JonFreeman | 4:67478861c670 | 176 | }*/ |
JonFreeman | 4:67478861c670 | 177 | |
JonFreeman | 4:67478861c670 | 178 | struct kb_command { |
JonFreeman | 4:67478861c670 | 179 | const char * cmd_word; // points to text e.g. "menu" |
JonFreeman | 4:67478861c670 | 180 | const char * explan; |
JonFreeman | 4:67478861c670 | 181 | void (*f)(struct parameters &); // points to function |
JonFreeman | 4:67478861c670 | 182 | } ; |
JonFreeman | 4:67478861c670 | 183 | |
JonFreeman | 4:67478861c670 | 184 | struct kb_command const command_list[] = { |
JonFreeman | 4:67478861c670 | 185 | {"ls", "Lists available commands", menucmd}, |
JonFreeman | 4:67478861c670 | 186 | {"?", "Lists available commands, same as ls", menucmd}, |
JonFreeman | 4:67478861c670 | 187 | // {"fw", "forward", fw_cmd}, |
JonFreeman | 4:67478861c670 | 188 | // {"re", "reverse", re_cmd}, |
JonFreeman | 4:67478861c670 | 189 | // {"rb", "regen brake 0 to 99 %", rb_cmd}, |
JonFreeman | 4:67478861c670 | 190 | // {"hb", "hand brake", hb_cmd}, |
JonFreeman | 4:67478861c670 | 191 | // {"v", "set motors V percent RANGE 0 to 100", v_cmd}, |
JonFreeman | 4:67478861c670 | 192 | // {"i", "set motors I percent RANGE 0 to 100", i_cmd}, |
JonFreeman | 4:67478861c670 | 193 | // {"vi", "set motors V and I percent RANGE 0 to 100", vi_cmd}, |
JonFreeman | 4:67478861c670 | 194 | // {"who", "search for connected units, e.g. 3who returs 'Hi there' if found", who_cmd}, |
JonFreeman | 10:0bdfd342f393 | 195 | {"mode", "read or set params in eeprom", mode_cmd}, |
JonFreeman | 4:67478861c670 | 196 | // {"erase", "set eeprom contents to all 0xff", erase_cmd}, |
JonFreeman | 4:67478861c670 | 197 | {"kd", "kick the dog", kd_cmd}, |
JonFreeman | 10:0bdfd342f393 | 198 | // {"rpm", "rpm reading from 2 motors", uprpm_cmd}, |
JonFreeman | 4:67478861c670 | 199 | {"nu", "do nothing", null_cmd}, |
JonFreeman | 4:67478861c670 | 200 | }; |
JonFreeman | 4:67478861c670 | 201 | |
JonFreeman | 4:67478861c670 | 202 | const int numof_menu_items = sizeof(command_list) / sizeof(kb_command); |
JonFreeman | 4:67478861c670 | 203 | void menucmd (struct parameters & a) |
JonFreeman | 4:67478861c670 | 204 | { |
JonFreeman | 5:21a8ac83142c | 205 | // struct kb_command const * cl = command_list; |
JonFreeman | 5:21a8ac83142c | 206 | pc.printf("\r\n\nTS_2018 Locomotive Touch Screen Controller\r\nAt menucmd function - listing commands:-\r\n"); |
JonFreeman | 4:67478861c670 | 207 | for(int i = 0; i < numof_menu_items; i++) |
JonFreeman | 4:67478861c670 | 208 | pc.printf("[%s]\t\t%s\r\n", command_list[i].cmd_word, command_list[i].explan); |
JonFreeman | 4:67478861c670 | 209 | pc.printf("End of List of Commands\r\n"); |
JonFreeman | 4:67478861c670 | 210 | } |
JonFreeman | 4:67478861c670 | 211 | |
JonFreeman | 5:21a8ac83142c | 212 | struct kb_command const loco_command_list[] = { |
JonFreeman | 5:21a8ac83142c | 213 | {"ls", "Lists available commands", menucmd}, |
JonFreeman | 5:21a8ac83142c | 214 | {"?", "Lists available commands, same as ls", menucmd}, |
JonFreeman | 5:21a8ac83142c | 215 | {"rpm", "rpm reading from 2 motors", uprpm_cmd}, |
JonFreeman | 5:21a8ac83142c | 216 | {"rdi", "rpm reading from 2 motors", uprdi_cmd}, |
JonFreeman | 10:0bdfd342f393 | 217 | {"tem", "report temperature", uptem_cmd}, |
JonFreeman | 10:0bdfd342f393 | 218 | {"bc", "bogie constants - wheel dia, motor pinion, wheel gear", upbc_cmd}, |
JonFreeman | 10:0bdfd342f393 | 219 | {"gbb", "get bogie bytes, all of them", gbb_cmd}, |
JonFreeman | 5:21a8ac83142c | 220 | {"nu", "do nothing", null_cmd}, |
JonFreeman | 5:21a8ac83142c | 221 | } ; |
JonFreeman | 5:21a8ac83142c | 222 | |
JonFreeman | 5:21a8ac83142c | 223 | |
JonFreeman | 5:21a8ac83142c | 224 | /*struct parameters { |
JonFreeman | 5:21a8ac83142c | 225 | struct kb_command const * clist; |
JonFreeman | 5:21a8ac83142c | 226 | char cmd_line[120]; |
JonFreeman | 5:21a8ac83142c | 227 | char * cmd_line_ptr; |
JonFreeman | 5:21a8ac83142c | 228 | int32_t position_in_list, numof_dbls, target_unit, numof_menu_items; |
JonFreeman | 5:21a8ac83142c | 229 | double dbl[MAX_PARAMS]; |
JonFreeman | 5:21a8ac83142c | 230 | bool respond; |
JonFreeman | 5:21a8ac83142c | 231 | } ; |
JonFreeman | 5:21a8ac83142c | 232 | */ |
JonFreeman | 5:21a8ac83142c | 233 | int getc (int which_port) |
JonFreeman | 5:21a8ac83142c | 234 | { |
JonFreeman | 5:21a8ac83142c | 235 | if (which_port == 0) |
JonFreeman | 5:21a8ac83142c | 236 | return pc.getc (); |
JonFreeman | 5:21a8ac83142c | 237 | if (which_port == 1) |
JonFreeman | 5:21a8ac83142c | 238 | return com.getc (); |
JonFreeman | 5:21a8ac83142c | 239 | return -1; |
JonFreeman | 5:21a8ac83142c | 240 | } |
JonFreeman | 5:21a8ac83142c | 241 | int readable (int which_port) |
JonFreeman | 5:21a8ac83142c | 242 | { |
JonFreeman | 5:21a8ac83142c | 243 | if (which_port == 0) |
JonFreeman | 5:21a8ac83142c | 244 | return pc.readable (); |
JonFreeman | 5:21a8ac83142c | 245 | if (which_port == 1) |
JonFreeman | 5:21a8ac83142c | 246 | return com.readable (); |
JonFreeman | 5:21a8ac83142c | 247 | return -1; |
JonFreeman | 5:21a8ac83142c | 248 | |
JonFreeman | 5:21a8ac83142c | 249 | } |
JonFreeman | 5:21a8ac83142c | 250 | |
JonFreeman | 5:21a8ac83142c | 251 | void setup_pccom () |
JonFreeman | 5:21a8ac83142c | 252 | { |
JonFreeman | 5:21a8ac83142c | 253 | pccom.clist = command_list; |
JonFreeman | 5:21a8ac83142c | 254 | pccom.numof_menu_items = sizeof(command_list) / sizeof(kb_command); |
JonFreeman | 5:21a8ac83142c | 255 | pccom.com_no = 0; |
JonFreeman | 5:21a8ac83142c | 256 | pccom.cl_index = 0; |
JonFreeman | 5:21a8ac83142c | 257 | pccom.gp_i = 0; // general puropse integer, not used to 30/4/2018 |
JonFreeman | 5:21a8ac83142c | 258 | } |
JonFreeman | 5:21a8ac83142c | 259 | |
JonFreeman | 5:21a8ac83142c | 260 | void setup_lococom () |
JonFreeman | 5:21a8ac83142c | 261 | { |
JonFreeman | 5:21a8ac83142c | 262 | lococom.clist = loco_command_list; |
JonFreeman | 5:21a8ac83142c | 263 | lococom.numof_menu_items = sizeof(loco_command_list) / sizeof(kb_command); |
JonFreeman | 5:21a8ac83142c | 264 | lococom.com_no = 1; |
JonFreeman | 5:21a8ac83142c | 265 | lococom.cl_index = 0; |
JonFreeman | 5:21a8ac83142c | 266 | lococom.gp_i = 0; // general puropse integer, toggles 0 / 1 to best guess source of rpm |
JonFreeman | 5:21a8ac83142c | 267 | } |
JonFreeman | 5:21a8ac83142c | 268 | |
JonFreeman | 5:21a8ac83142c | 269 | void clicore (struct parameters & a) |
JonFreeman | 5:21a8ac83142c | 270 | { |
JonFreeman | 5:21a8ac83142c | 271 | int ch; |
JonFreeman | 5:21a8ac83142c | 272 | char * pEnd; |
JonFreeman | 5:21a8ac83142c | 273 | while (readable(a.com_no)) { |
JonFreeman | 5:21a8ac83142c | 274 | ch = getc (a.com_no); |
JonFreeman | 10:0bdfd342f393 | 275 | if(ch != '\r') { // was this the 'Enter' key? |
JonFreeman | 10:0bdfd342f393 | 276 | // if(ch != '\r' && ch != '\n') // was this the 'Enter' key? |
JonFreeman | 10:0bdfd342f393 | 277 | // if (ch != '\n') a.cmd_line[a.cl_index++] = ch; // added char to command being assembled |
JonFreeman | 5:21a8ac83142c | 278 | a.cmd_line[a.cl_index++] = ch; // added char to command being assembled |
JonFreeman | 10:0bdfd342f393 | 279 | } |
JonFreeman | 5:21a8ac83142c | 280 | else { // key was CR, may or may not be command to lookup |
JonFreeman | 5:21a8ac83142c | 281 | a.target_unit = BROADCAST; // Broadcast |
JonFreeman | 5:21a8ac83142c | 282 | a.cmd_line_ptr = a.cmd_line; |
JonFreeman | 5:21a8ac83142c | 283 | a.cmd_line[a.cl_index] = 0; // null terminate command string |
JonFreeman | 5:21a8ac83142c | 284 | if(a.cl_index) { // If have got some chars to lookup |
JonFreeman | 5:21a8ac83142c | 285 | int i, wrdlen; |
JonFreeman | 5:21a8ac83142c | 286 | if (isdigit(a.cmd_line[0])) { // Look for command with prefix digit |
JonFreeman | 5:21a8ac83142c | 287 | a.cmd_line_ptr++; // point past identified digit prefix |
JonFreeman | 5:21a8ac83142c | 288 | a.target_unit = a.cmd_line[0]; // '0' to '9' |
JonFreeman | 5:21a8ac83142c | 289 | //pc.printf ("Got prefix %c\r\n", cmd_line[0]); |
JonFreeman | 5:21a8ac83142c | 290 | } |
JonFreeman | 5:21a8ac83142c | 291 | for (i = 0; i < a.numof_menu_items; i++) { // Look for input match in command list |
JonFreeman | 5:21a8ac83142c | 292 | wrdlen = strlen(a.clist[i].cmd_word); |
JonFreeman | 5:21a8ac83142c | 293 | if(strncmp(a.clist[i].cmd_word, a.cmd_line_ptr, wrdlen) == 0 && !isalpha(a.cmd_line_ptr[wrdlen])) { // If match found |
JonFreeman | 5:21a8ac83142c | 294 | for (int k = 0; k < MAX_PARAMS; k++) { |
JonFreeman | 5:21a8ac83142c | 295 | a.dbl[k] = 0.0; |
JonFreeman | 5:21a8ac83142c | 296 | } |
JonFreeman | 5:21a8ac83142c | 297 | a.position_in_list = i; |
JonFreeman | 5:21a8ac83142c | 298 | a.numof_dbls = 0; |
JonFreeman | 5:21a8ac83142c | 299 | pEnd = a.cmd_line_ptr + wrdlen; |
JonFreeman | 5:21a8ac83142c | 300 | while (*pEnd) { // Assemble all numerics as doubles |
JonFreeman | 5:21a8ac83142c | 301 | a.dbl[a.numof_dbls++] = strtod (pEnd, &pEnd); |
JonFreeman | 5:21a8ac83142c | 302 | while (*pEnd && !isdigit(*pEnd) && '-' != *pEnd && '+' != *pEnd) { |
JonFreeman | 5:21a8ac83142c | 303 | pEnd++; |
JonFreeman | 5:21a8ac83142c | 304 | } |
JonFreeman | 5:21a8ac83142c | 305 | } |
JonFreeman | 5:21a8ac83142c | 306 | //pc.printf ("\r\n"); // Not allowed as many may output this. |
JonFreeman | 5:21a8ac83142c | 307 | //for (int k = 0; k < param_block.numof_dbls; k++) |
JonFreeman | 5:21a8ac83142c | 308 | // pc.printf ("Read %.3f\r\n", param_block.dbl[k]); |
JonFreeman | 5:21a8ac83142c | 309 | // param_block.times[i] = clock(); |
JonFreeman | 5:21a8ac83142c | 310 | // if ((param_block.target_unit == BROADCAST) && (I_Am() == '0')) |
JonFreeman | 5:21a8ac83142c | 311 | // param_block.respond = true; |
JonFreeman | 5:21a8ac83142c | 312 | a.clist[i].f(a); // execute command |
JonFreeman | 5:21a8ac83142c | 313 | i = a.numof_menu_items + 1; // to exit for loop |
JonFreeman | 5:21a8ac83142c | 314 | } // end of match found |
JonFreeman | 5:21a8ac83142c | 315 | } // End of for numof_menu_items |
JonFreeman | 5:21a8ac83142c | 316 | if(i == a.numof_menu_items) |
JonFreeman | 5:21a8ac83142c | 317 | pc.printf("No Match Found for CMD [%s]\r\n", a.cmd_line); |
JonFreeman | 5:21a8ac83142c | 318 | } // End of If have got some chars to lookup |
JonFreeman | 5:21a8ac83142c | 319 | //pc.printf("\r\n>"); |
JonFreeman | 5:21a8ac83142c | 320 | a.cl_index = 0; |
JonFreeman | 5:21a8ac83142c | 321 | } // End of else key was CR, may or may not be command to lookup |
JonFreeman | 5:21a8ac83142c | 322 | } // End of while (pc.readable()) |
JonFreeman | 5:21a8ac83142c | 323 | } |
JonFreeman | 5:21a8ac83142c | 324 | |
JonFreeman | 4:67478861c670 | 325 | |
JonFreeman | 4:67478861c670 | 326 | /* |
JonFreeman | 4:67478861c670 | 327 | New - March 2018 |
JonFreeman | 4:67478861c670 | 328 | Using opto isolated serial port, paralleled up using same pair to multiple boards running this code. |
JonFreeman | 4:67478861c670 | 329 | New feature - commands have optional prefix digit 0-9 indicating which unit message is addressed to. |
JonFreeman | 4:67478861c670 | 330 | Commands without prefix digit - broadcast to all units, none to respond. |
JonFreeman | 4:67478861c670 | 331 | Only units recognising its address from prefix digit may respond. This avoids bus contention. |
JonFreeman | 4:67478861c670 | 332 | But for BROADCAST commands, '0' may respond on behalf of the group |
JonFreeman | 4:67478861c670 | 333 | */ |
JonFreeman | 4:67478861c670 | 334 | //void command_line_interpreter (void const *argument) |
JonFreeman | 5:21a8ac83142c | 335 | /*void command_line_interpreter () |
JonFreeman | 4:67478861c670 | 336 | { |
JonFreeman | 5:21a8ac83142c | 337 | const int MAX_CMD_LEN = 120; |
JonFreeman | 5:21a8ac83142c | 338 | static char cmd_line[MAX_CMD_LEN + 4]; |
JonFreeman | 5:21a8ac83142c | 339 | static int cl_index = 0; |
JonFreeman | 5:21a8ac83142c | 340 | int ch; |
JonFreeman | 5:21a8ac83142c | 341 | char * pEnd, * cmd_line_ptr; |
JonFreeman | 5:21a8ac83142c | 342 | static struct parameters param_block ; |
JonFreeman | 5:21a8ac83142c | 343 | while (pc.readable()) { |
JonFreeman | 5:21a8ac83142c | 344 | ch = pc.getc(); |
JonFreeman | 5:21a8ac83142c | 345 | // if (cl_index > MAX_CMD_LEN) { // trap out stupidly long command lines |
JonFreeman | 5:21a8ac83142c | 346 | // pc.printf ("Error!! Stupidly long cmd line\r\n"); |
JonFreeman | 5:21a8ac83142c | 347 | // cl_index = 0; |
JonFreeman | 5:21a8ac83142c | 348 | // } |
JonFreeman | 5:21a8ac83142c | 349 | if(ch != '\r') // was this the 'Enter' key? |
JonFreeman | 5:21a8ac83142c | 350 | cmd_line[cl_index++] = ch; // added char to command being assembled |
JonFreeman | 5:21a8ac83142c | 351 | else { // key was CR, may or may not be command to lookup |
JonFreeman | 5:21a8ac83142c | 352 | param_block.target_unit = BROADCAST; // Broadcast |
JonFreeman | 5:21a8ac83142c | 353 | cmd_line_ptr = cmd_line; |
JonFreeman | 5:21a8ac83142c | 354 | cmd_line[cl_index] = 0; // null terminate command string |
JonFreeman | 5:21a8ac83142c | 355 | if(cl_index) { // If have got some chars to lookup |
JonFreeman | 5:21a8ac83142c | 356 | int i, wrdlen; |
JonFreeman | 5:21a8ac83142c | 357 | if (isdigit(cmd_line[0])) { // Look for command with prefix digit |
JonFreeman | 5:21a8ac83142c | 358 | cmd_line_ptr++; // point past identified digit prefix |
JonFreeman | 5:21a8ac83142c | 359 | param_block.target_unit = cmd_line[0]; // '0' to '9' |
JonFreeman | 5:21a8ac83142c | 360 | //pc.printf ("Got prefix %c\r\n", cmd_line[0]); |
JonFreeman | 5:21a8ac83142c | 361 | } |
JonFreeman | 5:21a8ac83142c | 362 | for (i = 0; i < numof_menu_items; i++) { // Look for input match in command list |
JonFreeman | 5:21a8ac83142c | 363 | wrdlen = strlen(command_list[i].cmd_word); |
JonFreeman | 5:21a8ac83142c | 364 | if(strncmp(command_list[i].cmd_word, cmd_line_ptr, wrdlen) == 0 && !isalpha(cmd_line_ptr[wrdlen])) { // If match found |
JonFreeman | 5:21a8ac83142c | 365 | for (int k = 0; k < MAX_PARAMS; k++) { |
JonFreeman | 5:21a8ac83142c | 366 | param_block.dbl[k] = 0.0; |
JonFreeman | 5:21a8ac83142c | 367 | } |
JonFreeman | 5:21a8ac83142c | 368 | param_block.position_in_list = i; |
JonFreeman | 5:21a8ac83142c | 369 | param_block.numof_dbls = 0; |
JonFreeman | 5:21a8ac83142c | 370 | pEnd = cmd_line_ptr + wrdlen; |
JonFreeman | 5:21a8ac83142c | 371 | while (*pEnd) { // Assemble all numerics as doubles |
JonFreeman | 5:21a8ac83142c | 372 | param_block.dbl[param_block.numof_dbls++] = strtod (pEnd, &pEnd); |
JonFreeman | 5:21a8ac83142c | 373 | while (*pEnd && !isdigit(*pEnd) && '-' != *pEnd && '+' != *pEnd) { |
JonFreeman | 5:21a8ac83142c | 374 | pEnd++; |
JonFreeman | 4:67478861c670 | 375 | } |
JonFreeman | 5:21a8ac83142c | 376 | } |
JonFreeman | 5:21a8ac83142c | 377 | //pc.printf ("\r\n"); // Not allowed as many may output this. |
JonFreeman | 5:21a8ac83142c | 378 | //for (int k = 0; k < param_block.numof_dbls; k++) |
JonFreeman | 5:21a8ac83142c | 379 | // pc.printf ("Read %.3f\r\n", param_block.dbl[k]); |
JonFreeman | 4:67478861c670 | 380 | // param_block.times[i] = clock(); |
JonFreeman | 4:67478861c670 | 381 | // if ((param_block.target_unit == BROADCAST) && (I_Am() == '0')) |
JonFreeman | 4:67478861c670 | 382 | // param_block.respond = true; |
JonFreeman | 5:21a8ac83142c | 383 | command_list[i].f(param_block); // execute command |
JonFreeman | 5:21a8ac83142c | 384 | i = numof_menu_items + 1; // to exit for loop |
JonFreeman | 5:21a8ac83142c | 385 | } // end of match found |
JonFreeman | 5:21a8ac83142c | 386 | } // End of for numof_menu_items |
JonFreeman | 5:21a8ac83142c | 387 | if(i == numof_menu_items) |
JonFreeman | 5:21a8ac83142c | 388 | pc.printf("No Match Found for CMD [%s]\r\n", cmd_line); |
JonFreeman | 5:21a8ac83142c | 389 | } // End of If have got some chars to lookup |
JonFreeman | 5:21a8ac83142c | 390 | //pc.printf("\r\n>"); |
JonFreeman | 5:21a8ac83142c | 391 | cl_index = 0; |
JonFreeman | 5:21a8ac83142c | 392 | } // End of else key was CR, may or may not be command to lookup |
JonFreeman | 5:21a8ac83142c | 393 | } // End of while (pc.readable()) |
JonFreeman | 4:67478861c670 | 394 | // Thread::wait(20); // Using RTOS on this project |
JonFreeman | 4:67478861c670 | 395 | // } |
JonFreeman | 4:67478861c670 | 396 | } |
JonFreeman | 5:21a8ac83142c | 397 | */ |
JonFreeman | 4:67478861c670 | 398 |