/*
	OUTVDVR.C

   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program for setting
   the high current driver outputs using the keypad and the virtual drivers.

   Use Virtual Drivers to update the outputs though the shadow variables
   OUT1-OUT14 or OUT1-OUT12, RELAY1, RELAY2.  Outputs are updated every
   25 ms in the background.

   For the PK21xx/Rugged Giant:
      - selectively toggle the 10 driver outputs and the two relays.
      
   For the PK22xx/Little Star:
      - selectively toggle the first 12 driver outputs for the
        PK22xx/Little Star.
      
   Output channel corresponds to the numerical values of the 12 keys on the
   keypad. menu (1), item (2), ..., add (12)
*/

#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;
    
    uplc_init();
    lcd_printf(0x00000100L, "hello World");
    while (1)
    {
        while ((k = lc_kxget(0)) < 0) 
            runwatch();      // wait for a key
        toggle(k);
    }

}

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

    up_beep(200);
    switch (chan)
    {
        case 1:
            if (OUT1 == 0) 
                OUT1 = 1;
            else 
                OUT1 = 0;
            break;
        case 2:
            if (OUT2 == 0) 
                OUT2 = 1;
            else 
                OUT2 = 0;
            break;
        case 3:
            if (OUT3 == 0) 
                OUT3 = 1;
            else 
                OUT3 = 0;
            break;
        case 4:
            if (OUT4 == 0) 
                OUT4 = 1;
            else 
                OUT4 = 0;
            break;
        case 5:
            if (OUT5 == 0) 
                OUT5 = 1;
            else 
                OUT5 = 0;
            break;
        case 6:
            if (OUT6 == 0) 
                OUT6 = 1;
            else 
                OUT6 = 0;
            break;
        case 7:
            if (OUT7 == 0) 
                OUT7 = 1;
            else 
                OUT7 = 0;
            break;
        case 8:
            if (OUT8 == 0) 
                OUT8 = 1;
            else 
                OUT8 = 0;
            break;
        case 9:
            if (OUT9 == 0) 
                OUT9 = 1;
            else 
                OUT9 = 0;
            break;
        case 10:
            if (OUT10 == 0) 
                OUT10 = 1;
            else 
                OUT10 = 0;
            break;
#if BOARD_TYPE==CPLC_BOARD
        case 11:
            if (RELAY1 == 0) 
                RELAY1 = 1;
            else 
                RELAY1 = 0;
            break;
        case 12:
            if (RELAY2 == 0) 
                RELAY2 = 1;
            else 
                RELAY2 = 0;
            break;
#elif BOARD_TYPE==L_STAR
        case 11:
            if (OUT11 == 0) 
                OUT11 = 1;
            else 
                OUT11 = 0;
            break;
        case 12:
            if (OUT12 == 0) 
                OUT12 = 1;
            else 
                OUT12 = 0;
            break;
#endif
        default:
            break;
    }

    lcd_erase_line(1);
    
    lcd_printf(0x01000000L, "output %d is %s", 
    chan, onoff[*(&OUT1 + chan - 1)]);

    printf("output %d is %s\n", chan, onoff[*(&OUT1 + chan - 1)]);
}


