/*
	MGPLCRLY.C
	
	BL14xx/MicroG or BL15xx/MicroG2
	
   Uses the PIODA on to read/write any 4 bit PLCBUS device.

   The following wiring is used:

      PA0      /STRB  on the PLCBUS
      PA1      D0
      PA2      D1
      PA3      D2
      PA4      D3
      PA5      A1
      PA6      A2
      PA7      A3

      Vcc      Vcc
      Gnd      Gnd.

	The 24v (if needed) to the PLCBUS device should be brought in directly
	to the PLCBUS device.

	In this sample program, the relays of the first RELAY6 or RELAY8 board
	found are turned on and off 5 times.

	You may optionally use the MicroG-to-PLCBus/LCD adapter instead of
	performing the above wiring.
*/

main()
{
int i, j, k;
int logical_addr;
   
   mgreset_pbus();   // reset the PLCBUS

   logical_addr = -1;   // for no address
   for(i=0;i<64;i++)
   {
      if( mgplc_poll_node(mgplcrly_board(i)))
      {
         logical_addr = i;
         break;
      }
   }

   if(logical_addr == -1)
   {
      printf("Cannot find any relay board on the PLCBUS.\n");
   }
   else
   {
      printf("Found Relay Board with board addr >> 0x%x.\n", mgplcrly_board(logical_addr));
      for(i=0;i<5;i++)
      {
         printf("Loop  count %d\n", i);
         for(j=0;j<6;j++)
         {
            hitwd();
            runwatch();
            mgplc_set_relay(logical_addr, j, 1);   // turn on a relay
            for(k=0;k<5000;k++);
         }

         hitwd();
         for(j=0;j<6;j++)
            mgplc_set_relay(logical_addr, j, 0);   // turn off all the relays
         for(k=0;k<5000;k++);                      // delay
      }
   }
}