/*
	outdemo.c
	
   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program for setting
   the high current driver outputs using the keypad.
   
   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

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

main()
{
    int k;
    
    uplc_init();
    for (k = 0; k < 1000; k++)
        ;                    // delay a bit for the LCD

    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"
     };
    int k;
    char dsouts[12];

// GLOBAL_INIT allows for initialization of blocks within any function
// at the start of the execution.

#GLOBAL_INIT
    {
        for (k = 0; k < 12; k++)
        {
            dsouts[k] = 0;
            up_setout(k, 0);
        }
    }

    if (dsouts[chan - 1] == 0)
    {
        dsouts[chan - 1] = 1;
        up_beep(200);
    }
    else
    {
        dsouts[chan - 1] = 0;
        up_beep(100);
    }
    up_setout(chan, dsouts[chan - 1]);
    lcd_erase_line(1);
    lcd_printf(0x01000000L, "output %d is %s", chan, onoff[dsouts[chan - 1]]);
    printf("output %d is %s\n", chan, onoff[dsouts[chan - 1]]);
}


