arduino-emulator
Loading...
Searching...
No Matches
RingBufferExt.h
1/*
2 RingBufferExt.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#pragma once
20
21#include "stddef.h"
22#include "stdint.h"
23#include "vector"
24
25namespace arduino {
26
36 public:
37 RingBufferExt(int size = 1024) {
38 max_len = size;
39 buffer.resize(size);
40 }
41
42 // available to read
43 int available() { return actual_len; }
44
45 int availableToWrite() { return max_len - actual_len; }
46
47 // reads a single character and makes it availble on the buffer
48 int read() {
49 int result = peek();
50 if (result > -1) {
51 actual_read_pos++;
52 actual_len--;
53 }
54 // wrap to the start
55 if (actual_read_pos >= max_len) {
56 actual_read_pos = 0;
57 }
58 return result;
59 }
60
61 int read(char* str, int len) { return read((uint8_t*)str, len); }
62
63 int read(uint8_t* str, int len) {
64 for (int j = 0; j < len; j++) {
65 const int current = read();
66 if (current < 0) {
67 return j;
68 }
69 // A byte value of 0x00 is valid payload data; only -1 means empty.
70 str[j] = static_cast<uint8_t>(current);
71 }
72 return len;
73 }
74
75 // peeks the actual character
76 int peek() {
77 int result = -1;
78 if (actual_len > 0) {
79 result = buffer[actual_read_pos];
80 }
81 return result;
82 };
83
84 size_t write(uint8_t ch) {
85 int result = 0;
86 if (actual_len < max_len) {
87 result = 1;
88 buffer[actual_write_pos] = ch;
89 actual_write_pos++;
90 actual_len++;
91 if (actual_write_pos >= max_len) {
92 actual_write_pos = 0;
93 }
94 }
95 return result;
96 }
97
98 size_t write(char* str, int len) { return write((uint8_t*)str, len); }
99
100 size_t write(uint8_t* str, int len) {
101 for (int j = 0; j < len; j++) {
102 int result = write(str[j]);
103 if (result == 0) {
104 return j;
105 }
106 }
107 return len;
108 }
109
110 protected:
111 // Must be unsigned: peek()/read() return byte values widened to int, and
112 // -1 is the "no data" sentinel. A signed char storing byte value 0xFF
113 // sign-extends to -1, indistinguishable from "empty" — permanently
114 // wedging the buffer on any payload containing a 0xFF byte (e.g. Opus or
115 // raw PCM audio), since read() then refuses to ever advance past it.
116 std::vector<uint8_t> buffer;
117 int max_len;
118 int actual_len = 0;
119 int actual_read_pos = 0;
120 int actual_write_pos = 0;
121};
122
123} // namespace arduino
Definition DMAPool.h:103
Implementation of a Simple Circular Buffer. Instead of comparing the position of the read and write p...
Definition RingBufferExt.h:35
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31