/*
	GATER.C

   PK21xx/Rugged Giant (only) Sample Program that illustrates using ladderc or
   gate programming for control and monitor of i/o.
   
   Runs on Z-World Demo Unit One.
*/

#use "default.h"
#use cplc.lib
#use 5key.lib
#use 5keyextd.lib
#use rtk.lib
#use gate_p.lib

#if !(BOARD_TYPE==CPLC_BOARD)
#fatal "This program only runs on a PK21xx/Rugged Giant"
#endif

/***Begin Globals real_time_kernel  */
int task0(void), task1(void), task2(void), task3(void), 
    task4(void), task5(void), backgnd(void);
int (*Ftask[]) () =
{
    task0, task1, task2, task3, task4, task5, backgnd
};

#define PWORD  154
#define NTASKS 7

#define TASK0 0
#define TASK1 1
#define TASK2 2
#define TASK3 3
#define TASK4 4
#define TASK5 5

/***End Globals real_time_kernel */

#define AND &&
#define OR  ||
#define NOT !


#define RUNKERNEL 1
char time0[15];
char *time0_help[] =
     {
         "Set system time,",
         "use field, up &",
         "down key to chng"
     };
char date0[15];
char *date0_help[] =
     {
         "Set system date,",
         "use field, up &",
         "down key to chng"
     };


char cntrl_on_off;
char *cntrl_help[] =
     {
         "on/off restart",
         "for gate program"
     };
char reset_on_off;
char *reset_help[] =
     {
         "reset digital",
         "out to off,also",
         "null the gates"
     };

char single_step;
char *single_help[] =
     {
         "on/off single",
         "stepping for",
         "gate program"
     };

char ladder_on_off;
char *ladder_help[] =
     {
         "select ladder C",
         "or gate program"
     };

// Function Declarations
void input_output_menu(void);  // IO menu
void time_date_menu(void);     // time date menu
int  gate_server(void);        // gate programming menu
void traffic_lite(void);       // initialize gates, not used
void sequence_lite(void);      // initialize gates, not used
void stepper(void);            // initialize gates, not used
void motor_delay(void);        // initialize gates, not used
void reject_2_defect(void);    // initialize gates, not used
void elevator(void);           // initialize gates, not used
void traffic(void);            // ladder C, not used
void lad_elevator(void);       // ladder C, not used
void r_stepper(void);          // ladder C, not used
void three_speed(void);        // initialize gates, not used
void ladder_demo2(void);       // ladder C control for Demo Unit One
void demo2(void);              // initialize gates for Demo Unit One

extern void f1press(void), f2press(void), f3press(void), f4press(void);
extern void reset_p_gates(void);
extern void find_fkey_press(void);
extern void evaluate_counter(void);
extern void evaluate_gates(void);
extern void evaluate_dac(void);
extern void reset_fkey_press(void);
extern int  timer_menu(void);
extern int  counter_menu(void);
extern int  uset_menu(void);
extern int  dac_menu(void);
extern int  _5key_uinput(void);
extern int  _5key_diginput(void);
extern int  _5key_12out(void);
extern int  _5key_dacout(void);

main()
{
    uplc_init();             // init background drivers
    cntrl_on_off = 0;
    reset_on_off = 0;
    single_step  = 0;
    ladder_on_off= 1;

    reset_p_gates();         // reset gate_points to none
    demo2();                 // initiaze gates for demo2
    _5key_setfunc(f1press, f2press, f3press, f4press);

    upoint[0].ulow_set  = 6000;
    upoint[0].uhigh_set = 7000;
    U1HIGH = up_uncal(7000);
    U1LOW  = up_uncal(6000);
    upoint[1].ulow_set  = 3000;
    upoint[1].uhigh_set = 4000;
    U2HIGH = up_uncal(4000);
    U2LOW  = up_uncal(3000);
    DACOUT2 = 10000;
    C1RLD = 2;
    C2RLD = 1;
    C3RLD = 2;               // reload values for virtual counters
    backgnd();               // background task of the RTK
}

indirect task0(void)         // Control task
{
    if (cntrl_on_off)
    {
        find_fkey_press();   // evaluate function keys
        evaluate_counter();  // evaluate counters
        if (ladder_on_off)   // ladder C
            ladder_demo2();  // ladder C
        else if (!ladder_on_off) 
            evaluate_gates();// or gate program
        evaluate_dac();      // evaluate DACs
        reset_fkey_press();  // reset function keys
    }
}


indirect task3(void)
{
                             // task not used
}

indirect task1(void)
{
                             // task not used
}

indirect task2(void)
{
                             // task not used
}

// task server for 5key system
indirect task4(void)
{
    while (1)
    {
        time_date_menu();    // time date menu
        input_output_menu(); // IO menu
        timer_menu();        // virtual timer menu from gate_p.lib
        counter_menu();      // virtual counter menu from gate_p.lib
        uset_menu();         // universal input level menu from gate_p.lib
        dac_menu();          // discrete dac menu from gate_p.lib
        gate_server();       // gate programming menu
    }
}

indirect task5(void)
{
                             // task not used
}

indirect backgnd(void)
{
    request(TASK4);
    run_every(TASK0, 1);
    while (1) 
        runwatch();
}

// 5key system menu for IO
void input_output_menu(void)
{
    lcd_server(3, 0L, "Input Output Set");
    while (1)
    {
        if (_5key_uinput() == MENU) 
            return;
        if (_5key_diginput() == MENU) 
            return;
        if (_5key_12out() == MENU) 
            return;
        if (_5key_dacout() == MENU) 
            return;
    }
}

// 5key system menu for time and date
void time_date_menu(void)
{
    lcd_server(3, 0L, "Time Date Set");
    while (1)
    {
        if (_5key_time("time   ", time0, time0_help, 
        sizeof(time0_help), 1, 1, 10) == MENU) 
            return;

        if (_5key_date("date   ", date0, date0_help, 
        sizeof(date0_help), 1, 1, 10) == MENU) 
            return;
    }
}

// GATER
// 5 key server for gate programming
// gate_point is declared in gate_p.lib.
int gate_server(void)
{
    int  i, j, k;
    char old_control;

    while (1)
    {
        lcd_server(3, 0L, "GATE PROGRAMMER");
        if ((k = _5key_boolean("LADDER ", &ladder_on_off, ladder_help,
        sizeof(ladder_help), 1, 10) == MENU)) 
            return k;

        old_control = cntrl_on_off;
        
        k = _5key_boolean("ON_OFF ", &cntrl_on_off, cntrl_help,
        sizeof(cntrl_help), 1, 10);

        if (old_control == 1 && cntrl_on_off == 0)
        {
            for (i = 0; i < 12; i++) 
                *(&OUT1 + i) = 0; // make sure all out's zero
            up_dacout(0);         // make sure dacout is zero
            reset_timers();
            reset_cntrs();
            reset_latch();
            reset_fkeys();
            reset_ddacs();
        }
        if (k == MENU) 
            return k;

        if ((k = _5key_boolean("STEP   ", &single_step, single_help,
        sizeof(single_help), 1, 10) == MENU)) 
            return k;

        k = _5key_boolean("RESET  ", &reset_on_off, reset_help,
        sizeof(reset_help), 1, 10);

        if (reset_on_off == 1)
        {
            reset_on_off = 0;
            cntrl_on_off = 0;
            suspend(5);                 // make sure everything is run out
            for (i = 0; i < 12; i++) 
                *(&OUT1 + i) = 0;
            reset_p_gates();            // reset gate_points to none.
            init_gate(IL1, 1, 1, 1, 1); // put one dummy gate, gate_points =1
        }
        if (k == MENU) 
            return k;
        if ((k = do_the_gates(single_step)) == MENU) 
            return k;
    }
}

// Create gates for traffic/pedestrian light control
// not used
/*
void traffic_lite(void)
{
   init_gate(IL1,INOT,IO1,IONE,IONE);
   init_gate(IL2,INOT,IO2,IONE,IONE);
   init_gate(IL3,INOT,IO5,IONE,IONE);
   init_gate(IL4,IAND,IL1,IL2,IL3);
   init_gate(IL1,IAND,IL4,ID6,IONE);

   init_gate(IL10,INOT,IT1O,IONE,IONE);
   init_gate(IL9, IOR, ID7, IL1, IO3);
   init_gate(IL8, IOR, IL9, IT7O,IZERO);
   init_gate(IO3, IAND,IL8, IL10,IONE);

   init_gate(IL8,INOT,IO5,IONE,IONE);
   init_gate(IL7,IOR, ID7,IL1, IO4);
   init_gate(IL9,IOR, IL7,IT7O,IZERO);
   init_gate(IO4,IAND,IL8,IL9, IONE);

   init_gate(IL9, IOR, IL1,IT1I,IZERO);
   init_gate(IT1I,IAND,IL9,IL10,IONE);

   init_gate(IL9, IOR, IT1O,IO2, IZERO);
   init_gate(IL10,INOT,IT2O,IONE,IONE);
   init_gate(IO2, IAND,IL9, IL10,IONE);
   init_gate(IT2I,IAND,IO2, IONE,IONE);


   init_gate(IL9, IOR, IT2O,IT3I,IZERO);
   init_gate(IL10,INOT,IT3O,IONE,IONE);
   init_gate(IT3I,IAND,IL9, IL10,IONE);

   init_gate(IL9, IOR, IT2O,IO1, IZERO);
   init_gate(IL10,INOT,IT7O,IONE,IONE);
   init_gate(IO1, IAND,IL9, IL10,IONE);

   init_gate(IL9, INOT,IT4O,IONE,IONE);
   init_gate(IL10,IOR, IT4I,IT3O,IZERO);
   init_gate(IT4I,IAND,IL9, IL10,IONE);

   init_gate(IL9, INOT,IC1O,IONE,IONE);
   init_gate(IL10,IAND,IT6O,IL9, IONE);
   init_gate(IL8, IOR, IT5I,IT4O,IL10);
   init_gate(IL7, INOT,IT5O,IONE,IONE);
   init_gate(IT5I,IAND,IL7, IL8, IL9);

   init_gate(IL9, INOT,IT6O,IONE,IONE);
   init_gate(IL10,IOR, IT6I,IT5O,IZERO);
   init_gate(IT6I,IAND,IL9, IL10,IONE);

   init_gate(IO5, IOR, IT4I,IT6I,IZERO);
   init_gate(IC1P,IAND,IT6I,IONE,IONE);

   init_gate(IL9, IAND,IT6O,IC1O,IONE);
   init_gate(IL10,IOR, IT7I,IL9, IZERO);
   init_gate(IL8, INOT,IT7O,IONE,IONE);
   init_gate(IT7I,IAND,IL10,IL8, IONE);

   init_gate(IC1R,IAND,IT7I,IONE,IONE);

   init_gate(IDAC2,IAND,IO2,IONE,IONE);
}
*/

// Create gates for sequencing lights
// not used
/*
void sequence_lite(void)
{
   init_gate(IL8, INOT,IF2, IONE, IONE);
   init_gate(IL1, IOR, IF1, IO6,  ITAO);
   init_gate(IL2, INOT,IT8O,IONE, IONE);
   init_gate(IO6, IAND,IL1, IL2,  IL8);
   init_gate(IT8I,IOR, IO6, IZERO,IZERO);
   init_gate(IL1, IOR, IT8O,IO8,  IZERO);
   init_gate(IL2, INOT,IT9O,IONE, IONE);
   init_gate(IO8, IAND,IL8, IL1,  IL2);
   init_gate(IT9I,IOR, IO8, IZERO,IZERO);
   init_gate(IL1, IOR, IT9O,IO9,  IZERO);
   init_gate(IL2, INOT,ITAO,IONE, IONE);
   init_gate(IO9, IAND,IL8, IL1,  IL2);
   init_gate(ITAI,IOR, IO9, IZERO,IZERO);
}
*/

// Create gates for a stepper motor
// not used
/*
void stepper(void)
  {
   init_gate(IL8,INOT,IF2, IONE, IONE);
   init_gate(IL1,IOR, IF1, IO6,  IZERO);
   init_gate(IL2,INOT,IT1O,IONE, IONE);
   init_gate(IO6,IAND,IL1, IL2,  IL8);
   init_gate(IT1I,IOR,IO6, IZERO,IZERO);

   init_gate(IL1, IOR, IT1O,IO1,  ITAO);
   init_gate(IL2, INOT,IT7O,IONE, IONE);
   init_gate(IO1, IAND,IL1, IL2,  IL8);
   init_gate(IT7I,IOR, IO1, IZERO,IZERO);

   init_gate(IL1, IOR, IT7O,IO2,  IZERO);
   init_gate(IL2, INOT,IT8O,IONE, IONE);
   init_gate(IO2, IAND,IL1, IL2,  IL8);
   init_gate(IT8I,IOR, IO2, IZERO,IZERO);

   init_gate(IL1, IOR, IT8O,IO3,  IZERO);
   init_gate(IL2, INOT,IT9O,IONE, IONE);
   init_gate(IO3, IAND,IL1, IL2,  IL8);
   init_gate(IT9I,IOR, IO3, IZERO,IZERO);

   init_gate(IL1, IOR, IT9O,IO4,  IZERO);
   init_gate(IL2, INOT,ITAO,IONE, IONE);
   init_gate(IO4, IAND,IL1, IL2,  IL8);
   init_gate(ITAI,IOR, IO4, IZERO,IZERO);
  }
*/

// Create gates for motor delay control
// not used
/*
void motor_delay(void)
{
   init_gate(IL10,INOT,ID1, IONE,IONE);
   init_gate(IL9, IOR, IO1, IL10,IZERO);
   init_gate(IO1, IAND,IL9, ID2, IONE);
   init_gate(IT1I,IAND,IO1, IONE,IONE);
   init_gate(IL10,INOT,ID2, IONE,IONE);
   init_gate(IL7, IAND,IL10,IO2, IONE);
   init_gate(IL9, IOR, IL1, IL7, IZERO);
   init_gate(IL8, INOT,IT2O,IONE,IONE);
   init_gate(IL1, IAND,IL9, IL8, IONE);
   init_gate(IT2I,IAND,IL1, IONE,IONE);
   init_gate(IO2, IOR, IT1O,IL1, IZERO);
}
*/

// Create gates for a two defect rejector
// Not used
/*
void reject_2_defect(void)
{
   init_gate(IL1, INOT,ID2, IONE, IONE);
   init_gate(IC1R,IOR, IT1O,IL1,  IZERO);
   init_gate(IC1P,IAND,ID1, IONE, IONE);
   init_gate(IO1, IOR, IC1O,IZERO,IZERO);
   init_gate(IO2, IOR, IC1O,IZERO,IZERO);
   init_gate(IT1I,IOR, IC1O,IZERO,IZERO);
   init_gate(IO3, IOR, ID2, IZERO,IZERO);
}
*/

// Create gates for a elevator sequencer
// not used
/*
void elevator(void)
{
    init_gate(IL10,IAND,IO1, ID5, IONE);
    init_gate(IL9, INOT,IL10,IONE,IONE);
    init_gate(IL10,IAND,IO2, ID6, IONE);
    init_gate(IL7, INOT,IL10,IONE,IONE);
    init_gate(IL10,IAND,IO3, ID7, IONE);
    init_gate(IL8, INOT,IL10,IONE,IONE);

    init_gate(IL1,IOR, IO1,IF1, IZERO);
    init_gate(IL2,INOT,ID5,IONE,IONE);
  
    init_gate(IO1,IAND,IL1,IL2,IONE);

    init_gate(IL1,IOR, IO2,IF2, IZERO);
    init_gate(IL2,INOT,ID6,IONE,IONE);
   
    init_gate(IO2,IAND,IL1,IL2,IONE);

    init_gate(IL1,IOR, IO3,IF3, IZERO);
    init_gate(IL2,INOT,ID7,IONE,IONE);
   
    init_gate(IO3,IAND,IL1,IL2,IONE);

    init_gate(IL1,IAND,ID5,IO2, IONE);
    init_gate(IL2,IAND,ID5,IO3, IONE);
    init_gate(IL3,INOT,IO8,IONE,IONE);
    init_gate(IL4,INOT,IO1,IONE,IONE);
    init_gate(IL5,IAND,ID6,IL3, IL4);
    init_gate(IL6,IAND,IO3,IL5, IONE);
    init_gate(IL5,IOR, IL1,IL2, IL6);
    init_gate(IL6,IOR, IL5,IO9, IZERO);
    init_gate(IO9,IAND,IL7,IL8, IL6);

    init_gate(IL1,IAND,ID7,IO2, IONE);
    init_gate(IL2,IAND,ID7,IO1, IONE);
    init_gate(IL3,INOT,IO9,IONE,IONE);
    init_gate(IL4,IAND,ID6,IL3, IO1);
    init_gate(IL5,IOR, IL1,IL2, IL4);
    init_gate(IL6,IOR, IL5,IO8, IZERO);
    init_gate(IO8,IAND,IL6,IL7, IL9);

    init_gate(IO4,IAND,ID5,IONE,IONE);
    init_gate(IO5,IAND,ID6,IONE,IONE);
    init_gate(IO6,IAND,ID7,IONE,IONE);
}
 */


// Ladder C for Traffic Light Control
// not used
/*
void traffic(void)
{
                             // Traffic light example
      L1 = DIGIN6 AND NOT OUT1 AND NOT OUT2 AND NOT OUT5;
      
      OUT3 = (DIGIN7 OR L1 OR OUT3 OR T7O) AND (NOT T1O);
      OUT4 = (DIGIN7 OR L1 OR OUT4 OR T7O) AND (NOT OUT5);

      T1I = (L1 OR T1I) AND (NOT T1O);

      T2I = OUT2 = (T1O OR OUT2) AND (NOT T2O);
      T3I =        (T2O OR T3I ) AND (NOT T3O);
      OUT1=        (T2O OR OUT1) AND (NOT T7O);
      T4I =        (T3O OR T4I ) AND (NOT T4O);

      T5I = ((T6O AND NOT C1O) OR T5I OR T4O) AND (NOT T5O) AND (NOT C1O);

      T6I = (T6I OR T5O) AND (NOT T6O);
      
      C1P  = T6I;
      OUT5 = T6I OR T4I;
      T7I  = (T7I OR (T6O AND C1O) ) AND (NOT T7O);
      C1R  = T7I;
      DAC2 = OUT2;
}
*/

// Ladder C for elevator control
// not used
/*
void lad_elevator(void)
{

    L1 = NOT (OUT1 AND DIGIN5);
    L2 = NOT (OUT2 AND DIGIN6);
    L3 = NOT (OUT3 AND DIGIN7);

    OUT1 = (OUT1 OR F1) AND (NOT DIGIN5);
    OUT2 = (OUT2 OR F2) AND (NOT DIGIN6);
    OUT3 = (OUT3 OR F3) AND (NOT DIGIN7);

    OUT9 = (OUT9 OR NOT OUT8 AND 
             ( 
             (DIGIN5 AND OUT2) OR 
             (DIGIN5 AND OUT3) OR 
             (DIGIN6 AND OUT3 AND NOT OUT1) 
             ) 
           ) AND L2 AND L3;

    OUT8 = (OUT8 OR NOT OUT9 AND 
             (
             (DIGIN7 AND OUT2) OR
             (DIGIN7 AND OUT1) OR
             (DIGIN6 AND OUT1)
             )
           ) AND L1 AND L2;

    OUT4 = DIGIN5;
    OUT5 = DIGIN6;
    OUT6 = DIGIN7;
    DAC2 = OUT9 OR OUT8;
}
*/

// Ladder C for stepper motor control
// not used
/*
void r_stepper(void)
{
      L1  = NOT (OUT1 AND DIGIN5);
      L2  = NOT (OUT2 AND DIGIN6);
      L3  = NOT (OUT3 AND DIGIN7);
      L4  = NOT (OUT4 AND DIGIN3);

      T1I = (T1I OR NOT L1) AND NOT T1O;
      T2I = (T2I OR NOT L2) AND NOT T2O;
      T3I = (T3I OR NOT L3) AND NOT T3O;
      T4I = (T4I OR NOT L4) AND NOT T4O;

    OUT1  = (OUT1 OR F1) AND (NOT DIGIN5);
    OUT2  = (OUT2 OR F2) AND (NOT DIGIN6);
    OUT3  = (OUT3 OR F3) AND (NOT DIGIN7);
    OUT4  = (OUT4 OR F4) AND (NOT DIGIN3);

    OUT9  = ( OUT9 OR
              ( NOT OUT10 AND
                (
                (DIGIN5 AND OUT2) OR (DIGIN5 AND OUT3 AND NOT OUT4) OR 
                (DIGIN6 AND OUT3) OR (DIGIN6 AND OUT4 AND NOT OUT1) OR 
                (DIGIN7 AND OUT4) OR (DIGIN7 AND OUT1 AND NOT OUT2) OR 
                (DIGIN3 AND OUT1) OR (DIGIN3 AND OUT2 AND NOT OUT3)
                )
              ) 
            ) AND NOT T1I AND NOT T2I AND NOT T3I AND NOT T4I AND L1 AND 
            L2 AND L3 AND L4;

     OUT10 =( OUT10 OR
              ( NOT OUT9 AND
                (
                  (DIGIN5 AND OUT4) OR (DIGIN6 AND OUT1) OR 
                  (DIGIN7 AND OUT2) OR (DIGIN3 AND OUT3)
                )
              )
            ) AND NOT T1I AND NOT T2I AND NOT T3I AND NOT T4I AND 
            L1 AND L2 AND L3 AND L4;

      OUT5 = DIGIN5;
      OUT6 = DIGIN6;
      OUT7 = DIGIN7;
      OUT8 = DIGIN3;

      DAC2 = T1I OR T2I OR T3I OR T4I;
   }
*/


// Create Gate for three speed motor control
// not used
/*
void three_speed(void)
{
       init_gate(IL1, INOT, IF1, IONE, IONE);
       init_gate(IL2, INOT, IF2, IONE, IONE);
       init_gate(IL3, INOT, IF3, IONE, IONE);
       init_gate(IL4, INOT, IF4, IONE, IONE);

       init_gate(IL10, IOR,  IDAC1,IF1, IZERO);
       init_gate(IL9,  IAND, IL10, IL2, IL3);
       init_gate(IDAC1,IAND, IL9,  IL4, IONE);

       init_gate(IL10, IOR,  IDAC2,IF2, IZERO);
       init_gate(IL9,  IAND, IL10, IL1, IL3);
       init_gate(IDAC2,IAND, IL9,  IL4, IONE);

       init_gate(IL10, IOR,  IDAC3,IF3, IZERO);
       init_gate(IL9,  IAND, IL10, IL1, IL2);
       init_gate(IDAC3,IAND, IL9,  IL4, IONE);
}
*/


#define START        DIGIN1
#define STOP         NOT DIGIN2
#define PHOTO_SWITCH DIGIN3
#define PHOTO_CELL   U2IN
#define THERMISTOR   U1IN
#define C1RESET      C1R
#define C1DONE       C1O
#define C1PULSE      C1P
#define C2RESET      C2R
#define C2DONE       C2O
#define C2PULSE      C2P
#define COIL1        OUT1
#define COIL2        OUT2
#define COIL3        OUT3
#define COIL4        OUT4
#define ON_LITE      OUT9
#define OFF_LITE     OUT10
#define HEAT_LITE    OUT8
#define LITE1        OUT5
#define LITE2        OUT6
#define LITE3        OUT7
#define CCWISE       L3

// Ladder C demo for One Module to the Control Show 3/1993
void ladder_demo2(void)
{
    C1RESET = START OR C1DONE;
    C1PULSE = PHOTO_SWITCH;
    CCWISE  = (CCWISE OR C1DONE) AND NOT(CCWISE AND C1DONE);

    T1I = (T1I OR (START AND NOT ON_LITE) OR T4O) AND NOT T1O AND NOT STOP;

    T2I = (T2I OR T1O) AND NOT T2O AND NOT STOP;
    T3I = (T3I OR T2O) AND NOT T3O AND NOT STOP;
    T4I = (T4I OR T3O) AND NOT T4O AND NOT STOP;

    COIL1 = (NOT CCWISE AND T1I) OR (CCWISE AND T4I);
    COIL2 = (NOT CCWISE AND T2I) OR (CCWISE AND T3I);
    COIL3 = (NOT CCWISE AND T3I) OR (CCWISE AND T2I);
    COIL4 = (NOT CCWISE AND T4I) OR (CCWISE AND T1I);

    RELAY2 = HEAT_LITE = THERMISTOR AND ON_LITE;
    DAC2 = NOT THERMISTOR AND ON_LITE;

    C2RESET = START OR C2DONE;
    C2PULSE = PHOTO_CELL AND ON_LITE;

    T5I = (T5I OR T7O OR C2DONE) AND NOT T5O AND NOT C1DONE AND NOT STOP;
    
    T6I = (T6I OR T5O) AND NOT T6O AND NOT C1DONE AND NOT STOP;
    T7I = (T7I OR T6O) AND NOT T7O AND NOT C1DONE AND NOT STOP;

    LITE1 = (NOT CCWISE AND T5I) OR (CCWISE AND T7I);
    LITE2 = (NOT CCWISE AND T6I) OR (CCWISE AND T6I);
    LITE3 = (NOT CCWISE AND T7I) OR (CCWISE AND T5I);

    ON_LITE = (ON_LITE OR START) AND NOT STOP;
    OFF_LITE = NOT ON_LITE;

}

// Create gates for demo program for Control Show, 3/1993
void demo2(void)
{
    init_gate(IL1,  IAND, ID1,  IONE, IONE);  // latch start
    init_gate(IL2,  IAND, ID2,  IONE, IONE);  // latch stop

    init_gate(IC1R, IOR,  IL1,  IC1O, IZERO);
    init_gate(IC1P, IAND, ID3,  IONE, IONE);  // count pulse
    init_gate(IL10, IOR,  IL3,  IC1O, IZERO);
    init_gate(IL9,  IAND, IL3,  IC1O, IONE);
    init_gate(IL8,  INOT, IL9,  IONE, IONE);
    init_gate(IL3,  IAND, IL10, IL8,  IONE);  // switch direction

    init_gate(IL9,  INOT, IO9,  IONE, IONE);
    init_gate(IL10, IAND, IL9,  IL1,  IONE);
    init_gate(IL10, IOR,  IT1I, IL10, IT4O);
    init_gate(IL9,  INOT, IT1O, IONE, IONE);
    init_gate(IT1I, IAND, IL10, IL9,  IL2);

    init_gate(IL10, IOR,  IT2I, IT1O, IZERO);
    init_gate(IL9,  INOT, IT2O, IONE, IONE);
    init_gate(IT2I, IAND, IL10, IL9,  IL2);

    init_gate(IL10, IOR,  IT3I, IT2O, IZERO);
    init_gate(IL9,  INOT, IT3O, IONE, IONE);
    init_gate(IT3I, IAND, IL10, IL9,  IL2);

    init_gate(IL10, IOR,  IT4I, IT3O, IZERO);
    init_gate(IL9,  INOT, IT4O, IONE, IONE);
    init_gate(IT4I, IAND, IL10, IL9,  IL2);

    init_gate(IL4,  INOT, IL3,  IONE, IONE);

    init_gate(IL10, IAND, IL4,  IT1I, IONE);
    init_gate(IL9,  IAND, IL3,  IT4I, IONE);
    init_gate(IO1,  IOR,  IL10, IL9,  IZERO);

    init_gate(IL10,IAND, IL4,  IT2I, IONE);
    init_gate(IL9, IAND, IL3,  IT3I, IONE);
    init_gate(IO2, IOR,  IL10, IL9,  IZERO);

    init_gate(IL10,IAND, IL4,  IT3I, IONE);
    init_gate(IL9, IAND, IL3,  IT2I, IONE);
    init_gate(IO3, IOR,  IL10, IL9,  IZERO);

    init_gate(IL10,IAND, IL4,  IT4I, IONE);
    init_gate(IL9, IAND, IL3,  IT1I, IONE);
    init_gate(IO4, IOR,  IL10, IL9,  IZERO);

    init_gate(IR2,  IAND, IU1, IO9,  IONE);
    init_gate(IO8,  IAND, IR2, IONE, IONE);
    init_gate(IL10, INOT, IU1, IONE, IONE);
    init_gate(IDAC2,IAND, IL10,IO9,  IONE);

    init_gate(IC2R, IOR,  IL1, IC2O, IZERO);
    init_gate(IC2P, IAND, IU2, IO9,  IONE);


    init_gate(IL5,  INOT, IC1O, IONE, IONE);
    init_gate(IL10, IOR,  IT5I, IC2O, IT7O);
    init_gate(IL9,  INOT, IT5O, IONE, IONE);
    init_gate(IL10, IAND, IL10, IL9,  IL5);
    init_gate(IT5I, IAND, IL10, IL2,  IONE);

    init_gate(IL10, IOR,  IT6I, IT5O, IZERO);
    init_gate(IL9,  INOT, IT6O, IONE, IONE);
    init_gate(IL10, IAND, IL10, IL9,  IL5);
    init_gate(IT6I, IAND, IL10, IL2,  IONE);

    init_gate(IL10, IOR,  IT7I, IT6O, IZERO);
    init_gate(IL9,  INOT, IT7O, IONE, IONE);
    init_gate(IL10, IAND, IL10, IL9,  IL5);
    init_gate(IT7I, IAND, IL10, IL2,  IONE);

    init_gate(IL10, IAND, IL4, IT5I,  IONE);
    init_gate(IL9,  IAND, IL3, IT7I,  IONE);
    init_gate(IO5,  IOR,  IL10,IL9,   IZERO);

    init_gate(IL10, IAND, IL4, IT6I, IONE);
    init_gate(IL9,  IAND, IL3, IT6I, IONE);
    init_gate(IO6,  IOR,  IL10,IL9,  IZERO);

    init_gate(IL10, IAND, IL4, IT7I, IONE);
    init_gate(IL9,  IAND, IL3, IT5I, IONE);
    init_gate(IO7,  IOR,  IL10,IL9,  IZERO);

    init_gate(IL10, IOR,  IO9, IL1, IZERO);
    init_gate(IO9,  IAND, IL10,IL2, IONE);

    init_gate(IO10, INOT, IO9, IONE, IONE);
}
