#use "default.h"

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

/*
	GENIO.C

   Sample program to read and write the IO of the BL16xx/LittleG.
   
   Uses up_setout() function calls to access the IO.

   The high current sinking drivers HC1 through HC6 will output only
   0.6 volts if read directly. Connect an led in series with a
   330 Ohm resistor between HCx and +5 volts to easily observe the
   state of the output.
*/

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

    HC_onoff = 0;       // outputs are off
    OUTB_onoff = 0;     // outputs are off

    for(i=1;i<15;i++)
        up_setout(i, 0);    // all outputs are off
    
    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 IN00 to IN07.\n");
    printf("Hit 'd' to read inputs IN08 to IN11.\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");
                        for(i=1;i<9;i++)
                            up_setout(i, 0);    // do on bit basis
                        OUTB_onoff = 0;
                    }
                    else
                    {
                        printf("OUTB TTL outputs HIGH.\n");
                        outport(OUTBYTE, 0xff);  // do on byte basis
                        OUTB_onoff = 1;
                    }
                    break;

                case 'b':   
                    printf("Toggling High Voltage Output HC1-HC6.\n");
                    if( HC_onoff)
                    {
                        printf("High Voltage Drivers OFF\n");
                        for(i=9;i<15;i++)
                            up_setout(i, 0);
                        HC_onoff = 0;
                    }
                    else
                    {
                        printf("High Voltage Drivers ON\n");
                        for(i=9;i<15;i++)
                            up_setout(i, 1);
                        HC_onoff = 1;
                    }
                    break;

                
                case 'c':
                    printf("Reading Inputs IN00 to IN07.\n");
                    printf("IN00-IN07 >> %d %d %d %d %d %d %d %d\n",
                        up_digin(1), up_digin(2), up_digin(3),
                        up_digin(4), up_digin(5), up_digin(6),
                        up_digin(7), up_digin(8));
                    printf("Byte Read >> %x \n", inport(INENLO));
                    break;

                case 'd':
                    printf("Reading Inputs IN08 to IN11.\n");
                    printf("IN08-IN11 >> %d %d %d %d\n",
                        up_digin(9), up_digin(10), up_digin(11), 
                        up_digin(12));
                    printf("Byte Read >> %x \n",inport(INENHI) & 0x0f);
                    break;
                default:
                    printf("Try 'a', 'b', 'c' or 'd' only !!!\n");
                    break;
            }
        }
    }
}



