#include "main.h"
#include "process2.h"

#define Mc2h5oh 46.06844  // g/mol
#define Mh2o 18.01528	  // g/mol

I2C_HandleTypeDef hi2c1;
SPI_HandleTypeDef SpiHandle;
SPI_HandleTypeDef SpiTFTHandle;

RTC_HandleTypeDef RtcHandle;

TIM_HandleTypeDef TimHandle50ms;
TIM_HandleTypeDef TimHandleTRIAC;
TIM_HandleTypeDef TimHandleADC;
TIM_HandleTypeDef TimHandle6;
TIM_HandleTypeDef TimHandlePWM_Spirt;
TIM_HandleTypeDef TimHandlePWM_Motor;
TIM_HandleTypeDef TimHandlePWM_Dimrot;
TIM_HandleTypeDef TimHandlePWM_Buzzer;
TIM_HandleTypeDef TimHandlePWM_LED_ILI;

TIM_OC_InitTypeDef sConfig_Dimrot;
TIM_OC_InitTypeDef sConfig_Spirt;
TIM_OC_InitTypeDef sConfig_Motor;
TIM_OC_InitTypeDef sConfig_Buzzer;
TIM_OC_InitTypeDef sConfig__LED_ILI;
ADC_HandleTypeDef  AdcHandle;

ADC_HandleTypeDef  AdcHandle_ADC;

TIM_HandleTypeDef PWM_SPIRT_TimHandle;
RTC_TimeTypeDef  	stimestructure;

TIM_OC_InitTypeDef sConfigTRIAC;

IWDG_HandleTypeDef Hiwdg;

uint16_t ADCxConvertedValue[1024];
 
uint32_t ADC_5;
uint32_t number_ADC;
uint32_t sum_ADC;

uint16_t Triac_Power_Real;
uint16_t Triac_Power_Val 	= 0;
int32_t n_RC5_TRIAC 			= 1100;

uint8_t hour = 0;
uint8_t min  = 0;
uint8_t sec  = 0;
uint64_t time_loc   			= 0;

uint32_t Err_Power = 0;

char Buffer_LCD[NUM_ARR];
char Buffer_Att[200];

//static uint32_t GetSector(uint32_t Address)
//{	
//  uint32_t sector = 0;
//  
//  if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
//  {
//    sector = FLASH_SECTOR_0;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
//  {
//    sector = FLASH_SECTOR_1;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
//  {
//    sector = FLASH_SECTOR_2;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
//  {
//    sector = FLASH_SECTOR_3;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
//  {
//    sector = FLASH_SECTOR_4;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
//  {
//    sector = FLASH_SECTOR_5;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
//  {
//    sector = FLASH_SECTOR_6;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
//  {
//    sector = FLASH_SECTOR_7;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
//  {
//    sector = FLASH_SECTOR_8;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
//  {
//    sector = FLASH_SECTOR_9;  
//  }
//  else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
//  {
//    sector = FLASH_SECTOR_10;  
//  }
//  else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11) */
//  {
//    sector = FLASH_SECTOR_11;  
//  }

//  return sector;
//}

//void Erase_Sector(void){
//	
//  FLASH_EraseInitTypeDef EraseInitStruct;
//	uint32_t SectorError = 0;
//	
//  EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
//  EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3;
//  EraseInitStruct.Sector = GetSector(FLASH_USER_START_ADDR);
//  EraseInitStruct.NbSectors = 1;
//	
//  if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){ }
//	
//}

//void Write_Flash(uint32_t Address, uint32_t DATA){
//	
//	uint32_t Real_Address = 0;
//	uint32_t FirstSector = 0;	
//	
//	HAL_FLASH_Unlock();
//	
//	Erase_Sector();

//  Real_Address = FLASH_USER_START_ADDR+Address;

//  if (HAL_FLASH_Program(TYPEPROGRAM_WORD, Real_Address, DATA) == HAL_OK)
//   {
//     Real_Address = Real_Address + 4;
//		}

//  HAL_FLASH_Lock();

//}

//uint32_t Read_Flash(uint32_t Address){
//	
//	uint32_t Real_Address = 0, DATA;
//	
//  Real_Address = FLASH_USER_START_ADDR+Address;

//	DATA = *(__IO uint32_t*)Real_Address;
//	
//	return DATA;  

//}


void Update_Time(uint8_t start_mode){
	uint32_t time_tmp;
	
	RTC_DateTypeDef sdatestructureget;
  RTC_TimeTypeDef stimestructureget;

//	HAL_RTC_GetTime(&RtcHandle, &stimestructureget, FORMAT_BIN);
//	HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, FORMAT_BIN);
//	Hour  = stimestructureget.Hours;	
//	Min 	= stimestructureget.Minutes;
//	Sec 	= stimestructureget.Seconds;
//			
//	if (stop_start == 0){
//		sec_start = 0;
//	}
//	else{
//		if((sec_start == 0)|(start_mode)){
//			sec_start = 3600*Hour + 60*Min + Sec;
//		}
//		time_tmp = 3600*Hour + 60*Min + Sec;
//		time_tmp = time_tmp - sec_start;
//	}

	if (stop_start == 0){
		sec_start = 0;
	}
	else{
		if((sec_start == 0)|(start_mode)){
			sec_start = time_loc;
		}
		time_tmp = time_loc - sec_start;
	}

	Parse_Time(time_tmp, &hour_delta, &min_delta, &sec_delta); 
	Parse_Time(time_loc, &hour, &min, &sec); 
	
}

 float Get_Var(void *var, uint8_t type){
		float tmp_float;
	 
		switch (type)
      {    
      case 0: 
			//--------------------- temperature --------------------------  
			tmp_float = *(float *)(var);
			break; 
			case 8: //--------------------- uint8_t --------------------------  
				tmp_float = (float)*(uint8_t *)(var);
			break; 
			case 16: //--------------------- uint16_t --------------------------  
				tmp_float = (float)*(uint16_t *)(var);
			 break;
			case 32: //--------------------- uint32_t --------------------------  
				tmp_float = (float)*(uint32_t *)(var);
			 break;
			}

		return tmp_float;		
}
 
 uint32_t Get_uint32(void *var, uint8_t type){	 
	uint32_t tmp_uint32;

	 		switch (type)
      {    
      case 0: 
			//--------------------- temperature --------------------------  
			tmp_uint32 = (uint32_t)(1000*(*(float *)(var)));
			break; 
			case 8: //--------------------- uint8_t --------------------------  
				tmp_uint32 = (uint32_t)*(uint8_t *)(var);
			break; 
			case 16: //--------------------- uint16_t --------------------------  
				tmp_uint32 = (uint32_t)*(uint16_t *)(var);
			 break;
			case 32: //--------------------- uint32_t --------------------------  
				tmp_uint32 = (uint32_t)*(uint32_t *)(var);
			 break;
			}
			
	return tmp_uint32;			 
}

 void Parse_Time(uint32_t time, uint8_t *hour_l, uint8_t *min_l, uint8_t *sec_l){
	*hour_l = time/3600;
	*min_l = time%3600/60;
	*sec_l = time%3600%60;	 
}
 


uint32_t Interpolation(uint32_t x0,uint32_t y0,uint32_t x1,uint32_t y1, uint32_t var){
	uint32_t interpolation = 0;
	
	int64_t tmp = (1000*(var-x0))/(x1-x0);
	
	tmp = tmp*(int32_t)(y1-y0);
	
	//if (tmp<0){tmp *= -1;}
	interpolation =(1000*y0 + tmp)/1000;
	
	return interpolation;
}


void GFX_Rect(){
	uint8_t i, j;
	
	TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240

	GG_ILI9341_PutImage(240, 60, &pump_image );
	GG_ILI9341_PutImage(240, 117, &press_image );
	
	//Куб
	TM_ILI9341_INT_Fill(180,150,270,235,0xFD50); 
	TM_ILI9341_DrawRectangle(180,150,270,235,COLOR_BLACK);
	//Колона
	TM_ILI9341_INT_Fill(215,10,235,150,0xFD50); //252/170/135 
	TM_ILI9341_DrawRectangle(215,10,235,150,COLOR_BLACK);
	//Клапан спирта
	TM_ILI9341_INT_Fill(180,50,222,53,COLOR_BLUE); 
	TM_ILI9341_INT_Fill(180,50,183,75,COLOR_BLUE);
	TM_ILI9341_INT_Fill(176,60,186,70,COLOR_BLACK);
	//Линии температур
	TM_ILI9341_INT_Fill(170,10,214,13,COLOR_ORANGE);
	TM_ILI9341_INT_Fill(170,130,214,133,COLOR_ORANGE);
	TM_ILI9341_INT_Fill(170,210,179,213,COLOR_ORANGE);
	//линия подачи гидроселекции
	TM_ILI9341_INT_Fill(243,5,245,60,COLOR_BLUE);
	TM_ILI9341_INT_Fill(223,2,245,4,COLOR_BLUE);
	TM_ILI9341_INT_Fill(223,4,225,10,COLOR_BLUE);
	//Клапан воды
	TM_ILI9341_INT_Fill(235,16,267,20,COLOR_BROWN);
	TM_ILI9341_INT_Fill(250,8,260,22,COLOR_BROWN);
	
	//Димрот
	i = 0;
	for (i=12; i<30; i+=3){		
		TM_ILI9341_DrawLine(233,i,217,i+3,COLOR_BLUE);
	}
	
	//Насадка
	TM_ILI9341_SetSquare(216, 35, 234, 140);
	TM_SPI_SetDataSize(&SpiHandle, SPI_DATASIZE_16BIT);  // переключаем на 16-битный режим SPI
	uint16_t pixels_count = 24300;
	
	while (pixels_count) {
			pixels_count--;
		
		if (j==1){
			 TM_ILI9341_SendData16(COLOR_GRAY);
			 j = 0;
		}
		else{
			TM_ILI9341_SendData16(0xFD50);
			j = 1;
		}
	}	
	TM_SPI_SetDataSize(&SpiHandle, SPI_DATASIZE_8BIT);  // переключаем на 8-битный режим SPI
		
	PrintToLCD(1,0);
	

	  TimHandlePWM_Motor.Init.Prescaler = 50-1; //16.8 Khz   water
	
	  HAL_TIM_Base_Stop_IT(&TimHandlePWM_Motor);
	  HAL_TIM_PWM_DeInit(&TimHandlePWM_Motor);
		HAL_TIM_PWM_Stop(&TimHandlePWM_Motor, TIM_CHANNEL_2);
	
	  HAL_TIM_PWM_Init(&TimHandlePWM_Motor);
		HAL_TIM_PWM_Start(&TimHandlePWM_Motor, TIM_CHANNEL_2);
	
}

void GFX_NBK(){
	uint8_t i;
	
	TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240
	
	GG_ILI9341_PutImage(240, 70, &pump_image );
	GG_ILI9341_PutImage(240, 117, &press_image );

	//Куб
	TM_ILI9341_INT_Fill(180,150,270,235,0xFD50); 
	TM_ILI9341_DrawRectangle(180,150,270,235,COLOR_BLACK);
	//Колона
	TM_ILI9341_INT_Fill(215,15,235,150,0xFD50);
	TM_ILI9341_DrawRectangle(215,15,235,150,COLOR_BLACK);
	
	//Выход сырца
	TM_ILI9341_INT_Fill(170,7,227,10,COLOR_ORANGE);
	TM_ILI9341_INT_Fill(223,7,227,16,COLOR_ORANGE);
	TM_ILI9341_DrawRectangle(223,7,227,16,COLOR_BLACK);

	//линия подачи браги
	TM_ILI9341_INT_Fill(243,25,246,70,COLOR_BLUE);
	TM_ILI9341_INT_Fill(223,22,246,25,COLOR_BLUE);
	TM_ILI9341_INT_Fill(223,22,226,28,COLOR_BLUE);
	
	//Линии температур и спиртуозности пара
	TM_ILI9341_INT_Fill(170,120,215,123,COLOR_ORANGE);
	TM_ILI9341_INT_Fill(170,210,180,213,COLOR_ORANGE);

  //тарелки
	TM_ILI9341_INT_Fill(224,33,226,145,COLOR_GRAY);
	for (i=35; i<140; i=i+8){	
		TM_ILI9341_INT_Fill(216,i,234,i+2,0x09240); //150/75/0
	}
	
	PrintToLCD(1,0);
	
		TimHandlePWM_Motor.Init.Prescaler = 9000-1; // 93 hz // osmo
	
	  HAL_TIM_Base_Stop_IT(&TimHandlePWM_Motor);
	  HAL_TIM_PWM_DeInit(&TimHandlePWM_Motor);
		HAL_TIM_PWM_Stop(&TimHandlePWM_Motor, TIM_CHANNEL_2);
	
	  HAL_TIM_PWM_Init(&TimHandlePWM_Motor);
		HAL_TIM_PWM_Start(&TimHandlePWM_Motor, TIM_CHANNEL_2);
	
}

void RGB565ToRGB888(uint16_t rgb565,char *rgb888) {
	char Buffer[14];
	
  uint8_t b = (((rgb565) & 0x001F) << 3) & 0xFF;
  uint8_t g = (((rgb565) & 0x07E0) >> 2) & 0xFF;
  uint8_t r = (((rgb565) & 0xF800) >> 8) & 0xFF;
	
	sprintf(rgb888,"%d,%d,%d", r,g,b); 	

   // return 0x000000ff | (r << 24) | (g << 16) | (b << 8);

}
