CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Implementing a command line interface

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
srhoar



Joined: 17 Apr 2007
Posts: 3

View user's profile Send private message

PostPosted: Sat May 05, 2007 9:10 am     Reply with quote

Here is the state machine and parser I use for most of my projects.

It is fairly simple to modify.

Code:

#include <string.h>
#include <stdlib.h>

typedef enum { FAULT = 0, STBY, ARMED, FIRE } State_Type;
const char      state_text[4][6] = { "FAULT", "STBY", "ARMED", "FIRE" };

void         state_table(void);

State_Type      curr_state = FAULT;

char         line[17];   /* variables for parser */
int            i, c;

/* LIST ALL COMMAND TO RECOGNIZE HERE */
#define NUM_COMMANDS   10
#define EOL            '\r'

const char      commands[NUM_COMMANDS + 1][10] = {
            "FIRE", "STBY", "STAT", "SHOTS",
            "ARM", "PRF", "RED", "TEST",
            "HELP","NOHV" };


/* function protoypes void */
void         safe_system(void);
void         state_stby(void);
void         state_armed(void);
void         state_fire(void);
void         state_fault(void);
void         debug_test(void);
void         get_command(void);
void         send_stat(void);
void         delay_s(int);
void         help(void);

int            delay = 0;   /* used for state delays */

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void main()
{
   /* CHIP SETUPS */
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED, 0, 1);
   output_high(PIN_B6);   /* prevent slave resets */
   port_b_pullups(TRUE);

   enable_interrupts(INT_RB);
   enable_interrupts(GLOBAL);

   safe_system();

   while(1)
   {
      /* wait for command */
      get_command();

      printf("STATE=%d\n\r", curr_state);
      state_table();
      delay_ms(delay);
      delay = 0;
   }
}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void state_table()
{
   switch(curr_state)
   {
   case FAULT: state_fault(); break;
   case STBY:   state_stby(); break;
   case ARMED: state_armed(); break;
   case FIRE:   state_fire(); break;
   }
}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void safe_system(void)
{
   printf("SAFE ");
   help();
}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void state_stby()
{
   printf("STBY ");
}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void state_armed()
{
   printf("ARMED ");
   return;

}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void state_fire()
{
      curr_state = ARMED;
      return;
   }
   


/*
 =======================================================================================================================
 =======================================================================================================================
 */
void state_fault()
{
   printf("FAULT ");
}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void debug_test()
{

}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void get_command()
{
   /*~~~~~~~~~~~~~~~~~~~*/
   /* NEW PARSER */
   char   index, j;
   char   found = 0;
   char   command = 0;
   char   lookup_str[12];
   char   name[17];
   char   val[17];
   /*~~~~~~~~~~~~~~~~~~~*/

   /*
    * printf("Parser\r\n");
    */
   while(1)
   {
      state_table();
      printf(">");
      for(i = 0; i < 17; i++) line[i] = 0;   /* clear the line buffer */
      for(i = 0; i <= 16; i++)
      {                  /* get a whole line but limit it to buffer size */
         if((c = getch()) == EOL) break;
         line[i] = c;      /* add the character to the line buffer */
      }                  /* end for loop */

      if(i < 3) continue;      /* may be a blank line, just skip it, variables must be more than 3 */
      line[i] = '\0';

      for(i = 0; (line[i] != ' ') && (i < 10) && (line[i] != '\0'); i++)
      {
         name[i] = line[i];   /* extract the first word up to a space character */
      }

      name[i] = '\0';         /* terminate the string! */

      if(line[i] != '\0')
      {
         i++;            /* skip the space character */
         j = 0;
         for(; (i < 10) && (line[i] != '\0'); i++)
         {
            val[j++] = line[i]; /* now extract the rest of the line, the "value" */
         }

         val[j] = '\0';
      }

      /* LOOK UP COMMAND */
      found = 0;               /* set to not found to start */
      command = 0;
      for(index = 0; index < NUM_COMMANDS; index++)
      {
         sprintf(lookup_str, "%s", commands[index]);
         if(strcmp(name, lookup_str) == 0)
         {
            found = 1;         /* COMMAND IS IN TABLE */
            command = index;
         }
      }

      if(found == 1)            /* COMMAND IS VALID */
      {
         printf("OK\r\n");
         switch(command)
         {
         case 0:               /* FIRE */if(curr_state == ARMED) curr_state = FIRE; break;
         case 1:               /* STBY */curr_state = STBY; break;
         case 2:               /* STAT */send_stat(); break;
         case 3:               /* SHOTS X */shots = atol(val); break;
         case 4:               /* ARM */if(curr_state == STBY) curr_state = ARMED; break;
         case 5:               /* PRF */prf = atol(val); break;
         case 6:               /* RED */red(); break;
         case 7:               /* TEST */debug_test(); break;
         case 8:               /* HELP */help(); break;
         case 9:               /* NOHV */nohv=1; break;
         default:   break;
         }
      }
      else
         printf("ERROR\r\n");   /* COMMAND NOT FOUND */
      state_table();
   }
}

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void send_stat()
{
   printf("\n\rSTAT\n\r");

}



/*
 =======================================================================================================================
 =======================================================================================================================
 */
void help()
{
   printf("\nBlackjack controller V1.0\n\r");
   printf("Commands:\n\r");
   printf("STAT - Status of system\n\r");
   printf("STBY - Put system in Standby\n\r");
   printf("ARM  - Arm the system and turn on the SSU\n\r");
   printf("FIRE - Fire the system\n\r");
   printf("SHOTS # - Set the number of shots to Fire\n\r");
   printf("PRF # - Set the PRF of the System\n\r");
   printf("\n\r");
   printf("Written by Robert 'Red' Hoar for LMMFC - Dallas\n\r");
   printf("\n\r");
}

_________________
===================
|Robert 'Red' Hoar
|
|Program Manager
|Directed Energy RF
===================
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

Mark, can you help ?
PostPosted: Sat May 05, 2007 10:07 am     Reply with quote

I do wish Mark would convert his code to CCS from microchip C.

I think his approach is very good, and would make the basis of a very flexible command line parser.

Unfortunately, I dont have the C skill to do it.

please Mark if V4.033 is now functional for pointer to rom etc, could you have a go

Mike
Steve H.
Guest







PostPosted: Tue May 08, 2007 3:05 pm     Reply with quote

Future - While the parser presented is very, very good you might find a simpler one suitable for your needs. I designed a very simple one several years back. You can download it from the ARRL.org website at,

arrl.org/files/

search for PIC_DAS

It was written for PCM version 2.6XX but I think it should run under 4.XXX.

At any rate - it is a simpler - albeit not as functionally nice alternative, but it does work.

HTH - Steve H.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group