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

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

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

/**************************************************************************/
/* Driver Includes */
/**************************************************************************/

#include "../drivers/mss_uart/mss_uart.h"

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

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

#define MULTIMETER      '0'
#define WEBSERVER       '1'
#define LED             '2'
/**************************************************************************/
/* Extern Declarations */
/**************************************************************************/

extern xSemaphoreHandle         ledSemHndl;
extern xSemaphoreHandle         webSemHndl;
extern void show_ip();
extern void menu_show(char *, char *);

/**************************************************************************/
/* Global Declarations */
/**************************************************************************/

float                                 real_voltage_value;
float                                 real_current_value;
float                                 real_temperature_value_tc;
uint8_t                               key = 0;
uint8_t                               rx_size =0;
volatile unsigned char                std_menu;
volatile unsigned char                inWebTask;
volatile unsigned char                inLedTask;
volatile unsigned char                inMultimeter;

/**************************************************************************/
/*      Task Will work with hyper Terminal */
/*  Provides scrolling of demo option using Keyboard.*/
/**************************************************************************/

void hyperterminal_task(void *para)
{
    printf( "\n\r********* Hello... I am SmartFusion  *********\n\r" );
    printf( "             Want to play ?  \n\r" );
    while(1)
    {
        printf( "\n\r");
        printf( "********* SmartFusion Play Menu **************\n\r" );
        printf( "********* 0.  Multimeter *********************\n\r" );
        printf( "********* 1.  Web Server *********************\n\r" );
        printf( "********* 2.  LED Test ***********************\n\r" );
        printf( "\n\r");

        /* Waiting to recive any character from hyper terminal */

        do
        {
            rx_size = MSS_UART_get_rx(&g_mss_uart0, &key, 1);
        }while(rx_size == 0);

        rx_size = 0;
        inMultimeter = 0;
        switch(key)
        {
            case MULTIMETER: 
            {
                inWebTask = 0;
                inLedTask = 0;

                std_menu = 0;

                printf( "Voltage: %5.2f V \n\r", real_voltage_value );

                printf( "Current: %5.2f uA \n\r", real_current_value );

                printf( "Temparature %5.2f celsius\n\r",
                         real_temperature_value_tc );

                menu_show("Multimeter","SW2: Menu Scroll");

                break;
            }

            case WEBSERVER:  
            {
                inLedTask = 0;
                std_menu = 1;
                printf( "Testing Board Ethernet ... \n\r" );
                /* This semaphore starts the web server task */
                xSemaphoreGive( webSemHndl);
                printf("Please Browse SmartFusion website with below IP\n\r");

                printf("                 OR                           \n\r");

                printf("Choose the Menu\n\r");

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

                break;
            }

            case LED:  
            {
                inWebTask = 0;
                inLedTask = 1;
                std_menu = 2;

                printf( "LEDs are blinking .... \n\r" );

                printf( "Choose the other Option to come out \n\r" );

                menu_show("LED Test Running","SW2: Menu Scroll");
                /* This Semaphore starts the LED task */
                xSemaphoreGive(ledSemHndl);
                break;
            }

            default:  /* If selected key is out of range */
            {
                printf("Invalid Key \n\r");
                printf("**** Please Enter Your Choice      ****** \n\r");
                break;
            }

        }
    }
}
