/*
	DIGVDVR.C

   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program.

   The virtual shadow registers DIGINX are updated in the background every
   25 ms.  A state has to persist for 2 scan time to be considered valid.

   For the PK21xx/Rugged Giant:
      Selectively monitor the state of 1 of 7 digital inputs. Select channel
   through corresponding number on the keypad.

   For the PK22xx/Little Star:
      Selectively monitor the state of 1 of 16 digital inputs. Select channel
   through corresponding number (1-8) on the keypad. Digital bank toggles
   between 1-8 and 9-16 as the higher numerical keys (above 8) are pressed.

*/

#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

main()
{
    int k, oldk;
    int olddig, newdig;
    int keybase;
    
    uplc_init();

    keybase = 0;
    oldk = k = 1;
    olddig = newdig = 0;
    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 BOARD_TYPE==CPLC_BOARD
            if (k > 7) 
                k = 7; // only seven channels available
#elif BOARD_TYPE==L_STAR
            if (k > 8)
            {
                k = oldk - 8 * keybase;
                if (keybase) 
                    keybase = 0;
                else 
                    keybase = 1;
            }
#endif
            up_beep(100);
        }

        newdig = *(&DIGIN1 + keybase * 8 + k - 1);

        if ((olddig != newdig) || (oldk != keybase * 8 + k))
        {
            showdigin(keybase * 8 + k, newdig);
            olddig = newdig;
            oldk = keybase * 8 + k;
        }
    }
}

int showdigin(int chan, int digvalue)
{
    char *onoff[] =
    {
        "OFF", "ON"
    };

    lcd_erase_line(1);
    lcd_printf(0x01000000L, "input %d is %s", chan, onoff[digvalue]);
    printf("input %d is %s\n", chan, onoff[digvalue]);
}




