Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rolling buffer #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/helper.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ template <class T>
struct enableIf<true, T> {
using type = T;
};

template< typename T >
struct always_false {
enum { value = false };
};
127 changes: 127 additions & 0 deletions src/rolling_buffer.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#pragma once

#include "Arduino.h"

template <class T, size_t S>
class RollingBufferBase_ {
protected:
RollingBufferBase_() : _write_idx(0), _read_idx(0), _cnt(0), _dropped_cnt(0) {
// _mutex = xSemaphoreCreateMutexStatic(&_xMutexBuffer);
}

public:
void push(const T &value) {
// xSemaphoreTake(_mutex, portMAX_DELAY);

_items[_write_idx] = value;

_write_idx++;
if (_write_idx >= S) {
_write_idx = 0;
}

const bool HASFREESLOTS = (_cnt < S);
if (HASFREESLOTS) {
_cnt++;
} else {
_dropped_cnt++;
_read_idx++;
if (_read_idx >= S) {
_read_idx = 0;
}
}

// xSemaphoreGive(_mutex);
}

const T *pop() {
if (_cnt > 0) {
const T *item = &_items[_read_idx];

_read_idx++;
if (_read_idx >= S) {
_read_idx = 0;
}
_cnt--;

return item;
} else {
return NULL;
}
}

public:
size_t begin_read() {
// xSemaphoreTake(_mutex, portMAX_DELAY);
return _cnt;
}

void end_read() {
// xSemaphoreGive(_mutex);
}

const size_t capacity = S;

size_t count() {
// xSemaphoreTake(_mutex, portMAX_DELAY);
size_t cnt = _cnt;
// xSemaphoreGive(_mutex);
return cnt;
}

size_t dropped() {
// xSemaphoreTake(_mutex, portMAX_DELAY);
size_t cnt = _dropped_cnt;
// xSemaphoreGive(_mutex);
return cnt;
}

protected:
size_t _write_idx;
size_t _read_idx;
size_t _cnt;
size_t _dropped_cnt;

T _items[S];

// StaticSemaphore_t _xMutexBuffer;
// SemaphoreHandle_t _mutex;
};

template <class T, size_t S>
class RollingBuffer : public RollingBufferBase_<T, S> {
public:
RollingBuffer() : RollingBufferBase_<T,S>() {
}
};

template <size_t S>
class RollingBuffer<String, S> : public RollingBufferBase_<String, S>
{
public:
RollingBuffer() : RollingBufferBase_<String, S>() {
}

void push_cstr(const char *value) {
// xSemaphoreTake(this->_mutex, portMAX_DELAY);

this->_items[this->_write_idx] = value;

this->_write_idx++;
if (this->_write_idx >= S) {
this->_write_idx = 0;
}

const bool HASFREESLOTS = (this->_cnt < S);
if (HASFREESLOTS) {
this->_cnt++;
} else {
this->_dropped_cnt++;
this->_read_idx++;
if (this->_read_idx >= S)
this->_read_idx = 0;
}

// xSemaphoreGive(this->_mutex);
}
};
42 changes: 42 additions & 0 deletions src/signature.tcc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "types.tcc"
#include "helper.tcc"

//! \defgroup signature

Expand Down Expand Up @@ -63,6 +64,47 @@ void signature(Stream& io, T (*)(Ts...)) {
parameterTypes_(io, f_);
}

// specialization for reference return types
template <class T, class... Ts>
void signature(Stream& io, T& (*)(Ts...)) {
/*
* A dummy function pointer is prepared, referred to as `f_` in the template
* functions above, which will be used to isolate parameter types. The return
* type of this function pointer is removed to avoid unneeded template
* expansion.
*/
T data {};
rpcTypeOf(io, data);
rpcPrint(io, ":");
void (*f_)(Ts...) {};
parameterTypes_(io, f_);
}

template <class F, size_t S, class... Ts>
void signature(Stream& io, const RollingBuffer<F, S>& (*f)(Ts...))
{
static_assert(always_false<F>::value, "Don't use const qualifier for RollingBuffer<F,S> as return type. use RollingBuffer<F,S>& fun(...)");
}

template <class F, size_t S, class... Ts>
void signature(Stream& io, RollingBuffer<F, S>* (*f)(Ts...))
{
static_assert(always_false<F>::value, "Return RollingBuffer<F,S> only as a reference type: RollingBuffer<F,S>& fun(...)");
}

template <class F, size_t S, class... Ts>
void signature(Stream& io, const RollingBuffer<F, S>* (*f)(Ts...))
{
static_assert(always_false<F>::value, "Return RollingBuffer<F,S> only as a reference type: RollingBuffer<F,S>& fun(...)");
}

template <class F, size_t S, class... Ts>
void signature(Stream& io, RollingBuffer<F, S> (*f)(Ts...))
{
static_assert(always_false<F>::value, "Return RollingBuffer<F,S> only as a reference type: RollingBuffer<F,S>& fun(...)");
}


/*! \ingroup signature
* \copydoc signature(Stream&, T (*)(Ts...)) */
template <class T, class C, class... Ts>
Expand Down
10 changes: 10 additions & 0 deletions src/types.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "tuple.tcc"
#include "vector.tcc"
#include "array.tcc"
#include "rolling_buffer.tcc"

//! \defgroup types

Expand Down Expand Up @@ -170,6 +171,7 @@ void rpcTypeOf(Stream& io, Vector<T>&) {
template <class T, size_t n>
void rpcTypeOf(Stream& io, Array<T, n>&) {
rpcPrint(io, '[');
rpcPrint(io, '#');
size_t n_ {n};
rpcPrint(io, n_);
T x {};
Expand All @@ -187,6 +189,14 @@ void rpcTypeOf(Stream& io, T*) {
rpcPrint(io, ']');
}

template<class T, size_t n>
void rpcTypeOf(Stream& io, RollingBuffer<T, n>&) {
rpcPrint(io, '[');
T x {};
rpcTypeOf(io, x);
rpcPrint(io, ']');
}

// TODO: References to arrays can be returned, e.g., int (&test())[10] {}


Expand Down
14 changes: 14 additions & 0 deletions src/write.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "tuple.tcc"
#include "vector.tcc"
#include "array.tcc"
#include "rolling_buffer.tcc"

//! \defgroup write

Expand Down Expand Up @@ -61,6 +62,19 @@ void rpcWrite(Stream& io, Array<T, n>* data) {
}
}

/*! \ingroup write
* \copydoc rpcWrite(Stream&, T*) */
template <class T, size_t S>
void rpcWrite(Stream& io, RollingBuffer<T, S>* data)
{
size_t size = data->begin_read();
rpcWrite(io, &size);
for (size_t i = 0; i < size; i++)
{
rpcWrite(io, (T*)data->pop());
}
data->end_read();
}

//! Recursion terminator for `rpcWrite(Tuple*)()`.
inline void rpcWrite(Stream&, Tuple<>*) {}
Expand Down