#include "lcd/lcd.h"
#include "fatfs/tf_card.h"
#include <string.h>

unsigned char image[FRAME_SIZE];
FATFS fs;

void init_uart0(void)
{	
	/* enable GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    /* enable USART clock */
    rcu_periph_clock_enable(RCU_USART0);

    /* connect port to USARTx_Tx */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
    /* connect port to USARTx_Rx */
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

	/* USART configure */
    usart_deinit(USART0);
    usart_baudrate_set(USART0, 115200U);
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
    usart_enable(USART0);

    usart_interrupt_enable(USART0, USART_INT_RBNE);
}

int main(void)
{
    uint8_t mount_is_ok = 1; /* 0: mount successful ; 1: mount failed */
    int offset = 0;
    FIL fil;
    FRESULT fr;     /* FatFs return code */
    UINT br;
    FSIZE_t filesize;

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOC);
    gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1|GPIO_PIN_2);

    init_uart0();

    Lcd_Init();			// init OLED
    LCD_Clear(BLACK);
    BACK_COLOR=BLACK;

    LEDR(1);
    LEDG(1);
    LEDB(1);

    fr = f_mount(&fs, "", 1);
    if (fr == 0)
        mount_is_ok = 0;
    else
        mount_is_ok = 1;

    if (mount_is_ok == 0)
    {
        fr = f_open(&fil, "movie.bin", FA_READ);
        if (fr) printf("open error: %d!\n\r", (int)fr);

        if (!fr)
        {
            while(1)
            {
                filesize = f_size(&fil);
                offset = 0;

                LEDR(0);
                LEDB(0);
                while(offset < filesize)
                {
                    f_lseek(&fil, offset);
                    fr = f_read(&fil, image, sizeof(image), &br);
                    LCD_ShowPicture(0,0,159,79);
                    offset += FRAME_SIZE;
                    LEDB_TOG;
                    delay_1ms(6);
                }

                delay_1ms(4000);
            }

            f_close(&fil);
        }
        else
        {
            LCD_ShowString(12, 20, (u8 *)("Missing movie.bin"), YELLOW);
        }
    }
    else
    {
        LCD_ShowString(20, 20, (u8 *)("Insert SD-Card!"), YELLOW);
    }

    LCD_ShowString(2, 64, (u8 *)("Tiband"), MAGENTA);
    while (1)
    {
        LEDR_TOG;
        delay_1ms(500);
    }
}

int _put_char(int ch)
{
    usart_data_transmit(USART0, (uint8_t) ch );
    while ( usart_flag_get(USART0, USART_FLAG_TBE)== RESET){
    }

    return ch;
}
