This commit is contained in:
Jeff Epler 2024-12-13 11:36:13 -06:00
parent 0b884f3daa
commit c5b94477dc

View file

@ -73,25 +73,8 @@ static inline void protomatter_program_init(PIO pio, int sm, uint offset) {
}
constexpr uint16_t gamma_lut[256] = {
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4,
4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 28, 29, 30, 32, 33, 35,
36, 38, 39, 41, 43, 44, 46, 48, 49, 51, 53, 55, 57, 59, 60, 62,
64, 66, 69, 71, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 96, 98,
101, 103, 106, 108, 111, 114, 116, 119, 122, 125, 127, 130, 133, 136, 139, 142,
145, 148, 151, 154, 157, 160, 164, 167, 170, 173, 177, 180, 184, 187, 190, 194,
197, 201, 204, 208, 212, 215, 219, 223, 227, 230, 234, 238, 242, 246, 250, 254,
258, 262, 266, 270, 274, 278, 282, 287, 291, 295, 300, 304, 308, 313, 317, 322,
326, 331, 335, 340, 345, 349, 354, 359, 363, 368, 373, 378, 383, 388, 393, 398,
403, 408, 413, 418, 423, 428, 434, 439, 444, 449, 455, 460, 465, 471, 476, 482,
487, 493, 498, 504, 510, 515, 521, 527, 533, 538, 544, 550, 556, 562, 568, 574,
580, 586, 592, 598, 604, 611, 617, 623, 629, 636, 642, 648, 655, 661, 668, 674,
681, 687, 694, 700, 707, 714, 720, 727, 734, 741, 748, 755, 761, 768, 775, 782,
789, 796, 804, 811, 818, 825, 832, 839, 847, 854, 861, 869, 876, 884, 891, 899,
906, 914, 921, 929, 937, 944, 952, 960, 968, 975, 983, 991, 999,1007,1015,1023,
};
constexpr uint32_t rgb(unsigned r, unsigned g, unsigned b) {
uint16_t gamma_lut[256];
uint32_t rgb(unsigned r, unsigned g, unsigned b) {
assert(r < 256);
assert(g < 256);
assert(b < 256);
@ -102,9 +85,9 @@ constexpr uint32_t rgb(unsigned r, unsigned g, unsigned b) {
#define ACROSS (32)
#define DOWN (16)
#define _ (0)
#define r rgb(255,0,0)
#define g rgb(0,255,0)
#define b rgb(0,0,255)
#define r (1023 << 20)
#define g (1023 << 10)
#define b (1023)
#define y (r|g)
#define c (g|b)
#define m (r|b)
@ -148,9 +131,9 @@ constexpr uint32_t oe_bit = 1u << PIN_OE;
constexpr uint32_t oe_active = 0;
constexpr uint32_t oe_inactive = oe_bit;
constexpr uint32_t post_oe_delay = 10;
constexpr uint32_t post_latch_delay = 17;
constexpr uint32_t post_addr_delay = 20;
constexpr uint32_t post_oe_delay = 0;
constexpr uint32_t post_latch_delay = 0;
constexpr uint32_t post_addr_delay = 5000;
uint32_t colorwheel(int i) {
i = i & 0xff;
@ -165,7 +148,7 @@ uint32_t colorwheel(int i) {
return rgb(i * 3, 255 - i * 3, 0);
}
std::vector<uint32_t> test_pattern(int offs, int brightness) {
std::vector<uint32_t> test_pattern(int offs) {
std::vector<uint32_t> result;
uint32_t time=0;
@ -179,7 +162,7 @@ std::vector<uint32_t> test_pattern(int offs, int brightness) {
pixels[15][i] = colorwheel(2*i+192 + offs);
}
if(1) {
if(0) {
memset(pixels, 0, sizeof(pixels));
for(int i=0; i<DOWN; i++) {
pixels[i][2*i] = rgb(255,0,0);
@ -229,18 +212,17 @@ pixels[i][2*i] = rgb(255,0,0);
int last_bit = 0;
int prev_addr = 7;
for(int bit = 7; bit >= 0; bit--) {
for(int bit = 9; bit >= 0; bit--) {
uint32_t r = 1 << (20 + 2 + bit);
uint32_t g = 1 << (10 + 2 + bit);
uint32_t b = 1 << (2 + bit);
uint32_t r = 1 << (20 + bit);
uint32_t g = 1 << (10 + bit);
uint32_t b = 1 << (0 + bit);
for(int addr = 0; addr < 8; addr++) {
uint32_t desired_duration = base_active_time << last_bit;
uint32_t desired_duration = (base_active_time << last_bit) / ACROSS;
last_bit = bit;
uint32_t t_start = time;
printf("addr=%d prev_addr=%d\n", addr, prev_addr);
// illuminate the right row for data in the shift register (the previous address)
uint32_t addr_bits = calc_addr_bits(prev_addr);
prev_addr = addr;
@ -255,7 +237,7 @@ printf("addr=%d prev_addr=%d\n", addr, prev_addr);
auto g1 = pixel1 & g;
auto b1 = pixel1 & b;
add_pixels(addr_bits, r0, g0, b0, r1, g1, b1, across < brightness);
add_pixels(addr_bits, r0, g0, b0, r1, g1, b1, (time - t_start) < desired_duration);
}
uint32_t t_end = time;
@ -274,15 +256,26 @@ printf("addr=%d prev_addr=%d\n", addr, prev_addr);
do_data(addr_bits | oe_inactive, post_addr_delay);
}
}
// at end of frame set oe inactive
do_data(oe_inactive | calc_addr_bits(prev_addr), 0);
//printf("Time %d (%.1f us)\n", time, time*1e6/clock_get_hz(clk_sys));
return result;
}
void make_gamma_lut(double exponent) {
for(int i=0; i<256; i++) {
auto v = std::max(i, int(round(1023 * pow(i / 255, exponent))));
gamma_lut[i] = v;
}
}
int main(int argc, char **argv) {
PIO pio = pio0;
int sm = pio_claim_unused_sm(pio, true);
make_gamma_lut(3.2);
printf("clock %fMHz\n", clock_get_hz(clk_sys)/1e6);
uint offset = pio_add_program(pio, &protomatter_program);
@ -294,19 +287,9 @@ printf("clock %fMHz\n", clock_get_hz(clk_sys)/1e6);
protomatter_program_init(pio, sm, offset);
pio_sm_set_clkdiv(pio, sm, 1.0);
std::vector<uint32_t> data = test_pattern(0, 33);
FILE *f = fopen("pattern.txt", "w");
fprintf(f, "[\n");
for(auto i: data) { fprintf(f, "0x%08x,\n", i); }
fprintf(f, "]\n");
fclose(f);
int n = argc > 1 ? atoi(argv[1]) : 1;
for(int i=0; i<n; i++) {
float f = (1 + sin(i / 100.)) / 2;
int b = int(f * 32);
printf("%d\n", b);
std::vector<uint32_t> data = test_pattern(i, 16);
std::vector<uint32_t> data = test_pattern(i);
uint32_t *databuf = &data[0];
size_t datasize = data.size() * sizeof(uint32_t);