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

99
data/Color.h Normal file
View File

@@ -0,0 +1,99 @@
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;
}
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);
}
};

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;