RISC-V/V203F6P6/ws2812b/linux/main.cpp

75 lines
2.1 KiB
C++
Raw Normal View History

2025-01-25 10:55:11 +01:00
#include <stdint.h>
#include <cmath>
#include <cstdio>
extern "C" {
extern void print_table (void);
extern double ComputeRed (double);
extern double ComputeGreen (double);
extern double ComputeBlue (double);
};
union Color {
struct _c {
uint8_t g, r, b, a;
explicit _c (const uint8_t _r, const uint8_t _g, const uint8_t _b) noexcept :
g(_g), r(_r), b(_b), a(0u) {}
} c;
uint32_t number;
explicit Color (const uint8_t r, const uint8_t g, const uint8_t b) : c(r, g, b) {}
};
#if 0
static Color getColor (const int index) {
const double fi = 2.0 * M_PI / 3.0; // úhel posunu argumentu barvy
const double ph = M_PI / 128.0;
double a1, a2, a3;
uint8_t r,g,b; // proměnné barvy
// výpočet barvy - duha
a1 = (double) index * ph; // argument pro r
a2 = a1 + fi; // argument pro g
a3 = a2 + fi; // argument pro b
r = (int)(127.0 + 127.0*sin(a1));
g = (int)(127.0 + 127.0*sin(a2));
b = (int)(127.0 + 127.0*sin(a3));
Color color(r, g, b);
return color;
}
#else
static uint8_t Gauss (const double x, const double x0) {
const double sigma = 1.0 / 2048.0, arg = x - x0;
double r = 256.0 * exp (-sigma * arg * arg);
return uint8_t (r);
}
static Color getColor (const int index) {
uint8_t r=0,g=0,b=0; // proměnné barvy
r = Gauss(index, 85.333);
g = Gauss(index, 170.67);
b = Gauss(index, 0.01) + Gauss(index, 256.0);
Color color(r, g, b);
return color;
}
#endif
double ComputeRed (double x) {
Color c = getColor(x);
return double (c.c.r);
}
double ComputeGreen (double x) {
Color c = getColor(x);
return double (c.c.g);
}
double ComputeBlue (double x) {
Color c = getColor(x);
return double (c.c.b);
}
void print_table () {
FILE * out = fopen("../table.c","w");
if (!out) return;
fprintf(out, "const unsigned table [] = {");
for (unsigned n=0u; n<256u; n++) {
if ((n%8) == 0u) fprintf(out, "\n");
const Color c = getColor(n);
fprintf(out, " 0x%06X,", c.number);
}
fprintf(out, "\n};\n");
fclose(out);
}