Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
command.cpp
00001 //------------------------------------------------------------------------------- 00002 // 00003 // Treehouse Inc. 00004 // Colorado Springs, Colorado 00005 // 00006 // Copyright (c) 2016 by Treehouse Designs Inc. 00007 // 00008 // This code is the property of Treehouse, Inc. (Treehouse) 00009 // and may not be redistributed in any form without prior written 00010 // permission of the copyright holder, Treehouse. 00011 // 00012 // The above copyright notice and this permission notice shall be included in 00013 // all copies or substantial portions of the Software. 00014 // 00015 // 00016 //------------------------------------------------------------------------------- 00017 // 00018 // REVISION HISTORY: 00019 // 00020 // $Author: $ 00021 // $Rev: $ 00022 // $Date: $ 00023 // $URL: $ 00024 // 00025 //------------------------------------------------------------------------------- 00026 00027 #include "mbed.h" 00028 #include "string.h" 00029 #include "stdio.h" 00030 #include "stdlib.h" 00031 #include "ctype.h" 00032 #include "serial.h" 00033 #include "globals.h" 00034 #include "math.h" 00035 #include "parameters.h" 00036 #include "all_io.h" 00037 //#include "calibrate.h" 00038 #include "boards.h" 00039 #include "menu.h" 00040 #include "command.h" 00041 00042 unsigned int boardsActive = ALLON; 00043 unsigned int boardMults = 32; 00044 unsigned int commandData; 00045 extern unsigned short my12; 00046 00047 /************* FILE SCOPE VARIABLES ************************/ 00048 char setvalue = FALSE; 00049 int endOfCommand = 0; 00050 int commandError = 0; 00051 int menuLevel = LEVEL_MAIN; 00052 int readback = 0; 00053 00054 /************************************************************ 00055 * Routine: getDelimiter 00056 * Input: none 00057 * Output: none 00058 * Description: 00059 * searches for a delimiter and moves the buffer location 00060 * to be just past it 00061 * 00062 **************************************************************/ 00063 void getDelimiter(void) 00064 { 00065 ++bufloc; 00066 00067 while ((rxbuf[bufloc] != ' ') && 00068 (rxbuf[bufloc] != ',') && 00069 (rxbuf[bufloc] != '=') && 00070 (rxbuf[bufloc] != 0 )) 00071 { 00072 bufloc++; 00073 } 00074 } 00075 00076 /************************************************************ 00077 * Routine: gethex 00078 * Input: hex character 00079 * Returns: hex integer 00080 * Description: 00081 * Converts a hex character to a value 00082 **************************************************************/ 00083 char gethex(char val) 00084 { 00085 int retval; 00086 switch(val) 00087 { 00088 case '0': 00089 retval = 0; 00090 break; 00091 case '1': 00092 retval = 1; 00093 break; 00094 case '2': 00095 retval = 2; 00096 break; 00097 case '3': 00098 retval = 3; 00099 break; 00100 case '4': 00101 retval = 4; 00102 break; 00103 case '5': 00104 retval = 5; 00105 break; 00106 case '6': 00107 retval = 6; 00108 break; 00109 case '7': 00110 retval = 7; 00111 break; 00112 case '8': 00113 retval = 8; 00114 break; 00115 case '9': 00116 retval = 9; 00117 break; 00118 case 'A': 00119 retval = 10; 00120 break; 00121 case 'B': 00122 retval = 11; 00123 break; 00124 case 'C': 00125 retval = 12; 00126 break; 00127 case 'D': 00128 retval = 13; 00129 break; 00130 case 'E': 00131 retval = 14; 00132 break; 00133 case 'F': 00134 retval = 15; 00135 break; 00136 default: 00137 retval = 0; 00138 break; 00139 00140 } 00141 return retval; 00142 } 00143 00144 /************************************************************ 00145 * Routine: showfval 00146 * Input: setval (GET or SET) 00147 * value (float value to display) 00148 * Output: none 00149 * Description: 00150 * Sends a floating point number (value) over the serial port 00151 * if it is being retrieved (GET) 00152 * 00153 **************************************************************/ 00154 void showfval(char setval,float value) 00155 { 00156 if(!setval) 00157 { 00158 sprintf(strbuf," %4.9f",value); 00159 sendSerial(strbuf); 00160 } 00161 } 00162 00163 /************************************************************ 00164 * Routine: showival 00165 * Input: setval (GET or SET) 00166 * value (integer value to display) 00167 * Output: none 00168 * Description: 00169 * Sends an integer (value) over the serial port 00170 * if it is being retrieved (GET) 00171 * 00172 **************************************************************/ 00173 void showival(char setval, int value) 00174 { 00175 if(!setval) 00176 { 00177 sprintf(strbuf," %i",value); 00178 sendSerial(strbuf); 00179 } 00180 } 00181 /************************************************************ 00182 * Routine: showcval 00183 * Input: setval (GET or SET) 00184 * value (character to display) 00185 * Output: none 00186 * Description: 00187 * Sends a character over the serial port 00188 * if it is being retrieved (GET) 00189 * 00190 **************************************************************/ 00191 void showcval(char setval, int value) 00192 { 00193 if(!setval) 00194 { 00195 sprintf(strbuf," %c",(char)value); 00196 sendSerial(strbuf); 00197 } 00198 } 00199 00200 /************************************************************ 00201 * Routine: showlval 00202 * Input: setval (GET or SET) 00203 * value (integer value to display) 00204 * Output: none 00205 * Description: 00206 * Sends an long (value) over the serial port 00207 * if it is being retrieved (GET) 00208 * 00209 **************************************************************/ 00210 void showlval(char setval, long value) 00211 { 00212 if(!setval) 00213 { 00214 sprintf(strbuf," %ld",value); 00215 sendSerial(strbuf); 00216 } 00217 } 00218 00219 /************************************************************ 00220 * Routine: showuival 00221 * Input: setval (GET or SET) 00222 * value (integer value to display) 00223 * Output: none 00224 * Description: 00225 * Sends an unsigned int (value) over the serial port 00226 * if it is being retrieved (GET) 00227 * 00228 **************************************************************/ 00229 void showuival(char setval, unsigned int value) 00230 { 00231 if(!setval) 00232 { 00233 sprintf(strbuf," %u",value); 00234 sendSerial(strbuf); 00235 } 00236 } 00237 00238 /************************************************************ 00239 * Routine: showhval 00240 * Input: setval (GET or SET) 00241 * value (hex integeger value to display) 00242 * Output: none 00243 * Description: 00244 * Sends an integer (value) in hex over the serial port 00245 * if it is being retrieved (GET) 00246 * 00247 **************************************************************/ 00248 void showhval(char setval, int value) 00249 { 00250 if(!setval) 00251 { 00252 if(serialStatus.computer) 00253 sprintf(strbuf," %u",(unsigned int)value); 00254 else 00255 sprintf(strbuf," 0x%04x",value); 00256 sendSerial(strbuf); 00257 } 00258 } 00259 00260 00261 /************************************************************ 00262 * Routine: getival 00263 * Input: setval (GET or SET) 00264 * Returns: the value if it is being SET or 0 if it is a GET 00265 * Description: 00266 * Gets an integer from the serial port connection. 00267 * 00268 **************************************************************/ 00269 int getival(char setval) 00270 { 00271 if (setval) 00272 { 00273 return atoi(&rxbuf[++bufloc]); 00274 } 00275 00276 return 0; 00277 } 00278 00279 /************************************************************ 00280 * Routine: getcval 00281 * Input: setval (GET or SET) 00282 * Returns: the value if it is being SET or 0 if it is a GET 00283 * Description: 00284 * Gets an character from the serial port connection. 00285 * 00286 **************************************************************/ 00287 int getcval(char setval) 00288 { 00289 if(setval) 00290 { 00291 // skip one space 00292 ++bufloc; 00293 // skip spaces and the equals sign 00294 while((rxbuf[bufloc] == ' ') || (rxbuf[bufloc] == '=')) 00295 bufloc++; 00296 return rxbuf[bufloc++]; 00297 } 00298 else 00299 return 0; 00300 00301 } 00302 /************************************************************ 00303 * Routine: getlval 00304 * Input: setval (GET or SET) 00305 * Returns: the value if it is being SET or 0 if it is a GET 00306 * Description: 00307 * Gets an long from the serial port connection. 00308 * 00309 **************************************************************/ 00310 long getlval(char setval) 00311 { 00312 if(setval) 00313 return atol(&rxbuf[++bufloc]); 00314 else 00315 return 0; 00316 00317 } 00318 00319 /************************************************************ 00320 * Routine: getfval 00321 * Input: setval (GET or SET) 00322 * Returns: the value if it is being SET or 0 if it is a GET 00323 * Description: 00324 * Gets an float from the serial port connection. 00325 * 00326 **************************************************************/ 00327 float getfval(char setval) 00328 { 00329 if(setval) 00330 return atof(&rxbuf[++bufloc]); 00331 else 00332 return 0; 00333 } 00334 00335 /************************************************************ 00336 * Routine: validateEntry 00337 * Input: setval (GET or SET) 00338 * limlo -- low limit 00339 * limhi -- high limit 00340 * address -- address in eeprom to use 00341 * Returns: 0 if entry validates and is written 00342 * 1 if entry fails 00343 * Description: 00344 * Gets or sets a value in eeprom at the address but only 00345 * if it is between the limits will it write the value to 00346 * eeprom 00347 * 00348 **************************************************************/ 00349 int validateEntry(char setvalue, float limlo, float limhi, float *address) 00350 { 00351 float val; 00352 00353 if (setvalue) 00354 { 00355 val = getfval(SET); 00356 00357 if ((val >= limlo) && (val <= limhi)) 00358 { 00359 *address = val; 00360 } 00361 else 00362 { 00363 showRangeError(0, 0, val); 00364 return 0; 00365 } 00366 } 00367 else 00368 { 00369 val = *address; 00370 sprintf(strbuf, " %4.3f", val); 00371 sendSerial(strbuf); 00372 } 00373 00374 return 1; 00375 } 00376 00377 00378 /************************************************************ 00379 * Routine: validateEntry 00380 * Input: setval (GET or SET) 00381 * limlo -- low limit 00382 * limhi -- high limit 00383 * address -- address in eeprom to use 00384 * 00385 * Returns: FALSE if entry fails 00386 * TRUE if entry validates and is written 00387 * 00388 * Description: 00389 * Gets or sets a value in eeprom at the address but only 00390 * if it is between the limits will it write the value to 00391 * eeprom 00392 * 00393 **************************************************************/ 00394 int validateInt(char setvalue, int limlo, int limhi, int *address) 00395 { 00396 float val; 00397 00398 if (setvalue) 00399 { 00400 val = getfval(SET); 00401 00402 if ((val >= limlo) && (val <= limhi)) 00403 { 00404 *address = val; 00405 } 00406 else 00407 { 00408 showRangeError(1, val, 0); 00409 return FALSE; 00410 } 00411 } 00412 else 00413 { 00414 val = *address; 00415 sprintf(strbuf, " %4.0f", val); 00416 sendSerial(strbuf); 00417 } 00418 00419 return TRUE; 00420 } 00421 00422 00423 /************************************************************ 00424 * Routine: parseCommand 00425 * Input: setvalue (GET or SET), command buffer 00426 * Returns: none 00427 * Description: 00428 * parses a command and gets the commandstring 00429 **************************************************************/ 00430 void parseCommand(char setvalue, char *commandString) 00431 { 00432 int i, endofc; 00433 char store; 00434 00435 // Ignore any white space and the optional ';' character before the start of 00436 // the command string (any ']' character is from the last command so skip that, 00437 // too) 00438 while ((isspace(rxbuf[bufloc])) || (rxbuf[bufloc] == ';') || (rxbuf[bufloc] == ']')) 00439 { 00440 bufloc++; 00441 if ((rxbuf[bufloc] == 0x0D) || (rxbuf[bufloc] == 0)) break; 00442 } 00443 00444 if (setvalue) 00445 { 00446 // We need a value for SET so hitting the end is a problem 00447 if ((rxbuf[bufloc] == 0) || (rxbuf[bufloc] == 0x0D)) 00448 { 00449 commandError = 1; 00450 return; 00451 } 00452 } 00453 00454 // Find the end of the command string 00455 endofc = bufloc + 1; 00456 00457 // White space, '[' and '?' all terminate the command string 00458 while ((!isspace(rxbuf[endofc])) && (rxbuf[endofc] != '[') && (rxbuf[endofc] != '?')) 00459 { 00460 endofc++; 00461 // (As does hitting the end of rxbuf!) 00462 if ((rxbuf[endofc] == 0x0D) || (rxbuf[endofc] == 0)) break; 00463 } 00464 00465 // Save the character that marks the end of the command string 00466 store = rxbuf[endofc]; 00467 00468 // sprintf(strbuf, "store == %c\r\n", store); 00469 // sendSerial(strbuf); 00470 00471 // Command strings ending in '?' are readbacks 00472 readback = ((store == '?') ? 1 : 0); 00473 00474 // Set end to null character so string can now be copied 00475 rxbuf[endofc] = 0; 00476 00477 // Copy the command string into commandString 00478 char commandStringBuf[80]; 00479 char *tok; 00480 strcpy(commandStringBuf, &rxbuf[bufloc]); 00481 if(strstr(commandStringBuf, "=")){ 00482 tok = strtok(commandStringBuf, "="); 00483 strcpy(commandString, tok); 00484 tok = strtok(NULL, "="); 00485 commandData = atoi(tok); 00486 //if(DEBUG){ 00487 // sprintf(strbuf, "commandStringBuf= %s, commandData= %d", commandStringBuf, commandData); 00488 // sendSerial(strbuf); 00489 //} 00490 } 00491 else{ 00492 strcpy(commandString, commandStringBuf); 00493 } 00494 00495 // Convert the command string to all uppercase characters 00496 for (i = 0; i < strlen(commandString); i++) 00497 { 00498 commandString[i] = toupper(commandString[i]); 00499 } 00500 00501 // Replace the character we clobbered in rxbuf 00502 rxbuf[endofc] = store; 00503 00504 // Update bufloc to the end of the command string 00505 bufloc = endofc; 00506 } 00507 00508 /************************************************************ 00509 * Routine: doCommand 00510 * Input: none 00511 * Returns: none 00512 * Description: 00513 * This is the start of the command string. 00514 **************************************************************/ 00515 void doCommand(void) 00516 { 00517 int ival; 00518 unsigned int boardEnables; 00519 00520 char commandString[80] = { 0 }; 00521 00522 bufloc = 0; 00523 commandError = 0; 00524 00525 parseCommand(GET, commandString); 00526 00527 if (!strcmp(commandString, "MENU")) 00528 { 00529 menuRedraw(NO_PROMPT); 00530 } 00531 else if (!strcmp(commandString, "HELP")) 00532 { 00533 menuRedraw(NO_PROMPT); 00534 } 00535 else if (!strcmp(commandString, "MAXB")) 00536 // MAXB is used to get/set the max_boards value. 00537 // The integer value of max_boards is used to select the appropriate lookup table. 00538 { 00539 if (readback) 00540 { 00541 sprintf(strbuf, " %d", max_boards); 00542 sendSerial(strbuf); 00543 } 00544 else if (running == 1) 00545 { 00546 sprintf(strbuf, " Parameters may not be updated while running!"); 00547 sendSerial(strbuf); 00548 } 00549 else 00550 { 00551 max_boards = commandData; 00552 } 00553 } 00554 else if (!strcmp(commandString, "BRDS")) 00555 // BRDS is used to get/set the wr_out value. 00556 // The integer value of boardsActive is used to change wr_out via setBoardEnables(boardsActive). 00557 // Slots 12 to 0 are activated with the wr_out signals 00558 // wr_out[13] = slots[12:0] 00559 { 00560 if (readback) 00561 { 00562 sprintf(strbuf, " %d", boardsActive); 00563 sendSerial(strbuf); 00564 } 00565 else if (running == 1) 00566 { 00567 sprintf(strbuf, " Parameters may not be updated while running!"); 00568 sendSerial(strbuf); 00569 } 00570 else 00571 { 00572 boardsActive = commandData; 00573 if(checkRange(boardsActive, 0, 8191) == 1){ 00574 wr_out_code = setBoardEnables(boardsActive); 00575 testing = TRUE; 00576 }else{ 00577 showRangeError(1, boardsActive, 0.0); 00578 } 00579 } 00580 } 00581 else if (!strcmp(commandString, "MULT")) 00582 // MULT is used to get/set the en_out value. 00583 // The integer value of boardMults is used to change en_out via setBoardWeights(boardMults). 00584 // en_out are binary weighted signals that activate groups of DC-DC converters on the slot cards. 00585 // en_out[6] = {en32, en16, en8, en4, en2, en1} 00586 { 00587 if (readback) 00588 { 00589 sprintf(strbuf, " %d", boardMults); 00590 sendSerial(strbuf); 00591 } 00592 else if (running == 1) 00593 { 00594 sprintf(strbuf, " Parameters may not be updated while running!"); 00595 sendSerial(strbuf); 00596 } 00597 else 00598 { 00599 boardMults = commandData; 00600 if(checkRange(boardMults, 0, 63) == 1){ 00601 en_out_code = setBoardWeights(boardMults); 00602 /*unsigned int bmult=boardMults; 00603 for(int count=0;count<33;count++){ 00604 en_out_code = setBoardWeights(bmult++); 00605 wait(0.25); 00606 } 00607 en_out_code = setBoardWeights(boardMults); 00608 sprintf(strbuf, " en_out_code=%d\r\n", en_out_code); 00609 sendSerial(strbuf);*/ 00610 testing = TRUE; 00611 }else{ 00612 showRangeError(1, boardMults, 0.0); 00613 } 00614 } 00615 } 00616 else if (!strcmp(commandString, "MY12")) 00617 // MULT is used to get/set the en_out value. 00618 // The integer value of boardMults is used to change en_out via setBoardWeights(boardMults). 00619 // en_out are binary weighted signals that activate groups of DC-DC converters on the slot cards. 00620 // en_out[6] = {en32, en16, en8, en4, en2, en1} 00621 { 00622 //if(DEBUG){ 00623 // sprintf(strbuf, "my12=%d commandData=%d\r\n", my12, commandData); 00624 // sendSerial(strbuf); 00625 //} 00626 00627 if (readback) 00628 { 00629 sprintf(strbuf, " %d", my12); 00630 sendSerial(strbuf); 00631 }else{ 00632 my12 = commandData; 00633 testing = FALSE; 00634 } 00635 } 00636 else if (!strcmp(commandString, "ALLOFF")) 00637 { 00638 my12 = 0; 00639 running = FALSE; 00640 testing = FALSE; 00641 if(DEBUG){ 00642 sprintf(strbuf, "wr_out_code=%d\r\n", wr_out_code); 00643 sendSerial(strbuf); 00644 } 00645 } 00646 else if (!strcmp(commandString, "ALLON")) 00647 { 00648 wr_out_code = setBoardEnables((unsigned int)ALLON); 00649 testing = TRUE; 00650 } 00651 else if (!strcmp(commandString, "RUN")) 00652 { 00653 // Skip over any white space and the optional '[' character 00654 while ((isspace(rxbuf[bufloc])) || (rxbuf[bufloc] == '[')) bufloc++; 00655 00656 if(rxbuf[bufloc] == NULL){ 00657 boardsActive = ALLON; 00658 startConverter(boardsActive); 00659 testing = FALSE; 00660 } 00661 else if (rxbuf[bufloc] == '0') 00662 //if (rxbuf[bufloc] == '0') 00663 { 00664 stopConverter(); 00665 //setDacsToZeroVolts(); 00666 testing = FALSE; 00667 } 00668 else if ((rxbuf[bufloc] > '0') && (rxbuf[bufloc] < '0' + max_boards)) 00669 { 00670 ival = atoi(&rxbuf[bufloc]); 00671 //ival--; 00672 00673 if (running == 0) 00674 { 00675 boardsActive = ival; 00676 startConverter(boardsActive); 00677 testing = FALSE; 00678 } 00679 else 00680 { 00681 // Compare the board enable flags between registers 00682 //boardEnables = checkRegisterCompatibility(ival); 00683 00684 // If board enable flags match, change the register set 00685 if (boardEnables == 0) 00686 { 00687 boardsActive = ival; 00688 } 00689 else 00690 { 00691 sprintf(strbuf, " Board enable flags do not match (0x%08x)", boardEnables); 00692 sendSerial(strbuf); 00693 } 00694 } 00695 } 00696 else 00697 { 00698 sprintf(strbuf, " Invalid number of boards (1 - %d)", MAX_BOARDS); 00699 sendSerial(strbuf); 00700 commandError = 1; 00701 } 00702 } 00703 else if (!strcmp(commandString, "STOP")) 00704 { 00705 stopConverter(); 00706 testing = FALSE; 00707 my12 = 0; 00708 //hv_en = OFF; 00709 } 00710 else if(!strcmp(commandString, "CAL")) 00711 { 00712 if (running == 1) 00713 { 00714 sprintf(strbuf, " Parameters may not be updated while running!"); 00715 sendSerial(strbuf); 00716 commandError = 1; 00717 } 00718 00719 if (!commandError){ 00720 raw = TRUE; 00721 menuRedraw(NO_PROMPT); 00722 } 00723 } 00724 else if(!strcmp(commandString, "UNCAL")) 00725 { 00726 if (running == 1) 00727 { 00728 sprintf(strbuf, " Parameters may not be updated while running!"); 00729 sendSerial(strbuf); 00730 commandError = 1; 00731 } 00732 00733 if (!commandError){ 00734 raw = FALSE; 00735 menuRedraw(NO_PROMPT); 00736 } 00737 } 00738 else 00739 { 00740 if (strcmp(commandString, "")) 00741 { 00742 commandError = 1; 00743 } 00744 } 00745 00746 if (commandError) 00747 { 00748 sendSerial(" !"); 00749 } 00750 00751 //sendCRLF(); 00752 menuPrompt(MENU_DCM1); 00753 } 00754 00755 /************************************************************ 00756 * Routine: processCommand 00757 * Input: none 00758 * Returns: none 00759 * Description: 00760 * This is the main serial communications routine. Everything 00761 * starts here as soon as a command is avaiable for processing. 00762 **************************************************************/ 00763 void processCommand(void) 00764 { 00765 if (!serialStatus.command && !serialStatus.repeat) 00766 { 00767 return; 00768 } 00769 00770 doCommand(); // if not computer (i.e. terminal) you can do the command as well 00771 00772 bufloc = 0; 00773 rxbuf[bufloc] = 0; 00774 00775 serialStatus.computer = FALSE; 00776 serialStatus.command = FALSE; 00777 00778 //sendSerial("didCommand\r\n"); 00779 } 00780 00781 /************************************************************ 00782 * Routine: waitCommand 00783 * Input: none 00784 * Returns: none 00785 * Description: 00786 **************************************************************/ 00787 bool waitCommand(void) 00788 { 00789 if (!serialStatus.command && !serialStatus.repeat) 00790 { 00791 return TRUE; 00792 } 00793 00794 serialStatus.computer = FALSE; 00795 serialStatus.command = FALSE; 00796 00797 return FALSE; 00798 } 00799 00800 // Verify that the same boards are enabled in both the current register and 00801 // the specified register 00802 00803
Generated on Sat Aug 27 2022 12:36:03 by
1.7.2