/**
  ******************************************************************************
  * @file    main.cpp
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/

//#define USE_HAL_DRIVER // redefined?
#include "stm32f7xx.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32f7xx_hal.h"

#include <cstdio>
#include <iostream>
using namespace std;

int main(void)
{
	// HAL initialization
	SystemInit(); // task? (not required)
	HAL_Init(); // #include "stm32f7xx_hal.h"

	// LED configuration
	GPIO_InitTypeDef GPIO_InitStruct;
	__GPIOI_CLK_ENABLE(); // GPIO Ports Clock Enable
	GPIO_InitStruct.Pin = GPIO_PIN_1; // Configure GPIO pin : PI1
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
	HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
	/*while(1) {
		HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_1);
		HAL_Delay(1000);
	}*/

	// LCD configuration
	BSP_LCD_Init();
	uint32_t start_address = LCD_FB_START_ADDRESS; // LCD_FB_START_ADDRESS 0xC0000000
	uint32_t lcd_size = (BSP_LCD_GetXSize() * BSP_LCD_GetYSize()* sizeof(uint32_t));
	BSP_LCD_LayerDefaultInit(0, (uint32_t) (start_address + 0*lcd_size));
	BSP_LCD_LayerDefaultInit(1, (uint32_t) (start_address + 1*lcd_size));

	BSP_LCD_DisplayOn();
	BSP_LCD_SelectLayer(0);
	BSP_LCD_Clear(LCD_COLOR_RED);
	BSP_LCD_SelectLayer(1);
	BSP_LCD_Clear(LCD_COLOR_GREEN);
	BSP_LCD_SetTransparency(0, 255);
	BSP_LCD_SetTransparency(1, 200);
	BSP_LCD_SetLayerVisible(0, ENABLE);
	BSP_LCD_SetLayerVisible(1, DISABLE);

	// Draw pixels
	BSP_LCD_SelectLayer(0);
	BSP_LCD_DrawPixel(0,0,0xffffffff);
	BSP_LCD_DrawPixel(5,0,0xff00ff00);
	BSP_LCD_DrawPixel(10,0,0xff0000ff);
	BSP_LCD_DrawPixel(20,0,0xffffffff);
	BSP_LCD_DrawPixel(40,0,0xffffffff);
	BSP_LCD_DrawPixel(80,0,0xffffffff); // begins to flicker
	BSP_LCD_DrawPixel(160,0,0xffffffff); // flickers significantly

	// Check LED to ensure functions were executed properly before
	while(1) {
		HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_1);
		HAL_Delay(250);
	}
}