mirror of https://github.com/FOME-Tech/fome-fw.git
Hellen says cyclic buffer
This commit is contained in:
parent
985bd2cfa7
commit
c2c4c4c816
|
@ -1813,6 +1813,10 @@ cmd_set_engine_type_default = "w\x00\x31\x00\x00"
|
||||||
field = "TLE8888 Chip Select", tle8888_cs
|
field = "TLE8888 Chip Select", tle8888_cs
|
||||||
field = "TLE8888 CS Mode", tle8888_csPinMode
|
field = "TLE8888 CS Mode", tle8888_csPinMode
|
||||||
field = "TLE 8888 spi", tle8888spiDevice
|
field = "TLE 8888 spi", tle8888spiDevice
|
||||||
|
field = "DRV8860 CS", drv8860_cs
|
||||||
|
field = "DRV8860 CS Mode", drv8860_csPinMode
|
||||||
|
field = "DRV8860 MISO pin", drv8860_miso
|
||||||
|
field = "DRV8860 SPI", drv8860spiDevice
|
||||||
field = "servo#1", servoOutputPins1
|
field = "servo#1", servoOutputPins1
|
||||||
field = "servo#2", servoOutputPins2
|
field = "servo#2", servoOutputPins2
|
||||||
field = "servo#3", servoOutputPins3
|
field = "servo#3", servoOutputPins3
|
||||||
|
|
|
@ -41,7 +41,7 @@ class cyclic_buffer
|
||||||
volatile T elements[maxSize];
|
volatile T elements[maxSize];
|
||||||
volatile int currentIndex;
|
volatile int currentIndex;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
void baseC(int size);
|
void baseC(int size);
|
||||||
/**
|
/**
|
||||||
* number of elements added into this buffer, would be eventually bigger then size
|
* number of elements added into this buffer, would be eventually bigger then size
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
/**
|
||||||
|
* @file fifo_buffer.h
|
||||||
|
* @brief A FIFO buffer (base on cyclic_buffer)
|
||||||
|
*
|
||||||
|
* https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)
|
||||||
|
*
|
||||||
|
* @date Aug 6, 2020
|
||||||
|
* @author andreika <prometheus.pcb@gmail.com>
|
||||||
|
* @author Andrey Belomutskiy
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FIFO_BUFFER_H
|
||||||
|
#define FIFO_BUFFER_H
|
||||||
|
|
||||||
|
#include "cyclic_buffer.h"
|
||||||
|
|
||||||
|
// todo: this is not a thread-safe version!
|
||||||
|
template<typename T, size_t maxSize = CB_MAX_SIZE>
|
||||||
|
class fifo_buffer : public cyclic_buffer<T, maxSize> {
|
||||||
|
public:
|
||||||
|
fifo_buffer() : currentIndexRead(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(T item);
|
||||||
|
T get();
|
||||||
|
void clear() /*override*/;
|
||||||
|
|
||||||
|
void put(const T *items, int numItems);
|
||||||
|
|
||||||
|
bool isEmpty() const {
|
||||||
|
return getCount() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isFull() const {
|
||||||
|
return getCount() >= getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
volatile int currentIndexRead; // FIFO "tail"
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void fifo_buffer<T, maxSize>::put(T item) {
|
||||||
|
// check if full
|
||||||
|
if (!isFull()) {
|
||||||
|
cyclic_buffer::add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void fifo_buffer<T, maxSize>::put(const T *items, int numItems) {
|
||||||
|
for (int i = 0; i < numItems; i++) {
|
||||||
|
put(items[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
T fifo_buffer<T, maxSize>::get() {
|
||||||
|
auto ret = elements[currentIndexRead];
|
||||||
|
if (!isEmpty()) {
|
||||||
|
currentIndexRead = (currentIndexRead + 1) % size;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void fifo_buffer<T, maxSize>::clear() {
|
||||||
|
cyclic_buffer::clear();
|
||||||
|
currentIndexRead = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* FIFO_BUFFER_H */
|
Loading…
Reference in New Issue