/**
 * \file
 *
 * \brief Empty user application template
 *
 */

/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * Bare minimum empty user application template
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# "Insert system clock initialization code here" comment
 * -# Minimal main function that starts with a call to board_init()
 * -# "Insert application code here" comment
 *
 */

/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */
#include <asf.h>
#include <string.h>
#include <ili9325.h>
#include "resistive_touch_board.h"
#include "temperature.h"

FRESULT res;
FIL file_object;
FRESULT res;
Ctrl_status status;
FATFS fs;

void my_lcd_init()
{
	aat31xx_disable_backlight();

	lcd_init();

	lcd_set_foreground_color(UNI_COLOR_WHITE);
	lcd_draw_filled_rectangle(0, 0, LCD_WIDTH, LCD_HEIGHT);

	/* Turn on LCD */
	lcd_display_on();

	/* Switch on backlight */
	aat31xx_set_backlight(AAT31XX_MAX_BACKLIGHT_LEVEL);

	// Чертим рамку
	ili9325_set_foreground_color(COLOR_DARKBLUE);
	ili9325_draw_rectangle(5, 5, 235, 315);
	ili9325_draw_rectangle(4, 4, 236, 316);

	int i=0;
	for (i=0; i<8; ++i)
	{
		ili9325_draw_line(4, 40+HEIGHT_STRING*i, 236, 40+HEIGHT_STRING*i);
		ili9325_draw_line(4, 41+HEIGHT_STRING*i, 236, 41+HEIGHT_STRING*i);
	}
	
	ili9325_draw_line(155, 40, 155, 316);
	ili9325_draw_line(156, 40, 156, 316);
	
	ili9325_set_foreground_color(COLOR_BLACK);
	ili9325_draw_string(50, 17, (uint8_t *)"MultiProject");
	ili9325_draw_string(20, 17+HEIGHT_STRING, (uint8_t *)"Temp (Cels)");
	ili9325_draw_string(20, 17+HEIGHT_STRING*2, (uint8_t *)"Barometer");
	// Чертим рамку
	
}
// будет удалён символ начиная с (row; str) в колечестве num_char
void erase_str(int row, int str, int num_char)
{
	lcd_set_foreground_color(UNI_COLOR_WHITE);
	lcd_draw_filled_rectangle( 20+10*(row-1)+(row-1)*2, 17+HEIGHT_STRING*str,
	20+10*(row+num_char-1)+(row+num_char-2)*2, 17+HEIGHT_STRING*str+14);
	ili9325_set_foreground_color(COLOR_BLACK);
}

void print_temperature(char *str)
{
	if (therm_read_temperature(str)== ERROR_INIT)
	{
		sprintf(str, "%s", "ERROR");
	}
	erase_str(13, 1, 6);
	ili9325_draw_string(20+12*10+11*2, 17+HEIGHT_STRING*1, (uint8_t *)str);
}


int  sd_init()
{
	do
	{
		
		do {
			status = sd_mmc_test_unit_ready(0);
			if (CTRL_FAIL == status) {
				while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
				}
			}
		} while (CTRL_GOOD != status);
		
		status = sd_mmc_test_unit_ready(0);
		if (CTRL_FAIL == status)
		{
			while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
			}
		}
	}while (CTRL_GOOD != status);

	memset(&fs, 0, sizeof(FATFS));
	res = f_mount(LUN_ID_SD_MMC_0_MEM, &fs);
	if (FR_INVALID_DRIVE == res) {
		goto main_end_of_test;
	}

	erase_str(1, 5, 11);
	ili9325_draw_string(20, 17+HEIGHT_STRING*5, (uint8_t *)"mount ok");
	
	char file_name[] = "0:arch_param.txt";
	file_name[0] = LUN_ID_SD_MMC_0_MEM + '0';

	res = f_open(&file_object,
	(char const *)file_name,
	FA_OPEN_ALWAYS | FA_WRITE);

	if (res != FR_OK) {
		goto main_end_of_test;
	}
	
	return 0;
	
	main_end_of_test:
	erase_str(1, 5, 11);
	ili9325_draw_string(20, 17+HEIGHT_STRING*5, (uint8_t *)"SD err");
	
}

void sd_write(char *str)
{
	erase_str(1, 5, 11);
	ili9325_draw_string(20, 17+HEIGHT_STRING*5, (uint8_t *)"open ok");

	if (0 == f_puts(str, &file_object)) {
		goto SD_end_of_test;
	}
	f_puts("\n", &file_object);
	
	f_sync (&file_object);
	SD_end_of_test:
	
	erase_str(1, 5, 11);
	ili9325_draw_string(20, 17+HEIGHT_STRING*5, (uint8_t *)"write");
}


int main (void)
{
	char str[80];
	/* Insert system clock initialization code here (sysclk_init()). */
	sysclk_init();
	board_init();
	sd_mmc_init();
	my_lcd_init();
	sd_init();
	/* Insert application code here, after the board has been initialized. */
	
	// Настройка информационной линни датчика DS18B20
	pmc_enable_periph_clk(ID_TEMPERATURE);
	while (1)
	{
		print_temperature(str);
		delay_s(1);
		
		sd_write(str);
		
		erase_str(1, 5, 11);
		ili9325_draw_string(20, 17+HEIGHT_STRING*5, (uint8_t *)"wr ok");
	}
}
