libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
double_buffer.hpp
1#pragma once
2
3#include <cassert>
4#include <cstddef>
5#include <cstdint>
6#include <cstring>
7
8#include "libxr_type.hpp" // RawData
9
10namespace LibXR
11{
21{
22 public:
29 explicit DoubleBuffer(const LibXR::RawData& raw_data) : size_(raw_data.size_ / 2)
30 {
31 buffer_[0] = static_cast<uint8_t*>(raw_data.addr_);
32 buffer_[1] = static_cast<uint8_t*>(raw_data.addr_) + size_;
33 }
34
41 uint8_t* ActiveBuffer() { return buffer_[active_]; }
42
49 uint8_t* PendingBuffer() { return buffer_[1 - active_]; }
50
57 size_t Size() const { return size_; }
58
66 void Switch()
67 {
69 {
70 active_ ^= 1;
71 pending_valid_ = false;
72 }
73 }
74
81 bool HasPending() const { return pending_valid_; }
82
92 bool FillPending(const uint8_t* data, size_t len)
93 {
94 if (pending_valid_ || len > size_)
95 {
96 return false;
97 }
98 std::memcpy(PendingBuffer(), data, len);
99 pending_len_ = len;
100 pending_valid_ = true;
101 return true;
102 }
103
113 bool FillActive(const uint8_t* data, size_t len)
114 {
115 if (len > size_)
116 {
117 return false;
118 }
119 std::memcpy(ActiveBuffer(), data, len);
120 return true;
121 }
122
129 void EnablePending() { pending_valid_ = true; }
130
137 size_t PendingLength() const { return pending_valid_ ? pending_len_ : 0; }
138
139 private:
140 uint8_t* buffer_[2];
141 size_t size_;
142 int active_ = 0;
144 false;
145 size_t pending_len_ = 0;
146};
147} // namespace LibXR
双缓冲区管理类 / Double buffer manager class
int active_
当前活动缓冲区编号 / Index of active buffer
bool FillActive(const uint8_t *data, size_t len)
向当前使用的缓冲区直接写入数据 Fills the active buffer directly
bool HasPending() const
判断是否有待切换的缓冲区 Checks whether a pending buffer is ready
size_t PendingLength() const
获取 pending 缓冲区中准备好的数据长度 Gets the size of valid data in pending buffer
bool FillPending(const uint8_t *data, size_t len)
向备用缓冲区写入数据(不可重入) Fills the pending buffer with data (non-reentrant)
size_t size_
单个缓冲区大小 / Size of each buffer
uint8_t * ActiveBuffer()
获取当前正在使用的缓冲区指针 Returns the currently active buffer
bool pending_valid_
标记备用区是否准备好 / Whether pending buffer is ready
DoubleBuffer(const LibXR::RawData &raw_data)
构造函数,使用连续内存构造两个缓冲区 Constructs a double buffer using one continuous memory block
size_t pending_len_
备用缓冲区有效数据长度 / Length of pending data
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
uint8_t * PendingBuffer()
获取备用缓冲区的指针 Returns the pending (inactive) buffer
void EnablePending()
手动启用 pending 状态 Manually sets the pending state to true
size_t Size() const
获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
uint8_t * buffer_[2]
双缓冲区指针 / Double buffer pointers
原始数据封装类。 A class for encapsulating raw data.
void * addr_
数据存储地址。 The storage address of the data.
LibXR 命名空间
Definition ch32_gpio.hpp:9