minor changes, added ALL required externals

This commit is contained in:
2021-03-14 15:20:44 +01:00
parent 3da4722748
commit 5e8ba82e6e
22 changed files with 1853 additions and 30 deletions

View 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

View File

@@ -0,0 +1,116 @@
#ifndef ESP_COLOR_H
#define ESP_COLOR_H
#include <cstdint>
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
/** no-init */
Color() {
;
}
private:
/** Hidden ctor. RGB */
Color(const uint8_t r, const uint8_t g, const uint8_t b) : r(r), g(g), b(b) {
;
}
public:
/** get X-percent [0:100] of this color = darker/brighter */
Color inline getPercent(const int percent) const {
return Color(r*percent/100, g*percent/100, b*percent/100);
}
/** mix the two given colors */
static Color mix(const Color c1, const Color c2, int percentC1) {
return Color(
((c1.r * percentC1) + (c2.r * (100-percentC1))) / 100,
((c1.g * percentC1) + (c2.g * (100-percentC1))) / 100,
((c1.b * percentC1) + (c2.b * (100-percentC1))) / 100
);
}
static inline Color fromRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
return Color(r,g,b);
}
static inline Color fromHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
Color c; c.setHSV(h,s,v); return c;
}
/** get color with new brightness (0..255) */
Color brightness(const uint8_t brightness) const {
return Color(
((uint16_t)r)*brightness/255,
((uint16_t)g)*brightness/255,
((uint16_t)b)*brightness/255
);
}
void setHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
uint8_t region, remainder, p, q, t;
region = h / 43;
remainder = (h - (region * 43)) * 6;
p = (v * (255 - s)) >> 8;
q = (v * (255 - ((s * remainder) >> 8))) >> 8;
t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
switch (region) {
case 0:
r = v; g = t; b = p;
break;
case 1:
r = q; g = v; b = p;
break;
case 2:
r = p; g = v; b = t;
break;
case 3:
r = p; g = q; b = v;
break;
case 4:
r = t; g = p; b = v;
break;
default:
r = v; g = p; b = q;
break;
}
}
void setRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
this->r = r;
this->g = g;
this->b = b;
}
void setOff() {
this->r = 0;
this->g = 0;
this->b = 0;
}
/** add two colors */
Color operator + (const Color o) const {
return Color(r+o.r, g+o.g, b+o.g);
}
Color operator * (const float f) const {
return Color(r*f, g*f, b*f);
}
};
#endif // ESP_COLOR_H

View 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

View 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

View 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