2#include "TinyRobotics/utils/Buffers.h"
7namespace tinyrobotics {
11
12
13
14
15
16
17
18
22 SynchronizedStream(Stream &stream,
MutexBase &mutex) {
29 if (read_buffer.isEmpty()) {
31 p_stream->readBytes(read_buffer.address(), read_buffer.size());
34 bool result = read_buffer.read(data);
35 return result ? data : -1;
41 return p_stream->peek();
45 size_t write(uint8_t data)
override {
46 write_buffer.write(data);
47 if (write_buffer.isFull()) {
49 size_t written = p_stream->write((
const uint8_t *)write_buffer.data(),
51 assert(written == write_buffer.size());
58 int available() override {
60 return p_stream->available();
64 int availableForWrite() override {
66 return p_stream->availableForWrite();
71 read_buffer.resize(size);
72 write_buffer.resize(size);
76 Stream *p_stream =
nullptr;
RAII implementaion using a Mutex: Only a few microcontrollers provide lock guards,...
Definition: LockGuard.h:18
Empty Mutex implementation which does nothing.
Definition: Mutex.h:18
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition: Buffers.h:136
Wrapper class that can turn any Stream into a thread save implementation. This is done by adding a Mu...
Definition: SynchronizedStream.h:20
void setBufferSize(int size)
Defines the size of the internal buffers.
Definition: SynchronizedStream.h:70