/*
	5keylink.c
	
   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program
   
   Shows usage of link list driven 5key system.
*/

#use "default.h"
#use cplc.lib
#use 5key.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
#define DISPLAY    1
#define NO_DISPLAY 0

/*** End Globals real_time_kernel */

#define RUNKERNEL 1          // use the real time kernel

char *menu_type[] =
     {
         "Time and Date", "Variables", "Boolean",
         "Integer", "MIX Data"
     };

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);

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);

    _5key_setmenu(menu_type[0], "date   ", 
    _5key_Ddata, date0, 0.0, 0.0, date0_help, sizeof(date0_help), 
    USERTCLK | MODIFY, 10, NO_DISPLAY);

    _5key_setmenu(menu_type[0], "time   ", 
    _5key_Tdata, time0, 0.0, 0.0, time0_help, sizeof(time0_help), 
    USERTCLK | MODIFY, 10, NO_DISPLAY);

    _5key_setmenu(menu_type[4], "control", _5key_Bdata, &cntrl_on_off, 
    0.0, 0.0, cntrl_help, sizeof(cntrl_help), MODIFY, 10, DISPLAY);

                             // monitor state only
    _5key_setmenu(menu_type[4], "count1", _5key_Idata, &count1, 
    99.0, -100.0, c1_help, sizeof(c1_help), NOT_MODIFY, 10, DISPLAY);

    _5key_setmenu(menu_type[4], "count2", _5key_Idata, &count2, 
    134.0, -5.0, c2_help, sizeof(c2_help), MODIFY, 10, NO_DISPLAY);

    _5key_setmenu(menu_type[4], "temp0  ", _5key_Fdata, &temp0, 
    200.0, 23.0, temp0_help, sizeof(temp0_help), MODIFY, 10, NO_DISPLAY);

    _5key_setmenu(menu_type[4], "temp1  ", _5key_Fdata, &temp1, 
    100.0, 1.0, NO_HELP, 4, NOT_MODIFY, 10, NO_DISPLAY); // monitor data only

    backgnd();               // this runs after everything else
}

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


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

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

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

// task for the 5key system
indirect task4(void)
{
    _5key_menu();
}

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

indirect backgnd(void)
{
    run_every(TASK0, 4000);  // alarm1 comes on every 100 secs
    request(TASK4);
    while (1) 
        runwatch();          // monitor with the watch window

}                            // end of background

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");
    count1 = count1 + 10;
    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);
}

