/*
	5keycode.c

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

   Demonstrates the code driven 5key system.
*/

#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

#define MODIFY     1
#define NOT_MODIFY 0
#define USERTCLK   0x10
#define NORTCLK    0x00

/*** End Globals real_time_kernel */

#define RUNKERNEL 1          // use the real time kernel

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"
     };

int  count1;
char *c1_help[] =
     {
         "counter 1 help",
         "use field, up &",
         "down key to chng"
     };
int  count2;
char *c2_help[] =
     {
         "counter 2 help",
         "use field, up &",
         "down key to chng"
     };

float temp0;
char  *temp0_help[] =
     {
         "temp0 is param0",
         "use field, up &",
         "down key to chng"
     };

float temp1;
char  *temp1_help[] =
     {
         "temp1 is param1",
         "use field, up &",
         "down key to chng"
     };

char cntrl_on_off;
char *cntrl_help[] =
     {
         "on/off cntrl for",
         "something"
     };

void f1test(void), f2test(void), f3test(void), f4test(void);
void alarm1(void), alarm2(void), alarm3(void), alarm4(void);
void input_output_menu(void);
void time_date_menu(void);
void mix_var_menu(void);

main()
{
    uplc_init();
    cntrl_on_off = 0;
    count1 = 5;
    count2 = 6;
    temp0  = 45.0;
    temp1  = 35.23;

    _5key_setmsg(0, "Demo 5key System");
    _5key_setmsg(1, "Message Bulletin");
    _5key_setfunc(f1test, f2test, f3test, f4test);
    _5key_setalarm(alarm1, alarm2, alarm3, alarm4);
    backgnd();               // background routine for the RTK
}


indirect task0(void)
{
    _ALARM1 = 1;             // alarm1 comes on every 100 secs
}


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

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

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

// task server for 5key system menu
indirect task4(void)
{
    while (1)
    {
        time_date_menu();    // time date menu
        input_output_menu(); // input output menu
        mix_var_menu();      // mix variables menu
    }
}

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

indirect backgnd(void)
{
    run_every(TASK0, 4000);
    request(TASK4);
    while (1) 
        runwatch();          // monitor with the watch window

}                            // end of background

// 5key system IO menu
void 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
    }
}

// 5key system time date menu
void time_date_menu(void)
{
    lcd_server(3, 0L, "Time Date Set");
    while (1)
    {
        if (_5key_date("date   ", date0, date0_help, sizeof(date0_help), 
        USERTCLK, MODIFY, 10) == MENU) 
            return;
        if (_5key_time("time   ", time0, time0_help, sizeof(time0_help), 
        USERTCLK, MODIFY, 10) == MENU) 
            return;
    }
}

// 5key system mix variables menu
void mix_var_menu(void)
{
    lcd_server(3, 0L, "Mix Variable");
    while (1)
    {
        if (_5key_boolean("ON_OFF ", &cntrl_on_off, NO_HELP,
        sizeof(cntrl_help), MODIFY, 10) == MENU) 
            return;
        if (_5key_integer("count1", &count1, 200, 1, c1_help,
        sizeof(c1_help), NOT_MODIFY, 10) == MENU) 
            return;
        if (_5key_integer("count2", &count2, 50, 10, c2_help,
        sizeof(c2_help), MODIFY, 10) == MENU) 
            return;
        if (_5key_float("temp0", &temp0, 200.0, 0.02, temp0_help,
        sizeof(temp0_help), MODIFY, 10) == MENU) 
            return;
        if (_5key_float("temp1", &temp1, 200.0, 0.02, temp1_help,
        sizeof(temp1_help), NOT_MODIFY, 10) == MENU) 
            return;
    }
}

indirect void f1test(void)
{
    lcd_server(3, 0x00000000L, "hello function1");
    lcd_server(0, 0x01000000L, "bye function1");
    suspend(30);
}

indirect void f2test(void)
{
    lcd_server(3, 0x00000000L, "hello function2");
    lcd_server(0, 0x01000000L, "bye function2");
    suspend(30);
}

indirect void f3test(void)
{
    lcd_server(3, 0x00000000L, "hello function3");
    lcd_server(0, 0x01000000L, "bye function3");
    suspend(30);
}

indirect void f4test(void)
{
    lcd_server(3, 0x00000000L, "hello function4");
    lcd_server(0, 0x01000000L, "bye function4");
    suspend(30);
}

indirect void alarm1(void)
{
    lcd_server(3, 0x00000000L, "hello alarm1");
    lcd_server(0, 0x01000000L, "bye alarm1");
    suspend(30);
}

indirect void alarm2(void)
{
    lcd_server(3, 0x00000000L, "hello alarm2");
    lcd_server(0, 0x01000000L, "bye alarm2");
    suspend(30);
}

indirect void alarm3(void)
{
    lcd_server(3, 0x00000000L, "hello alarm3");
    lcd_server(0, 0x01000000L, "bye alarm3");
    suspend(30);
}

indirect void alarm4(void)
{
    lcd_server(3, 0x00000000L, "hello alarm4");
    lcd_server(0, 0x01000000L, "bye alarm4");
    suspend(30);
}

