/*******************************************************************************
* 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()
{
    volatile char key;
    const uint8_t welcome1[] = "********************************************\n\r";
    const uint8_t welcome2[] = "  Interfacing OLED with I2C on SmartFusion  \n\r";
    const uint8_t welcome3[] = "********************************************";
    const uint8_t pattern[]  = "\n\r\n\r1 - Plain Text Display"
			                   "\n\r2 - Vertical Scrolling"
			                   "\n\r3 - Horizontal Scrolling"
			                   "\n\r4 - Sine Wave\n\r"
			                   "\n\rEnter the Option (1/2/3/4): ";
    char *string1=" SMARTFUSION";
    char *string2=" INNOVATIVE ";
    char *string3=" INTELLIGENT ";
    char *string4=" MIXED SIGNAL";
    char *string5=" FPGA ";

    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)
    {
        MSS_UART_polled_tx_string(&g_mss_uart0, pattern);
        key = readchar();
        switch(key)
        {
            case('1'):
                /* plain Text display */
                OLED_write_data(&write_data,FIRST_TWO_LINES);
            break;

            case('2'):
                /* Vertical scrolling */
                write_data.ver_scr_on_off = 0x01;
                for(i=0; i<10; i++)
                {
                    write_data.ver_off_set = 0x07;
                    OLED_vertical_scroll(&write_data);
                    delay(7000000);
                    write_data.ver_off_set = 0x0F;
                    OLED_vertical_scroll(&write_data);
                    delay(7000000);
                    write_data.ver_off_set = 0x17;
                    OLED_vertical_scroll(&write_data);
                    delay(7000000);
                    write_data.ver_off_set = 0x00;
                    OLED_vertical_scroll(&write_data);
                    delay(7000000);
                }
            break;

            case('3'):
                /* Horizontal scrolling */
                write_data.column_scrool_per_step = 0x08;
                write_data.start_page = 0x01;
                write_data.time_intrval_btw_scroll_step = 0x00;
                write_data.end_page = 0x05;
                write_data.hor_scr_on_off = 0x01;
                OLED_horizontal_scroll(&write_data);
                delay(50000000);
                write_data.hor_scr_on_off = 0x00;
                OLED_horizontal_scroll(&write_data);
            break;

            case('4'):
                /* Sine wave generation  */
                for(i =0; i<100; i++)
                {
                    put_pixel(3.0);
                    put_pixel(2.5);
                    put_pixel(1.5);
                    put_pixel(1.0);
                    put_pixel(0.5);
                    put_pixel(1.0);
                    put_pixel(1.5);
                    put_pixel(2.5);
                    put_pixel(3.0);
                    put_pixel(2.5);
                    put_pixel(1.5);
                    put_pixel(1.0);
                    put_pixel(0.5);
                    put_pixel(1.0);
                    put_pixel(1.5);
                    put_pixel(2.5);
                    put_pixel(3.0);
                    put_pixel(2.5);
                    put_pixel(1.5);
                    put_pixel(1.0);
                    put_pixel(0.5);
                    put_pixel(1.0);
                    put_pixel(1.5);
                    put_pixel(2.5);
                    put_pixel(3.0);
                    delay(1000000);;
                    OLED_clear_display(ALL_LINES);
                }
            /* plain Text display */
            OLED_write_data(&write_data,FIRST_TWO_LINES);
            break;

            default  :
                /* plain Text display */
                OLED_write_data(&write_data,ALL_LINES);
            break;
        }
    }

    return 0;
}
