
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef LAB_H
#define LAB_H


/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "lcd_g.h"
#include "onewire.h"
#include "RC5_IR_Emul_Receiver.h"
#include "init_l.h"
#include "process.h"
#include "process2.h"

/* ZERO ************************************************/
#define		ZERO_PORT			GPIOA
#define		ZERO					GPIO_PIN_3
/* TFT_IRQ ************************************************/
#define		TFT_IRQ_PORT		GPIOD
#define		TFT_IRQ					GPIO_PIN_7
/* BEPPER ************************************************/
#define		BEPPER_PORT		GPIOD
#define		BEPPER			  GPIO_PIN_15
/* FLOOD ************************************************/
#define		FLOOD_PORT		GPIOC
#define		FLOOD					GPIO_PIN_2
/* TRIAC ************************************************/
#define		TRIAC_PORT		GPIOC
#define		TRIAC					GPIO_PIN_8

/* DIMROT ************************************************/
#define		PWM_DIMROT_PORT		GPIOB
#define		PWM_DIMROT				GPIO_PIN_1

/* PWM_SPIRT ************************************************/
#define		PWM_SPIRT_PORT		GPIOE
#define		PWM_SPIRT					GPIO_PIN_9

/* PWM_MOTOR ************************************************/
#define		PWM_MOTOR_PORT		GPIOC
#define		PWM_MOTOR					GPIO_PIN_7

/* BUTTON ************************************************/
//#define		BUTTON_PORT		GPIOB
//#define		BUTTON				GPIO_PIN_1

/* I2C ************************************************/
#define I2C_PORT        GPIOB  
#define I2C_D1          GPIO_PIN_6
#define I2C_D2          GPIO_PIN_7

/* ADC ************************************************/
#define ADC_TRIAC_PORT   		GPIOA  
#define ADC_TRIAC    				GPIO_PIN_0
#define ADC_CHANNEL_TRIAC   ADC_CHANNEL_0

#define ADC5_PORT        GPIOC  
#define ADC5          	 GPIO_PIN_3
#define ADC_CHANNEL5     ADC_CHANNEL_13

#define ADC_MPX_PORT     GPIOA  
#define ADC_MPX          GPIO_PIN_4
#define ADC_CHANNEL_MPX  ADC_CHANNEL_4

#define ADC_SPIRT_PORT     GPIOC  
#define ADC_SPIRT          GPIO_PIN_0
#define ADC_CHANNEL_SPIRT  ADC_CHANNEL_10

#define COOLER_PORT     GPIOB  
#define COOLER          GPIO_PIN_8

/* IP LAN ************************************************/	
#define DEBUG_PROC 0                                                                            

#define SPI_GPIO_PORT       GPIOC  
#define SPI_SCK_PIN					GPIO_PIN_10
#define SPI_MISO_PIN				GPIO_PIN_11
#define SPI_MOSI_PIN				GPIO_PIN_12

#define ILI9341_WRX_PORT 	GPIOD
#define ILI9341_WRX_PIN  GPIO_PIN_0

#define ILI9341_RST_PORT 	GPIOD
#define ILI9341_RST_PIN  	GPIO_PIN_1

#define ILI9341_CS_PORT 	GPIOD
#define ILI9341_CS_PIN  	GPIO_PIN_2

#define LED_ILI         GPIO_PIN_9 
#define LED_ILI_PORT    GPIOB 

#define VECT_TAB_OFFSET  0x00000
//#if DEBUG_PROC
//#define VECT_TAB_OFFSET  0x00000
//#else
//#define VECT_TAB_OFFSET  0x20000 												 
//#endif

#define DP83848_PHY_ADDRESS             0x01  
#define PHY_SR                          ((uint16_t)0x10)    /*!< PHY status register Offset                      */
#define PHY_SPEED_STATUS                ((uint16_t)0x0002)  /*!< PHY Speed mask                                  */
#define PHY_DUPLEX_STATUS               ((uint16_t)0x0004)  /*!< PHY Duplex mask                                 */

#define IP_ADDR0   192
#define IP_ADDR1   168
#define IP_ADDR2   1
 #if DEBUG_PROC
	#define IP_ADDR3   56
#else
	#define IP_ADDR3   55
#endif 	
		
/* IP LAN ************************************************/	


#define NETMASK_ADDR0   255
#define NETMASK_ADDR1   255
#define NETMASK_ADDR2   255
#define NETMASK_ADDR3   0

#define GW_ADDR0   192
#define GW_ADDR1   168
#define GW_ADDR2   1
#define GW_ADDR3   1

#define		EEPROM_I2C_ADDRESS	0xA0
#define		DS2482_HW_ADDRESS		0x31

#define		DEV_RST_PORT	GPIOA
#define		PHY_RST_PIN		GPIO_PIN_5
/* IR ************************************************/
//#define RC5_EXTI_LINE        	 ((uint32_t)0x00001)  /*EXTI_Line0*/
#define	IR_PORT					GPIOB
#define	IR							GPIO_PIN_0
#define RC5_EXTI_IRQn   EXTI0_IRQn          /* RC5 EXTI IRQ */
#define RC5_TIM         TIM7                 /* Timer used for RC5 decoding */
#define RC5_TIM_IRQn    TIM7_IRQn            /* RC5 TIM IRQ */
#define RC5_TIM         TIM7            /* RC5 TIMx */

/* Key ************************************************/
#define Key_Start                        12 
#define Key_Lock                         52 
#define Key_Menu                         59 
#define Key_No_At                      	 50
#define Key_Save                         7 
#define Key_Esc                          34 
#define Key_Set                          48 
#define Key_At                           55 

#define Key_Up                          16 
#define Key_Down                        17
#define Key_VOL_M                       32 
#define Key_VOL_P                       33 
	
#define Key_SAVE                        13 
#define Key_UPDATE_FIRMWARE             100 
#define Key_RESTART                     101

/* FLASH ************************************************/
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */

#define FLASH_USER_START_ADDR   ADDR_FLASH_SECTOR_8  

#define MAX_MOTOR 			200
#define ENABLE_LCD_TIME 120

#define countof(a) (sizeof(a) / sizeof(*(a)))
	
#define __ASM __asm
#define __INLINE __inline

#define ave_quantity    3 

	
//extern Menu const *MenuTime;


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

extern uint16_t ADCxConvertedValue[1024];
extern ADC_HandleTypeDef  AdcHandle;

extern TIM_OC_InitTypeDef sConfigTRIAC;
extern TIM_HandleTypeDef TimHandleTRIAC;
extern TIM_HandleTypeDef TimHandleADC;
extern TIM_HandleTypeDef TimHandle50ms;
extern TIM_HandleTypeDef TimHandle6;
extern TIM_OC_InitTypeDef sConfig;
extern ADC_HandleTypeDef  AdcHandle_ADC;

extern IWDG_HandleTypeDef Hiwdg;

extern TIM_HandleTypeDef TimHandlePWM_Spirt;
extern TIM_HandleTypeDef TimHandlePWM_Dimrot;
extern TIM_HandleTypeDef TimHandlePWM_Motor;
extern TIM_HandleTypeDef TimHandlePWM_Buzzer;
extern TIM_HandleTypeDef TimHandlePWM_LED_ILI;

extern TIM_OC_InitTypeDef sConfig_Dimrot;
extern TIM_OC_InitTypeDef sConfig_Spirt;
extern TIM_OC_InitTypeDef sConfig_Motor;
extern TIM_OC_InitTypeDef sConfig_Buzzer;
extern TIM_OC_InitTypeDef sConfig__LED_ILI;

extern struct netif gnetif;

extern I2C_HandleTypeDef hi2c1;
extern SPI_HandleTypeDef SpiHandle;
extern SPI_HandleTypeDef SpiTFTHandle;
extern RTC_HandleTypeDef RtcHandle;

extern uint16_t Triac_Power_Real;
extern uint16_t Triac_Power_Val;
extern uint32_t ADC_5;
extern uint16_t Counter_Triac;
extern int32_t n_RC5_TRIAC;
extern char LED_out[4][40];
extern uint8_t LED_edit[3];

extern uint32_t number_ADC;
extern uint32_t sum_ADC;

extern uint32_t i_ADC;
extern uint8_t i_int;
extern uint64_t time_loc;

extern uint8_t hour;
extern uint8_t min;
extern uint8_t sec;

extern uint32_t Err_Power;
	
typedef struct {
	uint8_t		address[8];
	uint8_t		attention;
} Device;


void Update_Time(uint8_t start_mode);
float Get_Var(void *var, uint8_t type);
uint32_t Get_uint32(void *var, uint8_t type);
void Parse_Time(uint32_t time, uint8_t *hour, uint8_t *min, uint8_t *sec);

void Erase_Sector(void);
void Write_Flash(uint32_t Address, uint32_t DATA);
void GFX_Rect();
void GFX_NBK();
void RGB565ToRGB888(uint16_t rgb565,char *rgb888);

uint32_t Interpolation(uint32_t x0,uint32_t y0,uint32_t x1,uint32_t y1, uint32_t var);

uint32_t Read_Flash(uint32_t Address);

#endif /* LAB_H */



