tr_queue.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * queue.h
  3. *
  4. * Created on: 2025年8月20日
  5. * Author: 龙三郎
  6. */
  7. #ifndef USER_INCLUDES_TR_QUEUE_H_
  8. #define USER_INCLUDES_TR_QUEUE_H_
  9. #include "stdbool.h"
  10. // 队列相关
  11. #define QUEUE_MAX_SIZE 4096 // 定义队列的最大容量
  12. #define QUEUE_NUM 4 // 队列的数量
  13. #define QUEUE_INDEX_USB 0 // usb用的队列索引
  14. #define QUEUE_INDEX_ETHERNET 1 // usb用的队列索引
  15. #define QUEUE_INDEX_HEAR 2 // usb用的队列索引
  16. #define QUEUE_INDEX_UART 3 // usb用的队列索引
  17. // 缓存相关
  18. #define BUFFER_SIZE_1K 1024
  19. #define BUFFER_SIZE_2K 2048
  20. // 模块地址
  21. #define DSP_ADDRESS 1 // DSP主控模块的地址为1
  22. #define F4_ADDRESS 2 // F4模块的地址为2
  23. #define GUI_ADDRESS 3 // GUI模块的地址为3
  24. #define UART_ADDRESS 4 // 串口模块的地址为4
  25. #define THIS_ADDRESS DSP_ADDRESS // 本模块为DSP主控模块
  26. // 数据类型
  27. #define TYPE_DEFAULT 0 // 自定义的
  28. #define TYPE_WAVEFORM 1 // 波形数据
  29. #define TYPE_COMMAND 2 // 指令
  30. // 操作
  31. //#define isFull(q) ((q->rear + 1) % QUEUE_MAX_SIZE == q->front)
  32. typedef struct {
  33. unsigned char items[QUEUE_MAX_SIZE]; // 存储队列元素的数组
  34. int front; // 队首指针
  35. int rear; // 队尾指针
  36. } Queue;
  37. extern Queue queueList[QUEUE_NUM];
  38. void initializeQueue(Queue *q); // 初始化队列
  39. bool isFull(Queue *q); // 判断队列是否已满
  40. bool isEmpty(Queue *q); // 判断队列是否为空
  41. int currentItems(Queue *q); // 队列现有元素个数
  42. int remainSpace(Queue *q); // 队列剩余空间
  43. bool enqueue(Queue *q, unsigned char element); // 入队列,单个元素
  44. bool enqueueBatch(Queue *q, unsigned char *elements, unsigned int elSize); // 批量入队列
  45. bool dequeue(Queue *q, unsigned char *element); // 出队列,单个元素
  46. bool dequeueBatch(Queue *q, unsigned short len, unsigned char *element); // 批量出队列
  47. // 协议解析相关
  48. bool getVerFromProtocol(unsigned char *p, unsigned short pl, unsigned char *ver); // 获取协议版本
  49. bool getDaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *da); // 获取目的地址
  50. bool getSaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *sa); // 获取来源地址
  51. bool getTypeFromProtocol(unsigned char *p, unsigned short pl, unsigned char *type); // 获取数据类型
  52. bool getPayloadFromProtocol(unsigned char *p, unsigned short pl, unsigned char *s, unsigned short *sl); // 获取源数据
  53. // 数据转发协议相关
  54. bool dequeueBatchWithProtocol(Queue *q, unsigned char *d, unsigned short *dl); // 从队列中取出一个数据包。
  55. // 转发业务相关
  56. void dataforwardInit(void); // 初始化
  57. void dataforwardMain(void); // 主流程
  58. bool enqueueFromUsb(unsigned char *s, unsigned short sl); // 将从usb发来的协议数据入队列
  59. bool enqueueFromEthernet(unsigned char *s, unsigned short sl); // 将从以太网发来的协议数据入队列
  60. bool enqueueFromUart(unsigned char c); // 将从串口发来的数据入队列,一次一字符
  61. bool enqueueFromUartBatch(unsigned char *s, unsigned short sl); // 将从串口发来的数据入队列,批量
  62. bool enqueueFromHere(unsigned char *s, unsigned short sl, unsigned char da, unsigned char type); // 从本模块发出的数据入队列,源数据。不带协议格式
  63. extern void dataForwardedHandle(unsigned char *s, unsigned short sl, unsigned char da); // 数据转发业务处理,需要进行转发的数据在这个里面处理
  64. extern void dataNotForwardedHandle(unsigned char *s, unsigned short sl, unsigned char sa, unsigned char type); // 不需要转发的数据。即其他模块发送给本模块的数据。
  65. #endif /* USER_INCLUDES_TR_QUEUE_H_ */