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

110 lines
3.2 KiB
C++
Raw Normal View History

2025-01-25 10:55:11 +01:00
#include <stdint.h>
#include <cmath>
#include <cstdio>
2025-01-25 15:08:53 +01:00
#include "ws2812b.h"
2025-01-25 10:55:11 +01:00
extern "C" {
2025-01-25 15:08:53 +01:00
extern void test (void);
2025-01-25 10:55:11 +01:00
extern void print_table (void);
extern double ComputeRed (double);
extern double ComputeGreen (double);
extern double ComputeBlue (double);
};
2025-01-25 15:08:53 +01:00
union MColor {
2025-01-25 10:55:11 +01:00
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;
2025-01-25 15:08:53 +01:00
explicit MColor (const uint8_t r, const uint8_t g, const uint8_t b) : c(r, g, b) {}
2025-01-25 10:55:11 +01:00
};
#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);
}
2025-01-25 15:08:53 +01:00
static MColor getColor (const int index) {
2025-01-25 10:55:11 +01:00
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);
2025-01-25 15:08:53 +01:00
MColor color(r, g, b);
2025-01-25 10:55:11 +01:00
return color;
}
#endif
double ComputeRed (double x) {
2025-01-25 15:08:53 +01:00
MColor c = getColor(x);
2025-01-25 10:55:11 +01:00
return double (c.c.r);
}
double ComputeGreen (double x) {
2025-01-25 15:08:53 +01:00
MColor c = getColor(x);
2025-01-25 10:55:11 +01:00
return double (c.c.g);
}
double ComputeBlue (double x) {
2025-01-25 15:08:53 +01:00
MColor c = getColor(x);
2025-01-25 10:55:11 +01:00
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");
2025-01-25 15:08:53 +01:00
const MColor c = getColor(n);
2025-01-25 10:55:11 +01:00
fprintf(out, " 0x%06X,", c.number);
}
fprintf(out, "\n};\n");
fclose(out);
}
2025-01-25 15:08:53 +01:00
static constexpr unsigned PADDING = 10 * sizeof (Color);
static constexpr unsigned LEDS_LEN = NUMLEDS * sizeof (Color);
static constexpr unsigned FULL_LEN = PADDING + LEDS_LEN;
static uint8_t buffer [FULL_LEN];
void test () {
printf ("led color size = %zd\n", sizeof (Color));
for (unsigned n=0; n<FULL_LEN; n++) buffer[n] = 0;
Color * const cptr = reinterpret_cast<Color*>(buffer + PADDING);
for (unsigned n=0; n<NUMLEDS; n++) {
Color & c = cptr [n];
const OneColor cb (0xff), cg (0x55), cr (0x00);
c.b = cb; c.g = cg; c.r = cr;
}
const unsigned mlen = 1024;
char text [mlen];
for (unsigned n=0; n<NUMLEDS; n++) {
Color & c = cptr [n];
unsigned k = 0u;
k += c.g.to_string(text + k);
text [k++] = '|';
k += c.r.to_string(text + k);
text [k++] = '|';
k += c.b.to_string(text + k);
printf("led %d => %s\n", n, text);
}
printf("buffer dump :");
for (unsigned n=0; n<FULL_LEN; n++) {
if ((n%10) == 0) printf("\n");
printf("<%02x>", buffer[n]);
}
printf("\n");
}