/*
	adcdemo.c

   PK21xx/Rugged Giant (only) sample program.

   - selectively read the 7 analog input channels
   - select the channel with the keypad
*/

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

#if !(BOARD_TYPE==CPLC_BOARD)
#fatal "This program only runs on a PK21xx/Rugged Giant"
#endif

#define NOUNIVERSAL 1        // disable the virtual drivers for the io's

main()
{
    int k, oldk;
    int oldad, newad;
    
    uplc_init();
    oldad = newad = 0;
    oldk  = k = 1;
    lcd_printf(0x00000100L, "hello World");
    while (1)
    {
        if (lc_kxget(1) < 0) 
            runwatch();      // wait for a key
        else                 // runwatch allows monitoring with the
                             // watch window
        {
            k = lc_kxget(0); // get key out of the buffer
            if (k > 7) 
                k = 7;       // only seven channels available
            up_beep(100);
        }

        newad = up_adcal(k);
        if ((oldad != newad) || (oldk != k))
        {
            showanalog(k, newad);
            oldad = newad;
            oldk  = k;
        }
    }
}

int showanalog(int chan, int advalue)
{
    lcd_erase_line(1);
    lcd_printf(0x01000000L, "input %d is %6.3f", 
    chan, (float)(advalue / 1000.));

    printf("input %d is %6.3f\n", chan, (float)advalue / 1000.0);
}


