浏览代码

循环队列读取蓝牙数据。取消扫描蓝牙时间。

xuxinyi 1 月之前
父节点
当前提交
3a863ebd93
共有 6 个文件被更改,包括 53 次插入103 次删除
  1. 7 5
      Core/Inc/E52.h
  2. 4 4
      Core/Inc/ring_buffer.h
  3. 4 4
      Core/Src/E52.c
  4. 19 82
      Core/Src/main.c
  5. 1 1
      Core/Src/ring_buffer.c
  6. 18 7
      Core/Src/stm32f1xx_it.c

+ 7 - 5
Core/Inc/E52.h

@@ -10,7 +10,7 @@
 #include "string.h"
 #include "stdlib.h"
 #include "RTC_SLEEP.h"
-
+#include "ring_buffer.h"
 // 组合命令和步骤的宏
 #define COMBINE_CMD_STEP(cmd, step) ((cmd) | (step))
 
@@ -98,7 +98,7 @@ typedef enum {
 }BROADCAST;
 
 // 设备信息结构体
-typedef struct __attribute__((packed)){
+typedef struct {
     uint8_t isOnline;            // 设备是否就绪
     uint8_t broadcast_type;         // 广播类型             01:单波 02:广播 03:多播
     uint8_t loraDeviceAddress_H;    //设备地址高位           默认0
@@ -110,7 +110,7 @@ typedef struct __attribute__((packed)){
     // uint8_t receivingNewDataFlag;       //接收lora新数据
 
     //使能蓝牙收发数据
-    uint8_t BlDataFlag;         //发送蓝牙数据
+    uint8_t BlDataFlag;         //BL_START:开始接收新蓝牙数据,BL_STOP.停止接收新蓝牙数据
 
     //后台的远程命令
     uint8_t commandFromCloud; //从云端接收到的命令     1.请求蓝牙数据,2.请求lora数据,3.睡眠 4,重新发送点卯数据
@@ -122,6 +122,8 @@ typedef struct __attribute__((packed)){
     uint8_t  timerEnable;           //定时器使能
     uint16_t timeCount;             //定时器计数
     uint16_t forwardBLAndLoraDataDuration;   //转发蓝牙数据持续时间高位ms
+    RingBuffer queueBLData;
+    // RingBuffer queueSenderData;
 
 } Device_structure;
 
@@ -158,7 +160,7 @@ typedef struct __attribute__((packed)){
     uint8_t frame_tail_H;    // 帧尾高字节
     uint8_t frame_tail_L;    // 帧尾低字节
 } SendBlData_structure;
-void E52_Send_Bl_Data_Fun(uint8_t,uint8_t);
+void E52_Send_Bl_Data_Fun(uint8_t *sendData,uint8_t);
 
 
 // 发送SOS结构体
@@ -176,7 +178,7 @@ typedef struct __attribute__((packed)){
     uint8_t frame_tail_H;    // 帧尾高字节
     uint8_t frame_tail_L;    // 帧尾低字节
 } SendSOS_structure;
-void E52_Send_SOS_Fun(uint8_t i,uint8_t finishData);
+void E52_Send_SOS_Fun(uint8_t *sendData,uint8_t finishData);
 
 
 

+ 4 - 4
Core/Inc/ring_buffer.h

@@ -9,7 +9,7 @@
 #include <stdint.h>
 #include "string.h"
 
-#define QUEUE_SIZE 10   // 队列大小,可以根据需求调整
+#define QUEUE_SIZE 30   // 队列大小,可以根据需求调整
 #define BL_DATA_SIZE 8   //存储一组蓝牙数据
 
 typedef struct {
@@ -22,13 +22,13 @@ typedef struct {
 void RingBuffer_Init(RingBuffer *q);
 
 // 判空
-int  RingBuffer_IsEmpty(RingBuffer *q);
+int  RingBuffer_IsEmpty(RingBuffer *q);   // 返回1表示队列为空,返回0表示队列不为空
 
 // 判满
-int  RingBuffer_IsFull(RingBuffer *q);
+int  RingBuffer_IsFull(RingBuffer *q);    // 返回1表示队列已满,返回0表示队列未满
 
 // 入队
-int  RingBuffer_Enqueue(RingBuffer *q, uint8_t *data);
+int  RingBuffer_Enqueue(RingBuffer *q, const uint8_t *data);
 
 // 出队
 int  RingBuffer_Dequeue(RingBuffer *q, uint8_t *data);

+ 4 - 4
Core/Src/E52.c

@@ -75,7 +75,7 @@ void E52_Heartbeat_Fun() {
     // TODO: command:01
 }
 
-void E52_Send_Bl_Data_Fun(uint8_t i, uint8_t finishData) {
+void E52_Send_Bl_Data_Fun( uint8_t *sendData,  uint8_t finishData) {
     printf("这是发送蓝牙数据\r\n");
     SendBlData_structure sendBlData_structure;
     sendBlData_structure.frame_header_H = 0x55;
@@ -87,7 +87,7 @@ void E52_Send_Bl_Data_Fun(uint8_t i, uint8_t finishData) {
     }
     sendBlData_structure.badge_addr_high = deviceInfo.loraDeviceAddress_H;
     sendBlData_structure.badge_addr_low = deviceInfo.loraDeviceAddress_L;
-    memcpy(sendBlData_structure.bl_data, totalData[i], 8);
+    memcpy(sendBlData_structure.bl_data, sendData, 8);
     if (sendBlData_structure.bl_data[0] == 0x00) { //如果蓝牙数据未空,信号强度改为FF。
         sendBlData_structure.bl_data[7] = 0xFF;
     }
@@ -102,7 +102,7 @@ void E52_Send_Bl_Data_Fun(uint8_t i, uint8_t finishData) {
     // TODO: command:02
 }
 
-void E52_Send_SOS_Fun(uint8_t i,uint8_t finishData) {
+void E52_Send_SOS_Fun( uint8_t *sendData,uint8_t finishData) {
     printf("这是发送SOS数据\r\n");
     SendSOS_structure SendSOS_structure;
     SendSOS_structure.frame_header_H = 0x55;
@@ -111,7 +111,7 @@ void E52_Send_SOS_Fun(uint8_t i,uint8_t finishData) {
     SendSOS_structure.badge_addr_high = deviceInfo.loraDeviceAddress_H;
     SendSOS_structure.badge_addr_low = deviceInfo.loraDeviceAddress_L;
     // SendSOS_structure.SOS_data = 0x01; //暂定01
-    memcpy(SendSOS_structure.bl_data, totalData[i], 8);
+    memcpy(SendSOS_structure.bl_data, sendData, 8);
     SendSOS_structure.finish_send = finishData;
     SendSOS_structure.frame_tail_H = 0xFF;
     SendSOS_structure.frame_tail_L = 0xEE;

+ 19 - 82
Core/Src/main.c

@@ -63,14 +63,7 @@ uint8_t rx_buf_uart2_DMA[25] = {0};
 uint8_t totalData[50][25] = {0};
 uint8_t uart2_rx_byte[10] = {0};
 uint8_t loraSendNextDataFlag = 0;
-uint8_t baseRandomTimer = 3*60;
-uint8_t RandomTimer= 3*60;
 
-RingBuffer queue1;
-RingBuffer queue2;
-
-// uint8_t loraDataErrorCount = 0;
-uint8_t workMode = 0;  // 0: 接收蓝牙数据 1: 使用lora发生数据 2:等待间隔时间
 uint8_t receiveBlDataCount = 0;
 
 /* USER CODE END PV */
@@ -99,14 +92,6 @@ int __io_putchar(int ch)
   return ch;
 }
 
-void sentLoraData(uint8_t  sendDataNum) {
-  HAL_UART_Transmit(&huart2, totalData[sendDataNum], 8, HAL_MAX_DELAY);
-}
-
-uint16_t randomFun() {
-  return rand() % RandomTimer + baseRandomTimer; //随机时间1分钟到3分钟之间随机数
-}
-
 void Device_Info_Init_Fun() {
   deviceInfo.isOnline = FALSE;
   deviceInfo.isSOS = FALSE;
@@ -120,6 +105,7 @@ void Device_Info_Init_Fun() {
   // deviceInfo.newLoraDataFlag = 0;
   // deviceInfo.isReady = 0;
   deviceInfo.loraSendSuccessFlag = 0;
+  deviceInfo.BlDataFlag = BL_START;
   deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);  //初始化
   //TODO: 大循环,获取设备地址信息,否则重新发送。发送上线信息,等待反馈,否则重新发送上线信息。一切准备就绪后,开启isready为1,进入工作模式。
   // while (1){}
@@ -263,50 +249,8 @@ int main(void)
 
   powerOn();
   E52_CtlPowerOn_Fun();
-  RingBuffer q;
-  RingBuffer_Init(&q);
-
-  uint8_t data_in[BL_DATA_SIZE];
-  uint8_t data_out[BL_DATA_SIZE];
-
-  printf("=== 环形队列循环入队/出队测试开始 ===\n");
-
-  // 循环 20 次,模拟不断写入和读取
-  for (int n = 0; n < 20; n++) {
-    // 准备一组数据(内容累加)
-    for (int i = 0; i < BL_DATA_SIZE; i++) {
-      data_in[i] = (n << 4) + (i + 1);  // 每轮累加 0x10
-    }
-
-
-    // 入队
-    if (RingBuffer_Enqueue(&q, data_in) == 0) {
-      printf("[%02d] 入队成功: ", n);
-      for (int i = 0; i < BL_DATA_SIZE; i++) {
-        printf("%d ", data_in[i]);
-      }
-      printf("\n");
-    } else {
-      printf("[%02d] 入队失败: 队列已满\n", n);
-    }
-
-    // 出队
-    if (RingBuffer_Dequeue(&q, data_out) == 0) {
-      printf("[%02d] 出队成功: ", n);
-      for (int i = 0; i < BL_DATA_SIZE; i++) {
-        printf("%d ", data_out[i]);
-      }
-      printf("\n");
-    } else {
-      printf("[%02d] 出队失败: 队列为空\n", n);
-    }
-    printf("=== 下一轮测试 ===\n");
-  }
-
-  printf("=== 测试结束 ===\n");
-
-  while (TRUE);
-
+  //初始化环形缓冲区
+  RingBuffer_Init(&deviceInfo.queueBLData);
 
   // 启动 USART2 DMA 接收
   HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
@@ -375,6 +319,7 @@ int main(void)
     /* USER CODE BEGIN 3 */
 
     // 检查是否接收到"心跳"命令,且设备在线
+    printf("设备地址:0x%02X\n", deviceInfo.commandFromCloud);
     if (!deviceInfo.isOnline) {
       RTC_TimeTypeDef now = {0};
       HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
@@ -439,34 +384,26 @@ int main(void)
     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);
-          deviceInfo.BlDataFlag = BL_START;
-          printf("开启蓝牙扫描\r\n");
-          break;    //扫描蓝牙阶段
-
-        case REQUEST_BLUETOOTH_DATA_SEND:
-          Timer_Managment_Fun(&htim4, TIMER_OP_STOP);
           deviceInfo.BlDataFlag = BL_STOP;
-          i = 0;
-          while (receiveBlDataCount > i) {
+          deviceInfo.commandFromCloud = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, REQUEST_BLUETOOTH_DATA_SEND);
+          break;
 
-            // printf("发送第%d个蓝牙数据给Lora\r\n", i);
-            if (deviceInfo.loraSendSuccessFlag == 0) {
-              E52_Send_Bl_Data_Fun(i,0x00);
-            }else {
-              continue;
-            }
-            memset(totalData[i], 0, sizeof(totalData[i]));
-            i++;
+        case REQUEST_BLUETOOTH_DATA_SEND:
+          uint8_t sentData_Temp[25] = {0};
+          if (RingBuffer_Dequeue(&deviceInfo.queueBLData, sentData_Temp) == 0) {
+            //不为空的情况下
+            E52_Send_Bl_Data_Fun(sentData_Temp,0x00);
+          } else {
+            //为空的情况下
+            // printf("发送最后一位,开始发送蓝牙数据给Lora\r\n");
+            HAL_Delay(1000);
+            E52_Send_Bl_Data_Fun(sentData_Temp,0x01);
+            deviceInfo.BlDataFlag = BL_START;
+            deviceInfo.commandFromCloud = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, STEP_COMPLETE);
+            printf("发送完成\r\n");
           }
-          // printf("发送最后一位,开始发送蓝牙数据给Lora\r\n");
-          E52_Send_Bl_Data_Fun(i,0x01);
-          deviceInfo.commandFromCloud = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, STEP_COMPLETE);
-          printf("发送完成\r\n");
           break;          //发送数据阶段
         case STEP_COMPLETE:
-          receiveBlDataCount = 0;
-          deviceInfo.timeCount = 0;
           deviceInfo.forwardBLAndLoraDataDuration = 0;
           i = 0;
           deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);

+ 1 - 1
Core/Src/ring_buffer.c

@@ -17,7 +17,7 @@ int RingBuffer_IsFull(RingBuffer *q) {
     return ((q->tail + 1) % QUEUE_SIZE == q->head);
 }
 
-int RingBuffer_Enqueue(RingBuffer *q, uint8_t *data) {
+int RingBuffer_Enqueue(RingBuffer *q, const uint8_t *data) {
     if (RingBuffer_IsFull(q)) {
         return -1;  // 队列已满
     }

+ 18 - 7
Core/Src/stm32f1xx_it.c

@@ -30,6 +30,7 @@
 #include "E104-BT5005A.h"
 #include "E52.h"
 #include "RTC_SLEEP.h"
+#include "ring_buffer.h"
 
 /* USER CODE END Includes */
 
@@ -365,14 +366,24 @@ void USART3_IRQHandler(void)
 
     if (strncmp((char *)rx_buf, "AT", 2) != 0) {
       if (deviceInfo.BlDataFlag == BL_START) {
-        if (receiveBlDataCount < 45)
-        {
-          memcpy(totalData[receiveBlDataCount], rx_buf, received_len);
-          receiveBlDataCount++;
-          memset(rx_buf, 0, received_len);
-          HAL_UART_Receive_DMA(&huart3, rx_buf, received_len); // 重新启动
+        // 检查队列是否已满
+        if (RingBuffer_IsFull(&deviceInfo.queueBLData)) {
+          uint8_t temp[BL_DATA_SIZE];
+          // 先出队一个数据腾出空间
+          if (RingBuffer_Dequeue(&deviceInfo.queueBLData, temp) == 0) {
+            // 可选:打印或处理被删除的数据
+            // printf("队列满,出队一个数据腾出空间\n");
+          }
+        }
+
+        // 再入队新数据
+        if (RingBuffer_Enqueue(&deviceInfo.queueBLData, rx_buf) == 0) {
+          // 入队成功
+          // printf("入队成功\n");
+        } else {
+          // 理论上不会再失败,因为已经腾出一个位置
+          // printf("入队失败\n");
         }
-      }else if (deviceInfo.BlDataFlag == BL_STOP) {
       }
     }