Timothy Mulrooney / Mbed 2 deprecated frdm_i2c_test

Dependencies:   PinDetect libmDot mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers commandline.cpp Source File

commandline.cpp

Go to the documentation of this file.
00001 /* ========================================
00002  * Filename: commandLine.c
00003  *
00004  * Description: functions and operations needed for  command line operation
00005  *
00006  * Copyright J-Factor Embedded Technologies 2015
00007  * Copyright TJM Embedded Software 2015
00008  * All Rights Reserved
00009  * UNPUBLISHED, LICENSED SOFTWARE.
00010  *
00011  * CONFIDENTIAL AND PROPRIETARY INFORMATION
00012  * WHICH IS THE PROPERTY OF J-Factor Embedded Technologies.
00013  *
00014  * ========================================
00015 */
00016 //#include <project.h>
00017 #include "mbed.h"
00018 #include "stdio.h"
00019 #include "common.h"
00020 
00021 /** \file
00022  * \brief command line - contains all functions related to the command line.
00023  */
00024 
00025 /* externals */
00026 
00027 /* Globals */
00028 char line[LINE_SIZE];                       //!< character array read 
00029 char lastLine[NUM_LINES][LINE_SIZE];        //!< array of previous command lines
00030 int8 indexNextLineIn = 0;                   //!< index of next position to save current command line
00031 int8 indexNextLineOut = 0;                  //!< index of next position to read out saved comman dlines
00032 int8 linesInBuffer = 0;                     //!< number of lines in  command line buffer
00033 uint8 enableCLI = 1;
00034 uint8 escapeSeen = 0;                       //!< escape character seen
00035 int8 charCount;                            //!< number of characters in the current command line
00036 int8 cursorPosition;                       //!< current cursor position in the current command line
00037 char TransmitBuffer[TRANSMIT_BUFFER_SIZE];  //!< charaters to be sent to the console
00038 uint8 ptrCmd = 0;
00039 uint8 binaryParameter;                      //!< result of any 8 bit integer read operation
00040 uint16 digitalParameter;                    //!< result of any 16 bit integer read operation
00041 float analogParameter;                      //!< result of any floating point read operation
00042 char *validCmds[] = {"help","version","read","readReg","write","writeReg","info","find","run","stop","date","hvstate"};    //!< array of valid commands - case insensitive    
00043 uint16 regAddress;
00044 uint16 regLength;
00045 uint8 regData[MAX_REGISTER_DATA];
00046 
00047 /** 
00048  * \fn getLine
00049  *
00050  * \brief Reads characters from UART and retuns a line when new line seen.
00051  * if enableCLI == 1 - process input characters<p>
00052  * if enableCLI == 0 - return 0<p>
00053  *
00054  * will call UART_1_CetChar() to get the next character.<p>
00055  * if ch == 0 - no character available<p>
00056  * if ch != 0 - add ch to line array<p>
00057  * features: <p>
00058  * up arrow will go through last NUM_LINES commands. <p>
00059  * backspace will remove last typed character<p>
00060  *
00061  * \return uint8 - 0 - no character ready or line not finished, 1 - line complete (CR seen)<p>
00062  *
00063  */
00064 uint8 getLine()
00065 {
00066      /* Variable to store UART received character */
00067     uint8 ch,i;
00068     
00069     /* check if CLI enabled */
00070     if(enableCLI == 0)
00071         return 0;
00072     
00073     /* Non-blocking call to get the latest data recieved  */
00074     if(pc.readable())
00075         ch = pc.getc();
00076     else
00077         ch = '\0';
00078         
00079     /* Set flags based on UART command */
00080     switch(ch)
00081     {
00082         case 0:
00083             /* No new data was recieved */
00084             return 0;
00085          
00086         case 0x1B:                      /* up arrow seen */
00087             escapeSeen = 1;
00088             break;
00089         
00090         case '\n':                      /* new line seen */
00091         case '\r':
00092             pc.printf("\r\n");
00093             line[charCount] = '\0';
00094             charCount = 0;
00095             cursorPosition = charCount;
00096             escapeSeen = 0;
00097             if(strlen(line) > 0)
00098             {
00099                 strcpy(&lastLine[indexNextLineIn][0],line); /* save current command line in  buffer */
00100                 if(linesInBuffer < NUM_LINES)
00101                     linesInBuffer++;
00102                 indexNextLineIn++;                          /* point to next free space in command line buffer */
00103                 if(indexNextLineIn == NUM_LINES - 1)        /* wrap around if past end */
00104                     indexNextLineIn = 0;
00105                 indexNextLineOut = indexNextLineIn;
00106             }
00107             return 1;
00108             
00109         default:
00110 //            sprintf(TransmitBuffer,"char: %02X %c  charCount: %d\r\n",ch,ch,charCount);
00111 //            UART_1_PutString(TransmitBuffer);
00112             if( (escapeSeen == 1) && (ch == '[') )
00113             {
00114                 escapeSeen++;
00115                 break;
00116             }
00117             else if( (escapeSeen == 2) && (ch == 'A') )      /* look for up arrow escape sequence */
00118             {
00119                 if(linesInBuffer == 0)
00120                     break;
00121                 indexNextLineOut--;                     /* point to last line in */
00122                 if(indexNextLineOut < 0)                /* check if wrap around */
00123                 {
00124                     if(linesInBuffer < NUM_LINES)
00125                         indexNextLineOut = linesInBuffer -1;
00126                     else
00127                         indexNextLineOut = NUM_LINES - 1;
00128                 }
00129                 strcpy(line,&lastLine[indexNextLineOut][0]); /* copy command line from buffer to command line */
00130                 charCount = strlen(line);
00131                 cursorPosition = charCount;
00132                 escapeSeen = 0;
00133                 pc.printf("\r");
00134                 for (i=0;i<sizeof(line);i++)                 /* clear line of last command */
00135                     pc.printf(" ");
00136                 sprintf(TransmitBuffer,"\r%s cmd>%s",time_string,line);    /* restore with selected command */
00137                 pc.printf(TransmitBuffer);
00138                 break;
00139             }
00140             else if( (escapeSeen == 2) && (ch == 'D') )      /* look for left arrow escape sequence */
00141             {
00142                 if( cursorPosition > 0 )
00143                 {
00144                     pc.printf("\b");
00145                     cursorPosition--;
00146                 }
00147                 escapeSeen = 0;
00148                 break;
00149             }
00150             else if( (escapeSeen == 2) && (ch == 'C') )      /* look for right arrow escape sequence */
00151             {
00152                 if( cursorPosition < charCount )
00153                 {
00154                     sprintf(TransmitBuffer,"%c",line[cursorPosition]);
00155                     pc.printf(TransmitBuffer);
00156                     cursorPosition++;
00157                 }
00158                 escapeSeen = 0;
00159                 break;
00160             }
00161             else if( (ch == 0x7F) || (ch == '\b') )  /* backspace seen */
00162             {
00163                 if( (charCount > 0) && (charCount == cursorPosition) )
00164                 {
00165                     pc.printf("\b \b");
00166                     charCount--;
00167                     cursorPosition = charCount;
00168                     line[charCount] = '\0';
00169                 }
00170             }
00171             else
00172             {
00173                 if(charCount < (LINE_SIZE - 1))
00174                 {
00175                     sprintf(TransmitBuffer,"%c",ch);
00176                     pc.printf(TransmitBuffer);
00177                     if(charCount != cursorPosition)         /* move characters at cursor up to fit in new character */
00178                     {
00179                         for(i = charCount ; i > cursorPosition; i--)
00180                             line[i] = line[i-1];
00181                         line[cursorPosition] = ch;
00182                         line[++charCount] = '\0';
00183                         for (i=cursorPosition + 1;i<charCount;i++) 
00184                         {
00185                             sprintf(TransmitBuffer,"%c",line[i]);
00186                             pc.printf(TransmitBuffer);
00187                         }
00188                         cursorPosition = charCount;
00189                     }
00190                     else
00191                     {
00192                         line[charCount++] = ch;
00193                         cursorPosition = charCount;
00194                     }
00195                  }
00196             }
00197             break;    
00198     }
00199     return 0;
00200 }
00201 
00202 
00203 /** 
00204  * \fn executeCmd
00205  *
00206  * \brief executes the command in line array.
00207  *
00208  * calls getCmd to parse command and then switches to proper command handler.
00209  * command list kept short for simplicity.<p>
00210  * read, write, and info command will read and verify parametsr on  command line.
00211  * \return None<p>
00212  *
00213  */
00214 void executeCmd()
00215 {
00216     int8 cmd;
00217     uint8 type,i;
00218     int32 convertFloat1,convertFloat2;
00219     float floatValue;
00220     int value,num, pos;
00221     char str[40] = "", str2[40] = "";
00222     uint8 statusRegister;
00223     
00224     cmd = getCmd();
00225 //    sprintf(TransmitBuffer,"cmd: %d ptrCmd: %d\r\n",cmd,ptrCmd);
00226 //    UART_1_PutString(TransmitBuffer);
00227     
00228     /* execute command */
00229     switch (cmd)
00230     {
00231         case HELP:
00232             pc.printf("Valid Commands (case insensitive)\r\n");
00233             pc.printf("find [string] - find all parameters or all parameters that contain the string\r\n");
00234             pc.printf("help - display list of valid commands\r\n");
00235             pc.printf("info [parameter]- display info on all or parameter\r\n");
00236             pc.printf("read parameter - read value of parameter\r\n");
00237             pc.printf("readReg parameter register length - read N values from parameter registers\r\n");
00238             pc.printf("version - display version information\r\n");
00239             pc.printf("write parameter register value - write value to parameter register\r\n");
00240             pc.printf("writeReg parameter value - write value to parameter\r\n");
00241             pc.printf("run - start control running\r\n");
00242             pc.printf("stop - stop control running\r\n");
00243             pc.printf("date [date string]- display/set current time and date\r\n");
00244             pc.printf("hvstate - current HV status\r\n");
00245             break;
00246            
00247         case DATE:
00248             num = sscanf(&line[ptrCmd],"%s",str);       /* optional parameter on the command line - date string */
00249             if(num > 0)
00250             {
00251                 setDate(&line[ptrCmd]);
00252             }
00253             date();
00254             sprintf(TransmitBuffer,"current time is %s\r\n",c_time_string);
00255             pc.printf(TransmitBuffer);
00256             break;
00257              
00258         case VERSION:
00259             printVersion();
00260 //            sprintf(TransmitBuffer,"PSK15-5 CSE: V%d.%02d %s\r\n",VERSION_MAJOR,VERSION_MINOR,VERSION_DATE);
00261 //            UART_1_PutString(TransmitBuffer);
00262 //            sprintf(TransmitBuffer,"Built on: %s %s\r\n",__DATE__,__TIME__);
00263 //            UART_1_PutString(TransmitBuffer);
00264             break;
00265           
00266         case READ:
00267             num = sscanf(&line[ptrCmd],"%s",str);       /* expecting one parameter on the command line - parameter name */
00268             if(num == 1)
00269             {
00270                 num = getParameter(str);                /* get paramter position in parameters array from paramter name */
00271                 if (num >= 0)
00272                 {
00273                     type = readParam(num);              /* read paramter value and return parameter type */
00274                     if(type == BINARY)
00275                     {
00276                         sprintf(TransmitBuffer,"%s: %d %s\r\n",parameters[num].name,binaryParameter,parameters[num].uints);
00277                     }
00278                     else if(type == DIGITAL)
00279                     {
00280                         sprintf(TransmitBuffer,"%s: %d %s\r\n",parameters[num].name,digitalParameter,parameters[num].uints);
00281                     }
00282                     else if(type == ANALOG)
00283                     {
00284                         convertFloat1 = analogParameter * 100.0;
00285                         sprintf(TransmitBuffer,"%s: %ld.%02ld %s\r\n",parameters[num].name,convertFloat1/100,convertFloat1%100,parameters[num].uints);
00286                     }
00287                      pc.printf(TransmitBuffer);
00288                 }
00289                 else
00290                 {
00291                     sprintf(TransmitBuffer,"Invalid parameter\r\nType help for list of valid parameters\r\n");
00292                     pc.printf(TransmitBuffer);
00293                }
00294             }
00295             else
00296             {
00297                 sprintf(TransmitBuffer,"Missing parameter\r\nformat: read parameter\r\n");
00298                 pc.printf(TransmitBuffer);
00299            }
00300             break;
00301          
00302         case READ_REG:
00303             num = sscanf(&line[ptrCmd],"%s %X %X",str, &regAddress, &regLength); 
00304             if(num < 0)
00305             {
00306                 sprintf(TransmitBuffer,"Missing parameter\r\nformat: read parameter [Reg Address (hex)] [Length (hex)]\r\n");
00307                 pc.printf(TransmitBuffer);
00308                 return;
00309             }
00310             pos = getParameter(str);                /* get paramter position in parameters array from paramter name */
00311             if(pos == -1)
00312             {
00313                 sprintf(TransmitBuffer,"Invalid parameter\r\nType help for list of valid parameters\r\n");
00314                 pc.printf(TransmitBuffer);
00315                 return;
00316            }
00317             switch(num)
00318             {
00319                  case 1:
00320                     regAddress = 0;
00321                     regLength = 1;
00322                     break;
00323                
00324                 case 2:
00325                      regLength = 1;
00326                     break;
00327                
00328                 case 3:
00329                      break;
00330            
00331             }
00332             type = readParam(pos);              /* read paramter value and return parameter type */
00333             for(i=0; i< bytesRead; i++)
00334             {
00335                 if( i == 0 )
00336                     pc.printf("%04X ",regAddress);
00337                 else if( (i % 0x10) == 0 )
00338                     pc.printf("\r\n%04X ",regAddress + i);
00339                 pc.printf("%02X ",regData[i]);
00340             }
00341             pc.printf("\r\n");
00342             break;
00343  #if 0       
00344         case WRITE:
00345             floatValue = 0.0;
00346             num = sscanf(&line[ptrCmd],"%s %s",str,str2);       /* expecting two parameters on the command line - parameter name and parameter value */
00347             if(num == 2)
00348             {
00349                 num = getParameter(str);                        /* get paramter position in parameters array from paramter name */
00350                 if (num >= 0)
00351                 {
00352                     type = readParam(num);                      /* read paramter value and return parameter type */
00353                     if(type == ANALOG)
00354                     {
00355                         floatValue = strtofloat(str2);          /* paramters name and floating point number */
00356                     }
00357                     else
00358                     {
00359                         sscanf(&line[ptrCmd],"%s %d",str,&value);   /* parameters name and integer value */
00360                     }
00361                     if(type == BINARY)
00362                     {
00363                         if(value > parameters[num].maxInt)
00364                         {
00365                            sprintf(TransmitBuffer,"%d exceeds maximum value %d\r\n",value,parameters[num].maxInt);
00366                         }
00367                         else
00368                         {
00369                             writeParam(num,value,floatValue);
00370                             type = readParam(num);
00371                             sprintf(TransmitBuffer,"%s: %d %s\r\n",parameters[num].name,binaryParameter,parameters[num].uints);
00372                         }
00373                     }
00374                     else if(type == DIGITAL)
00375                     {
00376                         if(value > parameters[num].maxInt)
00377                         {
00378                            sprintf(TransmitBuffer,"%d %s exceeds maximum value %d %s\r\n",value,parameters[num].uints,parameters[num].maxInt,parameters[num].uints);
00379                         }
00380                         else if(value < parameters[num].minInt)
00381                         {
00382                            sprintf(TransmitBuffer,"%d %s less than minimum value %d %s\r\n",value,parameters[num].uints,parameters[num].minInt,parameters[num].uints);
00383                         }
00384                         else
00385                         {
00386                             writeParam(num,value,floatValue);
00387                             type = readParam(num);
00388                             sprintf(TransmitBuffer,"%s: %d %s\r\n",parameters[num].name,digitalParameter,parameters[num].uints);
00389                         }
00390                     }
00391                     else if(type == ANALOG)
00392                     {
00393                         if(floatValue > parameters[num].maxFloat)
00394                         {
00395                             convertFloat1 = floatValue * 100;
00396                             convertFloat2 = parameters[num].maxFloat * 100;
00397                             sprintf(TransmitBuffer,"%ld.%02ld %s exceeds maximum value %ld.%02ld %s\r\n",convertFloat1/100,convertFloat1%100,parameters[num].uints,convertFloat2/100,convertFloat2%100,parameters[num].uints);
00398                         }
00399                         else if(floatValue < parameters[num].minFloat)
00400                         {
00401                             convertFloat1 = floatValue * 100;
00402                             convertFloat2 = parameters[num].minFloat * 100;
00403                             sprintf(TransmitBuffer,"%ld.%02ld %s is less than value %ld.%02ld %s\r\n",convertFloat1/100,convertFloat1%100,parameters[num].uints,convertFloat2/100,convertFloat2%100,parameters[num].uints);
00404                         }
00405                         else
00406                         {
00407                             writeParam(num,value,floatValue);
00408                             type = readParam(num);
00409                             convertFloat1 = analogParameter * 100;
00410                             sprintf(TransmitBuffer,"%s: %ld.%02ld %s\r\n",parameters[num].name,convertFloat1/100,convertFloat1%100,parameters[num].uints);
00411                         }
00412                     }
00413                      pc.printf(TransmitBuffer);
00414                 }
00415                 else
00416                 {
00417                     pc.printf("Invalid parameter\r\nType help for list of valid parameters\r\n");
00418                 }
00419             }
00420             else
00421             {
00422                 pc.printf("Missing parameter(s)\r\nformat: write parameter value\r\n");
00423             }
00424             break;
00425  #endif       
00426         case WRITE_REG:
00427              num = sscanf(&line[ptrCmd],"%s %X %X",str, &regAddress, &regData); 
00428             if(num != 3)
00429             {
00430                 pc.printf("Missing parameter\r\nformat: write parameter Reg Address (hex) Value (hex)\r\n");
00431                 return;
00432             }
00433             pos = getParameter(str);                /* get paramter position in parameters array from paramter name */
00434              if(pos == -1)
00435             {
00436                 sprintf(TransmitBuffer,"Invalid parameter\r\nType help for list of valid parameters\r\n");
00437                 pc.printf(TransmitBuffer);
00438                 return;
00439            }
00440            writeParam(pos,regData);              /* write paramter value and return parameter type */
00441             regLength = 1;
00442             readParam(pos);
00443             for(i=0; i< bytesRead; i++)
00444             {
00445                 if( i == 0 )
00446                     pc.printf("%04X ",regAddress);
00447                 else if( (i % 0x10) == 0 )
00448                     pc.printf("\r\n%04X ",regAddress + i);
00449                 pc.printf("%02X ",regData[i]);
00450             }
00451             pc.printf("\r\n");
00452             break;
00453 #if 0
00454            floatValue = 0.0;
00455             num = sscanf(&line[ptrCmd],"%s %s",str,str2);       /* expecting two parameters on the command line - parameter name and parameter value */
00456             if(num == 2)
00457             {
00458                 num = getParameter(str);                        /* get paramter position in parameters array from paramter name */
00459                 if (num >= 0)
00460                 {
00461                     type = readParam(num);                      /* read paramter value and return parameter type */
00462                     if(type == ANALOG)
00463                     {
00464                         floatValue = strtofloat(str2);          /* paramters name and floating point number */
00465                     }
00466                     else
00467                     {
00468                         sscanf(&line[ptrCmd],"%s %d",str,&value);   /* parameters name and integer value */
00469                     }
00470                     if(type == BINARY)
00471                     {
00472                         if(value > parameters[num].maxInt)
00473                         {
00474                            sprintf(TransmitBuffer,"%d exceeds maximum value %d\r\n",value,parameters[num].maxInt);
00475                         }
00476                         else
00477                         {
00478                             writeParam(num,value,floatValue);
00479                             type = readParam(num);
00480                             sprintf(TransmitBuffer,"%s: %d %s\r\n",parameters[num].name,binaryParameter,parameters[num].uints);
00481                         }
00482                     }
00483                     else if(type == DIGITAL)
00484                     {
00485                         if(value > parameters[num].maxInt)
00486                         {
00487                            sprintf(TransmitBuffer,"%d %s exceeds maximum value %d %s\r\n",value,parameters[num].uints,parameters[num].maxInt,parameters[num].uints);
00488                         }
00489                         else if(value < parameters[num].minInt)
00490                         {
00491                            sprintf(TransmitBuffer,"%d %s less than minimum value %d %s\r\n",value,parameters[num].uints,parameters[num].minInt,parameters[num].uints);
00492                         }
00493                         else
00494                         {
00495                             writeParam(num,value,floatValue);
00496                             type = readParam(num);
00497                             sprintf(TransmitBuffer,"%s: %d %s\r\n",parameters[num].name,digitalParameter,parameters[num].uints);
00498                         }
00499                     }
00500                     else if(type == ANALOG)
00501                     {
00502                         if(floatValue > parameters[num].maxFloat)
00503                         {
00504                             convertFloat1 = floatValue * 100;
00505                             convertFloat2 = parameters[num].maxFloat * 100;
00506                             sprintf(TransmitBuffer,"%ld.%02ld %s exceeds maximum value %ld.%02ld %s\r\n",convertFloat1/100,convertFloat1%100,parameters[num].uints,convertFloat2/100,convertFloat2%100,parameters[num].uints);
00507                         }
00508                         else if(floatValue < parameters[num].minFloat)
00509                         {
00510                             convertFloat1 = floatValue * 100;
00511                             convertFloat2 = parameters[num].minFloat * 100;
00512                             sprintf(TransmitBuffer,"%ld.%02ld %s is less than value %ld.%02ld %s\r\n",convertFloat1/100,convertFloat1%100,parameters[num].uints,convertFloat2/100,convertFloat2%100,parameters[num].uints);
00513                         }
00514                         else
00515                         {
00516                             writeParam(num,value,floatValue);
00517                             type = readParam(num);
00518                             convertFloat1 = analogParameter * 100;
00519                             sprintf(TransmitBuffer,"%s: %ld.%02ld %s\r\n",parameters[num].name,convertFloat1/100,convertFloat1%100,parameters[num].uints);
00520                         }
00521                     }
00522                      pc.printf(TransmitBuffer);
00523                 }
00524                 else
00525                 {
00526                     pc.printf("Invalid parameter\r\nType help for list of valid parameters\r\n");
00527                 }
00528             }
00529             else
00530             {
00531                 pc.printf("Missing parameter(s)\r\nformat: write parameter value\r\n");
00532             }
00533             break;
00534 #endif
00535 #if 0       
00536         case INFO:
00537             num = sscanf(&line[ptrCmd],"%s",str);       /* expecting one parameter on the command line - parameter name */
00538             if(num == 1)
00539             {
00540                 num = getParameter(str);                /* get paramter position in parameters array from paramter name */
00541                 if (num >= 0)
00542                 {
00543          //           type = readParam(num);
00544                     displayParameter(num);
00545                 }
00546                 else
00547                 {
00548                     UART_1_PutString("Invalid parameter\r\nType help for list of valid parameters\r\n");
00549                 }
00550             }
00551             else
00552             {
00553 //                for(i=0;strcmpi(parameters[i].name,"end") != 0;i++)
00554                 for(i=0;strcasecmp(parameters[i].name,"end") != 0;i++)
00555                     displayParameter(i);
00556             }
00557             break;
00558        
00559         case FIND:
00560             num = sscanf(&line[ptrCmd],"%s",str);       /* expecting one parameter on the command line - parameter name */
00561             if(num == 1)
00562             {
00563                 num = findParameter(str);                /* get paramter position in parameters array from paramter name */
00564                 if (num < 0)
00565                 {
00566                     sprintf(TransmitBuffer,"String %s not found in any parameter name\r\n",str);
00567                     UART_1_PutString(TransmitBuffer);
00568                 }
00569             }
00570             else
00571             {
00572                 UART_1_PutString("\r\nValid Paramters\r\n");
00573     
00574                 /* print parameter list */
00575 //                for(i=0;strcmpi(parameters[i].name,"end") != 0;i++)
00576                 for(i=0;strcasecmp(parameters[i].name,"end") != 0;i++)
00577                 {
00578                     sprintf(TransmitBuffer,"%s\r\n",parameters[i].name);
00579                     UART_1_PutString(TransmitBuffer);   
00580                 }
00581             }
00582             break;
00583             
00584         case RUN:
00585                 if(runStatus == TRUE)
00586                 {
00587                     sprintf(TransmitBuffer,"%s Control already Running\r\n",time_string);
00588                     UART_1_PutString(TransmitBuffer);  
00589                 }
00590                 else
00591                 {
00592                     runStatus = TRUE;
00593                     lampTestStatus = LAMPTESTNOTRUN;
00594                     sprintf(TransmitBuffer,"%s Control Running\r\n",time_string);
00595                     UART_1_PutString(TransmitBuffer);   
00596                 }
00597                 break;
00598             
00599         case STOP:
00600                 if(runStatus == FALSE)
00601                 {
00602                     sprintf(TransmitBuffer,"%s Control already Stopped\r\n",time_string);
00603                     UART_1_PutString(TransmitBuffer);   
00604                 }
00605                 else
00606                 {
00607                     runStatus = FALSE;
00608                     sprintf(TransmitBuffer,"%s Control Stopped\r\n",time_string);
00609                     UART_1_PutString(TransmitBuffer);   
00610                     phaseUV = (PHASE_AB_UV_Read() << 0);
00611                     phaseUV |= (PHASE_AC_UV_Read() << 1);
00612                     phaseUV |= (PHASE_BC_UV_Read() << 2);
00613                     statusRegister = STATUS_REGISTER1_ReadMask();
00614                     statusRegister |= (PHASEABUVIN | PHASEBCUVIN | PHASEACUVIN);
00615                     STATUS_REGISTER1_INT_ClearPending();
00616                     STATUS_REGISTER1_WriteMask(statusRegister);
00617                     HVState = HVSTATEOFF;
00618                     eStopSeen = FALSE;
00619                     outputOCSeen = FALSE;
00620                     HVStateMsg = 0;                 /* no messages printed yet */
00621                     flashCtrlSys = 0;
00622                     tempFailed = FALSE;
00623                     pendantTestStatus = 0;
00624                     pendantPresent = FALSE;
00625                     waitForPowerCycle = FALSE;
00626                     PEND_PRESENT_NOT_INT_Stop();
00627                     SOFT_START_RELAY_CTRL_Write(0);
00628                     PWM_DC_Stop();
00629                     statusRegister = STATUS_REGISTER1_ReadMask();
00630                     statusRegister &= ~(BRIDGEOCFLAGIN | OUTPUTOCFLAGIN | UTILOCFLAGIN  );
00631                     STATUS_REGISTER1_WriteMask(statusRegister);
00632                     ENCODER_COUNTER_Stop();
00633                     initializeAllParameters();
00634 writeCurrent0(0.0);                               /* test prurposes only */
00635                 }
00636                 break;
00637                 
00638         case HVSTATE:
00639             sprintf(TransmitBuffer,"%s HV State: %02X\r\n",time_string,HVState);
00640             UART_1_PutString(TransmitBuffer);
00641             printHVState();
00642             break;
00643             #endif
00644            
00645         default:
00646             if(strlen(line) !=0)
00647             {
00648                 sprintf(TransmitBuffer,"Unknown cmd: %s\r\nType help for list of valid commands\r\n",line);
00649                 pc.printf(TransmitBuffer);
00650 //                linesInBuffer--;
00651 //                indexNextLineIn--;
00652 //                if(linesInBuffer == NUM_LINES)
00653 //                    indexNextLineIn = NUM_LINES -1;
00654             }
00655             break;
00656      }
00657     for(i=0;i < sizeof(line);i++)
00658         line[i] = '\0';
00659 }
00660 
00661 /** 
00662  * \fn getCmd
00663  *
00664  * \brief parses command line to find position of command in validCmds array.
00665  *
00666  * copies line array into cmd array until a space or end of line then
00667  * searches cmd array for a match in validCmds array.
00668  * \return int8 - position of command in command array if found. -1 if not found<p>
00669  *
00670  */
00671 int8 getCmd()
00672 {
00673     uint i;
00674     char cmd[40];
00675     
00676     for(i=0;i<strlen(line);i++)
00677     {
00678         if(line[i] == ' ')
00679         {
00680             break;
00681         }
00682         cmd[i] = line[i];
00683     }
00684     cmd[i] = '\0';
00685     ptrCmd = strlen(cmd) + 1;
00686 //    sprintf(TransmitBuffer,"cmd: %d %s\r\n",strlen(cmd),cmd);
00687 //    UART_1_PutString(TransmitBuffer);
00688     
00689     for(i=0;i< sizeof(validCmds)/sizeof(validCmds[0]);i++)
00690     {
00691 //        if(strcmpi(validCmds[i],cmd) == 0)
00692         if(strcasecmp(validCmds[i],cmd) == 0)
00693         {
00694             return i;
00695         }
00696     }
00697     return -1;
00698 }
00699 
00700 /** 
00701  * \fn int8 getParameter(char *param)
00702  *
00703  * \brief searches parameters array to find position of command in parameters array.
00704  *
00705  * searches the entire parameters array for a name match with param.
00706  *
00707  * \param[in] param - pointer to an array containing the parameter name.
00708  *
00709  * \return int8 - position of command in command array if found. -1 if not found<p>
00710  *
00711  */
00712 int8 getParameter(char *param)
00713 {
00714     uint i;
00715     
00716  //   sprintf(TransmitBuffer,"param: %d %s\r\n",strlen(param),param);
00717  //   pc.printf(TransmitBuffer);
00718     
00719     for(i=0;strcasecmp(parameters[i].name,"end") != 0;i++)
00720     {
00721         if(strcasecmp(parameters[i].name,param) == 0)
00722         {
00723             return i;
00724         }
00725     }
00726     return -1;
00727 }
00728 
00729 /** 
00730  * \fn int8 findParameter(char *string)
00731  *
00732  * \brief finds all parameters with string in the name.
00733  *
00734  * searches the entire parameters array for all names 
00735  * that contain string.
00736  *
00737  * \param[in] string - pointer to a string.
00738  *
00739  * \return int8 - 0 string found in some names. -1 if string not found in any name<p>
00740  *
00741  */
00742 int8 findParameter(char *string)
00743 {
00744     uint i,j;
00745     int8 result = -1;
00746     char name[40];
00747     char string1[40];
00748     
00749 //    sprintf(TransmitBuffer,"param: %d %s\r\n",strlen(param),param);
00750 //    UART_1_PutString(TransmitBuffer);
00751    
00752     strcpy(string1,string);
00753     for(i=0;i < strlen(string1);i++)
00754         string1[i] = tolower((unsigned char)string1[i]);
00755         
00756 //    for(i=0;strcmpi(parameters[i].name,"end") != 0;i++)
00757     for(i=0;strcasecmp(parameters[i].name,"end") != 0;i++)
00758     {
00759         strcpy(name,parameters[i].name);        /* copy parameter name and convert to lower case */
00760         for(j=0;j < strlen(name);j++)
00761             name[j] = tolower((unsigned char)name[j]);
00762         if(strstr(name,string1) != NULL)
00763         {
00764             sprintf(TransmitBuffer,"%s\r\n",parameters[i].name);
00765             pc.printf(TransmitBuffer);
00766             result = 0;
00767         }
00768     }
00769     return result;
00770 }
00771 
00772 /** 
00773  * \fn uint8 readParam(uint8 paramNum)
00774  *
00775  * \brief reads in parameter at position paramNum.
00776  *
00777  * This will only be called if there was a valid parameter match found.
00778  * will read correct parameter based on parameter type. This will call the
00779  * correct read function specified in the paramter array. A parameter will be
00780  * read into one of binaryParameter, digitalParameter, or analogParameter based on type.
00781  *
00782  * \param[in] paramNum - pointer to a character string containing  parameter name
00783  * \return uint8 - type of parameter<p>
00784  *
00785  */
00786 uint8 readParam(uint8 paramNum)
00787 {
00788     uint8 type =  parameters[paramNum].type;
00789     
00790     if(type == BINARY)
00791     {
00792         if(parameters[paramNum].ptrRead == NULL)
00793         {
00794             sprintf(TransmitBuffer,"%s has a NULL ptrRead parameter\r\n",parameters[paramNum].name);
00795             pc.printf(TransmitBuffer);
00796             binaryParameter = 0;
00797         }
00798         else
00799             binaryParameter = parameters[paramNum].ptrRead();
00800         return type;
00801     }
00802     if(type == ANALOG)
00803     {
00804         if(parameters[paramNum].ptrRead == NULL)
00805         {
00806             sprintf(TransmitBuffer,"%s has a NULL ptrRead parameter\r\n",parameters[paramNum].name);
00807             pc.printf(TransmitBuffer);
00808             analogParameter = 0.0;
00809         }
00810         else
00811             parameters[paramNum].ptrRead();
00812         return type;
00813     }
00814     if(type == DIGITAL)
00815     {
00816         if(parameters[paramNum].ptrRead == NULL)
00817         {
00818             sprintf(TransmitBuffer,"%s has a NULL ptrRead parameter\r\n",parameters[paramNum].name);
00819             pc.printf(TransmitBuffer);
00820             digitalParameter = 0;
00821         }
00822         else
00823             parameters[paramNum].ptrRead();
00824         return type;
00825     }
00826     if(type == REGISTER)
00827     {
00828         bytesRead = 0;
00829         if(parameters[paramNum].ptrReadRegLength == NULL)
00830         {
00831             sprintf(TransmitBuffer,"%s has a NULL ptrReadRegLength parameter\r\n",parameters[paramNum].name);
00832             pc.printf(TransmitBuffer);
00833             digitalParameter = 0;
00834         }
00835         else
00836             bytesRead = parameters[paramNum].ptrReadRegLength(regAddress, regData, regLength);
00837         return type;
00838     }
00839     return type;
00840 }
00841 
00842 /** 
00843  * \fn void writeParam(uint8 paramNum,uint16 value, float floatValue)
00844  *
00845  * \brief reads in parameter at position paramNum.
00846  *
00847  * This will only be called if there was a valid parameter match found.
00848  * will write value to the correct parameter based on parameter type. This will call the
00849  * correct write function specified in the paramter array. value will not be written
00850  * to read only paramters.
00851  *
00852  * \param[in] paramNum - pointer to a character string containing the parameter name
00853  * \param[in] value - 16 bit value to write to a digital parameter.
00854  * \param[in] floatValue - analog value to write to an anlog parameter.
00855  * \return uint8 - type of parameter<p>
00856  *
00857  */
00858 void writeParam(uint8 paramNum,uint8 *value)
00859 {   
00860     uint8 type =  parameters[paramNum].type;
00861      if(parameters[paramNum].mode == READONLY)
00862     {
00863             sprintf(TransmitBuffer,"%s is a read only parameter\r\n",parameters[paramNum].name);
00864             pc.printf(TransmitBuffer);
00865             return;
00866     }
00867    #if 0    
00868     if(type == BINARY)
00869     {
00870         if(parameters[paramNum].ptrWrite8 == NULL)
00871         {
00872             sprintf(TransmitBuffer,"%s has a NULL pteWrite8 parameter\r\n",parameters[paramNum].name);
00873             pc.printf(TransmitBuffer);
00874         }
00875         else
00876             parameters[paramNum].ptrWrite8(value);
00877     }
00878     else if(type == DIGITAL)
00879     {
00880         if(parameters[paramNum].ptrWrite16 == NULL)
00881         {
00882             sprintf(TransmitBuffer,"%s has a NULL pteWrite16 parameter\r\n",parameters[paramNum].name);
00883             pc.printf(TransmitBuffer);
00884         }
00885         else
00886             parameters[paramNum].ptrWrite16(value);
00887     }
00888     #endif
00889     else if(type == REGISTER)
00890     {
00891         if(parameters[paramNum].ptrWriteReg8 == NULL)
00892         {
00893             sprintf(TransmitBuffer,"%s has a NULL ptrWriteReg8 parameter\r\n",parameters[paramNum].name);
00894             pc.printf(TransmitBuffer);
00895         }
00896         else
00897             parameters[paramNum].ptrWriteReg8(regAddress, value);
00898     }
00899     #if 0
00900     else if(type == ANALOG)
00901     {
00902         if(parameters[paramNum].ptrWriteAnalog == NULL)
00903         {
00904             sprintf(TransmitBuffer,"%s has a NULL pteWriteAnalog parameter\r\n",parameters[paramNum].name);
00905             pc.printf(TransmitBuffer);
00906         }
00907         else
00908             parameters[paramNum].ptrWriteAnalog(floatValue);
00909     }
00910     #endif
00911         return;
00912 }
00913 
00914 /** 
00915  * \fn void displayParameter(uint8 num)
00916  *
00917  * \brief displays information about a parameter
00918  *
00919  * This routine all of the information in the parameters array
00920  * for the selected parameter.
00921  *
00922  * \param[in] num - 8 bit integer location in the parameters array 
00923   * \return uint16 - 16 bit num to the power pow<p>
00924  *
00925  */
00926 void displayParameter(uint8 num)
00927 {
00928     char *strType;
00929     char *strMode;
00930     uint16 convertFloat1,convertFloat2,convertFloat3;
00931     
00932     switch (parameters[num].type)
00933     {
00934         case BINARY:
00935             strType = "Binary";
00936             break;
00937           
00938         case DIGITAL:
00939             strType = "16 Bit";
00940             break;
00941         
00942         case ANALOG:
00943             strType = "Analog";
00944             break;
00945         
00946         default:
00947             strType = "Unknown";
00948             break;
00949     }
00950     
00951     switch (parameters[num].mode)
00952     {
00953         case READONLY:
00954             strMode = "Read Only";
00955             break;
00956         
00957         case READWRITE:
00958             strMode = "Read/Write";
00959             break;
00960         
00961         case WRITEONLY:
00962             strMode = "Write Only";
00963             break;
00964         
00965         default:
00966             strMode = "Unknown";
00967             break;
00968     }
00969     
00970     sprintf(TransmitBuffer,"Name:\t'%s' Port: '%s' Connector: '%s' Units: '%s'\r\n",parameters[num].name,parameters[num].port,parameters[num].connector,parameters[num].uints);
00971     pc.printf(TransmitBuffer);   
00972     sprintf(TransmitBuffer,"\tDescription: '%s'\r\n",parameters[num].description);
00973     pc.printf(TransmitBuffer); 
00974     
00975     if(parameters[num].type != ANALOG)
00976     {
00977         sprintf(TransmitBuffer,"\tType: '%s' mode: '%s' Initial: %d Min: %d Max: %d\r\n",strType,strMode,parameters[num].initialInt,parameters[num].minInt,parameters[num].maxInt);
00978         pc.printf(TransmitBuffer);   
00979     }
00980     else
00981     {
00982         convertFloat1 = parameters[num].initialFloat * 100;
00983         convertFloat2 = parameters[num].minFloat * 100;
00984         convertFloat3 = parameters[num].maxFloat * 100;
00985         sprintf(TransmitBuffer,"\tType: '%s' mode: '%s' Initial: %d.%02d Min: %d.%02d Max: %d.%02d\r\n",strType,strMode,convertFloat1/100,convertFloat1%100,convertFloat2/100,convertFloat2%100,convertFloat3/100,convertFloat3%100);
00986         pc.printf(TransmitBuffer);   
00987     }
00988 }
00989 
00990 uint16 readSPIReg(uint16 regAddress, uint8 *regData, uint16 length)
00991 {
00992     
00993 //    sprintf(TransmitBuffer,"read SPI %02X %04X\r\n",regAddress, length);
00994 //    pc.printf(TransmitBuffer); 
00995     if(length > MAX_REGISTER_DATA)
00996     {
00997         sprintf(TransmitBuffer,"Length 0x%X > 0x%X",length,MAX_REGISTER_DATA);
00998         pc.printf(TransmitBuffer);   
00999       return 0;
01000     }  
01001     SPIRead(regAddress,regData,length);
01002     return(length);
01003 }
01004 
01005 void writeSPIReg(uint16 regAddress, uint8 *regData)
01006 {
01007     sprintf(TransmitBuffer,"write SPI %02X %02X\r\n",regAddress, regData[0]);
01008     pc.printf(TransmitBuffer); 
01009     SPIWrite(regAddress, regData,1);
01010     return;
01011 }
01012 
01013 uint16 readI2CReg(uint16 regAddress, uint8 *regData, uint16 length)
01014 {
01015     
01016 //    sprintf(TransmitBuffer,"read I2C %02X %04X\r\n",regAddress, length);
01017 //    pc.printf(TransmitBuffer); 
01018     if(length > MAX_REGISTER_DATA)
01019     {
01020         sprintf(TransmitBuffer,"Length 0x%X > 0x%X",length,MAX_REGISTER_DATA);
01021         pc.printf(TransmitBuffer);   
01022       return 0;
01023     }  
01024     I2CRead(regAddress,regData,length);
01025     return(length);
01026 }
01027 
01028 
01029 void writeI2CReg(uint16 regAddress, uint8 *regData)
01030 {
01031     sprintf(TransmitBuffer,"write I2C %02X %02X\r\n",regAddress, regData[0]);
01032     pc.printf(TransmitBuffer); 
01033     I2CWrite(regAddress, regData,1);
01034     return;
01035 }
01036 
01037 /* [] END OF FILE */