get_stream_helper()
Helper object for using mwx::stream
template <typename T, int N> smplbuf_local
template <typename T> smplbuf_attach
template <typename T> smplbuf_heap
smplbuf
is a container class that provides array operations for a memory region specified by the element type T
and the memory allocation method alloc
. Since specifying alloc
directly is cumbersome, aliases are defined using using
.
Below is an example of object declaration. After declaration, call an initialization method. Each object starts with a maximum size of 128 bytes and a current size of 0. Expand the size as needed during use.
// Array area is a fixed array as a class member variable
smplbuf_local<uint8_t, 128> b1;
// Array area references an existing buffer
uint8_t buf[128];
smplbuf_attach<uint8_t> b2(;
// Array area is allocated on the heap
smplbuf_heap<uint8_t> b3;
// Initialization (if defined globally, do this in setup())
void setup() {
b1.init_local();
b2.attach(buf, 0, 128);
b3.init_heap(128);
}
// Inside a processing function
void some_func() {
smplbuf_local<uint8_t, 128> bl;
// Can be omitted if smplbuf_local is defined locally
bl.push_back('a');
}
Aliases are available for the uint8_t
type only.
template <int N>
smplbuf_u8
// smplbuf<uint8_t, alloc_local<uint8_t, N>>
smplbuf_u8_attach
// smplbuf<uint8_t, alloc_attach<uint8_t>>
smplbuf_u8_heap
// smplbuf<uint8_t, alloc_heap<uint8_t>>
You can access elements using the []
operator like a normal array, and also use iterators.
void begin() { // begin() runs only once at startup
smplbuf_u8<32> b1;
b1.reserve(5); // Initializes 5 bytes of usable area (accessible via b1[0..5])
b1[0] = 1;
b1[1] = 4;
b1[2] = 9;
b1[3] = 16;
b1[4] = 25;
for(uint8_t x : b1) { // Loop using .begin() and .end() implicitly
Serial << int(x) << ",";
}
}
The push_back()
method is defined, allowing algorithms that append data to the end.
smplbuf_local<T,N>()
smplbuf_local<T,N>::init_local()
smplbuf_attach<T>(T* buf, uint16_t size, uint16_t N)
smplbuf_attach<T>::attach(T* buf, uint16_t size, uint16_t N)
smplbuf_heap<T>()
smplbuf_heap<T>::init_heap(uint16_t N)
// Example
// Fixed-size internal array
smplbuf_local<uint8_t, 128> b1;
b1.init_local();
// Using an existing array
uint8_t buf[128];
smplbuf_attach<uint8_t> b2;
b2.attach(buf, 0, 128);
// Allocated on the heap
smplbuf_heap<uint8_t> b3;
b3.init_heap(128);
Declares a container of type T
and size N
. Call an initialization method after declaration.
smplbuf_local
allocates memory using an internal fixed array. Initialization via constructor is also supported.
smplbuf_attach
requires the pointer to the buffer T* buf
, the initial size size
, and the maximum size N
. Initialization via constructor is also supported.
smplbuf_heap
allocates memory in the heap (a memory region that cannot be released but can be allocated at runtime). Since the memory cannot be freed once allocated, it is typically used as a global object. Memory allocation is done via init_heap()
. Allocation via constructor is not supported; always use init_heap()
.
init_local()
, attach()
, or init_heap()
) at the start of execution (e.g., in setup()
).
void in_some_func() {
smplbuf_local<uint8_t, 5> b1;
b1.init_local();
b1 = { 0, 1, 2, 3, 4 };
smplbuf_local<uint8_t, 5> b2{0, 1, 2, 3, 4};
}
Members can be initialized using an initializer list { ... }
. Except for local declarations of smplbuf_local
, you must call an initialization method first.
smplbuf_local
, smplbuf_attach
, smplbuf_heap
)smplbuf_local
, not allowed in global scope)append()
, push_back()
, pop_back()
inline bool append(T&& c)
inline bool append(const T& c)
inline void push_back(T&& c)
inline void push_back(const T& c)
inline void pop_back()
Adds the member c
to the end. append()
returns a bool
, which is false
if the buffer is full and the element cannot be added.
pop_back()
removes the last entry. Note that it does not clear the content.
empty()
, size()
, capacity()
inline bool empty()
inline bool is_end()
inline uint16_t size()
inline uint16_t capacity()
empty()
returns true
if the array has no elements. Conversely, is_end()
returns true
when the array is full.
size()
returns the number of elements in the array.
capacity()
returns the maximum number of elements the array can hold.
reserve()
, reserve_head()
, redim()
inline bool reserve(uint16_t len)
inline void reserve_head(uint16_t len)
inline void redim(uint16_t len)
reserve()
expands the array size. The newly allocated region is initialized by default.
reserve_head()
reserves space at the head of the array that is not accessible via the container. This can be used, for example, when accessing a subarray after skipping the header of a packet payload. To restore access to the reserved region, provide the same negative value used during reservation.
redim()
changes the size of the active region. Unlike reserve()
, it does not initialize unused areas.
operator []
inline T& operator [] (int i)
inline T operator [] (int i) const
Accesses an element by index.
If a negative value is given to i
, the element is accessed from the end. -1
refers to the last element, -2
to the second last, and so on.
mwx::stream
Array objects of type uint8_t
(smplbuf<uint8_t, *>
) can be directly output to derived objects of mwx::stream
.
<<
Operator
template <class L_STRM, class AL>
mwx::stream<L_STRM>& operator << (
mwx::stream<L_STRM>& lhs, mwx::_smplbuf<uint8_t, AL>& rhs)
//例
smplbuf_u8<128> buf;
buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
Serial << buf;
// Output: abc
Outputs the byte array to a derived object of mwx::stream
, such as Serial
.
to_stream()
inline std::pair<T*, T*> to_stream()
// Example
smplbuf_u8<128> buf;
buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
Serial << buf.to_stream();
// Output: 0123
Used for stream output purposes. This method is used in the implementation of the <<
operator.
mwx::stream
mwx::stream
provides functions and operators such as the <<
operator and printfmt()
method for outputting byte arrays to a stream. You can use a smplbuf
array of uint8_t
as a stream output target.
There are two methods.
get_stream_helper()
.mwx::stream
.Helper object for using mwx::stream
Type that allows direct use of stream methods