/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : uType.h
  * @brief          : Header for remote.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

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

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h> //memset
#include <stdio.h>	//printf
#include <stdbool.h> // bool
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
#define MAX_FRAME_SIZE 270  // 485接收回调一帧字节
#define BUFFER_SIZE485 270  // 485串口接收缓冲区大小
#define BUFFER_SIZEP 270  // 调试串口接收缓冲区大小
#define FILTER_BUFFER_SIZE 30  // 滤波缓冲区大小

#define PRINT_BUF_SIZE 270	// 打印结构体的缓冲区
	
#define FILTER_THRESHOLD 600  // 

#define CURRENT_FIRMWARE_VERSION 0x0007C0EC // 固件版本号

extern volatile uint8_t debug_print_enabled;
extern volatile uint8_t fdc_print_enabled;
extern volatile uint8_t ads_print_enabled;
extern volatile uint8_t kv_print_enabled;  // 默认关闭KV数据库打印
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */


/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
typedef  struct _Uart485Rx_msg
{
	uint8_t data[MAX_FRAME_SIZE];
	uint16_t len;
}Uart485Rx_msg;

typedef struct {
    char msg[PRINT_BUF_SIZE];
} Print_msg;

typedef  struct _Console_msg
{
	uint8_t data[MAX_FRAME_SIZE];
	uint16_t len;
}Console_msg;

typedef  struct _Filter_msg1
{
	float data;
}Filter_msg1;

typedef  struct _Filter_msg2
{
	float data;
}Filter_msg2;

typedef  struct _Filter_msg3
{
	float data;
}Filter_msg3;

typedef  struct _Filter_msg4
{
	float data;
}Filter_msg4;

typedef  struct _Filter_msg5
{
	float data;
}Filter_msg5;

typedef enum {
    releasemode ,   // 发布模式
    IAPbootloader,  // 升级模式
		Debugmode , 		// 调试模式
		BACKUPMODE
} sysmode ;
extern sysmode Systemmode;

#define CALIB_POINT_NUM 11  // 油水检测标定为11个插值点
#define CALIB_HEIGHT_POINT_NUM 6  // 余油液位高度检测标定为6个插值点
#define CALIB_ROD_POINT_NUM 7  // 阀杆电压检测标定
	
typedef struct {
    float cap_values[CALIB_POINT_NUM];     // 电容值（pF）
} CalibPoints_t; // 油水检测电容float

typedef struct {
    float cap_values[CALIB_HEIGHT_POINT_NUM];    // 电容值（pF）
		float vol_values[CALIB_HEIGHT_POINT_NUM];    // 电压值（V）
} Calib_residual_oil_t; // 余油双检测电容压力

typedef struct {
    float vol_values[CALIB_ROD_POINT_NUM];    // 电压值（V）
} Calib_rod_t; // 阀杆电压值

typedef struct {	
	// 标定数据
	Calib_rod_t             calib_rod;
	CalibPoints_t           calib_oil_water;
	Calib_residual_oil_t    calib_residual_oil;
	
	
	float rod_vol_offset;
	float rod_dialog;
	uint16_t rod_coltime;
	
	float oil_para_indu;
	float oil_para_cap;	
	float oil_cap_offset;
	float oil_dialog;
	uint16_t rod_oil_coltime;
	
	float res_para_indu;
	float res_para_cap;
	float res_cap_offset;
	float res_vol_offset;
	float res_cap_dialog;
	float res_vol_dialog;
	uint16_t res_oil_cap_coltime;
	uint16_t res_oil_vol_coltime;
	
	float temp_vol_offset;
	float temp_dialog;
	uint16_t temp_coltime;
	
}JsonDat_Work;
extern JsonDat_Work JsWork;

// 架构上通用的基础根数据
typedef struct   __attribute__((aligned(8))){
    char mac[13];       // 12位数字形式的SN号，如"112233445566"
		uint8_t addr;       // modbus通讯地址，1~250
    int role;           // 设备角色，数字形式
		uint32_t vers_id;		// 固件内置的常数值
		int age;						// 引擎工作时长，单位秒
		long age_ms;				// 引擎工作时长，单位毫秒
		int iapSize;				// IAP程序大小(字节数)
		char iapMd5[9];     // IAP-md5校验码(前4字节的16进制字符串表示)
		uint32_t iapLoadStatus;   // IAP装载状态 值0-烧写区 2-APP2区
		uint16_t subcode; 	// 升级文件要不要md5	
		int ubootback;			// Bootloader状态码
	
		int debug; 					// 控制台激活剩余时间，单位分钟， 如值0表示控制台被关闭	
    int console;        // 控制台端口选择，数字形式   
    int baudrate;       // modbus通讯波特率，4800的整数倍	
    int update_ok;      // IAP升级成功次数
    int update_err;     // IAP升级失败次数
		JsonDat_Work *work;
} JsonDat_Root;
extern JsonDat_Root JsRoot;

typedef struct {
    GPIO_TypeDef* SDA_Port;
    uint16_t SDA_Pin;
    GPIO_TypeDef* SCL_Port;
    uint16_t SCL_Pin;
} SoftI2C_HandleTypeDef;

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/


/* USER CODE BEGIN EFP */


/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/

/* USER CODE BEGIN Private defines */
//#define DEBUG_PRINT   1  // 1=enable debug print, 0=disable
//#if DEBUG_PRINT
//#define DEBUG_PRINTF(...)    printf(__VA_ARGS__)
//#else
//#define DEBUG_PRINTF(...)
//#endif

#define DEBUG_PRINTF(...)    \
    do { if (debug_print_enabled) printf(__VA_ARGS__); } while(0)
		
#define FDC_PRINTF(...)    \
do { if (fdc_print_enabled) printf(__VA_ARGS__); } while(0)

#define ADS_PRINTF(...)    \
do { if (ads_print_enabled) printf(__VA_ARGS__); } while(0)

#define KV_PRINTF(...)    \
do { if (kv_print_enabled) printf(__VA_ARGS__); } while(0)

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __UTYPE_H */
 
