HIT Project #3 https://community.freescale.com/docs/DOC-99621

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Terminal.cpp Source File

Terminal.cpp

00001 
00002 #include "Terminal.h"
00003 #include "Types.h"
00004 #include "Queue.h"
00005 #include <string.h>
00006 #include <stdint.h>
00007 
00008 
00009 //*****************************************************************
00010 //Terminal Configuration
00011 //*****************************************************************
00012 
00013 #define MAX_TERMINAL_LINE_CHARS 64
00014 #define MAX_TERMINAL_CMD_CHARS  32
00015 
00016 ByteQueue TERMINAL_OUTPUT_QUEUE;
00017 ByteQueue TERMINAL_INPUT_QUEUE;
00018 
00019 #define TERMINAL_QUEUE_SIZE     512
00020 
00021 uint8_t TERMINAL_OUTPUT_QUEUE_Storage[TERMINAL_QUEUE_SIZE];
00022 uint8_t TERMINAL_INPUT_QUEUE_Storage[TERMINAL_QUEUE_SIZE];
00023 
00024 
00025 
00026 //Terminal Prototypes Here!
00027 void TerminalCmd_Help(char *arg);
00028 void TerminalCmd_ssr(char *arg);
00029 
00030 //Populate this array with the callback functions and their terminal command string
00031 TerminalCallbackRecord MyTerminalCallbackRecords[] ={   {"help",TerminalCmd_Help,"Lists available commands"},
00032                                                         {"ssr",TerminalCmd_ssr,"Enables /disables an ssr"}
00033                                                  
00034                                                         };
00035 
00036 
00037 #define NUM_TERMINAL_COMMANDS  (sizeof(MyTerminalCallbackRecords)/sizeof(TerminalCallbackRecord))
00038                                                         
00039                                                         
00040 //Terminal Callback Implementation Here!                                              
00041 
00042 void TerminalCmd_Help(char *arg)
00043 {
00044     uint8_t i;
00045 
00046     TERMINAL_PRINTF("\r\n\r\nCommand List:\r\n");
00047     TERMINAL_PRINTF("----------------------\r\n");
00048 
00049     for(i=0;i<NUM_TERMINAL_COMMANDS;i++)
00050     {
00051          TERMINAL_PRINTF("%s  ---->  %s\r\n",MyTerminalCallbackRecords[i].CommandString,MyTerminalCallbackRecords[i].HelpString);    
00052     }
00053 
00054     TERMINAL_PRINTF("\r\n\r\n");
00055 }
00056 
00057 void TerminalCmd_ssr(char *arg)
00058 {
00059     uint32_t Items;
00060     uint32_t RelayIndex;
00061     uint32_t RelayState;
00062     Items = sscanf(arg,"%d %d",&RelayIndex,&RelayState);   
00063     
00064     if(Items!=2)
00065     {
00066         TERMINAL_PRINTF("\r\nssr requires 2 arguments:\r\n");
00067         TERMINAL_PRINTF("1st:   Relay Index    Valid values are 0 (high current) and 1 (low current)\r\n");
00068         TERMINAL_PRINTF("2nd:   Relay State     0 == off  1 == on   anything else == off\r\n");
00069     }
00070     else
00071     {
00072         if(RelayIndex >1)
00073             RelayIndex = 1;
00074             
00075         if(RelayState > 1)
00076             RelayState = 0;
00077             
00078          TERMINAL_PRINTF("Setting ssr %d to state of %d",RelayIndex,RelayState);   
00079     
00080         if(RelayIndex == 0)
00081         {
00082             if(RelayState == 0 )
00083             {
00084                   DISABLE_HIGH_CURRENT_RELAY ;
00085             }
00086             else
00087             {
00088                  ENABLE_HIGH_CURRENT_RELAY;
00089             }
00090         }
00091         else
00092         {
00093              if(RelayState == 0 )
00094             {
00095                    DISABLE_LOW_CURRENT_RELAY;
00096             }
00097             else
00098             {
00099                    ENABLE_LOW_CURRENT_RELAY;
00100             }
00101         }
00102         
00103     
00104     } 
00105 }
00106 
00107 
00108 
00109 
00110 //*****************************************************************
00111 //Plumbing.....
00112 //*****************************************************************
00113 
00114 
00115 
00116 char TerminalLineBuf[MAX_TERMINAL_LINE_CHARS];
00117 uint8_t TerminalPos;
00118 char TerminalCmdBuf[MAX_TERMINAL_CMD_CHARS+1];
00119 char TerminalArgs[MAX_TERMINAL_LINE_CHARS-MAX_TERMINAL_CMD_CHARS];
00120 uint8_t NextCharIn;
00121 uint8_t CmdFound;
00122  
00123 void TerminalBootMsg()
00124 {
00125      // TERMINAL_PRINTF("\r\n\r\nMonkey Do!\r\n");
00126 }
00127 
00128 void InitTerminal()
00129 {
00130     TerminalPos = 0;
00131     CmdFound = 0;
00132     
00133     InitByteQueue(&TERMINAL_OUTPUT_QUEUE,TERMINAL_QUEUE_SIZE,&TERMINAL_OUTPUT_QUEUE_Storage[0]); 
00134     InitByteQueue(&TERMINAL_INPUT_QUEUE,TERMINAL_QUEUE_SIZE,&TERMINAL_INPUT_QUEUE_Storage[0]); 
00135     
00136     
00137   //  TerminalBootMsg();
00138 }
00139 
00140   
00141 
00142 void ProcessTerminal()
00143 {
00144      uint8_t i,j;
00145      uint8_t ArgsFound;
00146         
00147     if(TERMINAL_READABLE)
00148     {
00149        NextCharIn = TERMINAL_GETC;
00150        
00151         switch(NextCharIn)
00152         {
00153             case '\r':
00154              
00155              TerminalLineBuf[TerminalPos++] = 0x0;
00156              TERMINAL_PUTC(NextCharIn);
00157            
00158              if(TerminalPos > 1)
00159              {
00160                  //find the command
00161                  i=0;
00162                  while(TerminalLineBuf[i]>0x20 &&  TerminalLineBuf[i]<0x7f)
00163                  {
00164                       TerminalCmdBuf[i] = TerminalLineBuf[i];
00165                       i++;
00166     
00167                     if(i==MAX_TERMINAL_CMD_CHARS)
00168                         {
00169                          break;
00170                         }
00171                  }
00172                     
00173                 TerminalCmdBuf[i] = 0;
00174                 TerminalCmdBuf[i+1] = 0;
00175                 
00176                 
00177                 ArgsFound = TRUE;
00178                 memset(TerminalArgs,0x00,sizeof(TerminalArgs));
00179                 //scan for num terminator or next non whitespace
00180                 while(TerminalLineBuf[i]<=0x20 && (i<MAX_TERMINAL_LINE_CHARS))
00181                 {
00182                     if(TerminalLineBuf[i] == 0x00)
00183                     {
00184                     
00185                         //if we find a NULL terminator before a non whitespace character they flag for no arguments
00186                         ArgsFound = FALSE;
00187                         break;
00188                     }   
00189                     i++; 
00190                 }
00191                 
00192                 if(ArgsFound == TRUE)
00193                 {
00194                     strcpy(TerminalArgs,&TerminalLineBuf[i]);
00195                     
00196                     //trim trailing whitespace
00197                     i = sizeof(TerminalArgs)-1;
00198                     
00199                     while((TerminalArgs[i]<0x21) && (i>0))
00200                     {
00201                         TerminalArgs[i]= 0x00;
00202                         i--;
00203                     }       
00204                 }
00205                 
00206                 CmdFound = FALSE;
00207                 for(j=0;j<NUM_TERMINAL_COMMANDS;j++)
00208                 {           
00209                     if(strcmp(TerminalCmdBuf,MyTerminalCallbackRecords[j].CommandString) == 0)
00210                     {
00211                         TERMINAL_PRINTF("\r\n");
00212                         if(MyTerminalCallbackRecords[j].Callback != NULL)
00213                             MyTerminalCallbackRecords[j].Callback(TerminalArgs);
00214                     
00215                         CmdFound = TRUE;
00216                         break;
00217                     }             
00218                 }        
00219                 if(CmdFound == FALSE)
00220                 {
00221                   TERMINAL_PRINTF("\r\n%s command not recognized.\r\n\r\n",TerminalCmdBuf);
00222                  
00223                 }
00224               }    
00225              TERMINAL_PRINTF("\r\n>");
00226              TerminalPos = 0;
00227             
00228             break;
00229             
00230             case '\b':
00231                 if(TerminalPos > 0)
00232                 {
00233                     TerminalPos--;    
00234                     TERMINAL_PUTC(NextCharIn);
00235                 }
00236             break;
00237             
00238             default:
00239                 
00240                 if(TerminalPos == 0 && NextCharIn == 0x020)
00241                 {
00242                      //Do nothing if space bar is pressed at beginning of line    
00243                 }
00244                    else if(NextCharIn >= 0x20 && NextCharIn<0x7F)
00245                 {
00246                     
00247                     if(TerminalPos < MAX_TERMINAL_LINE_CHARS-1)
00248                     {
00249                          TerminalLineBuf[TerminalPos++] = NextCharIn;
00250                         TERMINAL_PUTC(NextCharIn);
00251                     }
00252                 }
00253             
00254             break;
00255         
00256         }
00257     }
00258  
00259 }
00260 
00261