#use "default.h"

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

/*
	UIEXPIO.C
	
   - Sample program for using the XP8100/universal input board with any
     Z-World board that has the PLCBUS.

   - The library is set so that the accessing of the io channels is done
     with a channel number. The address of the board is automatically
     computed. For the user's information:

     Board Address    Input chans    Output chans
     0x040            0-15           0-5
     0x041            16-31          16-21
     .
     .
     0x04e            224-239        224-229
     0x04f            240-255        240-245

  - For digital input, connect jumper DAV to VL, the threshold is set with
         Vthresh = CV *( R5/(R4+R5)).
      with R4=R5=10k,  Vthresh = 0.5 * CV.    

  - Use _uiDigin(chan) and _uiOut(chan,state) to access the digital io's.

  - Use _uiLocateBrd() to find the available ui boards (only the first 16 
    boards are located, i.e. 0x040 to 0x04f).

  - If the board is not present, _uiDigin(chan) and _uiOut(chan,state) will
    return a -1.

  - If the XP21XX/CPLC is used as the processor board, the input channels
    can be configured to read analog voltage by removing the channel's jumper
    on header J2, J3, J4 or J5 and connecting H4 pin DAV to pin DA. Bring in the
    XP21xx/CPLC UEXP output to the XP8100/UIO board's DA wago connection. Use
    _uiAnalog(chan) to read in the 10 bit analog voltage ( result is 0-10000 mv).
*/

main()  
{
int i;
char c;

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

#if (BOARD_TYPE==CPLC_BOARD)
		init_expcal();				// exposed calibration values of EXPOUT.
#endif

    reset_pbus();
    reset_pbus_wait();

#if BOARD_TYPE==L_STAR    
    reset_pbus_wait();    // do twice to accomodate the PK22XX/Little Star
#endif    

    _uiLocateBrd();  // find the available universal input boards

    printf("Toggle F4 (DOS only) to make keyboard input active as STDIO.");
    printf("\n\nHit 'a' to turn on outputs 0-5, 16-21.");
    printf("\nHit 'b' to turn off outputs 0-5, 16-21.");
    printf("\nHit 'c' to read digital ch 0-15 and 16-31.");
    printf("\nHit 'd' if using PK21xx/CPLC analog channels 0-7, 16-23.");
    printf("\n\n-1 is returned if the channel is not present.\n");
    
    while(1)
    {
       while(kbhit()==0) {
       	runwatch(); // do nothing
        	hitwd();
       }

       c = getchar();

       switch(c)
       {
         case 'a': printf("OUTPUTS are ON.\n");
       	  // board 0 or 0x040 for chans 0-5
           _uiOut(0,1);_uiOut(1,1);_uiOut(2,1);
           _uiOut(3,1);_uiOut(4,1);_uiOut(5,1);
           // board 1 or 0x041 for chans 16-21
           _uiOut(16,1);_uiOut(17,1);_uiOut(18,1);
           _uiOut(19,1);_uiOut(20,1);_uiOut(21,1);
         break;
         case 'b': printf("OUTPUTS are OFF.\n");
           // board 0 or 0x040 for chans 0-5
           _uiOut(0,0);_uiOut(1,0);_uiOut(2,0);
           _uiOut(3,0);_uiOut(4,0);_uiOut(5,0);
           // board 1 or 0x041 for chans 16-21
           _uiOut(16,0);_uiOut(17,0);_uiOut(18,0);
           _uiOut(19,0);_uiOut(20,0);_uiOut(21,0);
         break;
         case 'c': printf("DIGITAL INPUTS\n");
           printf("chans 0-15 for board 0 (0x040)\n");
           printf("%d%d%d%d%d%d%d%d   %d%d%d%d%d%d%d%d\n",
           _uiDigin(0),_uiDigin(1),_uiDigin(2),_uiDigin(3),
           _uiDigin(4),_uiDigin(5),_uiDigin(6),_uiDigin(7),
           _uiDigin(8),_uiDigin(9),_uiDigin(10),
           _uiDigin(11),_uiDigin(12),_uiDigin(13),
           _uiDigin(14),_uiDigin(15));
           printf("chans 16-31 for board 1 (0x041)\n");
           printf("%d%d%d%d%d%d%d%d   %d%d%d%d%d%d%d%d\n",
           _uiDigin(16),_uiDigin(17),_uiDigin(18),
           _uiDigin(19),_uiDigin(20),_uiDigin(21),
           _uiDigin(22),_uiDigin(23),_uiDigin(24),
           _uiDigin(25),_uiDigin(26),_uiDigin(27),
           _uiDigin(28),_uiDigin(29),_uiDigin(30),
           _uiDigin(31));
         break;
#if BOARD_TYPE==CPLC_BOARD
         case 'd': printf("ANALOG INPUTS\n");
           printf("chans 0-7 for board 0 (0x040)\n");
           printf("%d %d %d %d %d %d %d %d millivolts\n",
           _uiAnalog(0),_uiAnalog(1),_uiAnalog(2),
           _uiAnalog(3),_uiAnalog(4),_uiAnalog(5),
           _uiAnalog(6),_uiAnalog(7));
           printf("chans 16-23 for board 1 (0x041)\n");
           printf("%d %d %d %d %d %d %d %d millivolts\n",
           _uiAnalog(16),_uiAnalog(17),_uiAnalog(18),
           _uiAnalog(19),_uiAnalog(20),_uiAnalog(21),
           _uiAnalog(22),_uiAnalog(23));
           break;
#else
         case 'd':
           printf("ANALOG OPTION IS NOT POSSIBLE OR NOT USED.\n");
           break;
#endif

         default: printf(" Try 'a', 'b', 'c' or 'd'\n");
         break;
       }
    }
 }


