#use "default.h"
#use vdriver.lib

#if (BOARD_TYPE!=LittleG)
#fatal "This program only runs on the BL16xx/LittleG."
#endif

/*
	GENCSTAT.C
	
   Example of costatement (formerly function block) programming and
   the use of the virtual output variables OUTB1 through OUTB8.

   Use waitfor() statements to control the sequencing of bits of the
   output port OUTBYTE and use CoBegin() statements to explicitly
   execute the costatements "check_input" and "clean_up".
   
   Loop for five times.

   All outputs will be off if the variable not_stop is 0.

   The state of the output can be read back on inport(INENLO) or could be
   monitored individually via DIGIN1 to DIGIN8. Loop back the output 
   to the input:
    
            Header H1           Header H2 
            1, IN00, DIGIN1     13, OUTB1
            4, IN01, DIGIN2     14, OUTB2
            3, IN02, DIGIN3     15, OUTB3
            6, IN03, DIGIN4     16, OUTB4
            5, IN04, DIGIN5     17, OUTB5
            8, IN05, DIGIN6     18, OUTB6
            7, IN06, DIGIN7     19, OUTB7
           10, IN07, DIGIN8     20, OUTB8
*/

extern CoData check_input;
extern CoData clean_up;

main()
{
int not_stop;
int  start;
char inp_previous;
int  loop_count;

    VdInit();              // call the virtual drivers
    
    not_stop = 1;             // 
    loop_count = 5;           // loop for 5 times
    inp_previous = 0xff;

    // turn all outputs off using virtual output variables
    
    OUTB1 = OUTB2 = OUTB3 = OUTB4 = OUTB5 = OUTB6 = OUTB7 = OUTB8 = 0; 

    while(1)
    {
        runwatch();
        
        costate round_robin always_on
        {
            if( !(loop_count--)) 
            {
               CoBegin(&clean_up);
               yield;                  // yield and end after 5 loops
            }

            if( loop_count >= 0)
               printf("\n%d input STATES >> ", 5 - loop_count);
            if( inport(INENLO) == 0xff)
                printf("NO IO Loop Back !!! ");
            OUTB8 = 0;
            OUTB1 = 1;
            CoBegin(&check_input);
            
            waitfor( DelayMs(500L) || !not_stop);
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }
            
            OUTB1 = 0;
            OUTB2 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }
              
            OUTB2 = 0;
            OUTB3 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }

            OUTB3 = 0;
            OUTB4 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }
            
            OUTB4 = 0;
            OUTB5 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }
            
            OUTB5 = 0;
            OUTB6 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }
            
            OUTB6 = 0;
            OUTB7 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }
            
            OUTB7 = 0;
            OUTB8 = 1;
            CoBegin(&check_input);
            waitfor( DelayMs(500L) || !not_stop); 
            if(!not_stop)
            {
               CoBegin(&clean_up); 
               yield;
            }

        }

        costate check_input
        {
            waitfor(DelayMs(25L));  // make sure OUT's are updated
            if( inport(INENLO) != inp_previous )
            {
                inp_previous = inport(INENLO);
            }
            printf("%x ", inp_previous);
        }

        costate clean_up
        {
            OUTB1 = OUTB2 = OUTB3 = OUTB4 = OUTB5 = OUTB6 = OUTB7 = OUTB8 = 0;
            break; 
        }

   }
   printf("\nDone.");
}








































































        

