#use "default.h"

#if ((BOARD_TYPE==CPLC_BOARD) || (BOARD_TYPE==L_STAR))
#use cplc.lib
#endif

/*
	UITEST.C
	
   - Tests the PK8200/universal input card with any board that has
     the PLCBUS.

   - Tests digital outputs and digital inputs on PK8200/UIO board
     number 0 or 1.

   - To pull all inputs low, connect jumpers to J3 and J4.

   - To pull all inputs high, connect jumpers to J2 and J5.
*/

#define TESTMODE UIDIGITAL   // for testing only.
                             // UIDIGITAL for digital input mode.
                             // UIUNIVERSAL for analog input mode.

char acopy[17]="xxxxxxxxxxxxxxxx";

main()  
{
char c;
int i,board;
char latchinput[20],oldlatch[20];

#if ((BOARD_TYPE==CPLC_BOARD) + (BOARD_TYPE==L_STAR))
   uplc_init();
#endif

   reset_pbus();
   reset_pbus_wait();
  
#if (BOARD_TYPE==L_STAR)
  reset_pbus_wait();    // do twice to accommodate the PK2200/Little Star
#endif

	board = 0;
	ui_there = 0;
		
   _uiLocateBrd();      // set bits in ui_there for active boards
                        // (bit 0 = board 0, bit 1 = board 1, etc...)

   if( bit(&ui_there,0) ) board=0;  // determine lowest board number
   if( bit(&ui_there,1) ) board=1;

   printf("Board %d is the last board found.\n\n",board);
   
   _uiSetIMode(board,UIDIGITAL);

   strcpy(latchinput,acopy); // copy xxxx...xxx to latchinput
   strcpy(oldlatch,acopy);

   ui[board].OUT[0]=ui[board].OUT[1]=ui[board].OUT[2]=ui[board].OUT[3]=0;
   ui[board].OUT[4]=ui[board].OUT[5]=0;

   printf("Outputs are OFF. \nTest Outputs...\n");
   printf("\nWaiting for all Inputs to be pulled HIGH...\n");
   
   while( strcmp( latchinput,"1111111111111111") != 0 )
   {
      runwatch();
      hitwd();
      ui_drivers();
      for(i=0;i<16;i++)
      {
         if(ui[board].DIG[i]==1) latchinput[i]='1';
      }
      if(strcmp(oldlatch,latchinput)!=0)
      {
         printf("%s\r",latchinput);
         strcpy(oldlatch,acopy);
      }
   }

   printf("\nInputs are now pulled HIGH!\n");
   printf("\nWaiting for all inputs to be pulled LOW....\n");
   strcpy(latchinput,acopy);

   while(strcmp(latchinput,"0000000000000000")!=0)
   {
      runwatch();
      hitwd();
      ui_drivers();
      for(i=0;i<16;i++)
      {
	      if(ui[board].DIG[i]==0) *(latchinput+i)='0';
      }
      if(strcmp(oldlatch,latchinput)!=0)
      {
         printf("%s\r",latchinput);
         strcpy(oldlatch,acopy);
      } 
   }

   printf("\nInputs are now pulled LOW!\n");
   printf("\nOutputs are now ON. \nTest Outputs...\n");
   ui[board].OUT[0]=ui[board].OUT[1]=ui[board].OUT[2]=ui[board].OUT[3]=1;
   ui[board].OUT[4]=ui[board].OUT[5]=1;
   ui_drivers();
}