Skip to content

Commit

Permalink
Merge pull request #2 from jepler/pre-commit-clang-format
Browse files Browse the repository at this point in the history
let's format our source code consistently
  • Loading branch information
jepler authored Jan 14, 2025
2 parents dfd5513 + 01ca95d commit a2494b9
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 187 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: LLVM
IndentWidth: 4
60 changes: 60 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def temporary_stdout(filename):
yield sys.stdout
finally:
sys.stdout = old_stdout

@click.command
@click.argument("infile")
@click.argument("outfile")
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)

Expand Down
20 changes: 6 additions & 14 deletions include/piomatter/buffer_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
64 changes: 31 additions & 33 deletions include/piomatter/matrixmap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <vector>
#include <stdexcept>
#include <vector>

namespace piomatter {

Expand All @@ -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) {

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));
}
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));
}
}
}
} // 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) {
Expand All @@ -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;

Expand All @@ -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
2 changes: 1 addition & 1 deletion include/piomatter/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ struct adafruit_matrix_bonnet_pinout {
static constexpr uint32_t post_addr_delay = 500;
};

}
} // namespace piomatter
53 changes: 32 additions & 21 deletions include/piomatter/piomatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}
Expand All @@ -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);
Expand All @@ -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");
}
Expand All @@ -90,16 +97,18 @@ struct piomatter : piomatter_base {
};

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");
}

pio_sm_clear_fifos(pio, sm);
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);
Expand All @@ -110,10 +119,10 @@ struct piomatter : piomatter_base {
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]);
}
}
Expand All @@ -133,8 +142,9 @@ struct piomatter : piomatter_base {
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);
Expand All @@ -144,7 +154,8 @@ struct piomatter : piomatter_base {
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));
}
Expand All @@ -161,4 +172,4 @@ struct piomatter : piomatter_base {
std::thread blitter_thread;
};

}
} // namespace piomatter
Loading

0 comments on commit a2494b9

Please sign in to comment.