edit test

This commit is contained in:
Kizarm 2025-01-25 15:08:53 +01:00
parent 831931a37d
commit ab5a62a3f1
5 changed files with 60 additions and 19 deletions

View file

@ -1,9 +1,10 @@
CC = gcc
CX = g++
LD = ld
CFLAGS = -c -Os -fPIC
VPATH = . ..
CFLAGS = -c -Os -fPIC -I../ -I../common
OBJS = main.o
OBJS = main.o ws2812b.o
SLIB = graph.so
all: $(SLIB)

View file

@ -5,6 +5,7 @@ import cffi
import numpy as np
import matplotlib.pyplot as plt
c_header = '''
void test (void);
void print_table (void);
double ComputeRed (double);
double ComputeGreen (double);
@ -25,16 +26,17 @@ def plot_colors(C):
plt.grid()
plt.ylabel('Intenzity')
plt.xlabel('N')
#plt.savefig('color.png')
plt.show()
plt.savefig('color.png')
#plt.show()
############################ MAIN ##############################################################
def main_func():
ffi = cffi.FFI()
ffi.cdef(c_header)
C = ffi.dlopen("./graph.so")
C.test()
plot_colors(C)
#C.print_table()
C.print_table()
ffi.dlclose(C)
return True
if __name__ == '__main__':

View file

@ -1,20 +1,22 @@
#include <stdint.h>
#include <cmath>
#include <cstdio>
#include "ws2812b.h"
extern "C" {
extern void test (void);
extern void print_table (void);
extern double ComputeRed (double);
extern double ComputeGreen (double);
extern double ComputeBlue (double);
};
union Color {
union MColor {
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) {}
explicit MColor (const uint8_t r, const uint8_t g, const uint8_t b) : c(r, g, b) {}
};
#if 0
static Color getColor (const int index) {
@ -38,25 +40,25 @@ static uint8_t Gauss (const double x, const double x0) {
double r = 256.0 * exp (-sigma * arg * arg);
return uint8_t (r);
}
static Color getColor (const int index) {
static MColor 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);
MColor color(r, g, b);
return color;
}
#endif
double ComputeRed (double x) {
Color c = getColor(x);
MColor c = getColor(x);
return double (c.c.r);
}
double ComputeGreen (double x) {
Color c = getColor(x);
MColor c = getColor(x);
return double (c.c.g);
}
double ComputeBlue (double x) {
Color c = getColor(x);
MColor c = getColor(x);
return double (c.c.b);
}
@ -66,9 +68,42 @@ void print_table () {
fprintf(out, "const unsigned table [] = {");
for (unsigned n=0u; n<256u; n++) {
if ((n%8) == 0u) fprintf(out, "\n");
const Color c = getColor(n);
const MColor c = getColor(n);
fprintf(out, " 0x%06X,", c.number);
}
fprintf(out, "\n};\n");
fclose(out);
}
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");
}

View file

@ -2,13 +2,13 @@ const unsigned table [] = {
0xFF0700, 0xFF0700, 0xFF0800, 0xFE0900, 0xFE0A00, 0xFC0A00, 0xFB0B00, 0xF90C00,
0xF80D00, 0xF60E00, 0xF31000, 0xF11100, 0xEE1200, 0xEB1300, 0xE81500, 0xE51600,
0xE11800, 0xDE1A00, 0xDA1B00, 0xD61D00, 0xD21F00, 0xCE2100, 0xCA2400, 0xC52600,
0xC12800, 0xBC2B00, 0xB82D00, 0xB33000, 0xAE3300, 0xA93600, 0xA43900, 0xA03C00,
0x9B3F00, 0x964300, 0x914600, 0x8C4A00, 0x874E00, 0x835100, 0x7E5500, 0x795900,
0xC12800, 0xBC2B00, 0xB82D00, 0xB33000, 0xAE3300, 0xA93600, 0xA53900, 0xA03C00,
0x9B3F00, 0x964300, 0x914600, 0x8C4A00, 0x884E00, 0x835100, 0x7E5500, 0x795900,
0x755D00, 0x706200, 0x6C6600, 0x676A00, 0x636F00, 0x5F7300, 0x5B7800, 0x577C00,
0x538100, 0x4F8600, 0x4B8B00, 0x478F00, 0x449400, 0x409900, 0x3D9E00, 0x3AA300,
0x37A800, 0x34AC00, 0x31B100, 0x2EB600, 0x2CBB00, 0x29BF00, 0x27C400, 0x24C800,
0x22CD00, 0x20D101, 0x1ED501, 0x1CD901, 0x1ADD01, 0x19E001, 0x17E401, 0x15E702,
0x14EA02, 0x12ED02, 0x11F002, 0x10F302, 0x0FF503, 0x0EF703, 0x0DF903, 0x0CFB04,
0x22CC00, 0x20D101, 0x1ED501, 0x1CD901, 0x1ADD01, 0x19E001, 0x17E401, 0x15E702,
0x14EA02, 0x12ED02, 0x11F002, 0x10F202, 0x0FF503, 0x0EF703, 0x0DF903, 0x0CFB04,
0x0BFC04, 0x0AFD05, 0x09FE05, 0x08FF06, 0x08FF06, 0x07FF07, 0x06FF07, 0x06FF08,
0x05FF09, 0x05FE09, 0x04FD0A, 0x04FC0B, 0x04FA0C, 0x03F80D, 0x03F60E, 0x03F40F,
0x02F210, 0x02EF12, 0x02EC13, 0x02E914, 0x01E616, 0x01E317, 0x01DF19, 0x01DB1B,
@ -17,7 +17,7 @@ const unsigned table [] = {
0x008E49, 0x00894C, 0x008450, 0x008054, 0x007B58, 0x00765C, 0x007260, 0x006D64,
0x006969, 0x00646D, 0x006072, 0x005C76, 0x00587B, 0x005480, 0x005084, 0x004C89,
0x00498E, 0x004593, 0x004298, 0x003E9C, 0x003BA1, 0x0038A6, 0x0035AB, 0x0032B0,
0x002FB4, 0x002CB9, 0x002ABE, 0x0027C2, 0x0025C7, 0x0023CB, 0x0121CF, 0x011FD3,
0x002FB4, 0x002DB9, 0x002ABE, 0x0027C2, 0x0025C7, 0x0023CB, 0x0121CF, 0x011FD3,
0x011DD7, 0x011BDB, 0x0119DF, 0x0117E3, 0x0116E6, 0x0214E9, 0x0213EC, 0x0212EF,
0x0210F2, 0x030FF4, 0x030EF6, 0x030DF8, 0x040CFA, 0x040BFC, 0x040AFD, 0x0509FE,
0x0509FF, 0x0608FF, 0x0607FF, 0x0707FF, 0x0806FF, 0x0806FF, 0x0905FE, 0x0A05FD,

View file

@ -38,8 +38,11 @@ static constexpr unsigned FIFOLEN = 8u; // min. depth je 8, jinak mocnina 2 >=
* pořád, v 1. části dělá "reset", tedy časování rámce (vysílá nuly).
* V 2. části si vybere z fronty ring data a uloží je do buferu ve správném
* tvaru. Využívá se toho, že přerušení přijde na konci a naplnění daty
* netrvá dlouho, takže se přepisuje jen část, která se nevysílá.
* netrvá dlouho, takže se přepisuje jen část, která se právě nevysílá.
* Fronta byla použita (celkem zbytečně) protože zatím netuším jaká data posílat.
* NOTE
* Protože WS2812B je 5V záležitost a procesor je napájen jen 3.3V, funguje to
* na hraně a je dobré zapojit mezi MOSI a +5V rezistor 1k. Je to pak stabilnější.
* */
class ws2812b : public OneWay<uint8_t> {
FIFO<uint32_t,FIFOLEN> & ring;