added data structures
added audio support added rfid support added spi support
This commit is contained in:
62
data/Bitstream.h
Normal file
62
data/Bitstream.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef BITSTREAM_H
|
||||
#define BITSTREAM_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
template <int maxBytes> class Bitstream {
|
||||
|
||||
public:
|
||||
|
||||
inline void reset() {
|
||||
bitMask = 128; // start with the MSB
|
||||
curIdx = 0; // start with the first byte
|
||||
buffer[curIdx] = 0; // initialize all bits with zero
|
||||
numBitsUsed = 0; // nothing added
|
||||
}
|
||||
|
||||
inline void addOne() {
|
||||
buffer[curIdx] |= (0xFF & bitMask);
|
||||
bitMask >>= 1;
|
||||
++numBitsUsed;
|
||||
checkNextByte();
|
||||
}
|
||||
|
||||
inline void addZero() {
|
||||
bitMask >>= 1;
|
||||
++numBitsUsed;
|
||||
checkNextByte();
|
||||
}
|
||||
|
||||
const uint8_t* getData() const {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/** number of used bits */
|
||||
inline uint16_t getNumBits() const {
|
||||
return numBitsUsed;
|
||||
}
|
||||
|
||||
/** number of used bytes. rounded-up to multiples of 8-bits */
|
||||
inline uint8_t getNumBytes() const {
|
||||
return (numBitsUsed - 1) / 8 + 1;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/** received 8 bits? byte complete? -> next one */
|
||||
inline void checkNextByte() {
|
||||
if(bitMask == 0) {
|
||||
bitMask = 128; // start with the MSB
|
||||
++curIdx; // next byte
|
||||
buffer[curIdx] = 0; // initialize all bits with zero
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t numBitsUsed = 0;
|
||||
uint8_t buffer[maxBytes]; //buffer containing received bits
|
||||
uint8_t curIdx = 0; //index pointing to the byte currently selected within the buffer
|
||||
uint8_t bitMask = 128; //mask for the next bit to write
|
||||
|
||||
};
|
||||
|
||||
#endif // BITSTREAM_H
|
||||
70
data/DoubleBuffer.h
Normal file
70
data/DoubleBuffer.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef DOUBLEBUFFER_H
|
||||
#define DOUBLEBUFFER_H
|
||||
|
||||
template <typename Scalar, int size> class DoubleBuffer {
|
||||
|
||||
private:
|
||||
|
||||
Scalar data[size*2];
|
||||
Scalar* ptr1;
|
||||
Scalar* ptr2;
|
||||
|
||||
volatile uint32_t head;
|
||||
volatile bool _needsFlush;
|
||||
|
||||
public:
|
||||
|
||||
void init() {
|
||||
ptr1 = &data[0];
|
||||
ptr2 = &data[size];
|
||||
head = 0;
|
||||
_needsFlush = false;
|
||||
}
|
||||
|
||||
/** add new values to buffer A. swaps A and B if A is full */
|
||||
void add(const Scalar value) {
|
||||
|
||||
ptr1[head] = value; // ptr1
|
||||
++head;
|
||||
|
||||
if (head >= size) {
|
||||
head = 0;
|
||||
swap();
|
||||
_needsFlush = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** get data from buffer B */
|
||||
const Scalar* getData() const {
|
||||
return ptr2;
|
||||
}
|
||||
|
||||
/** true if the buffer is currently empty */
|
||||
bool isEmpty() const {
|
||||
return head == 0;
|
||||
}
|
||||
|
||||
bool needsFlush() const {
|
||||
return _needsFlush;
|
||||
}
|
||||
|
||||
void markFlushed() {
|
||||
_needsFlush = false;
|
||||
}
|
||||
|
||||
uint32_t getSize() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void swap() {
|
||||
Scalar* tmp = ptr1;
|
||||
ptr1 = ptr2;
|
||||
ptr2 = tmp;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // DOUBLEBUFFER_H
|
||||
59
data/LinearBuffer.h
Normal file
59
data/LinearBuffer.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef LINEARBUFFER_H
|
||||
#define LINEARBUFFER_H
|
||||
|
||||
template <typename Scalar, int _size> class LinearBuffer {
|
||||
|
||||
private:
|
||||
|
||||
Scalar _data[_size];
|
||||
|
||||
uint16_t head = 0;
|
||||
|
||||
public:
|
||||
|
||||
void add(const Scalar value) {
|
||||
if (head >= _size) {return;}
|
||||
_data[head] = value;
|
||||
++head;
|
||||
}
|
||||
|
||||
/** get the number of used entries */
|
||||
uint16_t getNumUsed() const {
|
||||
return head;
|
||||
}
|
||||
|
||||
/** get the number of used bytes */
|
||||
uint32_t getBytesUsed() const {
|
||||
return getNumUsed() * sizeof(Scalar);
|
||||
}
|
||||
|
||||
uint16_t size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
/** set the buffer to empty */
|
||||
void reset() {
|
||||
head = 0;
|
||||
}
|
||||
|
||||
const Scalar* data() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
/** constant array access */
|
||||
const Scalar& operator [] (const size_t idx) const {
|
||||
return _data[idx];
|
||||
}
|
||||
|
||||
/** true if the buffer is currently empty */
|
||||
bool isEmpty() const {
|
||||
return head == 0;
|
||||
}
|
||||
|
||||
bool isFull() const {
|
||||
return head >= size;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // LINEARBUFFER_H
|
||||
79
data/RingBuffer.h
Normal file
79
data/RingBuffer.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#ifndef RINGBUFFER_H
|
||||
#define RINGBUFFER_H
|
||||
|
||||
template <typename Scalar, int size> class RingBuffer {
|
||||
|
||||
private:
|
||||
|
||||
Scalar data[size];
|
||||
|
||||
volatile size_t head;
|
||||
volatile size_t tail;
|
||||
volatile size_t used;
|
||||
|
||||
public:
|
||||
|
||||
void init() {
|
||||
head = 0;
|
||||
tail = 0;
|
||||
used = 0;
|
||||
}
|
||||
|
||||
/** add one value to the buffer */
|
||||
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 */
|
||||
void addIgnore(const Scalar value) {
|
||||
|
||||
if (used == size) {return;}
|
||||
|
||||
data[head] = value;
|
||||
head = (head + 1) % size;
|
||||
++used;
|
||||
}
|
||||
|
||||
/** add multiple values to the buffer */
|
||||
void add(const Scalar* value, const size_t len) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
add(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/** anything available? */
|
||||
bool hasNext() const {
|
||||
return used != 0;
|
||||
}
|
||||
|
||||
Scalar get() {
|
||||
const Scalar res = data[tail];
|
||||
tail = (tail + 1) % size;
|
||||
--used;
|
||||
return res;
|
||||
}
|
||||
|
||||
const Scalar& getConst() {
|
||||
const uint8_t idx = tail;
|
||||
tail = (tail + 1) % size;
|
||||
--used;
|
||||
return data[idx];
|
||||
}
|
||||
|
||||
// /** true if the buffer is currently empty */
|
||||
// bool isEmpty() const {
|
||||
// return head == tail;
|
||||
// }
|
||||
|
||||
size_t getNumUsed() const {
|
||||
return used;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // RINGBUFFER_H
|
||||
Reference in New Issue
Block a user