Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_CircularDifferentialBuffer.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Mar 16, 2015
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
9//#include <common/cs_Types.h>
10
11#include <cstddef>
12//#include <cstdint>
13
14//#include <stdlib.h> // malloc, free
15//#include <stdint.h> // uint32_t
16//
17//#include <logging/cs_Logger.h>
18
26template <typename T>
28public:
40 CircularDifferentialBuffer(uint16_t capacity = 32, bool initialize = false)
41 : _array(NULL), _capacity(capacity), _head(0), _tail(-1), _contentSize(0) {
42 if (initialize) {
43 init();
44 }
45 }
46
52
61 bool init() {
62 if (!_array) {
63 _array = (int8_t*)calloc(_capacity - 1, sizeof(int8_t));
64 }
65 // also call clear to make sure the head and tail are reset and we
66 // start with a clean buffer
67 clear();
68 return _array != NULL;
69 }
70
77 void clear() {
78 _head = 0;
79 _tail = -1;
80 _contentSize = 0;
81 }
82
87 uint16_t size() const { return _contentSize; }
88
96 uint16_t capacity() const { return _capacity; }
97
102 bool empty() const { return size() == 0; }
103
108 bool full() const { return size() == capacity(); }
109
118 T peek() const { return _firstVal; }
119
125 bool getFirstElement(T& val) {
126 if (!_contentSize) {
127 return false;
128 }
129 // _readIdx = _head;
130 _readIdx = 0;
131 val = _firstVal;
132 return true;
133 }
134
141 bool getNextElement(T& val) {
142 if (_readIdx >= (_contentSize - 1)) {
143 // if (_readIdx == _tail) {
144 return false;
145 }
146 // ++_readIdx;
147 // _readIdx %= (_capacity-1);
148 // val += _array[_readIdx];
149 val += _array[(_head + _readIdx++) % (_capacity - 1)];
150 return true;
151 }
152
158 bool getLastElement(T& val) {
159 if (!_contentSize) {
160 return false;
161 }
162 val = _lastVal;
163 return true;
164 }
165
170 uint16_t getSerializedLength() const {
171 if (empty()) {
172 return 0;
173 }
174 return sizeof(T) + size() - 1;
175 }
176
183 void serialize(uint8_t* buffer);
184
192 T pop() {
193 T res = peek();
195 incHead();
196 return res;
197 }
198
208 bool push(T value) {
209 if (!_contentSize) {
210 _firstVal = value;
211 _lastVal = value;
212 _contentSize = 1;
213 return true;
214 }
215
216 int32_t diff = (int32_t)value - _lastVal;
217 if (diff > 127 || diff < -127) {
218 // LOGd("difference too large! %i", diff);
219 clear();
220 return false;
221 }
222
223 incTail();
224 if (_contentSize > _capacity) {
226 incHead();
227 }
228 _array[_tail] = diff;
229 _lastVal = value;
230 return true;
231 }
232
233private:
235 int8_t* _array;
236
238 uint16_t _capacity;
239
241 uint16_t _head;
242
244 uint16_t _tail;
245
247 uint16_t _contentSize;
248
251
254
256 uint16_t _readIdx;
257
263 void incTail() {
264 ++_tail;
265 _tail %= (_capacity - 1);
266 ++_contentSize;
267 }
268
274 void incHead() {
275 ++_head;
276 _head %= (_capacity - 1);
277 --_contentSize;
278 }
279};
Differential Buffer implementation.
Definition: cs_CircularDifferentialBuffer.h:27
uint16_t _contentSize
Number of elements stored in the buffer.
Definition: cs_CircularDifferentialBuffer.h:247
uint16_t _tail
Index of the tail (where the next element will be inserted)
Definition: cs_CircularDifferentialBuffer.h:244
uint16_t _capacity
The capacity of the buffer (maximum number of elements)
Definition: cs_CircularDifferentialBuffer.h:238
T pop()
Get and remove the oldest element.
Definition: cs_CircularDifferentialBuffer.h:192
bool getFirstElement(T &val)
Get the first element of the buffer @val the value of the oldest element.
Definition: cs_CircularDifferentialBuffer.h:125
uint16_t capacity() const
Returns the capacity of the buffer.
Definition: cs_CircularDifferentialBuffer.h:96
uint16_t _readIdx
Index of the last read value, used in getNextElement()
Definition: cs_CircularDifferentialBuffer.h:256
T _lastVal
Value of the newest element in the buffer.
Definition: cs_CircularDifferentialBuffer.h:253
bool empty() const
Checks if the buffer is empty.
Definition: cs_CircularDifferentialBuffer.h:102
CircularDifferentialBuffer(uint16_t capacity=32, bool initialize=false)
Default constructor @capacity the size with which the buffer should be initialized.
Definition: cs_CircularDifferentialBuffer.h:40
uint16_t _head
Index of the head (next element to be removed)
Definition: cs_CircularDifferentialBuffer.h:241
T _firstVal
Value of the oldest element in the buffer.
Definition: cs_CircularDifferentialBuffer.h:250
uint16_t size() const
Returns the number of elements stored.
Definition: cs_CircularDifferentialBuffer.h:87
void incTail()
Increases the tail.
Definition: cs_CircularDifferentialBuffer.h:263
bool init()
Initializes and allocates memory for the buffer based on the capacity used with the constructor.
Definition: cs_CircularDifferentialBuffer.h:61
void serialize(uint8_t *buffer)
Write all elements to a buffer @buffer buffer to which to write the elements.
void incHead()
Increases the head.
Definition: cs_CircularDifferentialBuffer.h:274
T peek() const
Peek at the oldest element without removing it.
Definition: cs_CircularDifferentialBuffer.h:118
void clear()
Clears the buffer.
Definition: cs_CircularDifferentialBuffer.h:77
int8_t * _array
Pointer to the array storing the difference of elements compared to the previous element.
Definition: cs_CircularDifferentialBuffer.h:235
bool getNextElement(T &val)
Get the next value of the buffer, after calling <CircularDifferentialBuffer>>getFirstElement()>
Definition: cs_CircularDifferentialBuffer.h:141
bool full() const
Checks if the buffer is full.
Definition: cs_CircularDifferentialBuffer.h:108
bool push(T value)
Add an element to the end of the buffer @value the element to be added.
Definition: cs_CircularDifferentialBuffer.h:208
bool getLastElement(T &val)
Get the first element of the buffer @val the value of the oldest element.
Definition: cs_CircularDifferentialBuffer.h:158
uint16_t getSerializedLength() const
Get the serialized length.
Definition: cs_CircularDifferentialBuffer.h:170
virtual ~CircularDifferentialBuffer()
Default destructor.
Definition: cs_CircularDifferentialBuffer.h:51