ros2_control - galactic
Loading...
Searching...
No Matches
double_buffer.hpp
1// Copyright 2026 Open Source Robotics Foundation, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ZENBEDDED_RCL__DOUBLE_BUFFER_HPP_
16#define ZENBEDDED_RCL__DOUBLE_BUFFER_HPP_
17
18#include <stddef.h>
19#include <stdint.h>
20#include <string.h>
21#include <zephyr/spinlock.h>
22
23namespace zenbedded
24{
25
26template <size_t Capacity>
27class DoubleByteBuffer
28{
29public:
30 DoubleByteBuffer() { reset(); }
31
32 void reset()
33 {
34 K_SPINLOCK(&lock_)
35 {
36 active_idx_ = 0;
37 memset(buffer_, 0, sizeof(buffer_));
38 }
39 }
40
41 uint8_t * slot(size_t idx) { return buffer_[idx]; }
42 const uint8_t * slot(size_t idx) const { return buffer_[idx]; }
43
44 uint8_t * prepare_write(int & out_write_idx)
45 {
46 K_SPINLOCK(&lock_) { out_write_idx = active_idx_ ^ 1; }
47 return buffer_[out_write_idx];
48 }
49
50 void commit_write(int write_idx)
51 {
52 K_SPINLOCK(&lock_) { active_idx_ = write_idx; }
53 }
54
55 bool write(const uint8_t * data, size_t size)
56 {
57 if (!data || size > Capacity)
58 {
59 return false;
60 }
61 int write_idx = 0;
62 uint8_t * dst = prepare_write(write_idx);
63 memcpy(dst, data, size);
64 commit_write(write_idx);
65 return true;
66 }
67
68 bool read_latest(uint8_t * out, size_t size) const
69 {
70 if (!out || size > Capacity)
71 {
72 return false;
73 }
74 K_SPINLOCK(&lock_) { memcpy(out, buffer_[active_idx_], size); }
75 return true;
76 }
77
78 static constexpr size_t capacity() { return Capacity; }
79
80 [[nodiscard]] int active_index() const
81 {
82 int idx = 0;
83 K_SPINLOCK(&lock_) { idx = active_idx_; }
84 return idx;
85 }
86
87private:
88 uint8_t buffer_[2][Capacity]{};
89 mutable struct k_spinlock lock_{};
90 int active_idx_{0};
91};
92
93template <typename T>
94class DoubleBuffer
95{
96public:
97 DoubleBuffer() { reset(); }
98
99 void reset()
100 {
101 K_SPINLOCK(&lock_)
102 {
103 active_idx_ = 0;
104 slots_[0] = T{};
105 slots_[1] = T{};
106 }
107 }
108
109 T & slot(size_t idx) { return slots_[idx]; }
110 const T & slot(size_t idx) const { return slots_[idx]; }
111
112 T & prepare_write(int & out_write_idx)
113 {
114 K_SPINLOCK(&lock_) { out_write_idx = active_idx_ ^ 1; }
115 return slots_[out_write_idx];
116 }
117
118 void commit_write(int write_idx)
119 {
120 K_SPINLOCK(&lock_) { active_idx_ = write_idx; }
121 }
122
123 void write(const T & value)
124 {
125 int write_idx = 0;
126 prepare_write(write_idx) = value;
127 commit_write(write_idx);
128 }
129
130 bool read_latest(T & out) const
131 {
132 K_SPINLOCK(&lock_) { out = slots_[active_idx_]; }
133 return true;
134 }
135
136 [[nodiscard]] int active_index() const
137 {
138 int idx = 0;
139 K_SPINLOCK(&lock_) { idx = active_idx_; }
140 return idx;
141 }
142
143private:
144 T slots_[2]{};
145 mutable struct k_spinlock lock_{};
146 int active_idx_{0};
147};
148
149} // namespace zenbedded
150
151#endif // ZENBEDDED_RCL__DOUBLE_BUFFER_HPP_