#use "default.h"

#if !((BOARD_TYPE==LITTLE_GIANT))
#fatal "This program only runs on the BL11xx/Little Giant."
#endif

/*
	RELDEMOL.C
	
	Relay Demo Program - For BL11xx/Little Giant
    
   This Program first searches for all relay8 and relay6 boards on the
   PLCBus. Each possible relay board address (64 total) is polled to make
   a list of existing boards. This list is then 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.

	Use the special BL11xx/Little Giant PLCBus cable to attach the relay
	board(s). To power the Relay6, connect +24 Volts DC and Ground to
	header H3 and remove the jumper from J2.
*/

#use "pbus_lg.lib"

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

  Reset_PBus();
  stall(5000);   /* 350 ms wait required after reset */

  while(1)
  {
    found=0;

    printf("\nNodes Found: "); 

    // Locate all Relay Board Nodes

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

      if ( Poll_PBus_Node(Relay_Board_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++)
    {
      for (i=0;i<8;i++)
      {
        hitwd();
        Set_PBus_Relay(boardlist[j],i,1);          // Turn on relay
        stall(1000);
        Set_PBus_Relay(boardlist[j],i,0);          // Turn Off 
       }

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

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

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

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