/*
	5keydemo.c

   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program.
   
   - time and date menu using the code driven 5key system.
   - input output menu - allow for monitoring of the io's under the
                     code driven 5key system.
   - toggle F1 to start and stop output sequence program 1
   - toggle F2 to start and stop output sequence program 2
   - task 3 is used for sequence program 1
   - task 4 is used for sequence program 2
*/

#use "default.h"
#use cplc.lib
#use 5key.lib
#use 5keyextd.lib
#use rtk.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

/***Begin Globals real_time_kernel  */
int task0(void), task1(void), task2(void), task3(void), 
    task4(void), task5(void), backgnd(void);
int (*Ftask[]) () =
{
    task0, task1, task2, task3, task4, task5, backgnd
};

#define NTASKS 7

#define TASK0 0
#define TASK1 1
#define TASK2 2
#define TASK3 3
#define TASK4 4
#define TASK5 5

/***End Globals real_time_kernel */


#define LK_COLS     20
#define KEYPAD_SIZE 12
#define LK_LINES    2


#define RUNKERNEL 1

char time0[15];
char *time0_help[] =
     {
         "Set system time,",
         "use field, up &",
         "down key to chng"
     };
char date0[15];
char *date0_help[] =
     {
         "Set system date,",
         "use field, up &",
         "down key to chng"
     };

char F1PRESS, F2PRESS, F3PRESS, F4PRESS;
int  tog_f1(void), tog_f2(void), tog_f3(void), tog_f4(void);

main()
{
    uplc_init();             // initialize keypad, lcd, virtual drivers
    F1PRESS = F2PRESS = F3PRESS = F4PRESS = 0;
    _5key_setfunc(tog_f1, tog_f2, tog_f3, tog_f4);
    _5key_setmsg(0, "Toggle F1 4 tst1");
    _5key_setmsg(1, "Toggle F2 4 tst2");
    _5key_setmsg(2, "Use Input Output");
    _5key_setmsg(3, "set to view outs");
    backgnd();               // this runs after everything else
}

indirect task0(void)
{
                             // task not used
}

indirect task1(void)
{
                             // task not used
}

indirect task2(void)
{
                             // task not used
}

indirect task3(void)         // demo1
{
    char count;
    char limit_count;
    char imark;
    char imark2;

    while (1)
    {
        while (F1PRESS)      // while not stop
        {
            OUT1  = 1;
            count = 0;
            while (count++ < 50 && F1PRESS) 
                suspend(1);
            if (count < 50) 
                break;       // terminate loop
            OUT1  = 0;
            OUT2  = 1;
            count = 0;
            while (count++ < 40 && F1PRESS) 
                suspend(1);
            if (count < 40) 
                break;
            OUT2  = 0;
            OUT3  = 1;
            count = 0;
            while (count++ < 30 && F1PRESS) 
                suspend(1);
            if (count < 30) 
                break;
            OUT3  = 0;
            OUT4  = 1;
            count = 0;
            while (count++ < 20 && F1PRESS) 
                suspend(1);
            if (count < 20) 
                break;
            OUT4  = 0;
            OUT5  = 1;
            count = 0;
            while (count++ < 10 && F1PRESS) 
                suspend(1);
            if (count < 10) 
                break;
            OUT5 = 0;
        }
        OUT1 = OUT2 = OUT3 = OUT4 = OUT5 = 0;
        suspend(0);          // suspend forever
    }
}

indirect task4(void)         // demo2
{
    char count;

    while (1)
    {
        while (F2PRESS)      // while not stop
        {
            OUT8 = OUT9 = OUT10 = 0;
            count = 0;
            while (count++ < 100 && F2PRESS) 
                suspend(1);
            if (count < 100) 
                break;       // terminate loop
            OUT8 = OUT9 = OUT10 = 1;
            count = 0;
            while (count++ < 100 && F2PRESS) 
                suspend(1);
            if (count < 100) 
                break;
        }
        OUT8 = OUT9 = OUT10 = 0;
        suspend(0);          // suspend forever
    }
}

// 5key system task
indirect task5(void)
{
    while (1)
    {
        time_date_menu();
        input_output_menu();
    }
}

indirect backgnd(void)
{
    request(TASK5);          // request 5 key system task
    while (1)
    {
        if (F1PRESS)
            request(TASK3);
        if (F2PRESS)
            request(TASK4);
        runwatch();          // monitor with the watch window
    }
}

// item server for board input output
int input_output_menu(void)
{
    lcd_server(3, 0L, "Input Output Set");
    while (1)
    {
#if BOARD_TYPE==CPLC_BOARD
            if (_5key_uinput() == MENU) 
                return;
            if (_5key_diginput() == MENU) 
                return;
            if (_5key_12out() == MENU) 
                return;
            if (_5key_dacout() == MENU) 
                return;
#elif BOARD_TYPE==L_STAR
            if (_5key_bank1dig() == MENU) 
                return;
            if (_5key_bank2dig() == MENU) 
                return;
            if (_5key_14out() == MENU) 
                return;
#endif
    }
}

// item server for time date setup
int time_date_menu(void)
{
    lcd_server(3, 0L, "Time Date Set");
    while (1)
    {
        if (_5key_date("date   ", date0, date0_help, 
        sizeof(date0_help), 1, 1, 10) == MENU) 
            return;
        if (_5key_time("time   ", time0, time0_help, 
        sizeof(time0_help), 1, 1, 10) == MENU) 
            return;
    }
}


/*****************************
int tog_f1()
   - service for function key1
******************************/
indirect int tog_f1(void)
{
    if (F1PRESS == 0) 
        F1PRESS = 1;
    else 
        F1PRESS = 0;
}

/*****************************
int tog_f2()
   - service for function key2
******************************/
indirect int tog_f2(void)
{
    if (F2PRESS == 0) 
        F2PRESS = 1;
    else 
        F2PRESS = 0;
}

/*****************************
int tog_f3()
   - service for function key3
******************************/
indirect int tog_f3(void)
{
    if (F3PRESS == 0) 
        F3PRESS = 1;
    else 
        F3PRESS = 0;
}

/*****************************
int tog_f4()
   - service for function key4
******************************/
indirect int tog_f4(void)
{
    if (F4PRESS == 0) 
        F4PRESS = 1;
    else 
        F4PRESS = 0;
}

