/*
	UREADIO.C

   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program that illustrates
	using the PC keypad to monitor inputs and toggle outputs using the
	virtual driver.

	A jumper wire between any of the inputs and ground will provide a simple
	means for changing the state of the input.

   This program monitors and toggles shadow registers.
*/

#use "default.h"
#use cplc.lib

#if !(BOARD_TYPE==CPLC_BOARD || BOARD_TYPE==L_STAR)
#fatal "This program only runs on a PK21xx/Rugged Giant or a PK22xx/Little Star"
#endif

#define NOLCD                // assumes no LCD is available
main()
{
    int  i;
    int  onoff;
    char c;
    
    uplc_init();
    onoff = 0;

    printf("Toggle F4 (DOS only) to make keyboard input active as STDIN\n");
#if BOARD_TYPE==CPLC_BOARD

    printf("Toggle 'a' to turn on/off outputs O1-O10, R1 & R2.\n");
    printf("Hit 'c' to read digital chans 1 to 7.\n");
    printf("Hit 'd' to read analog chans 1-6,8.\n");

    // Lower threshold is set at 3 volts
    U1LOW = U2LOW = U3LOW = U4LOW = U5LOW = U6LOW = up_uncal(3000);
    
    // Upper threshold is set at 4 volts
    U1HIGH = U2HIGH = U3HIGH = U4HIGH = U5HIGH = U6HIGH = up_uncal(4000);


#elif BOARD_TYPE==L_STAR

    printf("Toggle 'a' to turn on/off outputs O1-O14.\n");
    printf("Hit 'c' to read digital chans 1 to 8.\n");
    printf("Hit 'd' to read digital chans 9 to 16.\n");

#endif

    while (1)
    {
    	  while (kbhit() == 0)
        		runwatch();      // do nothing
		  c = getchar();
        switch (c)
        {
            case 'a':
                if (!onoff)
                {
                    onoff = 1;
                    printf("OUTPUTS are ON.\n");
                    OUT1 = OUT2 = OUT3 = OUT4 = OUT5  = OUT6 = OUT7 = 
                    OUT8 = OUT9 = OUT10 = 1;

#if BOARD_TYPE==CPLC_BOARD
                    RELAY1 = RELAY2 = 1;
#elif BOARD_TYPE==L_STAR
                    OUT11 = OUT12 = OUT13 = OUT14 = 1;
#endif
                }
                else
                {
                    onoff = 0;
                    printf("OUTPUTS are OFF.\n");
                    OUT1 = OUT2 = OUT3 = OUT4 = OUT5  = OUT6 = OUT7 = 
                    OUT8 = OUT9 = OUT10 = 0;

#if BOARD_TYPE==CPLC_BOARD
                    RELAY1 = RELAY2 = 0;
#elif BOARD_TYPE==L_STAR
                    OUT11 = OUT12 = OUT13 = OUT14 = 0;
#endif
                }
                break;
#if BOARD_TYPE==CPLC_BOARD
            case 'c':
                printf("DIGITAL INPUTS chans 1-7\n");
                    
                printf("%d %d %d %d %d %d %d\n",
                DIGIN1, DIGIN2, DIGIN3, DIGIN4, DIGIN5, DIGIN6, DIGIN7);
                    
                break;
            case 'd':
                printf("UNIVERSAL INPUTS chans 1-6\n");
                    
                printf("%d %d %d %d %d %d \n",
                U1IN, U2IN, U3IN, U4IN, U5IN, U6IN);
                    
                break;
#elif BOARD_TYPE==L_STAR
            case 'c':
                printf("DIGITAL INPUTS chans 1-8\n");
                  
                printf("%d %d %d %d %d %d %d %d\n",
                DIGIN1, DIGIN2, DIGIN3, DIGIN4, DIGIN5, DIGIN6, DIGIN7, 
                DIGIN8);
                    
                break;
            case 'd':
                printf("DIGITAL INPUTS chans 9-16\n");
                    
                printf("%d %d %d %d %d %d %d %d\n",
                DIGIN9, DIGIN10, DIGIN11, DIGIN12, DIGIN13, DIGIN14, 
                DIGIN15, DIGIN16);
                    
                break;
#endif     
            default:
                printf(" Try 'a', 'c' or 'd'\n");
                break;
        }
    }
}


