Jon Freeman / Mbed 2 deprecated Altera_FPGA_test

Dependencies:   MODSERIAL mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers command_interpreter.cpp Source File

command_interpreter.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "MODSERIAL.h"
00004 #include "cnc.h"
00005 #include <cctype>
00006 
00007 extern  MODSERIAL pc;
00008 extern  void    report_inputs   ()  ;
00009 
00010 #if defined I2C_Enable
00011 extern    I2CSlave slave;//(PTE0, PTE1); on KL25
00012 
00013 int i2c_checksumchecker (char * buf, int len)    {
00014     int k, i = 0x01;
00015     for (k = 0; k < len; k++)
00016         i += buf[k];
00017     i &= 0x0ff;
00018     return  i;
00019 }
00020 
00021 int i2c_checksumchecker (char * buf)    {
00022     return  i2c_checksumchecker (buf, strlen(buf));
00023 }
00024 
00025 char * add_csum (char * buf, int len)    {           //  Adds checksum to end of binary string of known length
00026     int j;
00027     char    cs = 0;
00028     for (j = 0; j < len; j++) {
00029         cs += buf[j];
00030     }
00031     buf[len] = 0xff - cs;
00032     buf[len + 1] = 0;
00033     return  buf;
00034 }
00035 
00036 char * add_csum (char * buf)    {           //  Adds checksum to end of null terminated string
00037     return  add_csum    (buf, strlen(buf));
00038 }
00039 
00040 void    i2c_handler    (void const * name)
00041 {
00042     const int i2buflen = 16;
00043     int err = 0;
00044     char    buf[i2buflen];
00045     char msg[20] = "Message 2snd\0";
00046     add_csum(msg);
00047     slave.address(0xc0);
00048     err = slave.write(msg, strlen(msg) + 1); // Includes null char    //  returns 0 on success, nz otherwise
00049     while   (true)  {
00050         int i = slave.receive();
00051         switch (i) {
00052             case I2CSlave::NoData:  //  Happens most of the time    NoData - the slave has not been addressed
00053                 osThreadYield();  //  Using RTOS on this project
00054                 break;
00055             case I2CSlave::ReadAddressed:   //   - the master has requested a read from this slave
00056                 err = slave.write(msg, strlen(msg) + 1); // Includes null char    //  returns 0 on success, nz otherwise
00057                 pc.printf("RdAddr'd ");
00058                 break;
00059             case I2CSlave::WriteGeneral:    //   - the master is writing to all slave
00060                 err = slave.read(buf, i2buflen);    //  returns 0 on success, nz otherwise
00061                 pc.printf("i=%d, - the master is writing to all slave %s\r\n", i, buf);
00062                 break;
00063             case I2CSlave::WriteAddressed:  //   - the master is writing to this slave
00064                 err = slave.read(buf, i2buflen);    //  returns 0 on success, nz otherwise
00065                 pc.printf("M wr-> [%s]", buf);
00066                 for (int z = 0; z < strlen(buf); z++)
00067                     pc.printf("%2x, ", buf[z]);
00068                 pc.printf("cs %2x\r\n", i2c_checksumchecker(buf));
00069                 break;
00070             default:
00071                 pc.printf("Unknown I2C code %d\r\n");
00072                 break;
00073         }   //  end of switch (i) upon result of slave.receive()
00074         if  (err)   {
00075             pc.printf("Err %d with i = %d\r\n", err, i);
00076             err = 0;
00077         }
00078         memset  (buf, 0, i2buflen);    // Clear buffer
00079     }   //  end of while (true)
00080 }       //  end of void    i2c_handler    (void const * name)
00081 
00082 #endif
00083 
00084 extern  char const *    target_str_addr (void)   ;
00085 void    target_cmd (struct singleGparam * a)   {
00086     pc.printf("Computer is %s\r\n", target_str_addr());
00087 }
00088 
00089 //extern  void    FPGA_bit    (int whichbit, int hiorlo)    ;
00090 
00091 extern  void    setpir_cmd  (struct singleGparam * a)  ;
00092 extern  void    setcmd_cmd  (struct singleGparam * a)  ;
00093 extern  void    getdro_cmd  (struct singleGparam * a)  ;
00094 extern  void    getpir_cmd  (struct singleGparam * a)  ;
00095 extern  void    clrpir_cmd  (struct singleGparam * a)  ;
00096 extern  void    clrdro_cmd  (struct singleGparam * a)  ;
00097 extern  void    setdro_cmd  (struct singleGparam * a)  ;
00098 
00099 
00100 void    report_ins_cmd  (struct singleGparam * a)  {
00101    report_inputs();
00102 }
00103 void    menucmd (struct singleGparam * a);
00104 struct kb_command  {
00105     const char * cmd_word;         //  points to text e.g. "menu"
00106     const char * explan;
00107     void (*f)(struct singleGparam *);   //  points to function
00108 }  ;
00109 
00110 struct  kb_command const * command_list_ptr = NULL;   //  Pointer switched between 'input_syntax_check' and 'command_execute'
00111 
00112 struct  kb_command const  input_syntax_check  [] = {
00113     {"menu", "Lists available commands, same as ls", menucmd},
00114     {"ls", "Lists available commands, same as menu", menucmd}    
00115     }   ;
00116 
00117 struct  kb_command const command_execute[] = {
00118     {"menu", "Lists available commands, same as ls", menucmd},
00119     {"ls", "Lists available commands, same as menu", menucmd},
00120     {"inputs", "Report State of Input bits", report_ins_cmd},
00121     {"pir", "Send number to FPGA pir", setpir_cmd},
00122     {"cmd", "Send command to FPGA command reg", setcmd_cmd},
00123     {"rddro", "Read DRO from FPGA", getdro_cmd},
00124     {"rdpir", "Read PIR from FPGA", getpir_cmd},
00125     {"clrpir", "Zero PIR", clrpir_cmd},
00126     {"clrdro", "Zero DRO", clrdro_cmd},
00127     {"setdro", "Set DRO", setdro_cmd},
00128     {"target", "Identify computer device", target_cmd},
00129 };
00130 
00131 int numof_menu_items;
00132 void    menucmd (struct singleGparam * a)
00133 {
00134     pc.printf("At menucmd function - listing commands:-\r\n");
00135     for(int i = 0; i < numof_menu_items; i++)
00136         pc.printf("[%s]\t\t%s\r\n", command_list_ptr[i].cmd_word, command_list_ptr[i].explan);
00137     pc.printf("End of List of Commands\r\n");
00138 }
00139 
00140 extern  void    grain_clr   (struct singleGparam & g)  ;
00141 
00142 void    command_line_interpreter    (void const * name)
00143 {
00144 const int MAX_PARAMS = 10, MAX_CMD_LEN = 120;
00145 static  char    cmd_line[MAX_CMD_LEN + 4];
00146 static  struct  singleGparam   params[MAX_PARAMS + 1];
00147 static  int     cl_index = 0, lastalpha = 0;
00148 static  fl_typ  fracmul;
00149     if  (true)  {
00150         command_list_ptr = command_execute;
00151         numof_menu_items = sizeof(command_execute) / sizeof(kb_command);
00152     }
00153     else    {
00154         command_list_ptr = input_syntax_check;
00155         numof_menu_items = sizeof(input_syntax_check) / sizeof(kb_command);
00156     }
00157     while   (true)  {
00158         while  (pc.readable()) {
00159             int     ch;
00160             if  (cl_index > MAX_CMD_LEN)  {   //  trap out stupidly long command lines
00161                 pc.printf   ("Keyboard Error!! Killing stupidly long command line");
00162                 cl_index = 0;
00163             }
00164             ch = tolower(pc.getc());
00165             if  (ch == '\r' || ch >= ' ' && ch <= 'z')
00166                 pc.printf("%c", ch);
00167 //            else    {                   //  Using <Ctrl>+ 'F', 'B' for Y, 'L', 'R' for X, 'U', 'D' for Z
00168 //                cl_index = 0;           //                 6    2          12   18         21   4
00169 //                pc.printf("[%d]", ch);
00170 //                nudger  (ch);
00171 //            }
00172             if(ch != '\r')  //  was this the 'Enter' key?
00173                 cmd_line[cl_index++] = ch;  //  added char to command being assembled
00174             else    {   //  key was CR, may or may not be command to lookup
00175                 cmd_line[cl_index] = 0; //  null terminate command string
00176                 if(cl_index)    {   //  If have got some chars to lookup
00177                     int i, wrdlen;
00178                     for (i = 0; i < numof_menu_items; i++)   {   //  Look for input match in command list
00179                         wrdlen = strlen(command_list_ptr[i].cmd_word);
00180                         if(strncmp(command_list_ptr[i].cmd_word, cmd_line, wrdlen) == 0
00181                             && !isalpha(cmd_line[wrdlen]))  {   //  If match found
00182                             bool negflag = false;
00183                             int state = 0, paramindex;
00184     //                            pc.printf("Found match for word [%s]\r\n", kbc[i].wrd);
00185                             for(paramindex = 0; paramindex < MAX_PARAMS; paramindex++) 
00186                                 grain_clr   (params[paramindex]);
00187                             paramindex = 0;
00188                             //  read any parameters from command line here
00189                             //  Using parameters[0] as count of parameters to follow
00190                             while   (wrdlen <= cl_index)  {
00191                                 ch = cmd_line[wrdlen++];
00192                                 if(isalpha(ch)) lastalpha = ch;
00193                                 if(ch == '-')   negflag = true;
00194                                 if(ch == '+')   negflag = false;
00195                                 switch  (state) {
00196                                     case    0:  //  looking for start of a number string
00197                                         if(isdigit(ch)) {   //  found first digit of a number string
00198                                             paramindex++;
00199                                             if(paramindex > MAX_PARAMS)    {
00200                                                 wrdlen = cl_index;  //  exit condition
00201                                                 pc.printf("WARNING - too many parameters, ignoring extra\r\n");
00202                                             } else    {
00203                                                 params[paramindex].i = ch - '0';
00204                                                 params[paramindex].c = lastalpha;
00205                                                 state = 1;  //  Found first digit char of number string
00206                                             }
00207                                         }
00208                                         break;
00209                                     case    1:  //  looking for end of a number string
00210                                         if(isdigit(ch)) {   //  accumulating integer from string
00211                                             params[paramindex].i *= 10;
00212                                             params[paramindex].i += ch - '0';
00213                                         } else    { //  found non-digit terminating number
00214                                             if  (ch == '.')  {
00215                                                 state = 2;
00216                                                 fracmul = 0.1;
00217                                                 params[paramindex].flt = (fl_typ)params[paramindex].i;
00218                                             } else    {
00219                                                 params[0].i++;    //  count of validated parameters
00220                                                 state = 0;  //  Have read past last digit of number string
00221                                                 if(negflag) {
00222                                                     params[paramindex].i = -params[paramindex].i;
00223                                                     negflag = false;
00224                                                 }
00225                                                 params[paramindex].flt = (fl_typ)params[paramindex].i;
00226                                             }
00227                                         }
00228                                         break;
00229                                     case    2:  //  looking for fractional part of double
00230                                         if(isdigit(ch)) {   //  accumulating fractional part from string
00231                                             params[paramindex].flt += (fl_typ)((ch - '0') * fracmul);
00232                                             fracmul /= 10.0;
00233                                         } else    { //  found non-digit terminating double precision number
00234                                             params[0].i++;    //  count of validated parameters
00235                                             state = 0;  //  Have read past last digit of number string
00236                                             if(negflag) {
00237                                                 params[paramindex].i = -params[paramindex].i;
00238                                                 params[paramindex].flt = -params[paramindex].flt;
00239                                                 negflag = false;
00240                                             }
00241                                         }
00242                                         break;
00243                                     default:
00244                                         break;
00245                                 }   //  end of switch state
00246                             }       //  end of while wrdlen < cl_index
00247     //                            pc.printf("Found match to [%s] with %d parameters\r\n", command_list_ptr[i].wrd, paramindex);
00248                             command_list_ptr[i].f(params);   //  execute command
00249                             i = numof_menu_items + 1;    //  to exit for loop
00250                         }
00251                     }       // End of for numof_menu_items
00252                     if(i == numof_menu_items)
00253                         pc.printf("No Match Found for CMD [%s]\r\n", cmd_line);
00254                 }           //  End of If have got some chars to lookup
00255                 pc.printf("\r\n>");
00256                 cl_index = lastalpha = 0;
00257             }               // End of else key was CR, may or may not be command to lookup
00258         }                   //  End of while (pc.readable())
00259         osThreadYield();  //  Using RTOS on this project
00260     }
00261 }
00262 
00263 ////} cli;
00264