/* 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)
  // {
  //    // 鍒濆鍖朓2C
  //  // 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();
  Phosphorus_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);  // 闂傚倷绀侀幉锟犲垂閸忓吋鍙忛柕鍫濐槸濮规煡鏌ｉ弮鍌氬付缂佲偓閸愵喗鐓曟繛鎴炵懄缂嶆垿鏌涢弬璇插姎闂囧鏌涜箛姘汗闁绘搩鍨伴埞鎴︽倷閸欏鏋犻梺璇″枙缁瑩宕洪垾鏂ョ倳婵犳洘鎴縟ata[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 */
