Arduino DLNA Server
RingBuffer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Vector.h"
4 #include "stddef.h"
5 #include "stdint.h"
6 
7 namespace tiny_dlna {
8 
16 class RingBuffer {
17  public:
18  RingBuffer(int size = 512) {
19  max_len = size;
20  buffer.resize(size);
21  }
22 
23  // available to read
24  int available() { return actual_len; }
25 
26  int availableToWrite() { return max_len - actual_len; }
27 
28  // reads a single character and makes it availble on the buffer
29  int read() {
30  int result = peek();
31  if (result > -1) {
33  actual_len--;
34  }
35  // wrap to the start
36  if (actual_read_pos >= max_len) {
37  actual_read_pos = 0;
38  }
39  return result;
40  }
41 
42  int read(uint8_t *str, int len) {
43  for (int j = 0; j < len; j++) {
44  int current = read();
45  if (current == 0) {
46  return j - 1;
47  }
48  str[j] = current;
49  }
50  return len;
51  }
52 
53  // peeks the actual character
54  int peek() {
55  int result = -1;
56  if (actual_len > 0) {
57  result = buffer[actual_read_pos];
58  }
59  return result;
60  };
61 
62  size_t write(uint8_t ch) {
63  int result = 0;
64  if (actual_len < max_len) {
67  actual_len++;
68  if (actual_write_pos >= max_len) {
69  actual_write_pos = 0;
70  }
71  }
72  return result;
73  }
74 
75  size_t write(uint8_t *str, int len) {
76  for (int j = 0; j < len; j++) {
77  int result = write(str[j]);
78  if (result == 0) {
79  return j;
80  }
81  }
82  return len;
83  }
84 
85  void resize(int size) { buffer.resize(size); }
86 
87  protected:
89  int max_len;
90  int actual_len = 0;
91  int actual_read_pos = 0;
93 };
94 
95 } // namespace tiny_dlna
Implementation of a Simple Circular Buffer. Instead of comparing the position of the read and write p...
Definition: RingBuffer.h:16
size_t write(uint8_t ch)
Definition: RingBuffer.h:62
RingBuffer(int size=512)
Definition: RingBuffer.h:18
int peek()
Definition: RingBuffer.h:54
int read(uint8_t *str, int len)
Definition: RingBuffer.h:42
void resize(int size)
Definition: RingBuffer.h:85
int available()
Definition: RingBuffer.h:24
int actual_write_pos
Definition: RingBuffer.h:92
int max_len
Definition: RingBuffer.h:89
size_t write(uint8_t *str, int len)
Definition: RingBuffer.h:75
int actual_len
Definition: RingBuffer.h:90
int availableToWrite()
Definition: RingBuffer.h:26
Vector< char > buffer
Definition: RingBuffer.h:88
int actual_read_pos
Definition: RingBuffer.h:91
int read()
Definition: RingBuffer.h:29
bool resize(int newSize, T value)
Definition: Vector.h:247
Definition: Allocator.h:6