xuxinyi 2 meses atrás
pai
commit
fea30518aa
4 arquivos alterados com 47 adições e 39 exclusões
  1. 0 3
      Core/Inc/E52.h
  2. 1 0
      Core/Inc/RTC_SLEEP.h
  3. 1 0
      Core/Src/RTC_SLEEP.c
  4. 45 36
      Core/Src/main.c

+ 0 - 3
Core/Inc/E52.h

@@ -118,9 +118,6 @@ typedef struct __attribute__((packed)){
     //lora发生成功标志
     uint8_t loraSendSuccessFlag;
 
-    //SOS触发标志
-    uint8_t SOS_Flag;
-
     //定时器相关
     uint8_t  timerEnable;           //定时器使能
     uint16_t timeCount;             //定时器计数

+ 1 - 0
Core/Inc/RTC_SLEEP.h

@@ -35,6 +35,7 @@ extern RTC_TimeTypeDef SOS_key_struct;
 extern RTC_TimeTypeDef JUGE_key_struct;
 extern RTC_TimeTypeDef Online_struct;
 extern RTC_TimeTypeDef SOS_struct;
+extern RTC_TimeTypeDef IDLE_struct;
 
 // void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime);
 

+ 1 - 0
Core/Src/RTC_SLEEP.c

@@ -7,6 +7,7 @@ RTC_TimeTypeDef SOS_key_struct =  {0};
 RTC_TimeTypeDef JUGE_key_struct = {0};
 RTC_TimeTypeDef Online_struct = {0};
 RTC_TimeTypeDef SOS_struct = {0};
+RTC_TimeTypeDef IDLE_struct = {0};
 // RTC_key_struct SOS_key_struct = {
 //     .key_Count = 0,
 //     .key_Duration = 0,

+ 45 - 36
Core/Src/main.c

@@ -104,7 +104,7 @@ uint16_t randomFun() {
 
 void Device_Info_Init_Fun() {
   deviceInfo.isOnline = FALSE;
-  deviceInfo.SOS_Flag = FALSE;
+  deviceInfo.isSOS = FALSE;
   deviceInfo.broadcast_type = BROADCAST_ALL;
   deviceInfo.loraDeviceAddress_H = 0xFE; //默认地址高位
   deviceInfo.loraDeviceAddress_L = 0xEF; //默认地址低位
@@ -259,26 +259,6 @@ int main(void)
   powerOn();
   E52_CtlPowerOn_Fun();
 
-  // deviceInfo.loraDeviceAddress_H = 0xFE;
-  // deviceInfo.loraDeviceAddress_L = 0xE1;
-  // Flash_Write_LoraAddr(deviceInfo.loraDeviceAddress_H, deviceInfo.loraDeviceAddress_L);
-  // uint8_t addrH, addrL;
-  // while (TRUE) {
-  //   Flash_Read_LoraAddr(&addrH, &addrL);
-  //   printf("LoraAddr: %02X%02X\r\n", addrH, addrL);
-  //   HAL_Delay(1000);
-  // }
-  // 在需要设置RTC时间的地方直接调用
-  // 参数依次为:小时、分钟、秒、星期、月份、日期、年份(均为BCD格式)
-  // RTC_SetDateTime(&hrtc,
-  //                0x12,  // 12时
-  //                0x30,  // 30分
-  //                0x00,  // 00秒
-  //                RTC_WEEKDAY_WEDNESDAY,  // 星期三
-  //                RTC_MONTH_OCTOBER,      // 10月
-  //                0x17,  // 17日
-  //                0x25); // 25年(2025年)
-
   // 启动 USART2 DMA 接收
   HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
   __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
@@ -364,6 +344,7 @@ int main(void)
     }
 
     if (deviceInfo.isSOS) {
+      deviceInfo.isOnline = TRUE;
       RTC_TimeTypeDef now = {0};
       HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
 
@@ -377,6 +358,28 @@ int main(void)
       }
     }
 
+    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) {
+        deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
+        // printf("111111\r\n");
+        IDLE_struct = now;  // 记录上报时间
+      }
+    }
+
+
 
     // 解析后台命令
     if (GET_CMD( deviceInfo.commandFromCloud) == IDLE && GET_STEP(deviceInfo.commandFromCloud) == STEP_VERIFY) {
@@ -384,8 +387,7 @@ int main(void)
     }
 
     // 03:请求蓝牙数据
-    if ((GET_CMD( deviceInfo.commandFromCloud)  == REQUEST_BLUETOOTH_DATA && deviceInfo.isOnline) || deviceInfo.isSOS) {
-
+    if (((GET_CMD(deviceInfo.commandFromCloud) == REQUEST_BLUETOOTH_DATA && deviceInfo.isOnline ) || deviceInfo.isSOS))  {
       switch (GET_STEP(deviceInfo.commandFromCloud)) {
         case REQUEST_BLUETOOTH_DATA_SCAN:
           Timer_Managment_Fun(&htim4, TIMER_OP_START);
@@ -419,23 +421,30 @@ int main(void)
           deviceInfo.forwardBLAndLoraDataDuration = 0;
           i = 0;
           deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
-          // printf("发送结束\r\n");
           break;
 
         default:
           // 处理未知步骤
-          printf("收到未知步骤,忽略处理\r\n");
-          deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_VERIFY);
-          rx_buf_uart2[0] = 0x55;
-          rx_buf_uart2[1] = 0xBB;
-          rx_buf_uart2[2] = 0x03;
-          rx_buf_uart2[3] = deviceInfo.loraDeviceAddress_H;
-          rx_buf_uart2[4] = deviceInfo.loraDeviceAddress_L;
-          rx_buf_uart2[5] = 0x03;
-          rx_buf_uart2[6] = 0xE8;
-          rx_buf_uart2[7] = 0x00;
-          rx_buf_uart2[8] = 0xFF;
-          rx_buf_uart2[9] = 0xEE;
+          if (deviceInfo.isSOS) {
+            // HAL_Delay(1000);
+            deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_VERIFY);
+            rx_buf_uart2[0] = 0x55;
+            rx_buf_uart2[1] = 0xBB;
+            rx_buf_uart2[2] = 0x03;
+            rx_buf_uart2[3] = deviceInfo.loraDeviceAddress_H;
+            rx_buf_uart2[4] = deviceInfo.loraDeviceAddress_L;
+            rx_buf_uart2[5] = 0x03;
+            rx_buf_uart2[6] = 0xE8;
+            rx_buf_uart2[7] = 0x00;
+            rx_buf_uart2[8] = 0xFF;
+            rx_buf_uart2[9] = 0xEE;
+          }else {
+            receiveBlDataCount = 0;
+            deviceInfo.timeCount = 0;
+            deviceInfo.forwardBLAndLoraDataDuration = 0;
+            i = 0;
+            deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
+          }
           break;
       }
     }