Forráskód Böngészése

封装RTC定时方法

xuxinyi 1 hónapja
szülő
commit
fa2dd5cd41
1 módosított fájl, 32 hozzáadás és 40 törlés
  1. 32 40
      Core/Src/main.c

+ 32 - 40
Core/Src/main.c

@@ -208,6 +208,29 @@ void Flash_Read_LoraAddr(uint8_t *addrH, uint8_t *addrL)
   *addrH = (halfword >> 8) & 0xFF;
   *addrL = halfword & 0xFF;
 }
+
+// 工具函数:计算时间差(秒)
+int GetElapsedSeconds(RTC_TimeTypeDef *start, RTC_TimeTypeDef *end) {
+  int s1 = start->Hours * 3600 + start->Minutes * 60 + start->Seconds;
+  int s2 = end->Hours * 3600 + end->Minutes * 60 + end->Seconds;
+  int diff = s2 - s1;
+  if (diff < 0) diff += 24 * 3600;  // 跨天修正
+  return diff;
+}
+
+// 工具函数:判断是否超过指定秒数
+int IsTimeElapsed(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *last, uint32_t interval) {
+  RTC_TimeTypeDef now = {0};
+  HAL_RTC_GetTime(hrtc, &now, RTC_FORMAT_BIN);
+
+  int diff = GetElapsedSeconds(last, &now);
+  if (diff >= interval) {
+    *last = now;  // 更新上次时间
+    return 1;     // 超时
+  }
+  return 0;         // 未超时
+}
+
 /* USER CODE END 0 */
 
 /**
@@ -311,7 +334,6 @@ int main(void)
   printf("Device is ready\r\n");
   printf("设备地址:0x%02X%02X\n", deviceInfo.loraDeviceAddress_H, deviceInfo.loraDeviceAddress_L);
   uint8_t i = 0;
-  // uint8_t ledShanshuo = 0;
   while (1)
   {
     /* USER CODE END WHILE */
@@ -320,56 +342,26 @@ int main(void)
 
     // 检查是否接收到"心跳"命令,且设备在线
     printf("设备地址:0x%02X\n", deviceInfo.commandFromCloud);
+    // 心跳:不在线时,每 10 秒发一次
     if (!deviceInfo.isOnline) {
-      RTC_TimeTypeDef now = {0};
-      HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
-
-      int pressedSeconds = (now.Hours * 3600 + now.Minutes * 60 + now.Seconds)
-                         - (Online_struct.Hours * 3600 + Online_struct.Minutes * 60 + Online_struct.Seconds);
-      if (pressedSeconds < 0) pressedSeconds += 24 * 3600;
-
-      // printf("发送上报时间 按下持续时间: %d 秒\r\n", pressedSeconds);
-
-      if (pressedSeconds >= 10) {
+      if (IsTimeElapsed(&hrtc, &Online_struct, 10)) {
         E52_Heartbeat_Fun();
-        // printf("111111\r\n");
-        Online_struct = now;  // 记录上报时间
       }
     }
 
+    // SOS:如果超过 20 秒还没释放,自动关闭
     if (deviceInfo.isSOS) {
-      deviceInfo.isOnline = TRUE;
-      RTC_TimeTypeDef now = {0};
-      HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
-
-      int pressedSeconds = (now.Hours * 3600 + now.Minutes * 60 + now.Seconds)
-                         - (SOS_struct.Hours * 3600 + SOS_struct.Minutes * 60 + SOS_struct.Seconds);
-      if (pressedSeconds < 0) pressedSeconds += 24 * 3600;
-      printf("SOS按下时间为:%d\r\n",pressedSeconds);
-
-      if (pressedSeconds >= 20) { //如果出现异常情况,导致SOS一直处于被按下情况,关闭SOS。
+      if (IsTimeElapsed(&hrtc, &SOS_struct, 20)) {
         deviceInfo.isSOS = FALSE;
       }
     }
 
-    if (GET_CMD( deviceInfo.commandFromCloud) == IDLE || GET_STEP(deviceInfo.commandFromCloud) == STEP_INIT) {
-      // printf("为idle状态的时间为\r\n");
-      RTC_TimeTypeDef now = {0};
-      HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
-      IDLE_struct = now;  // 记录上报时间
-    }else {
-      RTC_TimeTypeDef now = {0};
-      HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
-
-      int pressedSeconds = (now.Hours * 3600 + now.Minutes * 60 + now.Seconds)
-                         - (IDLE_struct.Hours * 3600 + IDLE_struct.Minutes * 60 + IDLE_struct.Seconds);
-      if (pressedSeconds < 0) pressedSeconds += 24 * 3600;
-      // printf("不为idle状态的时间为:%d\r\n",pressedSeconds);
-
-      if (pressedSeconds >= 20) {
+    // IDLE 检测
+    if (GET_CMD(deviceInfo.commandFromCloud) == IDLE || GET_STEP(deviceInfo.commandFromCloud) == STEP_INIT) {
+      HAL_RTC_GetTime(&hrtc, &IDLE_struct, RTC_FORMAT_BIN);  // 直接更新
+    } else {
+      if (IsTimeElapsed(&hrtc, &IDLE_struct, 20)) {
         deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
-        // printf("111111\r\n");
-        IDLE_struct = now;  // 记录上报时间
       }
     }