/**************************************************************************
 * (c) Copyright 2009 Actel Corporation.  All rights reserved.
 *
 *  Application demo for Smartfusion
 *
 *
 * Author : Actel Application Team
 * Rev     : 1.0.0.3
 *
 **************************************************************************/

/**************************************************************************/
/* Standard Includes */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/**************************************************************************/
/* RTOS Includes */
/**************************************************************************/

#include "FreeRTOS.h"
#include "semphr.h"

/**************************************************************************/
/* Extern Declarations */
/**************************************************************************/

extern void delay ( volatile unsigned int n);
extern volatile unsigned char inLedTask;
extern volatile unsigned char inWebTask;
extern volatile unsigned char inMultimeter;
extern volatile unsigned char std_menu;
extern float                  real_voltage_value;
extern float                  real_current_value;
extern float                  real_temperature_value_tc;
extern xSemaphoreHandle       ledSemHndl;
extern xSemaphoreHandle       stdSemHndl;
extern xSemaphoreHandle       webSemHndl;
extern struct oled_data       write_data;
extern void show_ip();
extern void menu_show(char *, char *);
extern char *std_menu_str[];

#define MULTIMETER      0
#define WEBSERVER       1
#define LED             2

#define FIRST_CHARACTER 0

/*************************************************************************/
/* Creating Standalone Task to navigate the demo using SW1 and SW2 */
/* with OLED Displays */
/*************************************************************************/

void standalone_task(void *para)
{
    char testStr[20];
    std_menu = 0;
    while(1)
    {
        menu_show(std_menu_str[std_menu],"SW2: Menu Scroll");

        xSemaphoreTake( stdSemHndl, portMAX_DELAY ) ;

        switch(std_menu )
        {
            case MULTIMETER:
            {
                /* Multimeter */
                inWebTask = 0;
                inLedTask = 0;

                while(1)
                {
                    inMultimeter = 1;

                    sprintf(testStr,"Voltage: %5.2f V",real_voltage_value);
                    menu_show(testStr,"SW1: Main Menu");
                    delay(1000000);

                    sprintf(testStr,"Current:%5.2f uA",real_current_value);
                    menu_show(testStr,"SW1: Main Menu");
                    delay(1000000);

                    sprintf(testStr,"Temp:%5.2f c ",real_temperature_value_tc);
                    menu_show(testStr,"SW1: Main Menu");

                    delay(1000000);
                    /* This logic checks for the user input for 5ms time on semaphore
                     * to change test, if user is not changed the test then run the
                     * multimeter test */
                    if((xSemaphoreTake( stdSemHndl, 5 ) == pdTRUE) ||
                        (inMultimeter == 0))
                        break;
                }
                break;
            }

            case WEBSERVER:
            {
                inLedTask = 0;
                menu_show("Web Server Running","SW2: Menu Scroll");
                delay(10000);
                /* This Semaphore starts the webserver task */
                xSemaphoreGive( webSemHndl);

                if(inWebTask)
                {
                    show_ip();
                }
                else
                {
                    inWebTask = 1;
                }
                break;
            }

            case LED:
            {
                inWebTask = 0;
                inLedTask = 1;
                menu_show("LED Test Running","SW2: Menu Scroll");
                delay(10000);
                /* This semaphore will start the LED task */
                xSemaphoreGive(ledSemHndl);
                break;
            }
            default:
                break;
        }
    }
}
