加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
RingBuffer.cpp 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
louis_sun 提交于 2021-12-11 14:38 . serial_and_plot
#include "RingBuffer.h"
#include "iostream"
using namespace std;
#ifdef _WIN32
#include <windows.h>
#ifndef imsleep
#define imsleep(microsecond) Sleep(microsecond) // ms
#endif
#else
#include <unistd.h>
#define imsleep(microsecond) usleep(1000 * microsecond) // ms
#endif
RingBuffer::RingBuffer(int _buffer_size, int _frame_size)
{
buffer_size = _buffer_size;
frame_size = _frame_size;
p_buffer = new unsigned char *[buffer_size];
for (size_t i = 0; i < buffer_size; i++)
{
p_buffer[i] = new unsigned char[frame_size];
}
}
unsigned char *RingBuffer::get_next_to_push(void)
{
unsigned char *temp = p_buffer[now_point];
now_point++;
now_point %= buffer_size;
return temp;
}
unsigned char *RingBuffer::get_next_to_read(void)
{
while (need_to_read == now_point)
{
imsleep(3);
}
unsigned char *temp = p_buffer[need_to_read];
need_to_read++;
need_to_read %= buffer_size;
return temp;
}
void RingBuffer::test_buffer(void)
{
}
RingBuffer::~RingBuffer()
{
for (size_t i = 0; i < buffer_size; i++)
{
delete[] p_buffer[i];
}
delete[] p_buffer;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化