/*******************************************************************************
* Company: Actel Corporation
*
* File: main.c
* File history:
*     Revision: Initial version   Date: JUne 01, 2010
*
* Description:
*             This application print the string on OLED display based on user
*             selection.The following options are given:
*
*             1 - Plain Text Display
*             2 - Vertical Scrolling
*             3 - Horizontal Scrolling
*             4 - Sine Wave
*
* Author: Pavan Marisetti
*         pavan.marisetti@actel.com
*         Corporate Applications Engineering
********************************************************************************/

#include <stdio.h>
#include <string.h>


#include "BSP/oled_driver/oled.h"



/**************************************************************************/
/* Firmware Includes */
/**************************************************************************/

#include "hal.h"
#include "core_uart_apb.h"
#include "core_gpio.h"
#include "cortex_nvic.h"
#include "core_i2c.h"
#include "coreai.h"

/**************************************************************************/
/* System Memory Map */
/**************************************************************************/

#include "platform.h"

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

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/**************************************************************************/
/*Declaration of global variables*/
/**************************************************************************/

xSemaphoreHandle stdSemHndl = NULL, htSemHndl = NULL;
xSemaphoreHandle webSemHndl = NULL, ledSemHndl = NULL;
xSemaphoreHandle switchOneSemHndl = NULL;

xTaskHandle stdTaskHndl = NULL, htTaskHndl = NULL, switchOneTaskHndl = NULL;
xTaskHandle webTaskHndl = NULL, aceTaskHndl = NULL, ledTaskHndl = NULL;

/**************************************************************************/
/*TBD*/
/**************************************************************************/

UART_instance_t g_uart;
gpio_instance_t g_gpio;
i2c_instance_t g_i2c_inst;

#define BAUD_VALUE_57600    42

#define FIRST_CHARACTER 0

/* Function to read the input character */
char readchar (void)
{
    uint8_t rx_size = 0;
    unsigned char rx_char;
    do
    {
        rx_size = MSS_UART_get_rx( &g_mss_uart0, &rx_char, sizeof(rx_char) );
    }while ( rx_size == 0);

    return(rx_char);
}

/* Function for delay */
void delay ( volatile uint32_t n)
{
    while(n!=0)
    {
        n--;
    }
}


int main()
{

    uint8_t i;

    struct oled_data write_data;

    write_data.line1 = FIRST_LINE;
    write_data.char_offset1 = FIRST_CHARACTER;
    write_data.string1 = string1;

    write_data.line2 = SECOND_LINE;
    write_data.char_offset2 = FIRST_CHARACTER;
    write_data.string2 = string2;

    write_data.line3 = THIRD_LINE;
    write_data.char_offset3 = FIRST_CHARACTER;
    write_data.string3 = string3;

    write_data.line4 = FOURTH_LINE;
    write_data.char_offset4 = FIRST_CHARACTER;
    write_data.string4 = string4;

    write_data.line5 = FIFTH_LINE;
    write_data.char_offset5 = FIRST_CHARACTER;
    write_data.string5 = string5;

    write_data.contrast_val = 0xFF;

    /* Watchdog Disabling function*/
    MSS_WD_disable();

    /* OLED Initialization */
    OLED_init();

    /* UART Initialization and Configuration */
    MSS_UART_init
    (
        &g_mss_uart0,
        MSS_UART_57600_BAUD,
        MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT
    );

     stdSemHndl       = xSemaphoreCreateCounting( 1, 0);
     webSemHndl       = xSemaphoreCreateCounting( 1, 0);
     ledSemHndl       = xSemaphoreCreateCounting( 1, 0);
     switchOneSemHndl = xSemaphoreCreateCounting( 1, 0);

     /* Creating the tasks for the functional requirement of the demo */
     /* This task is responsible for controlling the demo using the
      * SW1 and SW2 switch inputs from the user */


    printf(" ********************************************\n\r");
    printf("  Interfacing OLED with I2C on SmartFusion  \n\r");
    printf("********************************************");
    /* plain Text display */
    OLED_write_data(&write_data,FIRST_TWO_LINES);

    while(1)
    {
    	    uint8_t j;
    	    write_data.line1          = FIRST_LINE;
    	    write_data.char_offset1   = FIRST_CHARACTER;
    	    write_data.string1        = "Hi I am SmartFusion";

    	    write_data.line2          = SECOND_LINE;
    	    write_data.char_offset2   = FIRST_CHARACTER;
    	    write_data.string2        = "Want to Play?";

    	    write_data.contrast_val                 = OLED_CONTRAST_VAL;
    	    write_data.hor_scr_on_off                       = OLED_HORIZ_SCROLL_OFF;
    	    write_data.column_scrool_per_step       = OLED_HORIZ_SCROLL_STEP;
    	    write_data.start_page                   = OLED_START_PAGE;
    	    write_data.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
    	    write_data.end_page                     = OLED_END_PAGE;

    	    OLED_write_data(&write_data,FIRST_TWO_LINES);
    	    /*This delay is required to show the menu for some time */
    	    for(j=0;j<100;j++)
    	    {
    	        delay(100000);
    	    }

    	    write_data.line1          = FIRST_LINE;
    	    write_data.char_offset1   = FIRST_CHARACTER;
    	    write_data.string1        = "First change";

    	    write_data.line2          = SECOND_LINE;
    	    write_data.char_offset2   = FIRST_CHARACTER;
    	    write_data.string2        = "Hello";

    	    write_data.contrast_val                 = OLED_CONTRAST_VAL;
    	    write_data.hor_scr_on_off                       = OLED_HORIZ_SCROLL_OFF;
    	    write_data.column_scrool_per_step       = OLED_HORIZ_SCROLL_STEP;
    	    write_data.start_page                   = OLED_START_PAGE;
    	    write_data.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
    	    write_data.end_page                     = OLED_END_PAGE;

    	    OLED_write_data(&write_data,FIRST_TWO_LINES);
    	    /*This delay is required to show the menu for some time */
    	    for(j=0;j<100;j++)
    	    {
    	        delay(100000);
    	    }

       	    write_data.line1          = FIRST_LINE;
        	    write_data.char_offset1   = FIRST_CHARACTER;
        	    write_data.string1        = "Second change";

        	    write_data.line2          = SECOND_LINE;
        	    write_data.char_offset2   = FIRST_CHARACTER;
        	    write_data.string2        = "Hello1";

        	    write_data.contrast_val                 = OLED_CONTRAST_VAL;
        	    write_data.hor_scr_on_off                       = OLED_HORIZ_SCROLL_OFF;
        	    write_data.column_scrool_per_step       = OLED_HORIZ_SCROLL_STEP;
        	    write_data.start_page                   = OLED_START_PAGE;
        	    write_data.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
        	    write_data.end_page                     = OLED_END_PAGE;

        	    OLED_write_data(&write_data,FIRST_TWO_LINES);
        	    /*This delay is required to show the menu for some time */
        	    for(j=0;j<100;j++)
        	    {
        	        delay(100000);
        	    }

        	    write_data.line1          = FIRST_LINE;
            	    write_data.char_offset1   = FIRST_CHARACTER;
            	    write_data.string1        = "Third change";

            	    write_data.line2          = SECOND_LINE;
            	    write_data.char_offset2   = FIRST_CHARACTER;
            	    write_data.string2        = "Hello2";

            	    write_data.contrast_val                 = OLED_CONTRAST_VAL;
            	    write_data.hor_scr_on_off                       = OLED_HORIZ_SCROLL_OFF;
            	    write_data.column_scrool_per_step       = OLED_HORIZ_SCROLL_STEP;
            	    write_data.start_page                   = OLED_START_PAGE;
            	    write_data.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
            	    write_data.end_page                     = OLED_END_PAGE;

            	    OLED_write_data(&write_data,FIRST_TWO_LINES);
            	    /*This delay is required to show the menu for some time */
            	    for(j=0;j<100;j++)
            	    {
            	        delay(100000);
            	    }
    }

    return 0;
}
