Peter Cooper / Mbed 2 deprecated Dome

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cmd.c Source File

cmd.c

00001 #include "mbed.h"
00002 #include "breakup.h"
00003 #include "useful.h"
00004 #include "i2c.h"
00005 #include "serial.h"
00006 #include "cmd.h"
00007 #include "local_defines.h"
00008 #include "scripting.h"
00009 
00010 
00011 LocalFileSystem local("local");
00012 FILE    *exec_fp;
00013 extern int sys_state;
00014 extern int var[MAX_VAR];
00015 extern int jmp[MAX_JMP];
00016 
00017 
00018 
00019 /******************************************/
00020 /*                                        */
00021 /* Exec commands, for the dome            */
00022 /*                                        */
00023 /******************************************/
00024 
00025 struct    cmd_callback {
00026     char    *label;            /* command */
00027     int     cmd;               /* switch value */
00028     int     args;              /* Number of args for this command */
00029     void    (*cmd_routine)(int,char **);    /* call */
00030 };
00031 struct cmd_callback cmd_cb[] ={
00032     {  "list",     0,   1,    list},                /* List I2C Devices to user */
00033     {  "list2",    0,   1,    list2},               /* List I2C Devices to user */
00034     {  "exec",     0,   2,    exec_file},           /* Run a script file from the local file store */
00035     {  "dir",      0,   1,    dir_file},            /* Get a directory listing from the local file store */
00036     {  "cat",      0,   2,    cat_file},            /* Cat a file on the file system to the current output device */
00037     {  "relay",    0,   2,    relay_control},       /* Switch a relay 0-8 */
00038     {  "light",    0,   3,    light_control},       /* Set a light level on a channel */
00039     {  "wait",     0,   2,    wait_control},        /* wait for X tenths of a second */
00040     {  "baud",     0,   2,    baud_rate},           /* Set the baudrate of the serial I/O */
00041     {  "pca",      0,   2,    init_pca},            /* Init a PCA9685 device from the command line */
00042     {  "print",    0,   0,    print_string},        /* Print a string to current output device */
00043     {   "set",      0,      0,    set_var},             /* Set the variable to a value */
00044     {   "sum",      0,      0,    sum_var},             /* Add some basic maths */
00045     {   "point",    0,      0,    point_var},           /* Set out loop point */
00046     {   "tst",      0,      0,    tst_var},             /* test the value of a var */
00047     {   "clr",      0,      0,    clr_var},             /* Clear the var's to zero */
00048     {  "END",      -1,  -1,   error_handler}
00049 };
00050 
00051 
00052 /* Scan down tags, to find the match, else return -1 */
00053 int find_cmd(char *tag)
00054 {
00055     int     cnt=0,c;
00056     int  length=strlen(tag);
00057     char *elements[0x10];
00058 
00059     if(tag[0]=='#') /* See is we have a command or a comment */
00060         return(0);
00061 
00062     c = breakup(tag,elements,' ');
00063     while(1){
00064         if((strcmp(cmd_cb[cnt].label,"END"))==0){
00065                   if(length>1)
00066             cmd_cb[cnt].cmd_routine(c,elements);  /* this will call the error routine */
00067             return(cmd_cb[cnt].cmd);
00068         } else if((strcmp(cmd_cb[cnt].label,tag))==0){
00069             if((cmd_cb[cnt].args==c) || (cmd_cb[cnt].args==0)){    /* if arg count set to zero, can be anything */
00070                 cmd_cb[cnt].cmd_routine(c,elements);        /* Call the function with args */
00071                 return(cnt);
00072             } else {
00073                 lprintf("\n\rWrong number of args passed to %s\n\r",cmd_cb[cnt].label);
00074                 return(-1);
00075             }
00076         }
00077         cnt++;
00078     }
00079 }
00080 
00081 
00082 void error_handler(int c, char **a)
00083 {
00084     int cnt=0;
00085     lprintf("\n\rCommands available are ");
00086     while(1){
00087         if((strcmp(cmd_cb[cnt].label,"END"))==0){
00088             lprintf("\n\r");
00089             return;
00090         } else
00091             lprintf("%s, ",cmd_cb[cnt].label);
00092         cnt++;
00093     }
00094 }
00095 
00096 /******************************************/
00097 /*                                        */
00098 /* Command stubs are here                 */
00099 /*                                        */
00100 /******************************************/
00101 
00102 void list(int c, char **a)
00103 {
00104     i2c_probe();
00105 }
00106 void list2(int c, char **a)
00107 {
00108     i2c_probe2();
00109 }
00110 
00111 void exec_file(int c, char **a)
00112 {
00113     /* File on a[1] */
00114     
00115     FILE    *fp;
00116     char     buf[0x60];
00117     char     cmd[0x60];
00118     
00119     sprintf(buf,"/local/%s",a[1]);
00120     
00121     if((fp = fopen(buf,"r"))==NULL){
00122         lprintf("Unable to open %s\n\r",buf);
00123         return;
00124     }
00125     sys_state = sys_state | EXEC_CALLED;
00126     while(!feof(fp)){
00127         fgets(buf,sizeof(buf),fp);
00128         if(strlen(buf) > 2){
00129             sprintf(cmd,"%s\n",buf);
00130             find_cmd(cmd);
00131         }
00132         buf[0]='\0';
00133         buf[1]='\0';
00134     }
00135     fclose(fp);
00136 }
00137 void dir_file(int c, char **a)
00138 {
00139     lprintf("List of files in /local\n\r");
00140 
00141     DIR *d;
00142     struct dirent *p;
00143     d = opendir("/local");
00144     if(d != NULL) {
00145         while((p = readdir(d)) != NULL) {
00146             lprintf(" - %s\n\r", p->d_name);
00147         }
00148     } else {
00149         error("Could not open directory!");
00150     }
00151 }
00152 void cat_file(int c, char **a)
00153 {
00154     /* File on a[1] */
00155     
00156     FILE    *fp;
00157     char     buf[0x60];
00158     
00159     sprintf(buf,"/local/%s",a[1]);
00160     
00161     if((fp = fopen(buf,"r"))==NULL){
00162         lprintf("Unable to open %s\n\r",buf);
00163         return;
00164     }
00165     
00166     while(!feof(fp)){
00167         fgets(buf,sizeof(buf),fp);
00168         if(strlen(buf) > 2){
00169             lprintf(buf);
00170         }
00171         buf[0]='\0';
00172         buf[1]='\0';
00173     }
00174     fclose(fp);
00175 }
00176 
00177 
00178 void relay_control(int c, char **a)
00179 {
00180     relay_operate(htoi(a[1]));
00181 }
00182 
00183 void wait_control(int c, char **a)
00184 {
00185     wait(htoi(a[1])/10);
00186 }
00187 
00188 void light_control(int c, char **a)
00189 {
00190      channel_light(htoi(a[1]), htoi(a[2]));
00191 }
00192 void init_pca(int c, char **a)
00193 {
00194     init_pca9685(htoi(a[1]));
00195 }
00196 
00197 void print_string(int c, char **a)
00198 {
00199     int cnt = 1;
00200     int len;
00201     while(cnt <= c){
00202         if(a[cnt][0]=='v' || a[cnt][0]=='V'){
00203             len = strlen(a[cnt]);
00204             if(len == 2){
00205                 lprintf(" %04x",htoi(a[cnt]));
00206             } else {
00207                 lprintf("%s",a[cnt]);
00208             }
00209         } else
00210             lprintf("%s",a[cnt]);
00211         cnt++;
00212     }
00213     lprintf("\n");
00214 }