Merge pull request #2 from jepler/pre-commit-clang-format

let's format our source code consistently
This commit is contained in:
Jeff Epler 2025-01-14 09:45:37 -06:00 committed by GitHub
commit a2494b9e7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 297 additions and 186 deletions

2
.clang-format Normal file
View file

@ -0,0 +1,2 @@
BasedOnStyle: LLVM
IndentWidth: 4

60
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,60 @@
# To use:
#
# pre-commit run -a
#
# Or:
#
# pre-commit install # (runs every time you commit in git)
#
# To update this file:
#
# pre-commit autoupdate
#
# See https://github.com/pre-commit/pre-commit
ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"
repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
exclude: ^conda\.recipe/meta\.yaml$
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace
# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
files: ^(docs)
# Lints code
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.275"
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.1
hooks:
- id: remove-tabs
# Suggested hook if you add a .clang-format file
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v13.0.0
hooks:
- id: clang-format

View file

@ -11,7 +11,7 @@ def temporary_stdout(filename):
yield sys.stdout
finally:
sys.stdout = old_stdout
@click.command
@click.argument("infile")
@click.argument("outfile")
@ -19,7 +19,7 @@ def main(infile, outfile):
program_name = infile.rpartition("/")[2].partition(".")[0]
print(program_name)
program = adafruit_pioasm.Program.from_file(infile, build_debuginfo=True)
with temporary_stdout(outfile):
program.print_c_program(program_name)

View file

@ -12,28 +12,20 @@ struct buffer_manager {
free_buffers.push(2);
}
int get_free_buffer() {
return free_buffers.pop_blocking();
}
void put_free_buffer(int i) {
free_buffers.push(i);
}
int get_free_buffer() { return free_buffers.pop_blocking(); }
void put_free_buffer(int i) { free_buffers.push(i); }
int get_filled_buffer() {
auto r = filled_buffers.pop_nonblocking();
return r ? r.value() : no_buffer;
}
void put_filled_buffer(int i) {
filled_buffers.push(i);
}
void put_filled_buffer(int i) { filled_buffers.push(i); }
void request_exit() {
filled_buffers.push(exit_request);
}
void request_exit() { filled_buffers.push(exit_request); }
private:
private:
thread_queue<int> free_buffers, filled_buffers;
};
}
} // namespace piomatter

View file

@ -1,7 +1,7 @@
#pragma once
#include <vector>
#include <stdexcept>
#include <vector>
namespace piomatter {
@ -26,31 +26,23 @@ int orientation_cw(int width, int height, int x, int y) {
}
namespace {
template<typename Cb>
void submap(std::vector<int> &result,
int width, int height,
int start_x, int dx, int count_x_in,
int start_y, int dy, int count_y,
int half_panel_height,
const Cb &cb) {
template <typename Cb>
void submap(std::vector<int> &result, int width, int height, int start_x,
int dx, int count_x_in, int start_y, int dy, int count_y,
int half_panel_height, const Cb &cb) {
for(int y = start_y; count_y; count_y -= 2, y += dy ) {
for(int x = start_x, count_x = count_x_in; count_x--; x += dx )
{
result.push_back(cb(width, height, x,y));
result.push_back(cb(width, height, x,y+dy*half_panel_height));
}
for (int y = start_y; count_y; count_y -= 2, y += dy) {
for (int x = start_x, count_x = count_x_in; count_x--; x += dx) {
result.push_back(cb(width, height, x, y));
result.push_back(cb(width, height, x, y + dy * half_panel_height));
}
}
}
} // namespace
template<typename Cb>
matrix_map make_matrixmap(
int width,
int height,
int n_addr_lines,
bool serpentine,
const Cb &cb) {
template <typename Cb>
matrix_map make_matrixmap(int width, int height, int n_addr_lines,
bool serpentine, const Cb &cb) {
int panel_height = 2 << n_addr_lines;
if (height % panel_height != 0) {
@ -61,11 +53,11 @@ matrix_map make_matrixmap(
int v_panels = height / panel_height;
int pixels_across = width * v_panels;
matrix_map result;
result.reserve(width*height);
result.reserve(width * height);
for(int i=0; i<half_panel_height; i++) {
for(int j=0; j<pixels_across; j++) {
int panel_no = j / width;
for (int i = 0; i < half_panel_height; i++) {
for (int j = 0; j < pixels_across; j++) {
int panel_no = j / width;
int panel_idx = j % width;
int x, y0, y1;
@ -87,14 +79,20 @@ matrix_map make_matrixmap(
}
struct matrix_geometry {
template<typename Cb>
matrix_geometry(int pixels_across, int n_addr_lines, int n_planes, int width, int height, bool serpentine, const Cb &cb) : pixels_across(pixels_across), n_addr_lines(n_addr_lines), n_planes(n_planes), width(width), height(height), map{make_matrixmap(width, height, n_addr_lines, serpentine, cb)} {
int pixels_down = 2 << n_addr_lines;
if (map.size() != pixels_down * pixels_across) {
throw std::range_error("map size does not match calculated pixel count");
template <typename Cb>
matrix_geometry(int pixels_across, int n_addr_lines, int n_planes,
int width, int height, bool serpentine, const Cb &cb)
: pixels_across(pixels_across), n_addr_lines(n_addr_lines),
n_planes(n_planes), width(width),
height(height), map{make_matrixmap(width, height, n_addr_lines,
serpentine, cb)} {
int pixels_down = 2 << n_addr_lines;
if (map.size() != pixels_down * pixels_across) {
throw std::range_error(
"map size does not match calculated pixel count");
}
}
}
int pixels_across, n_addr_lines, n_planes, width, height;
matrix_map map;
};
}
} // namespace piomatter

View file

@ -22,4 +22,4 @@ struct adafruit_matrix_bonnet_pinout {
static constexpr uint32_t post_addr_delay = 500;
};
}
} // namespace piomatter

View file

@ -2,24 +2,26 @@
#include <thread>
#include "hardware/pio.h"
#include "hardware/pio.h"
#include "piomatter/pins.h"
#include "piomatter/buffer_manager.h"
#include "piomatter/render.h"
#include "piomatter/matrixmap.h"
#include "piomatter/pins.h"
#include "piomatter/protomatter.pio.h"
#include "piomatter/render.h"
namespace piomatter {
constexpr size_t MAX_XFER = 65532;
void pio_sm_xfer_data_large(PIO pio, int sm, int direction, size_t size, uint32_t *databuf) {
while(size) {
void pio_sm_xfer_data_large(PIO pio, int sm, int direction, size_t size,
uint32_t *databuf) {
while (size) {
size_t xfersize = std::min(size_t{MAX_XFER}, size);
int r = pio_sm_xfer_data(pio, sm, direction, xfersize, databuf);
if (r) {
throw std::runtime_error("pio_sm_xfer_data (reboot may be required)");
throw std::runtime_error(
"pio_sm_xfer_data (reboot may be required)");
}
size -= xfersize;
databuf += xfersize / sizeof(*databuf);
@ -31,10 +33,14 @@ struct piomatter_base {
virtual void show() = 0;
};
template<class pinout=adafruit_matrix_bonnet_pinout, class colorspace=colorspace_rgb888>
template <class pinout = adafruit_matrix_bonnet_pinout,
class colorspace = colorspace_rgb888>
struct piomatter : piomatter_base {
using buffer_type = std::vector<uint32_t>;
piomatter(std::span<typename colorspace::data_type const> framebuffer, const matrix_geometry &geometry) : framebuffer(framebuffer), geometry{geometry}, converter{}, blitter_thread{&piomatter::blit_thread, this} {
piomatter(std::span<typename colorspace::data_type const> framebuffer,
const matrix_geometry &geometry)
: framebuffer(framebuffer), geometry{geometry}, converter{},
blitter_thread{&piomatter::blit_thread, this} {
if (geometry.n_addr_lines > std::size(pinout::PIN_ADDR)) {
throw std::runtime_error("too many address lines requested");
}
@ -57,10 +63,10 @@ struct piomatter : piomatter_base {
pin_deinit_one(pinout::PIN_CLK);
pin_deinit_one(pinout::PIN_LAT);
for(const auto p : pinout::PIN_RGB)
for (const auto p : pinout::PIN_RGB)
pin_deinit_one(p);
for(size_t i=0; i<geometry.n_addr_lines; i++) {
for (size_t i = 0; i < geometry.n_addr_lines; i++) {
pin_deinit_one(pinout::PIN_ADDR[i]);
}
pio_sm_unclaim(pio, sm);
@ -71,14 +77,15 @@ struct piomatter : piomatter_base {
blitter_thread.join();
}
}
private:
private:
void program_init() {
pio = pio0;
sm = pio_claim_unused_sm(pio, true);
if (sm < 0) {
throw std::runtime_error("pio_claim_unused_sm");
}
int r = pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, MAX_XFER, 2);
int r = pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, MAX_XFER, 2);
if (r) {
throw std::runtime_error("pio_sm_config_xfer");
}
@ -90,7 +97,7 @@ private:
};
uint offset = pio_add_program(pio, &protomatter_program);
if (offset== PIO_ORIGIN_INVALID) {
if (offset == PIO_ORIGIN_INVALID) {
throw std::runtime_error("pio_add_program");
}
@ -98,8 +105,10 @@ private:
pio_sm_set_clkdiv(pio, sm, 1.0);
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + protomatter_wrap_target, offset + protomatter_wrap);
sm_config_set_out_shift(&c, /* shift_right= */ false, /* auto_pull = */ true, 32);
sm_config_set_wrap(&c, offset + protomatter_wrap_target,
offset + protomatter_wrap);
sm_config_set_out_shift(&c, /* shift_right= */ false,
/* auto_pull = */ true, 32);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
sm_config_set_clkdiv(&c, 1.0);
sm_config_set_out_pins(&c, 0, 28);
@ -110,10 +119,10 @@ private:
pin_init_one(pinout::PIN_CLK);
pin_init_one(pinout::PIN_LAT);
for(const auto p : pinout::PIN_RGB)
for (const auto p : pinout::PIN_RGB)
pin_init_one(p);
for(size_t i=0; i<geometry.n_addr_lines; i++) {
for (size_t i = 0; i < geometry.n_addr_lines; i++) {
pin_init_one(pinout::PIN_ADDR[i]);
}
}
@ -133,8 +142,9 @@ private:
size_t datasize = 0;
int old_buffer_idx = buffer_manager::no_buffer;
int buffer_idx;
while((buffer_idx = manager.get_filled_buffer()) != buffer_manager::exit_request) {
if(buffer_idx != buffer_manager::no_buffer) {
while ((buffer_idx = manager.get_filled_buffer()) !=
buffer_manager::exit_request) {
if (buffer_idx != buffer_manager::no_buffer) {
const auto &buffer = buffers[buffer_idx];
databuf = &buffer[0];
datasize = buffer.size() * sizeof(*databuf);
@ -144,7 +154,8 @@ private:
old_buffer_idx = buffer_idx;
}
if (datasize) {
pio_sm_xfer_data_large(pio, sm, PIO_DIR_TO_SM, datasize, (uint32_t*)databuf);
pio_sm_xfer_data_large(pio, sm, PIO_DIR_TO_SM, datasize,
(uint32_t *)databuf);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
@ -161,4 +172,4 @@ private:
std::thread blitter_thread;
};
}
} // namespace piomatter

View file

@ -1,14 +1,16 @@
#pragma once
const int protomatter_wrap = 4;
const int protomatter_wrap_target = 0;
const int protomatter_sideset_pin_count = 0;
const bool protomatter_sideset_enable = 0;
const uint16_t protomatter[] = {
// ; data format (out-shift-right):
// ; MSB ... LSB
// ; 0 ddd......ddd: 31-bit delay
// ; 1 ccc......ccc: 31 bit data count
// .wrap_target
// top:
// ; data format (out-shift-right):
// ; MSB ... LSB
// ; 0 ddd......ddd: 31-bit delay
// ; 1 ccc......ccc: 31 bit data count
// .wrap_target
// top:
0x6021, // out x, 1
0x605f, // out y, 31
0x0025, // jmp !x delay_loop
@ -19,7 +21,7 @@ const uint16_t protomatter[] = {
// delay_loop:
0x0085, // jmp y--, delay_loop
0x0000, // jmp top
// ;; fill program out to 32 instructions so nothing else can load
// ;; fill program out to 32 instructions so nothing else can load
0xa042, // nop
0xa042, // nop
0xa042, // nop
@ -46,4 +48,3 @@ const uint16_t protomatter[] = {
0xa042, // nop
0xa042, // nop
};

View file

@ -1,48 +1,49 @@
#pragma once
#include "matrixmap.h"
#include <cassert>
#include <span>
#include <vector>
#include <cassert>
#include "matrixmap.h"
namespace piomatter {
constexpr unsigned DATA_OVERHEAD = 3;
constexpr unsigned CLOCKS_PER_DATA = 2;
constexpr unsigned DELAY_OVERHEAD = 5;
constexpr unsigned CLOCKS_PER_DELAY = 1;
constexpr uint32_t command_data = 1u <<31;
constexpr uint32_t command_data = 1u << 31;
constexpr uint32_t command_delay = 0;
struct gamma_lut {
gamma_lut(double exponent=2.2) {
for(int i=0; i<256; i++) {
gamma_lut(double exponent = 2.2) {
for (int i = 0; i < 256; i++) {
auto v = std::max(i, int(round(1023 * pow(i / 255, exponent))));
lut[i] = v;
}
}
unsigned convert(unsigned v) {
if(v >= std::size(lut)) return 1023;
if (v >= std::size(lut))
return 1023;
return lut[v];
}
void convert_rgb888_packed_to_rgb10(std::vector<uint32_t> &result, std::span<const uint8_t> source) {
void convert_rgb888_packed_to_rgb10(std::vector<uint32_t> &result,
std::span<const uint8_t> source) {
result.resize(source.size() / 3);
for(size_t i=0; i<source.size(); i+=3) {
uint32_t r = source[i+0] & 0xff;
uint32_t g = source[i+1] & 0xff;
uint32_t b = source[i+2] & 0xff;
for (size_t i = 0; i < source.size(); i += 3) {
uint32_t r = source[i + 0] & 0xff;
uint32_t g = source[i + 1] & 0xff;
uint32_t b = source[i + 2] & 0xff;
result[i] = (convert(r) << 20) | (convert(g) << 10) | convert(b);
}
}
void convert_rgb888_to_rgb10(std::vector<uint32_t> &result, std::span<const uint32_t> source) {
void convert_rgb888_to_rgb10(std::vector<uint32_t> &result,
std::span<const uint32_t> source) {
result.resize(source.size());
for(size_t i=0; i<source.size(); i++) {
for (size_t i = 0; i < source.size(); i++) {
uint32_t data = source[i];
uint32_t r = (data >> 16) & 0xff;
uint32_t g = (data >> 8) & 0xff;
@ -51,16 +52,16 @@ struct gamma_lut {
}
}
void convert_rgb565_to_rgb10(std::vector<uint32_t> &result, std::span<const uint16_t> source) {
void convert_rgb565_to_rgb10(std::vector<uint32_t> &result,
std::span<const uint16_t> source) {
result.resize(source.size());
for(size_t i=0; i<source.size(); i++) {
for (size_t i = 0; i < source.size(); i++) {
uint32_t data = source[i];
unsigned r5 = (data >> 11) & 0x1f;
unsigned r = (r5 << 3) | (r5 >> 2);
unsigned g6 = (data >> 5) & 0x3f;
unsigned g = (g6 << 2) | (g6 >> 4);
unsigned b5 = (data) & 0x1f;
unsigned b5 = (data)&0x1f;
unsigned b = (b5 << 3) | (b5 >> 2);
result[i] = (convert(r) << 20) | (convert(g) << 10) | convert(b);
@ -73,9 +74,10 @@ struct gamma_lut {
struct colorspace_rgb565 {
using data_type = uint16_t;
colorspace_rgb565(float gamma=2.2) : lut{gamma} {}
colorspace_rgb565(float gamma = 2.2) : lut{gamma} {}
gamma_lut lut;
const std::span<const uint32_t> convert(std::span<const data_type> data_in) {
const std::span<const uint32_t>
convert(std::span<const data_type> data_in) {
lut.convert_rgb565_to_rgb10(rgb10, data_in);
return rgb10;
}
@ -85,9 +87,10 @@ struct colorspace_rgb565 {
struct colorspace_rgb888 {
using data_type = uint32_t;
colorspace_rgb888(float gamma=2.2) : lut{gamma} {}
colorspace_rgb888(float gamma = 2.2) : lut{gamma} {}
gamma_lut lut;
const std::span<const uint32_t> convert(std::span<const data_type> data_in) {
const std::span<const uint32_t>
convert(std::span<const data_type> data_in) {
lut.convert_rgb888_to_rgb10(rgb10, data_in);
return rgb10;
}
@ -97,9 +100,10 @@ struct colorspace_rgb888 {
struct colorspace_rgb888_packed {
using data_type = uint8_t;
colorspace_rgb888_packed(float gamma=2.2) : lut{gamma} {}
colorspace_rgb888_packed(float gamma = 2.2) : lut{gamma} {}
gamma_lut lut;
const std::span<const uint32_t> convert(std::span<const data_type> data_in) {
const std::span<const uint32_t>
convert(std::span<const data_type> data_in) {
lut.convert_rgb888_packed_to_rgb10(rgb10, data_in);
return rgb10;
}
@ -109,21 +113,24 @@ struct colorspace_rgb888_packed {
struct colorspace_rgb10 {
using data_type = uint32_t;
const std::span<const uint32_t> convert(std::span<const data_type> data_in) {
const std::span<const uint32_t>
convert(std::span<const data_type> data_in) {
return data_in;
}
};
// Render a buffer in linear RGB10 format into a piomatter stream
template<typename pinout>
void protomatter_render_rgb10(std::vector<uint32_t> &result, const matrix_geometry &matrixmap, const uint32_t *pixels) {
template <typename pinout>
void protomatter_render_rgb10(std::vector<uint32_t> &result,
const matrix_geometry &matrixmap,
const uint32_t *pixels) {
result.clear();
int data_count = 0;
auto do_delay = [&](uint32_t delay) {
if (delay == 0) return;
if (delay == 0)
return;
assert(delay < 1000000);
assert(!data_count);
result.push_back(command_delay | (delay ? delay - 1 : 0));
@ -139,7 +146,7 @@ void protomatter_render_rgb10(std::vector<uint32_t> &result, const matrix_geomet
auto do_data = [&](uint32_t d) {
assert(data_count);
data_count --;
data_count--;
result.push_back(d);
};
@ -151,35 +158,49 @@ void protomatter_render_rgb10(std::vector<uint32_t> &result, const matrix_geomet
auto calc_addr_bits = [](int addr) {
uint32_t data = 0;
if(addr & 1) data |= (1 << pinout::PIN_ADDR[0]);
if(addr & 2) data |= (1 << pinout::PIN_ADDR[1]);
if(addr & 4) data |= (1 << pinout::PIN_ADDR[2]);
if constexpr(std::size(pinout::PIN_ADDR) >= 4) {
if(addr & 8) data |= (1 << pinout::PIN_ADDR[3]);
if (addr & 1)
data |= (1 << pinout::PIN_ADDR[0]);
if (addr & 2)
data |= (1 << pinout::PIN_ADDR[1]);
if (addr & 4)
data |= (1 << pinout::PIN_ADDR[2]);
if constexpr (std::size(pinout::PIN_ADDR) >= 4) {
if (addr & 8)
data |= (1 << pinout::PIN_ADDR[3]);
}
if constexpr(std::size(pinout::PIN_ADDR) >= 5) {
if(addr & 16) data |= (1 << pinout::PIN_ADDR[4]);
if constexpr (std::size(pinout::PIN_ADDR) >= 5) {
if (addr & 16)
data |= (1 << pinout::PIN_ADDR[4]);
}
return data;
};
auto add_pixels = [&do_data, &result](uint32_t addr_bits, bool r0, bool g0, bool b0, bool r1, bool g1, bool b1, bool active) {
uint32_t data = (active ? pinout::oe_active : pinout::oe_inactive) | addr_bits;
if(r0) data |= (1 << pinout::PIN_RGB[0]);
if(g0) data |= (1 << pinout::PIN_RGB[1]);
if(b0) data |= (1 << pinout::PIN_RGB[2]);
if(r1) data |= (1 << pinout::PIN_RGB[3]);
if(g1) data |= (1 << pinout::PIN_RGB[4]);
if(b1) data |= (1 << pinout::PIN_RGB[5]);
auto add_pixels = [&do_data, &result](uint32_t addr_bits, bool r0, bool g0,
bool b0, bool r1, bool g1, bool b1,
bool active) {
uint32_t data =
(active ? pinout::oe_active : pinout::oe_inactive) | addr_bits;
if (r0)
data |= (1 << pinout::PIN_RGB[0]);
if (g0)
data |= (1 << pinout::PIN_RGB[1]);
if (b0)
data |= (1 << pinout::PIN_RGB[2]);
if (r1)
data |= (1 << pinout::PIN_RGB[3]);
if (g1)
data |= (1 << pinout::PIN_RGB[4]);
if (b1)
data |= (1 << pinout::PIN_RGB[5]);
do_data(data);
do_data(data | pinout::clk_bit);
};
int last_bit = 0;
int prev_addr = 7;
// illuminate the right row for data in the shift register (the previous address)
// illuminate the right row for data in the shift register (the previous
// address)
uint32_t addr_bits = calc_addr_bits(prev_addr);
const auto n_addr = 1u << matrixmap.n_addr_lines;
@ -188,21 +209,21 @@ void protomatter_render_rgb10(std::vector<uint32_t> &result, const matrix_geomet
unsigned offset = n_bits - n_planes;
const auto pixels_across = matrixmap.pixels_across;
for(int addr = 0; addr < n_addr; addr++) {
for(int bit = n_planes - 1; bit >= 0; bit--) {
for (int addr = 0; addr < n_addr; addr++) {
for (int bit = n_planes - 1; bit >= 0; bit--) {
uint32_t r = 1 << (20 + offset + bit);
uint32_t g = 1 << (10 + offset + bit);
uint32_t b = 1 << (0 + offset + bit);
// the shortest /OE we can do is one DATA_OVERHEAD...
// TODO: should make sure desired duration of MSB is at least `pixels_across`
// TODO: should make sure desired duration of MSB is at least
// `pixels_across`
uint32_t desired_duration = 1 << last_bit;
last_bit = bit;
prep_data(2 * pixels_across);
auto mapiter = matrixmap.map.begin() + 2 * addr * pixels_across;
for(int x = 0; x < pixels_across; x++) {
for (int x = 0; x < pixels_across; x++) {
assert(mapiter != matrixmap.map.end());
auto pixel0 = pixels[*mapiter++];
auto r0 = pixel0 & r;
@ -214,26 +235,32 @@ void protomatter_render_rgb10(std::vector<uint32_t> &result, const matrix_geomet
auto g1 = pixel1 & g;
auto b1 = pixel1 & b;
add_pixels(addr_bits, r0, g0, b0, r1, g1, b1, x < desired_duration);
add_pixels(addr_bits, r0, g0, b0, r1, g1, b1,
x < desired_duration);
}
// hold /OE low until desired time has elapsed to illuminate the LAST line
// hold /OE low until desired time has elapsed to illuminate the
// LAST line
int remain = desired_duration - pixels_across;
if (remain > 0) {
do_data_delay(addr_bits | pinout::oe_active, remain * CLOCKS_PER_DATA - DELAY_OVERHEAD);
do_data_delay(addr_bits | pinout::oe_active,
remain * CLOCKS_PER_DATA - DELAY_OVERHEAD);
}
do_data_delay(addr_bits | pinout::oe_inactive, pinout::post_oe_delay);
do_data_delay(addr_bits | pinout::oe_inactive | pinout::lat_bit, pinout::post_latch_delay);
do_data_delay(addr_bits | pinout::oe_inactive,
pinout::post_oe_delay);
do_data_delay(addr_bits | pinout::oe_inactive | pinout::lat_bit,
pinout::post_latch_delay);
// with oe inactive, set address bits to illuminate THIS line
if (addr != prev_addr) {
addr_bits = calc_addr_bits(addr);
do_data_delay(addr_bits | pinout::oe_inactive, pinout::post_addr_delay);
do_data_delay(addr_bits | pinout::oe_inactive,
pinout::post_addr_delay);
prev_addr = addr;
}
}
}
}
}
} // namespace piomatter

View file

@ -1,13 +1,12 @@
#pragma once
#include <optional>
#include <mutex>
#include <condition_variable>
#include <mutex>
#include <optional>
#include <queue>
namespace piomatter {
template<class T>
struct thread_queue {
template <class T> struct thread_queue {
thread_queue() : queue{}, mutex{}, cv{} {}
void push(T t) {
@ -18,7 +17,9 @@ struct thread_queue {
std::optional<T> pop_nonblocking() {
std::unique_lock<std::mutex> lock(mutex);
if (queue.empty()) { return {}; }
if (queue.empty()) {
return {};
}
T val = queue.front();
queue.pop();
return val;
@ -26,15 +27,18 @@ struct thread_queue {
T pop_blocking() {
std::unique_lock<std::mutex> lock(mutex);
while (queue.empty()) { cv.wait(lock); }
while (queue.empty()) {
cv.wait(lock);
}
T val = queue.front();
queue.pop();
return val;
}
private:
private:
std::queue<T> queue;
std::mutex mutex;
std::condition_variable cv;
};
}
} // namespace piomatter

View file

@ -1,10 +1,10 @@
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <vector>
#include "piomatter/piomatter.h"
@ -12,30 +12,46 @@
#define r (255 << 16)
#define g (255 << 8)
#define b (255)
#define y (r|g)
#define c (g|b)
#define m (r|b)
#define w (r|g|b)
#define y (r | g)
#define c (g | b)
#define m (r | b)
#define w (r | g | b)
constexpr int width = 64, height = 64;
uint32_t pixels[height][width] = {
{_,w,_,_,r,r,_,_,_,g,_,_,b,b,b,_,c,c,_,_,y,_,y,_,m,m,m,_,w,w,w,_}, // 0
{w,_,w,_,r,_,r,_,g,_,g,_,b,_,_,_,c,_,c,_,y,_,y,_,_,m,_,_,_,w,_,_}, // 1
{w,w,w,_,r,_,r,_,g,g,g,_,b,b,_,_,c,c,_,_,y,_,y,_,_,m,_,_,_,w,_,_}, // 2
{w,_,w,_,r,_,r,_,g,_,g,_,b,_,_,_,c,_,c,_,y,_,y,_,_,m,_,_,_,w,_,_}, // 3
{w,_,w,_,r,r,_,_,g,_,g,_,b,_,_,_,c,_,c,_,_,y,_,_,m,m,m,_,_,w,_,_}, // 4
{_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_}, // 5
{_,c,_,_,y,y,_,_,_,m,_,_,r,r,r,_,g,g,_,_,b,_,b,_,w,w,w,_,c,c,c,_}, // 6
{c,_,c,_,y,_,y,_,m,_,m,_,r,_,_,_,g,_,g,_,b,_,b,_,_,w,_,_,_,c,_,_}, // 7
{c,c,c,_,y,_,y,_,m,m,m,_,r,r,_,_,g,g,_,_,b,_,b,_,_,w,_,_,_,c,_,_}, // 8
{c,_,c,_,y,_,y,_,m,_,m,_,r,_,_,_,g,_,g,_,b,_,b,_,_,w,_,_,_,c,_,_}, // 9
{c,_,c,_,y,y,_,_,m,_,m,_,r,_,_,_,g,_,g,_,_,b,_,_,w,w,w,_,_,c,_,_}, // 10
{_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_}, // 11
{r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,g}, // 12
{y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,g,y}, // 13
{g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,g,c,b}, // 14
{c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,y,g,c,b,m,r,g,c,b,m}, // 15
{_, w, _, _, r, r, _, _, _, g, _, _, b, b, b, _,
c, c, _, _, y, _, y, _, m, m, m, _, w, w, w, _}, // 0
{w, _, w, _, r, _, r, _, g, _, g, _, b, _, _, _,
c, _, c, _, y, _, y, _, _, m, _, _, _, w, _, _}, // 1
{w, w, w, _, r, _, r, _, g, g, g, _, b, b, _, _,
c, c, _, _, y, _, y, _, _, m, _, _, _, w, _, _}, // 2
{w, _, w, _, r, _, r, _, g, _, g, _, b, _, _, _,
c, _, c, _, y, _, y, _, _, m, _, _, _, w, _, _}, // 3
{w, _, w, _, r, r, _, _, g, _, g, _, b, _, _, _,
c, _, c, _, _, y, _, _, m, m, m, _, _, w, _, _}, // 4
{_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, // 5
{_, c, _, _, y, y, _, _, _, m, _, _, r, r, r, _,
g, g, _, _, b, _, b, _, w, w, w, _, c, c, c, _}, // 6
{c, _, c, _, y, _, y, _, m, _, m, _, r, _, _, _,
g, _, g, _, b, _, b, _, _, w, _, _, _, c, _, _}, // 7
{c, c, c, _, y, _, y, _, m, m, m, _, r, r, _, _,
g, g, _, _, b, _, b, _, _, w, _, _, _, c, _, _}, // 8
{c, _, c, _, y, _, y, _, m, _, m, _, r, _, _, _,
g, _, g, _, b, _, b, _, _, w, _, _, _, c, _, _}, // 9
{c, _, c, _, y, y, _, _, m, _, m, _, r, _, _, _,
g, _, g, _, _, b, _, _, w, w, w, _, _, c, _, _}, // 10
{_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, // 11
{r, y, g, c, b, m, r, y, g, c, b, m, r, y, g, c,
b, m, r, y, g, c, b, m, r, y, g, c, b, m, r, g}, // 12
{y, g, c, b, m, r, y, g, c, b, m, r, y, g, c, b,
m, r, y, g, c, b, m, r, y, g, c, b, m, r, g, y}, // 13
{g, c, b, m, r, y, g, c, b, m, r, y, g, c, b, m,
r, y, g, c, b, m, r, y, g, c, b, m, r, g, c, b}, // 14
{c, b, m, r, y, g, c, b, m, r, y, g, c, b, m, r,
y, g, c, b, m, r, y, g, c, b, m, r, g, c, b, m}, // 15
};
#undef r
#undef g
@ -45,14 +61,14 @@ uint32_t pixels[height][width] = {
#undef w
#undef _
#define rgb(r,g,b) ((r << 16) | (g << 8) | b)
#define rgb(r, g, b) ((r << 16) | (g << 8) | b)
uint32_t colorwheel(int i) {
i = i & 0xff;
if(i < 85) {
if (i < 85) {
return rgb(255 - i * 3, 0, i * 3);
}
if(i < 170) {
if (i < 170) {
i -= 85;
return rgb(0, i * 3, 255 - i * 3);
}
@ -61,15 +77,15 @@ uint32_t colorwheel(int i) {
}
void test_pattern(int offs) {
for(int i=0; i<width; i++) {
pixels[height-5][i] = rgb(1+i*4, 1+i*4, 1+i*4);
pixels[height-4][i] = colorwheel(2*i + offs / 3);
pixels[height-3][i] = colorwheel(2*i+64 + offs / 5);
pixels[height-2][i] = colorwheel(2*i+128 + offs / 2);
pixels[height-1][i] = colorwheel(2*i+192 + offs / 7);
for (int i = 0; i < width; i++) {
pixels[height - 5][i] = rgb(1 + i * 4, 1 + i * 4, 1 + i * 4);
pixels[height - 4][i] = colorwheel(2 * i + offs / 3);
pixels[height - 3][i] = colorwheel(2 * i + 64 + offs / 5);
pixels[height - 2][i] = colorwheel(2 * i + 128 + offs / 2);
pixels[height - 1][i] = colorwheel(2 * i + 192 + offs / 7);
}
for(int i=0; i<height; i++) {
pixels[i][i] = rgb(0xff,0xff,0xff);
for (int i = 0; i < height; i++) {
pixels[i][i] = rgb(0xff, 0xff, 0xff);
}
}
@ -77,18 +93,18 @@ static uint64_t monotonicns64() {
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return tp.tv_sec * UINT64_C(1000000000)+ tp.tv_nsec;
return tp.tv_sec * UINT64_C(1000000000) + tp.tv_nsec;
}
int main(int argc, char **argv) {
int n = argc > 1 ? atoi(argv[1]) : 0;
piomatter::matrix_geometry geometry(128, 4, 10, 64, 64, true, piomatter::orientation_normal);
piomatter::piomatter p(std::span(&pixels[0][0], 64*64), geometry);
piomatter::matrix_geometry geometry(128, 4, 10, 64, 64, true,
piomatter::orientation_normal);
piomatter::piomatter p(std::span(&pixels[0][0], 64 * 64), geometry);
uint64_t start = monotonicns64();
for(int i=0; i<n; i++) {
for (int i = 0; i < n; i++) {
test_pattern(i);
p.show();
}