Files
lora_plug/hx_ringbuffer.h
2025-02-19 16:09:44 +08:00

46 lines
1.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef _BY_RINGBUF_H__
#define _BY_RINGBUF_H__
#include <stdint.h>
#include <stdbool.h>
// 定义宏来选择内存分配方式
// 如果定义了 BY_RINGBUF_STATIC_ALLOC则使用预先分配的内存
// 否则使用 malloc 动态分配内存
// #define BY_RINGBUF_STATIC_ALLOC
#define BY_RINGBUF_STATIC_BUFFER_SIZE (8192)
typedef struct {
uint8_t *buffer; // 缓冲区指针
int size; // 缓冲区大小
int head; // 写指针
int tail; // 读指针
bool is_full; // 缓冲区是否已满
} by_ringbuf_t;
// 初始化环形缓冲区
extern int by_ringbuf_init(by_ringbuf_t *rb, int size);
// 释放环形缓冲区
extern void by_ringbuf_free(by_ringbuf_t *rb);
// 向缓冲区追加数据
extern int by_ringbuf_append(by_ringbuf_t *rb, const uint8_t *data, int len);
// 从缓冲区弹出数据
extern int by_ringbuf_pop(by_ringbuf_t *rb, uint8_t *data, int len);
// 查找缓冲区中是否包含特定数据
extern int by_ringbuf_find(by_ringbuf_t *rb, const uint8_t *pattern, int pattern_len);
// 获取缓冲区中可用数据的大小
extern int by_ringbuf_available_data(by_ringbuf_t *rb);
// 获取缓冲区中空闲空间的大小
extern int by_ringbuf_available_space(by_ringbuf_t *rb);
// 调试函数:打印环形缓冲区中的数据及其序号
extern void by_ringbuf_debug_print(by_ringbuf_t *rb);
#endif // _BY_RINGBUF_H__