/*
	READIO.C

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

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

#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  noutput;
    int  onoff;
    char c;
    
    uplc_init();

    onoff = 0;
    printf("Toggle F4 to make keyboard input active as STDIN (DOS only)\n");

#if BOARD_TYPE==CPLC_BOARD
    noutput = 12;
    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");
#elif BOARD_TYPE==L_STAR
    noutput = 14;
    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");
                    for (i = 1; i < noutput + 1; i++)
                    {
                        up_setout(i, 1);
                    }
                }
                else
                {
                    onoff = 0;
                    printf("OUTPUT are OFF.\n");
                    for (i = 1; i < noutput + 1; i++)
                    {
                        up_setout(i, 0);
                    }
                }
                break;
#if BOARD_TYPE==CPLC_BOARD
            case 'c':
                printf("DIGITAL INPUTS chans 1-7\n");
                
                printf("%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));
                
                break;
            case 'd':
                printf("ANALOG INPUTS chans 1-6, 8\n");
                
                printf("%d %d %d %d %d %d %d millivolts\n",
                up_adcal(1), up_adcal(2), up_adcal(3), up_adcal(4),
                up_adcal(5), up_adcal(6), up_adcal(8));
                
                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",
                up_digin(1), up_digin(2), up_digin(3), up_digin(4),
                up_digin(5), up_digin(6), up_digin(7), up_digin(8));
                
                break;
            case 'd':
                printf("DIGITAL INPUTS chans 9-16\n");
                
                printf("%d %d %d %d %d %d %d %d\n",
                up_digin(9), up_digin(10), up_digin(11), up_digin(12),
                up_digin(13), up_digin(14), up_digin(15), up_digin(16));
                
                break;
#endif
            default:
                printf(" Try 'a', 'c' or 'd'\n");
                break;
        }
    }
}


