libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
double_buffer.cpp
1#include "double_buffer.hpp"
2
3#include "libxr_mem.hpp"
4
5using namespace LibXR;
6
7DoubleBuffer::DoubleBuffer(const LibXR::RawData& raw_data) : SIZE(raw_data.size_ / 2)
8{
9 buffer_[0] = static_cast<uint8_t*>(raw_data.addr_);
10 buffer_[1] = static_cast<uint8_t*>(raw_data.addr_) + SIZE;
11}
12
13uint8_t* DoubleBuffer::ActiveBuffer() const { return buffer_[active_]; }
14
15uint8_t* DoubleBuffer::PendingBuffer() const { return buffer_[1 - active_]; }
16
17size_t DoubleBuffer::Size() const { return SIZE; }
18
20{
22 {
23 active_ ^= 1;
24 pending_valid_ = false;
25 }
26}
27
29
30bool DoubleBuffer::FillPending(const uint8_t* data, size_t len)
31{
32 if (pending_valid_ || len > SIZE)
33 {
34 return false;
35 }
37 pending_len_ = len;
38 pending_valid_ = true;
39 return true;
40}
41
42bool DoubleBuffer::FillActive(const uint8_t* data, size_t len)
43{
44 if (len > SIZE)
45 {
46 return false;
47 }
49 return true;
50}
51
53
55{
56 return pending_valid_ ? pending_len_ : 0;
57}
int active_
当前活动缓冲区编号 / Index of active buffer
DoubleBuffer(const LibXR::RawData &raw_data)
构造函数,使用连续内存构造两个缓冲区 Constructs a double buffer using one continuous memory block
const size_t SIZE
单个缓冲区大小 / Size of each buffer
bool pending_valid_
标记备用区是否准备好 / Whether pending buffer is ready
void EnablePending()
手动启用 pending 状态 Manually sets the pending state to true
bool HasPending() const
判断是否有待切换的缓冲区 Checks whether a pending buffer is ready
size_t Size() const
获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
size_t pending_len_
备用缓冲区有效数据长度 / Length of pending data
bool FillActive(const uint8_t *data, size_t len)
向当前使用的缓冲区直接写入数据 Fills the active buffer directly
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
uint8_t * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
size_t GetPendingLength() const
获取 pending 缓冲区中准备好的数据长度 Gets the size of valid data in pending buffer
uint8_t * buffer_[2]
双缓冲区指针 / Double buffer pointers
bool FillPending(const uint8_t *data, size_t len)
向备用缓冲区写入数据(不可重入) Fills the pending buffer with data (non-reentrant)
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:5
原始数据封装类。 A class for encapsulating raw data.
void * addr_
数据存储地址。 The storage address of the data.
LibXR 命名空间
Definition ch32_can.hpp:14