/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 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 */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Inf_4G.h"
#include "stdio.h"
#include "driver.h"
#include "Irrigation.h"
#include "Ozone.h"
#include "wendu.h"
#include "HLW8032.h"
#include "EEPROM.h"
#include "energy_manager.h"
#include "electrode.h"
#include "paini.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
// 闂傚倸鍊搁崐鎼佸磹妞嬪海鐭嗗〒姘ｅ亾妤犵偛顦甸弫宥夊礋椤掍焦顔囨繝寰锋澘鈧洟宕姘辨殾闁哄被鍎查悡鏇犫偓鍏夊亾闁逞屽墴瀹曟洟骞嬮悩鐢殿槸闂佸搫绋侀崢浠嬫偂濞嗘挻鐓熸俊銈傚亾闁绘锕﹀▎銏ゆ嚑椤掑倻锛滈梺閫炲苯澧柣锝嗙箞瀹曠喖顢楅崒姘闂佽楠哥粻宥夊磿鏉堚晜宕查柟鐑橆殕閸嬧晝鈧娲栧ú銊у婵傚憡鐓冪憸婊堝礈閻旂厧鏋侀柟鍓х帛閺呮悂鏌ㄩ悤鍌涘0
uint8_t aRxBuffer;

uint8_t received_control = 0; // 婵犵數濮烽弫鍛婃叏閻戣棄鏋侀柟闂寸绾惧潡鏌熺€电ǹ啸缂佲偓婵犲洦鐓冪憸婊堝礈濮樿泛桅闁告洦鍨伴崡鎶芥煕閳╁喚娈旀い蹇ｄ邯閺屾稑鈻庤箛鏇狀啋闂佸搫鐭夌紞渚€鐛崶顒夋晢濠㈣泛锕ラ宥夋⒒娴ｅ摜锛嶇紒顕呭灠铻為柛鎰靛枛閽冪喓鈧箍鍎遍悧婊冾瀶閵娾晜鈷戦柛娑橈攻鐏忕増鎱ㄥΟ绋垮闁告帗甯掗埢搴ㄥ箛椤掑倻鐛梻浣筋潐濠㈡﹢鎮樺璺虹柧婵犲﹤鐗婇埛鎴︽煕濞戞﹫姊楃紒鍫曚憾閺屾盯濡搁妶鍛ギ闂佽鍠楅敃銏ゅ蓟閸℃鍚嬮柛鈾€鏅滈ˉ濠冧繆閻愵亜鈧牠宕濋幋锕€鍨傞柣鎴灻欢鐐烘煕閺囥劌鍘靛ù婊勭矒閺屾洘绻涢崹顔煎濡炪們鍎茬划宥夊Φ閸曨垼鏁囬柣鎰綑閺嬬娀姊虹拠鈥虫灆缂侇喗鐟ラ悾鐤亹閹烘繃鏅濆銈庡亽閸橀箖鏌﹂悽鍛娾拻闁稿本鐟х粣鏃€绻涙担鍐叉濞咃絽鈹戦悙鑸靛涧缂傚秳绶氳棢闁圭偓鐣禒姘舵煙閹澘袚闁抽攱甯掗湁闁挎繂鎳忛崯鐐烘煙椤栨氨澧﹂柟顔筋殔椤繈宕￠悙鎻掔闁诲氦顫夊ú婵嬶綖婢跺备鍋撻棃娑栧仮鐎殿喖鐖奸獮瀣偐閸偅鐦炲┑鐘垫暩閸嬬偤骞愭繝姘槬闁告劦鍠栫壕濠氭煙閹呬邯闁稿鎸搁埢鎾诲垂椤旂晫浜栭梻浣告啞閺屻劑鏌婇敐澶屽祦闁硅揪绠戠粈瀣亜閺嶃劎鐓紒鍗炵埣濮婃椽宕ㄦ繝鍐槱闂佸憡枪閸嬫劙寮鈧獮妯肩磼濡攱瀚藉┑鐐舵彧缁茶偐鎷冮敃鍌氱哗濞寸厧鐡ㄩ悡鐔搞亜閹捐泛浠﹂柛鐘愁焽閳ь剝顫夊ú姗€鏁冮姀鈥茬箚婵繂鐭堝Σ璇参旈悩闈涗汗闁稿鎹囧濠氬磼濮橆兘鍋撴搴ｇ焼濞撴埃鍋撴鐐差樀閺佸秹宕熼鐘靛幆闂備礁婀遍崕銈夈€冮崨鏉戝瀭闁稿本绋撶弧鈧梻鍌氱墛娓氭宕曡箛鏇犵＜闁绘瑢鍋撻柛锝忕秮瀵濡搁埡鍌氫簽闂佺ǹ鏈粙鎴︻敂閿燂拷
volatile uint8_t mode_control_flag = 0;
#define READ_CONTROL_INTERVAL 100  // 闂傚倸鍊搁崐鎼佸磹瀹勬噴褰掑炊椤掑﹦绋忔繝銏ｆ硾椤戝洭銆呴幓鎹楀綊鎮╁顔煎壈缂佺偓鍎冲锟犲蓟閿涘嫪娌悹鍥ㄥ絻婵酣姊洪崫鍕靛剮闁煎啿鐖奸獮澶愬箹娴ｇ懓浜遍梺鍓插亝缁诲嫰鎮烽妸褏纾藉ù锝呮惈瀛濈紒鍓ц檸閸欏啴鐛径宀€鐭欐繛鍡樺劤閹垶绻濋姀锝嗙【闁挎洏鍎卞嵄妞ゆ巻鍋撻柍瑙勫灦楠炲﹪鏌涙繝鍐╃妤犵偛锕ラ幆鏃堝煡閸℃娅婃繝鐢靛█濞佳囶敄閸℃稒鍋傛繛鍡樻尰閸婂灚绻涢幋鐑嗕痪妞ゅ繐妫涚粻鏃€鎱ㄥ鍡楀幋闁衡偓閼恒儯浜滈柡鍌涘閹牓鏌ｈ箛鏃傚弨闁哄备鈧磭鏆嗛悗锝庡墰琚﹀┑鐘殿暯閸撴繈骞冮崒娑楃箚闁绘垼妫勫敮濡炪倖妫佸畷鐢靛閹€鏀介柣妯活問閺嗩垱淇婇幓鎺撳殗鐎规洘鍨垮畷鐔碱敃椤愩倕濮洪梻浣芥硶閸犳挻鎱ㄩ幘顔煎嚑濞撴埃鍋撻柡宀€鍠栧畷婊嗩槾閻㈩垱鐩弻锟犲川椤旇棄鈧劙鏌＄仦璇插闁诡喓鍊濆畷鎺戔槈濮楀棔绱�?(ms)
#define DATA_UPLOAD_INTERVAL 10000 // 闂傚倸鍊搁崐鎼佸磹瀹勬噴褰掑炊瑜滃ù鏍煏婵炵偓娅嗛柛銈呭閺屻倗绮欑捄銊ょ驳濠电偛鎳愭慨鍨┍婵犲洤围闁稿本鐭竟鏇㈡⒒娴ｉ涓茬紒韫矙閹ê顫濈捄铏诡唵闂佸憡绋掑娆撳箲閼哥偣浜滈柟鎯ь嚟閳洟鏌ｈ箛鎾虫殭闁宠鍨块幃鈺呭垂椤愶絾鐦庡┑鐘愁問閸犳绻涙繝鍥х疇婵°倕鎳庨柋鍥煛閸モ晛鏋庨柍褜鍓欓崲鏌ュ煘閹达附鍋愰柛娆忣槸椤︹晠姊虹紒妯诲暗闁哥姵鐗犲濠氬即閻旇櫣顔曢梺鍓茬厛閸犳牗鎱ㄦ惔鈽嗘富闁靛牆鎳忕粋瀣煕濡灝袚缂佸矁椴哥换婵嬪炊閼稿灚娅囬梻渚€娼х换鍡涘焵椤掆偓閸樻牕菐椤曗偓濮婄粯鎷呴懞銉ｂ偓鍐煟閹虹偟鐣垫鐐村灴瀹曞爼顢楅埀顒勫几娓氣偓閺岀喖宕滆鐢盯鏌￠崨顔藉€愰柡灞诲姂閹倝宕掑☉姗嗕紦?(ms)
#define CONTROL_PRIORITY_DELAY 500 // 闂傚倸鍊搁崐鎼佸磹閻戣姤鍤勯柛顐ｆ磸閳ь兛鐒︾换婵嬪炊閵娿儱澹掗梻浣规偠閸庢椽宕滃▎鎴犵焼闁告劦鍠楅悡蹇撯攽閻愭垵鍟弳娆戠磼閻樺啿鐏存慨濠傤煼瀹曟帒鈻庨幋顓熜滈梻浣侯攰椤曟粎妲愰弴鐘插灊婵炲棙鎸哥粻铏繆閵堝倸浜鹃梺鎼炲妼閸婂湱鎹㈠┑瀣棃婵炴垵宕崜鎵磽娴ｅ搫顎岄柛锝忕秮瀵寮撮姀鐘诲敹濠电娀娼уù鍌毼涢悙鐢电＝濞达絿枪椤ｆ娊鏌涚€ｃ劌鈧繂顕ｇ拠娴嬫闁靛繒濮村畵鍡涙⒑闂堟胆褰掑磿鏉堚晝涓嶆繛宸簼閳锋垹绱掔€ｎ亜鐨￠柛鏃傚枛閺屾稓鈧絻鍔岄埀顒佺箓椤曪絿鎷犲ù瀣潔濠殿喗顨呭Λ娆撳磽闂堟侗娓婚柕鍫濇缁楁帡鎮楀鍗炲幋闁糕斁鍋撳銈嗗笂缁垛€斥枔閺囩姷纾肩紓浣诡焽濞插鈧娲熸禍鍫曠嵁閸ヮ剦鏁嗛柛灞剧懅濡垰鈹戦悩鍨毄闁稿鍠栧畷銏ｎ樄闁炽儻绠撻幃婊堟寠婢跺瞼鏆梻渚€娼х换鍫ュ磹閺囩姷鐭嗛柛顐ｇ贩瑜版帗鏅查柛銉戝啫顬嗛梻浣告憸閸犳劕顭垮鈧崺鐐哄箣閿旇棄鈧攱绻涢崼鐔奉嚋妞ゃ儲鎹囧铏圭矙濞嗘儳鍓遍梺鍛婃⒐閻楃娀鐛崼銉ノ╅柕澹嫭鐤傞梻渚€鈧稓绁锋繛鍛礋閹ɑ绗熼埀顒€顫忛搹瑙勫厹闁告侗鍠栧☉褏绱撴担鍝勑ｉ柛銊ョ秺閹噣骞嗚閸氬顭跨捄渚剳闁告ê宕—鍐Χ閸℃衼缂備焦褰冩晶鐣屽垝閸儱绀冩い鏃傛櫕閸欏棗鈹戦悙鏉戠伇婵炲懏娲滈埀顒佺啲閹凤拷(ms)
uint16_t data1[3]={0};
#define SECONDS_PER_MINUTE 60 // 60闂傚倸鍊搁崐鎼佸磹瀹勬噴褰掑炊瑜夐弸鏍煛閸ャ儱鐏╅梻鍌ゅ灦閺屻劑寮撮悙娴嬪亾閸濄儳涓嶆い鏍仦閻撱儵鏌ｉ弴鐐测偓鍦偓姘炬嫹? = 1闂傚倸鍊搁崐鎼佸磹妞嬪海鐭嗗〒姘ｅ亾妤犵偛顦甸弫宥夊礋椤掍焦顔囬梻浣告贡閸庛倝宕甸敃鈧埥澶娢熼柨瀣澑闂佽鍑界紞鍡樼閻愪警鏁婇柛銉墯閳锋垿鎮归幁鎺戝婵炲懏鍔欓弻鐔煎礄閵堝棗顏�

static uint8_t second_count = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
  // void App_Init(void)
  // {
  //    // 初始化I2C
  //  // Inf_AT24C02_Init();
  // }
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_TIM6_Init();
  MX_TIM2_Init();
  MX_TIM7_Init();
  MX_ADC3_Init();
  MX_UART4_Init();
  MX_I2C2_Init();
  MX_TIM3_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
  
  timer_1s_cnt = 0;
  timer_1min_cnt = 0;
  timer_1hour_cnt = 0;

  
  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_RESET);

  
  Apphl8032_Init();
  
  
  HAL_ADC_Start_DMA(&hadc3, (uint32_t*)data1, 3);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
 
  Inf_4G_Init();

  Inf_EC800_MQTT_Init();

  Device_Init();
                 
 // Clear_EEPROM_All();
  Ozone_Init();

  printf("Initializing ozone control...\n");
  
  //Inf_AT24C02_Init();
  ozone_running = 0; 
  ozone_timer = 0;  
  Ozone_Control(0); 
  printf("Ozone initialized, waiting for remote control\n");

  //  printf("Starting timers...\n");
  HAL_TIM_Base_Start_IT(&htim6); 

  HAL_TIM_Base_Start_IT(&htim7); 
  HAL_TIM_Base_Start_IT(&htim2); 
  HAL_TIM_Base_Start_IT(&htim3); 
  Load_Temp_Limits_From_EEPROM();

  while (1)
  {
   // printf("ADC: %d, %d, %d\r\n", data1[0], data1[1], data1[2]);
    // Control_Fan2(1);
   // Print_Temperature(data1);  // 闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶缁€鍐煕濞戞瑦缍戦柛鏂诲劚闇夐柛蹇氬亹閻擃垰鈹戦悙鍙夊枠闁诡喕绮欓崺鈥斥炕濠曟房data[1]
    Inf_EC800_ReadData(); 
   // Direct_Mode_Control();
   
    Check_4G_Connection(); 

     if (need_upload)
      {
        Upload_Device_Data();
        need_upload = 0; // 婵犵數濮烽弫鍛婃叏閻戣棄鏋侀柟闂寸绾惧鏌ｉ幇顒佹儓缂佺姳鍗抽弻鐔兼⒒鐎靛壊妲紓浣哄Х婵灚绌辨繝鍥舵晬婵犻潧瀚ч崑鎾诲焵椤掑嫭鐓涢悗锝庝簽鏁堝Δ鐘靛仦閻楁洝褰佸銈嗗坊閸嬫捇鏌ｈ箛锝呮珝闁哄苯绉剁槐鎺懳熼懡銈呭汲闂備礁鐤囬～澶愬垂閸ф绠栭柍鍝勫€圭紞鍥ㄣ亜閹扳晛鐏╅悽顖ｅ灦濮婄粯鎷呴搹鐟扮闂佸憡姊瑰ú鐔煎极閸愵噮鏁傞柛顐ｇ箚閹芥洟姊洪悙钘夊姕闁告挻宀稿畷銉р偓锝庡枟閻撴洘绻涢幋婵嗚埞闁哄鍠栭弻鐔碱敊閻ｅ本鍣紓浣介哺閹稿骞忛崨顕呮Ч閹煎瓨绻嗛々顐︽⒒娴ｇ儤鍤€闁搞倖鐗犻獮蹇涙晸閿燂拷
      }
     switch(current_mode) {
       case MODE_DIRECT:
         Direct_Mode_Control();
         break;
       case MODE_IRRIGATION:
         Irrigation_Mode_Control();
       break;
       case MODE_FLUSH:
         Flush_Mode_Control();
         break;
       case MODE_SPECIAL:
         Special_Mode_Control();
         break;
       default:
         current_mode = MODE_DIRECT;
         break;
      }

   // printf("ADC闂備胶枪楗敻骞忛敓锟�: %d, %d, %d\r\n", data[0], data[1], data[2]);
     
   
     if(UART4_RX_STA & 0x8000)
     {
         Data_Processing();
    }

   

    // Energy_Management();
    // Update_All_Status();
    HAL_Delay(10);
    // Control_WaterPump(1);
    // HAL_Delay(5000);
    // Control_WaterPump(0);
    // HAL_Delay(5000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 144;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{

  if (htim->Instance == TIM6)
  {

    static uint16_t upload_counter = 0;
    upload_counter++;

    if (upload_counter >= 5000) // 5缂傚倸鍊搁崐鎼佸磹閹间礁纾瑰瀣捣閻棗銆掑锝呬壕闁芥ɑ绻堥弻鐔封枔閸喗鐏嶉梺鍝勬缁捇寮婚悢鍏煎€绘慨妤€妫欓悾鍫曟⒑閸濆嫬顏ラ柛搴㈠▕閸┾偓妞ゆ巻鍋撻柛妯荤矒瀹曟垿骞樼紒妯煎幈闂佸搫娲㈤崝灞炬櫠椤栨稓绠鹃柛娑卞亜閻忓弶鎱ㄦ繝鍛仩闁圭懓瀚版俊鎼佸Ψ閿旀儳缍掗梻鍌欒兌閹虫捇宕甸弽顓炵闁跨噦鎷�?(1ms * 2000 = 2s)
    {
      upload_counter = 0; // 婵犵數濮烽弫鍛婃叏閻戣棄鏋侀柟闂寸绾惧鏌ｉ幇顒佹儓缂佺姳鍗抽弻鐔兼⒒鐎靛壊妲紓浣哄Х婵灚绌辨繝鍥舵晬婵ǜ鍎辨禒娲偡濠婂懎顣奸悽顖涘笧婢规洟宕楅懖鈺冪槇婵犵數濮撮崐鎼侇敂閳哄倶浜滈幖娣灮濞插瓨鎱ㄦ繝鍐┿仢鐎殿噮鍓熸俊鐑芥晜閻ｅ苯绲介梻鍌欑閹芥粓宕抽妷鈺佸瀭濞村吋娼欓悡婵嬫煙閹规劦鍤欓梺鍗炴喘閺岋繝宕堕埡浣圭亖闂佸憡鐟ュΛ婵嗩潖閾忓湱纾兼俊顖濆吹椤︺儵姊虹粙鍖″伐婵犫偓闁秴鐒垫い鎺嶈兌閸熸煡鏌熼崙銈嗗?
      need_upload = 1;    // 闂傚倸鍊搁崐宄懊归崶褏鏆﹂柛顭戝亝閸欏繒鈧娲栧ú銊╂儗閸℃褰掓晲閸偅缍堝┑鐐叉噽婵挳婀侀梺缁樏Ο濠囧磿閹扮増鐓曢悗锝呭悁闁垶鏌＄仦鍓ф创闁糕晛瀚板畷姗€宕滆婵椽姊绘担鍝ワ紞缂侇噮鍨跺濠氬Ω閳轰胶鍘撮梺纭呮彧闂勫嫰宕戦幇鏉跨骇闁割偒鍋勬禍婊堟煕濮楀牏绡€婵﹨娅ｇ槐鎺懳熼崫鍕垫綋闂備焦瀵уú锔界椤忓嫷鍤曞┑鐘宠壘閻忔娊鏌熸０浣侯槮闁诡喗鍨垮娲川婵犱胶绻侀梺鍝ュУ椤ㄥ﹪鐛崘銊㈡瀻闁瑰濮烽敍婵囩箾鏉堝墽绉い銉︽尰缁嬪寮婚妷锔惧幍闂佸憡鍨崐鏍偓姘炬嫹
    }

    //  Check_Flow();
  }
  else if (htim->Instance == TIM2)
  {
    pump_timer++;
    fan_timer++;
   
    if (current_mode == MODE_SPECIAL)
    {
  
      special_phase_timer++;
      special_cycle_timer++;
    }
    // ???????
    //static uint32_t interrupt_count = 0;
        
    //?????????????????
    // if(time_just_modified)
    // {
    //     interrupt_count++;
    //     if(interrupt_count >= 60) {  // ????????
    //         pump_timer = 0;
    //         time_just_modified = 0;
    //         interrupt_count = 0;
    //        // printf("??????????????\n");
    //     }
    //     return;  // ????????????
    // }
   // ?????????
   
    // if(!timer_pause) 
    // {  // ??????????
    //   pump_timer++;
    //   fan_timer++;
    //   if (current_mode == MODE_SPECIAL)
    //   {
    //       special_phase_timer++;
    //       special_cycle_timer++;
    //   }
    // }

   
  
  }
  else if (htim->Instance == TIM7)
  {

    // 闂傚倸鍊搁崐鎼佸磹閻戣姤鍊块柨鏇楀亾妞ゎ亜鍟村畷绋课旈埀顒勬儗濡ゅ懎绠规繛锝庡墮婵″ジ鏌涚仦璇插闁哄矉绠戣灒濞撴凹鍨卞瓭闂備礁鎼鍡椢涢崘顔藉亗妞ゆ劧绠戦悙濠囨煏婵犲繘妾柛搴￠叄濮婃椽骞栭悙娴嬪亾閺囥垺鍋嬮柟鎹愵嚙閽冪喖鏌曢崼婵囧窛闁活厽鐟╅幃褰掑箒閹烘垵顬嗙紓渚囧櫍濞佳団€旈崘顔嘉︽い鏍ㄧ懅椤︼箓鏌熼搹顐㈠閾荤偞銇勯弽銊р棨濞存粍绮撻弻鐔兼倷椤掑鐣堕梺鍝ュ櫏閸ㄥ爼骞冨鈧畷鍗炩枎閹寸媴绱冲┑鐐舵彧缂嶁偓婵炲拑绲块弫顔尖槈閵忥紕鍘鹃悷婊呭鐢偤鎮鹃崫鍕ㄦ斀闁斥晛鍟亸锔筋殽閻愭潙绗掗摶鏍煃瑜滈崜鐔煎箖閳ユ枼妲堟俊顖氱箰缂嶅﹪寮幇鏉垮窛妞ゆ挆鍕垫闂傚倷绀侀幗婊堝磻閻愬樊娓婚柦妯侯樈濞兼牗绻涘顔荤盎濞磋偐濞€閺屾盯寮撮妸銉ヮ潽闂佽鍠楅悷鈺侇潖閾忓厜鍋撻惂鎼佹婵犮垺蓱閹便劑宕掑┃鎯т壕婵炲牆鐏濋弸銈囩磼椤曞懎鐏ｉ柟骞垮灩閳规垹鈧綆浜為ˇ浼存⒑闂堟胆褰掑磿閻楀牏鐝堕柡鍥ュ灪閳锋垿鏌ゆ慨鎰偓鏇㈠几閺傛鐔嗙憸搴ㄣ€冮崨瀛樻櫇闁靛繆鈧剚鍤ら柣搴㈢⊕椤洭宕㈤崡鐑嗘富闁靛牆楠搁獮妤呮偨椤栨侗娈橀柍褜鍓氶悢顒勫箯閿燂拷
    // Inf_EC800_PublishDeviceStatus(); // 闂傚倸鍊搁崐鎼佸磹妞嬪海鐭嗗〒姘ｅ亾妤犵偛顦甸弫鎾绘偐閼碱剦妲烽梻浣告惈濞层劍鎱ㄩ悜鑺ュ剹闁规儳澧庡Λ顖炴煙椤栧棗鑻崜顓熺箾鐎涙鐭婄紓宥咃躬瀵鈽夊Ο鍏兼畷闂侀€炲苯澧寸€规洘鍨块幃娆撴倻濡　鍋撻崸妤佸仯闁搞儺浜跺璺ㄦ喐閻楀牆绗氶柡鍛叀閺屾稑鈽夐崡鐐寸亶闂佺ǹ瀛╅幐鍓ф閹烘嚦鏃€鎷呴崫銉ょ礉婵犳鍠栭敃銊モ枍閿濆绠查柛鏇ㄥ灠鎯熼梺闈涱檧婵″洩銇愬鑸碘拻濞达絿鍎ら崵鈧梺鎼炲€栭悧鐘荤嵁韫囨稒鏅搁柨鐕傛嫹?
    if (++timer_1s_cnt >= 60)
    {
      timer_1s_cnt = 0;
      if (++timer_1min_cnt >= 60)
      {
        timer_1min_cnt = 0;
        timer_1hour_cnt++;
       
      }
      Dredge_Control(); 
    }
  }
  else if (htim->Instance == TIM3)
  {
    second_count++;
    if(second_count >= 60)  // 缂備線纭搁崹鐢割敇閹间礁绀嗛柨鐕傛嫹60缂備礁顧€閹凤拷
    {
        // 闁哄鏅滈悷鈺呭闯閻戣棄缁╅柟顖嗗啯鏋鹃梺鍝勵槶閸庢煡鎯侀敍鍕煋闁圭儤鎸搁埛鏃堟偠濞戞鐏辨繛鍫熷灦缁傛帡鏁愰崶鈺佺仯
        // ... 闂佸憡顭囬崰鎰耿娴ｅ湱顩烽柨婵嗘川閸拷 ...
        Ozone_Control_Update();

        phosphorus_timer++;  // 闂勩倗锛楃拋鈩冩閸ｃ劌濮�1
        Phosphorus_Control_Update();  // 閺囧瓨鏌婇梽銈囷紬閹貉冨煑閻樿埖鈧拷
        // 闂備焦褰冪粔鍫曟偪閸℃瑦濯奸柍鈺佸暞濞堝爼鏌涢敐蹇斿
        second_count = 0;
       
    }
    
  }
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
