#use "default.h"
#use "plc_exp.lib"

/*
	RELDEMOP.C
	
	Relay Demo Program - For PLC
    
   This Program 1st searches for all XP8400/relay8 and XP83x0/relay6 boards
	on the PLCBus.

	Each possible relay board address (64 total) is polled to make a list
	of existing boards.  This list is displayed in the standard I/O window.

   Each of the existing board's relays are then activated and deactivated
	in order, finishing with an all on/off sequence. This cycle continues
	indefinitely.
*/

main()
{
int i,j,found,boardlist[64];

  reset_pbus();
  Delay(1000);               // wait required after reset 

  while(1) {
  
  	 found=0;
    printf("\nLogical relay addresses found: ");

    // Locate all Relay Board Nodes - Flash to show those found

    for (i=0;i<64;i++)
    {
      hitwd();

      if ( plc_poll_node(plcrel_addr(i)))
      {
        boardlist[found++]=i;

        printf(" %d ",i); 

        if ((found%10)==0) printf("\n");
       }
     }

   
    // Set Relays on found boards

    for (j=0;j<found;j+=1)
    {
      for (i=0;i<8;i++)
      {
        hitwd();
        plc_set_relay(boardlist[j],i,1);          // Turn on relay 
        stall(1000);                             
        plc_set_relay(boardlist[j],i,0);          // Turn Off 
      }

      for (i=0;i<8;i++)    
        plc_set_relay(boardlist[j],i,1);         // Turn on all on 1 board 

      hitwd();   
      stall(2300);
   
      for (i=0;i<8;i++)
        plc_set_relay(boardlist[j],i,0);         // Turn all off 
      }
   }
}

Delay(int ms)                      // Max Delay time = 2375 ms
{
unsigned int ival,i,j;

  ival = (int)(ms * 27.30667)+1;    
  for (i=0;i<ival;i++) {
  	j=j;
  	hitwd();
  }
}    

nodebug int stall(int tix)
{
static int k;

    for (; tix; --tix)
        for (k = 0; k < 10; ++k) hitwd();
}
