EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnigmaIOTRingBuffer.h
Go to the documentation of this file.
1 
9 #ifndef _ENIGMAIOTBUFFER_h
10 #define _ENIGMAIOTBUFFER_h
11 
12 #if defined(ARDUINO) && ARDUINO >= 100
13 #include "Arduino.h"
14 #else
15 #include "WProgram.h"
16 #endif
17 #include "helperFunctions.h"
18 
23 template <typename Telement>
25 protected:
26  int maxSize;
27  int numElements = 0;
28  int readIndex = 0;
29  int writeIndex = 0;
30  Telement* buffer;
31 
32 public:
37  EnigmaIOTRingBuffer <Telement> (int range) : maxSize (range) {
38  buffer = new Telement[maxSize];
39  }
40 
46  maxSize = 0;
47  delete[] (buffer);
48  }
49 
54  int size () { return numElements; }
55 
60  bool isFull () { return numElements == maxSize; }
61 
66  bool empty () { return (numElements == 0); }
67 
73  bool push (Telement* item) {
74  bool wasFull = isFull ();
75  DEBUG_DBG ("Add element. Buffer was %s", wasFull ? "full" : "not full");
76  DEBUG_DBG ("Before -- > ReadIdx: %d. WriteIdx: %d. Size: %d", readIndex, writeIndex, numElements);
77 #ifdef ESP32
78  portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
79  portENTER_CRITICAL (&myMutex);
80 #endif
81  memcpy (&(buffer[writeIndex]), item, sizeof (Telement));
82  //Serial.printf ("Copied: %d bytes\n", sizeof (Telement));
83  writeIndex++;
84  if (writeIndex >= maxSize) {
86  }
87  if (wasFull) { // old value is no longer valid
88  readIndex++;
89  if (readIndex >= maxSize) {
90  readIndex %= maxSize;
91  }
92  } else {
93  numElements++;
94  }
95 #ifdef ESP32
96  portEXIT_CRITICAL (&myMutex);
97 #endif
98  DEBUG_DBG ("After -- > ReadIdx: %d. WriteIdx: %d. Size: %d", readIndex, writeIndex, numElements);
99  return !wasFull;
100  }
101 
106  bool pop () {
107  bool wasEmpty = empty ();
108  DEBUG_DBG ("Remove element. Buffer was %s", wasEmpty ? "empty" : "not empty");
109  DEBUG_DBG ("Before -- > ReadIdx: %d. WriteIdx: %d. Size: %d", readIndex, writeIndex, numElements);
110  if (!wasEmpty) {
111  readIndex++;
112  if (readIndex >= maxSize) {
113  readIndex %= maxSize;
114  }
115  numElements--;
116  }
117  DEBUG_DBG ("After -- > ReadIdx: %d. WriteIdx: %d. Size: %d", readIndex, writeIndex, numElements);
118  return !wasEmpty;
119  }
120 
125  Telement* front () {
126  DEBUG_DBG ("Read element. ReadIdx: %d. WriteIdx: %d. Size: %d", readIndex, writeIndex, numElements);
127  if (!empty ()) {
128  return &(buffer[readIndex]);
129  } else {
130  return NULL;
131  }
132  }
133 };
134 
135 #endif
EnigmaIOTRingBuffer::readIndex
int readIndex
Pointer to next item to be read.
Definition: EnigmaIOTRingBuffer.h:28
EnigmaIOTRingBuffer::~EnigmaIOTRingBuffer
~EnigmaIOTRingBuffer()
EnigmaIOTRingBuffer destructor.
Definition: EnigmaIOTRingBuffer.h:45
EnigmaIOTRingBuffer::buffer
Telement * buffer
Actual buffer.
Definition: EnigmaIOTRingBuffer.h:30
EnigmaIOTRingBuffer
Ring buffer class. Used to implement message buffer.
Definition: EnigmaIOTRingBuffer.h:24
EnigmaIOTRingBuffer::size
int size()
Returns actual number of elements that buffer holds.
Definition: EnigmaIOTRingBuffer.h:54
EnigmaIOTRingBuffer::push
bool push(Telement *item)
Adds a new item to buffer, deleting older element if it is full.
Definition: EnigmaIOTRingBuffer.h:73
EnigmaIOTRingBuffer::empty
bool empty()
Checks if buffer is empty.
Definition: EnigmaIOTRingBuffer.h:66
EnigmaIOTRingBuffer::pop
bool pop()
Deletes older item from buffer, if buffer is not empty.
Definition: EnigmaIOTRingBuffer.h:106
EnigmaIOTRingBuffer::maxSize
int maxSize
Buffer size.
Definition: EnigmaIOTRingBuffer.h:26
EnigmaIOTRingBuffer::front
Telement * front()
Gets a pointer to older item in buffer, if buffer is not empty.
Definition: EnigmaIOTRingBuffer.h:125
EnigmaIOTRingBuffer::isFull
bool isFull()
Checks if buffer is full.
Definition: EnigmaIOTRingBuffer.h:60
helperFunctions.h
Auxiliary function definition.
EnigmaIOTRingBuffer::numElements
int numElements
Number of elements that buffer currently has.
Definition: EnigmaIOTRingBuffer.h:27
EnigmaIOTRingBuffer::writeIndex
int writeIndex
Pointer to next position to write onto.
Definition: EnigmaIOTRingBuffer.h:29