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 int current = read();
66 if (current <= 0) {
67 return j;
68 }
69 str[j] = current;
70 }
71 return len;
72 }
73
74 // peeks the actual character
75 int peek() {
76 int result = -1;
77 if (actual_len > 0) {
78 result = buffer[actual_read_pos];
79 }
80 return result;
81 };
82
83 size_t write(uint8_t ch) {
84 int result = 0;
85 if (actual_len < max_len) {
86 result = 1;
87 buffer[actual_write_pos] = ch;
88 actual_write_pos++;
89 actual_len++;
90 if (actual_write_pos >= max_len) {
91 actual_write_pos = 0;
92 }
93 }
94 return result;
95 }
96
97 size_t write(char* str, int len) { return write((uint8_t*)str, len); }
98
99 size_t write(uint8_t* str, int len) {
100 for (int j = 0; j < len; j++) {
101 int result = write(str[j]);
102 if (result == 0) {
103 return j;
104 }
105 }
106 return len;
107 }
108
109 protected:
110 std::vector<char> buffer;
111 int max_len;
112 int actual_len = 0;
113 int actual_read_pos = 0;
114 int actual_write_pos = 0;
115};
116
117} // 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