/***********************************************************************
 * Copyright (c)  2017 - 2023, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app.c
 * Description : app source file
 * Author(s)   : MCU Team
 * version     : V1.0
 * Modify date : 2023-06-13
 ***********************************************************************/
#include  "app.h"
#include  "config.h"
#include  "common.h"
#include  "uart0.h"
#include  "uart1.h"
#include  "um16436_spi.h"
#include  "i2c_master.h"


uint32_t g_data_buf[512];

uint8_t uart1_rx_flag = 0;
uint8_t uart1_rx_buf[64];
uint8_t uart1_rx_count = 0;

/************************************************************************
 * function   : uart1_rec_pro
 * Description: uart1_rec_pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart1_rec_pro(void)
{
	uart1_rx_buf[uart1_rx_count++] = uart1_recv_byte();
	if(uart1_rx_count==1)
		uart1_rx_flag = 1;	
}

/************************************************************************
 * function   : uart_init
 * Description: uart_init 串口初始化
 * input : none
 * return: none
 ************************************************************************/
void uart_init(void)
{
	uart1_rx_flag = 0;	
	uart1_init(PCLK,UART1_BAUD_RATE);
	uart1_irq_init(UART1_IRQ_ENABLE,uart1_rec_pro);	
}

/************************************************************************
 * function   : umlcd_clock_auto_calibrate
 * Description: umlcd_clock_auto_calibrate时钟自动校准
 * input : none
 * return: none
 ************************************************************************/
void umlcd_clock_auto_calibrate(void)
{
	umlcd_write_reg(UMLCD_TEST_REG, UMLCD_TESTEN_OFF | UMLCD_LCCTRL_OFF | UMLCD_VLCD_SET_0P9VDD | UMLCD_RCL_CAT_EN_ON);
	delay_us(250);//必要的时间，不能去掉
	umlcd_write_reg(UMLCD_TEST_REG, UMLCD_TESTEN_OFF | UMLCD_LCCTRL_OFF | UMLCD_VLCD_SET_0P9VDD | UMLCD_RCL_CAT_EN_OFF);	
}

/************************************************************************
 * function   : umlcd_init
 * Description: umlcd_init LCD初始化
 * input : none
 * return: none
 ************************************************************************/
void umlcd_init(void)
{
	umlcd_write_reg(UMLCD_TEST_REG, UMLCD_TESTEN_OFF | UMLCD_LCCTRL_OFF | UMLCD_VLCD_SET_0P9VDD | UMLCD_RCL_CAT_EN_OFF);//设置VLCD为0.9VDD
	umlcd_write_reg(UMLCD_DFSET_REG, UMLCD_DFSET_EN_ON | UMLCD_DFSET_TYPE_A_48Hz);//帧频48Hz
	umlcd_write_reg(UMLCD_LCDSET_REG, UMLCD_DISPLAY_MODE_36x4 | UMLCD_INV_MODE_TYPE_A | UMLCD_BIAS_SET_ONE_THIRD | UMLCD_IBIAS_SET_100nA);//4COM、36SEG、A类、1/3 Bias、偏置电流100nA
	umlcd_write_reg(UMLCD_FCSET_REG, UMLCD_FC_PW_CLOCK_10 | UMLCD_FC_DS_100UA);//快充时间10Clock、快充期间驱动强度100uA
	umlcd_write_reg(UMLCD_SYS_CTRL_REG, UMLCD_CLK_SEL_INTERNAL | UMLCD_SPI_LOCK_OFF | UMLCD_I2C_LOCK_OFF | UMLCD_CLK32K_EN_ON | UMLCD_WDT_EN_OFF | UMLCD_CLK_OUT_OFF);//使用内部32KHz时钟源，开启时钟
	umlcd_write_reg(UMLCD_DISPCTRL_REG, UMLCD_FELUSE_EN_OFF | UMLCD_PWEN_ON | UMLCD_LCD_EN_ON);//开启LCD 	
	umlcd_clear_display();//全屏清除显示
}

/************************************************************************
 * function   : umlcd_test
 * Description: umlcd test
 * input : none
 * return: none
 ************************************************************************/
void umlcd_test(void)
{
	uint32_t data = 0;
	float temperature = 0.0, humidity = 0.0;
	uint32_t command[5] = {0xE3, 0xE5}; /* T, H*/
	uint8_t softreset[1] = {0xFE};
	uint8_t receive_data[3];
	uint8_t display[2];
	
	char table1[][4] = {"C","%","P3","P4"};

	i2c_master_init( I2C_SPEED_HIGH );		
	
	umlcd_init();
	umlcd_clock_auto_calibrate();
	
	printfS("start to read temperature and humidity\r\n");

	umlcd_display(table1[0], 4);
	
	if(i2c_master_send(softreset, 1))
	{
		delay_ms(20);
		while(1)
		{	
			/* temperature */
			if(i2c_master_recv_mix(&command[0], 1, receive_data, 3))
			{
				data = ((receive_data[0]<<8) | (receive_data[1]));
				temperature = (175.72 * data)/65536 - 46.85;

				display[0] = (uint8_t)temperature;
				display[1] = (temperature - (uint8_t)temperature) * 10;
				printfS("temperature : %0.1f \r\n", temperature);			
				umlcd_num_display(2,display[0]/10);
				umlcd_num_display(3,display[0]%10);
				umlcd_num_display(4,display[1]);
			}
			else
				printfS("temperature error\r\n");
			/* humidity */
			if(i2c_master_recv_mix(&command[1], 1, receive_data, 3))
			{
				data = ((receive_data[0]<<8) | (receive_data[1]));
				humidity = (125.0 * data)/65536 - 6.0;

				display[0] = (uint8_t)humidity;			
				display[1] = (humidity - (uint8_t)humidity) * 10;
				printfS("humidity : %0.1f \r\n", humidity);			
				if(humidity<100)
				{
					umlcd_num_display(5,display[0]/10);
					umlcd_num_display(6,display[0]%10);
					umlcd_num_display(7,display[1]);
				}
				else
				{
					umlcd_num_display(5,9);
					umlcd_num_display(6,9);
					umlcd_num_display(7,9);
				}
				
			}
			else
				printfS("humidity error\r\n");	
		
			delay_ms(1000);
		}			
	}
	else
	{
		umlcd_num_display(2,2);
		umlcd_num_display(3,5);
		umlcd_num_display(4,0);	

		umlcd_num_display(5,6);
		umlcd_num_display(6,0);
		umlcd_num_display(7,0);		
	}
}

/************************************************************************
 * function   : soc_test
 * Description: soc test
 * input : none
 * return: none
 ************************************************************************/
void soc_test(void)
{
	printfS("UM321xA run at %dHz\n\r", SystemCoreClock);
	printfS("umlcd test start\n\r");	

	umlcd_test();
	
}


