many changes

This commit is contained in:
kazu
2018-09-22 15:44:26 +02:00
parent 528a00b0e9
commit 90e9fee101
11 changed files with 1581 additions and 476 deletions

View File

@@ -2,6 +2,7 @@
#define RINGBUFFER_H
#include "../Debug.h"
#include "../Platforms.h"
typedef void (*RingBufferFullCallback)();
@@ -16,7 +17,7 @@ private:
volatile size_t head;
volatile size_t tail;
volatile mutable size_t used;
volatile size_t used;
public:
@@ -34,13 +35,13 @@ public:
init();
}
/** add one value to the buffer. blocks until space is available */
void addBlocking(const Scalar value) {
while (used == size) {os_delay_us(1000);}
data[head] = value;
head = (head + 1) % size;
++used;
}
// /** add one value to the buffer. blocks until space is available */
// void addBlocking(const Scalar value) {
// while (used == size) {os_delay_us(1000);}
// data[head] = value;
// head = (head + 1) % size;
// ++used;
// }
/** add one value to the buffer, if there is enough space */
void addOrIgnore(const Scalar value) {
@@ -59,23 +60,23 @@ public:
}
/** add multiple values to the buffer. blocks until space is available */
void addBlocking(const Scalar* value, const size_t len) {
debugMod1(NAME, "addBlocking(%d)", len);
for (size_t i = 0; i < len; ++i) {
addBlocking(value[i]);
}
debugMod1(NAME, "used: %d", used);
}
// /** add multiple values to the buffer. blocks until space is available */
// void addBlocking(const Scalar* value, const size_t len) {
// debugMod1(NAME, "addBlocking(%d)", len);
// for (size_t i = 0; i < len; ++i) {
// addBlocking(value[i]);
// }
// debugMod1(NAME, "used: %d", used);
// }
/** add multiple values to the buffer, if there is enough space */
void addOrIgnore(const Scalar* value, const size_t len) {
debugMod1(NAME, "addOrIgnore(%d)", len);
for (size_t i = 0; i < len; ++i) {
addOrIgnore(value[i]);
}
debugMod1(NAME, "used: %d", used);
}
// /** add multiple values to the buffer, if there is enough space */
// void addOrIgnore(const Scalar* value, const size_t len) {
// debugMod1(NAME, "addOrIgnore(%d)", len);
// for (size_t i = 0; i < len; ++i) {
// addOrIgnore(value[i]);
// }
// debugMod1(NAME, "used: %d", used);
// }
/** add multiple values to the buffer, if there is enough space */
void addOrCallback(const Scalar* value, const size_t len, RingBufferFullCallback cb) {
@@ -86,6 +87,18 @@ public:
debugMod1(NAME, "used: %d", used);
}
/** add as many entries as possible */
size_t addOrIgnore(const Scalar* value, const size_t len) {
const size_t toAdd = min(len, getNumFree());
for (size_t i = 0; i < toAdd; ++i) {
data[head] = value[i];
head = (head + 1) % size;
}
used += toAdd;
debugMod3(NAME, "addOrIgnore(%d) added %d bytes. used: %d", len, toAdd, used);
return toAdd;
}
// /** anything available? */
// bool hasNext() const {
// return used != 0;
@@ -99,7 +112,7 @@ public:
return res;
}
const Scalar& getConst() const {
const Scalar& getConst() {
const uint8_t idx = tail;
tail = (tail + 1) % size;
--used;