Pārlūkot izejas kodu

设备上线完成。蓝牙还有点问题

xuxinyi 2 mēneši atpakaļ
vecāks
revīzija
f893ed1b48
6 mainītis faili ar 32 papildinājumiem un 109 dzēšanām
  1. 1 0
      Core/Inc/E52.h
  2. 1 0
      Core/Inc/RTC_SLEEP.h
  3. 6 2
      Core/Src/E52.c
  4. 1 0
      Core/Src/RTC_SLEEP.c
  5. 21 80
      Core/Src/main.c
  6. 2 27
      Core/Src/stm32f1xx_it.c

+ 1 - 0
Core/Inc/E52.h

@@ -106,6 +106,7 @@ typedef struct __attribute__((packed)){
     uint8_t target_addr_H;          // 目标地址高字节         默认FF
     uint8_t target_addr_L;          // 目标地址低字节         默认FF
     uint8_t powerValue;             //电量                  默认0
+    uint8_t isSOS;
     // uint8_t receivingNewDataFlag;       //接收lora新数据
 
     //使能蓝牙收发数据

+ 1 - 0
Core/Inc/RTC_SLEEP.h

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

+ 6 - 2
Core/Src/E52.c

@@ -55,7 +55,7 @@ void E104_BT5005A_DST_ADDR_Fun(void) {
  * 帧头:55 AA
  */
 void E52_Heartbeat_Fun() {
-    // printf("这是心跳包\r\n");
+    printf("这是心跳包\r\n");
     Heartbeat_structure heartbeat_structure;
 
     heartbeat_structure.frame_header_H = 0x55;
@@ -80,7 +80,11 @@ void E52_Send_Bl_Data_Fun(uint8_t i, uint8_t finishData) {
     SendBlData_structure sendBlData_structure;
     sendBlData_structure.frame_header_H = 0x55;
     sendBlData_structure.frame_header_L = 0xAA;
-    sendBlData_structure.function_code = 0x02;
+    if (deviceInfo.isSOS) {
+        sendBlData_structure.function_code = 0x02;
+    }else {
+        sendBlData_structure.function_code = 0x04;
+    }
     sendBlData_structure.badge_addr_high = deviceInfo.loraDeviceAddress_H;
     sendBlData_structure.badge_addr_low = deviceInfo.loraDeviceAddress_L;
     memcpy(sendBlData_structure.bl_data, totalData[i], 8);

+ 1 - 0
Core/Src/RTC_SLEEP.c

@@ -5,6 +5,7 @@
 #include "../Inc/RTC_SLEEP.h"
 RTC_TimeTypeDef SOS_key_struct =  {0};
 RTC_TimeTypeDef JUGE_key_struct = {0};
+RTC_TimeTypeDef Online_struct = {0};
 // RTC_key_struct SOS_key_struct = {
 //     .key_Count = 0,
 //     .key_Duration = 0,

+ 21 - 80
Core/Src/main.c

@@ -344,92 +344,33 @@ int main(void)
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-    // printf("deviceInfo.commandFromCloud:%02x\r\n",deviceInfo.commandFromCloud);
-    // GPIO_PinState sosState = HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin);
-    // GPIO_PinState powerState = HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin);
-    //
-    // if (powerState == GPIO_PIN_RESET) {
-    //   // powerOff();
-    //   deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
-    //   printf("收到未知步骤,忽略处理\r\n");
-    //   HAL_Delay(10*60*1000);
-    // }
-
-    // if (ledShanshuo  != now.seconds) {
-    //   ledShanshuo = now.seconds;
-    // }
-
-    // if (!sosState) {
-    //   // E52_Heartbeat_Fun();
-    //   printf("SOS中\r\n");
-    //
-    //   deviceInfo.BlDataFlag = BL_START;
-    //   printf("开启蓝牙扫描\r\n");
-    //   HAL_Delay(1000);
-    //   deviceInfo.BlDataFlag = BL_STOP;
-    //   int k = 0;
-    //   while (receiveBlDataCount > k) {
-    //
-    //     printf("发送第%d个蓝牙数据给Lora\r\n", k);
-    //     if (deviceInfo.loraSendSuccessFlag == 0) {
-    //       E52_Send_SOS_Fun(k,0x00);
-    //     }else {
-    //       continue;
-    //     }
-    //     memset(totalData[k], 0, sizeof(totalData[k]));
-    //     k++;
-    //   }
-    //   HAL_Delay(1000);
-    //   E52_Send_Bl_Data_Fun(k,0x01);
-    //   printf("停止蓝牙扫描,开始发送蓝牙数据给Lora\r\n");
-    //
-    //   HAL_Delay(2000);
-    //   receiveBlDataCount = 0;
-    //   deviceInfo.timeCount = 0;
-    //   deviceInfo.forwardBLAndLoraDataDuration = 0;
-    //   k = 0;
-    //   printf("发送结束\r\n");
-    //   HAL_Delay(2000);
-    //
-    //   continue;
-    // }
-
-    // if (SOS_key_struct.sosCheckActive) {
-    //   RTC_DateTimeTypeDef now;
-    //   RTC_GetDateTime(&hrtc, &now);
-    //
-    //   uint32_t diffSec = RTC_CalcDiffSeconds(&firstPressTime, &now);
-    //   if (diffSec > 60) {
-    //     // 超过5秒,停止检测
-    //     if (SOS_key_struct.SOS_key_Count >= 5) {
-    //       printf("SOS TRIGGERED!!!\r\n");
-    //       // TODO: 触发SOS逻辑
-    //     }
-    //     // 重置状态
-    //     SOS_key_struct.sosCheckActive = FALSE;
-    //     SOS_key_struct.SOS_key_Count = 0;
-    //   }else {
-    //
-    //   }
-    // }
 
     // 检查是否接收到"心跳"命令,且设备在线
-    // if (!deviceInfo.isOnline){
-    //   printf("The device is not online.\r\n");
-    //   deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
-    //   if (!deviceInfo.loraSendSuccessFlag) {
-    //     E52_Heartbeat_Fun();
-    //   }
-    //   HAL_Delay(3000);
-    // }
+    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) {
+        E52_Heartbeat_Fun();
+        // printf("111111\r\n");
+        Online_struct = now;  // 记录上报时间
+      }
+    }
+
 
     // 解析后台命令
-    // if (GET_CMD( deviceInfo.commandFromCloud) == IDLE && GET_STEP(deviceInfo.commandFromCloud) == STEP_VERIFY) {
-    //   E52_Analyze_Data();
-    // }
+    if (GET_CMD( deviceInfo.commandFromCloud) == IDLE && GET_STEP(deviceInfo.commandFromCloud) == STEP_VERIFY) {
+      E52_Analyze_Data();
+    }
 
     // 03:请求蓝牙数据
-    if (GET_CMD( deviceInfo.commandFromCloud)  == REQUEST_BLUETOOTH_DATA && deviceInfo.isOnline) {
+    if ((GET_CMD( deviceInfo.commandFromCloud)  == REQUEST_BLUETOOTH_DATA && deviceInfo.isOnline) || deviceInfo.isSOS) {
 
       switch (GET_STEP(deviceInfo.commandFromCloud)) {
         case REQUEST_BLUETOOTH_DATA_SCAN:

+ 2 - 27
Core/Src/stm32f1xx_it.c

@@ -421,22 +421,11 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
     if (HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin) == GPIO_PIN_SET) {
       // 松开(上升沿)
       printf("SOS_KEY 松开\r\n");
-
-      // 计算按下时间
-      int pressedSeconds = (currentTime.Hours * 3600 + currentTime.Minutes * 60 + currentTime.Seconds)
-                         - (SOS_key_struct.Hours * 3600 + SOS_key_struct.Minutes * 60 + SOS_key_struct.Seconds);
-
-      // 防止跨天导致负值
-      if (pressedSeconds < 0) {
-        pressedSeconds += 24 * 3600;
-      }
-
-      printf("SOS_KEY 按下持续时间: %d 秒\r\n", pressedSeconds);
-
+      deviceInfo.isSOS = FALSE;
     } else {
       // 按下(下降沿)
       printf("SOS_KEY 按下\r\n");
-      SOS_key_struct = currentTime;
+      deviceInfo.isSOS = TRUE;
     }
   }
   // ===== JUGE_KEY =====
@@ -444,23 +433,9 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
     if (HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin) == GPIO_PIN_SET) {
       // 松开(上升沿)
       printf("JUGE_KEY 松开\r\n");
-
-      // int pressedSeconds = (currentTime.Hours * 3600 + currentTime.Minutes * 60 + currentTime.Seconds)
-      //                    - (JUGE_key_struct.Hours * 3600 + JUGE_key_struct.Minutes * 60 + JUGE_key_struct.Seconds);
-      // if (pressedSeconds < 0) {
-      //   pressedSeconds += 24 * 3600;
-      // }
-      //
-      // printf("JUGE_KEY 按下持续时间: %d 秒\r\n", pressedSeconds);
-      // if (pressedSeconds > 1) {
-      //   // HAL_GPIO_WritePin(PowerLED_GPIO_Port, PowerLED_Pin, GPIO_PIN_RESET);
-      //   HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
-      // }
-
     } else {
       // 按下(下降沿)
       printf("JUGE_KEY 按下\r\n");
-      // JUGE_key_struct = currentTime;
       HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
     }
   }