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

#if (BOARD_TYPE!=LittleG)
#fatal "This program only runs on the BL16xx/LittleG."
#endif

/*
	GENVDVR.C
	
   Sample program to read and write the IO of the BL16xx/LittleG.

   Uses the virtual driver to access the IO.
   
   DIGIN1 to DIGIN12 correspond to IN00 to IN11, respectively.
   OUTB1 to OUTB8 are the bit outputs on H2.
   HC1 to HC6 are the high voltage drivers on J2.
*/

main()
{
char c;
int HC_onoff;
int OUTB_onoff;
int i;

    HC_onoff = 0;       // outputs are off
    OUTB_onoff = 0;     // outputs are off
        
    uplc_init();        // enable the virtual driver

    printf("Toggle F4 to make keyboard input active as STDIN (DOS only).\n");
    printf("Hit 'a' to toggle outputs OUTB1 to OUTB8 \n");
    printf("Hit 'b' to toggle outputs HC1 to HC6. \n");                  
    printf("Hit 'c' to read inputs DIGIN1 to DIGIN8.\n");
    printf("Hit 'd' for DIGIN9 to DIGIN12.\n");

    while(1)
    {
        runwatch();
        if( kbhit())
        {
            c = getchar();
            switch(c)
            {
                case 'a':
                    printf("Toggling TTL Outs OUTB on H2.\n");
                    if(OUTB_onoff)
                    {
                        printf("OUTB TTL outputs LOW.\n");
                        OUTB1 = OUTB2 = OUTB3 = OUTB4 = 0;
                        OUTB5 = OUTB6 = OUTB7 = OUTB8 = 0;
                        OUTB_onoff = 0;
                    }
                    else
                    {
                        printf("OUTB TTL outputs HIGH.\n");
                        OUTB1 = OUTB2 = OUTB3 = OUTB4 = 1;
                        OUTB5 = OUTB6 = OUTB7 = OUTB8 = 1;
                        OUTB_onoff = 1;
                    } 
                    break;

                case 'b':   
                    printf("Toggling High Voltage Output HC1-HC6.\n");
                    if( HC_onoff)
                    {
                        printf("High Voltage Drivers OFF\n");
                        HC1 = HC2 = HC3 = HC4 = HC5 = HC6 = 0;
                        HC_onoff = 0;
                    }
                    else
                    {
                        printf("High Voltage Drivers ON\n");
                        HC1 = HC2 = HC3 = HC4 = HC5 = HC6 = 1;
                        HC_onoff = 1;
                    }
                    break;

                
                case 'c':
                    printf("Reading Inputs DIGIN1 to DIGIN8.\n");
                    printf("DIGIN1 to DIGIN8 >> %d %d %d %d %d %d %d %d\n",
                        DIGIN1, DIGIN2, DIGIN3, DIGIN4,
                        DIGIN5, DIGIN6, DIGIN7, DIGIN8);
                    printf("Byte Read >> %x \n", inport(INENLO));
                    break;

                case 'd':
                    printf("Reading Inputs DIGIN9 to DIGIN12.\n");
                    printf("DIGIN9 to DIGIN12 >> %d %d %d %d\n",
                        DIGIN9, DIGIN10, DIGIN11, DIGIN12);
                    printf("Byte Read >> %x \n",inport(INENHI) & 0x0f);
                    break;
                default:
                    printf("Try 'a', 'b', 'c' or 'd' only !!!\n");
                    break;
            }
        }
    }
}



