Skip to content

Commit

Permalink
rename everything per discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
jepler committed Dec 10, 2024
1 parent 277e0a0 commit 9b64b9b
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 58 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
36 changes: 36 additions & 0 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Pip

on:
workflow_dispatch:
pull_request:
push:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

jobs:
build:
strategy:
fail-fast: false
matrix:
platform: ["ubuntu-latest"]
python-version: ["3.11", "3.12", "3.13"]

runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Add requirements
run: python -m pip install --upgrade wheel setuptools

- name: Build and install
run: pip install --verbose .
14 changes: 10 additions & 4 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0

- name: Build SDist
run: pipx run build --sdist
Expand All @@ -39,10 +42,13 @@ jobs:
strategy:
fail-fast: false
matrix:
os: ubuntu-latest
arch_linux: aarch64
os: ["ubuntu-latest"]
arch_linux: ["aarch64"]
steps:
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand All @@ -52,8 +58,8 @@ jobs:
- uses: pypa/[email protected]
env:
CIBW_ARCHS_LINUX: ${{ matrix.arch_linux }}
CIBW_BUILD: "cp311* cp312* cp313*"

CIBW_BUILD: "cp311-manylinux* cp312-manylinux* cp313-manylinux*"
CIBW_MANYLINUX_AARCH64_IMAGE: "manylinux_2_28"
- name: Verify clean directory
run: git diff --exit-code
shell: bash
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ instance/

# Sphinx documentation
docs/_build/
docs/_generate/

# PyBuilder
.pybuilder/
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ repos:
- 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
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v13.0.0
hooks:
- id: clang-format
9 changes: 7 additions & 2 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
# Required
version: 2

submodules:
include: all

build:
os: ubuntu-lts-latest
tools:
python: "3"

python:
install:
- requirements: docs/requirements.txt
- requirements: requirements.txt
- method: pip
path: .
extra_requirements:
- docs
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ adafruit-blinka-neopixel-pi5
| pip builds | [![Pip Actions Status][actions-pip-badge]][actions-pip-link] |
| [`cibuildwheel`][] | [![Wheels Actions Status][actions-wheels-badge]][actions-wheels-link] |

[actions-badge]: https://github.com/adafruit/adafruit-blinka-neopixel-pi5/workflows/Tests/badge.svg
[actions-pip-link]: https://github.com/adafruit/adafruit-blinka-neopixel-pi5/actions?query=workflow%3A%22Pip
[actions-pip-badge]: https://github.com/adafruit/adafruit-blinka-neopixel-pi5/workflows/Pip/badge.svg
[actions-wheels-link]: https://github.com/adafruit/adafruit-blinka-neopixel-pi5/actions?query=workflow%3AWheels
[actions-wheels-badge]: https://github.com/adafruit/adafruit-blinka-neopixel-pi5/workflows/Wheels/badge.svg
[actions-badge]: https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Neopixel/workflows/Tests/badge.svg
[actions-pip-link]: https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Neopixel/actions?query=workflow%3A%22Pip
[actions-pip-badge]: https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Neopixel/workflows/Pip/badge.svg
[actions-wheels-link]: https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Neopixel/actions?query=workflow%3AWheels
[actions-wheels-badge]: https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Neopixel/workflows/Wheels/badge.svg

Installation
------------

Installing from source:

- clone this repository
- `pip install ./adafruit-blinka-neopixel-pi5`
- `pip install ./Adafruit_Blinka_Raspberry_Pi5_Neopixel`

Installing from pip (not yet available):
Installing from pip:

- `pip install adafruit-blinka-neopixel-pi5`
- `pip install Adafruit-Blinka-Raspberry-Pi5-Neopixel`

Building the documentation
--------------------------
Expand All @@ -33,7 +33,7 @@ the extension module to generate beautiful documentation in a variety formats.
The following command generates HTML-based reference documentation; for other
formats please refer to the Sphinx manual:

- `cd adafruit-blinka-neopixel-pi5/docs`
- `cd Adafruit_Blinka_Raspberry_Pi5_Neopixel/docs`
- `make html`

License
Expand Down
1 change: 1 addition & 0 deletions docs/adafruit_raspberry_pi5_neopixel_write.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: adafruit_raspberry_pi5_neopixel_write
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = "alabaster"
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Contents:
.. toctree::
:maxdepth: 2

neopixel_write_pi5
adafruit_raspberry_pi5_neopixel_write
1 change: 0 additions & 1 deletion docs/python_example.rst

This file was deleted.

16 changes: 7 additions & 9 deletions examples/led_animation.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import adafruit_pixelbuf
import board
import neopixel_write_pi5
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_raspberry_pi5_neopixel_write import neopixel_write

NEOPIXEL = board.D13
num_pixels = 96

class Pi5Pixelbuf(adafruit_pixelbuf.PixelBuf):
def __init__(self, pin, size, **kwargs):
self._pin = pin
super().__init__(size=size, **kwargs)

def _transmit(self, buf):
neopixel_write_pi5.neopixel_write(self._pin, buf)
neopixel_write(self._pin, buf)

pixels = Pi5Pixelbuf(board.D13, 120, auto_write=True, byteorder="WBGR")
pixels = Pi5Pixelbuf(NEOPIXEL, num_pixels, auto_write=True, byteorder="BGR")

rainbow = Rainbow(pixels, speed=0.02, period=2)
rainbow_chase = RainbowChase(pixels, speed=0.02, size=5, spacing=3)
Expand All @@ -33,9 +35,5 @@ def _transmit(self, buf):
auto_clear=True,
)

try:
while True:
animations.animate()
finally:
pixels.fill(0)
pixels.show()
while True:
animations.animate()
13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
# say from a submodule.

ext_modules = [
Pybind11Extension("neopixel_write_pi5",
Pybind11Extension("adafruit_raspberry_pi5_neopixel_write",
["src/main.cpp", "src/utils/piolib/piolib.c", "src/utils/piolib/pio_rp1.c"],
define_macros = [('VERSION_INFO', __version__)],
include_dirs = ['./src/utils/piolib/include'],
cxx_std = "20",
extra_compile_args = ["-g3"],
# use this setting when debugging
#extra_compile_args = ["-g3", "-Og"],
),
]

setup(
name="adafruit-blinka-neopixel-pi5",
name="Adafruit-Blinka-Raspberry-Pi5-Neopixel",
version=__version__,
url="https://github.com/adafruit/adafruit-blinka-neopixel-pi5",
url="https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Neopixel",
description="Control NeoPixel & compatibles on a Pi 5",
long_description="A pio-based driver",
ext_modules=ext_modules,
Expand All @@ -32,4 +32,7 @@
cmdclass={"build_ext": build_ext},
zip_safe=False,
python_requires=">=3.11",
extras_require={
'docs': ["sphinx", "sphinx-rtd-theme", "sphinxcontrib-jquery"],
},
)
62 changes: 40 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static PIO pio{};
static int sm{-1};
static int offset{-1};
static int last_gpio{-1};
static size_t last_size{};

static void neopixel_write(py::object gpio_obj, py::buffer buf) {
int gpio = py::getattr(gpio_obj, "_pin", gpio_obj).attr("id").cast<int>();
Expand All @@ -26,9 +27,11 @@ static void neopixel_write(py::object gpio_obj, py::buffer buf) {

// can't use `pio0` macro as it will call exit() on failure!
pio = pio_open(0);
if(PIO_IS_ERR(pio)) {
if (PIO_IS_ERR(pio)) {
throw std::runtime_error(
py::str("Failed to open PIO device (error {})").attr("format")(PIO_ERR_VAL(pio)).cast<std::string>());
py::str("Failed to open PIO device (error {})")
.attr("format")(PIO_ERR_VAL(pio))
.cast<std::string>());
}

sm = pio_claim_unused_sm(pio, true);
Expand All @@ -38,61 +41,76 @@ static void neopixel_write(py::object gpio_obj, py::buffer buf) {
pio_sm_clear_fifos(pio, sm);
pio_sm_set_clkdiv(pio, sm, 1.0);

printf("Loaded program at %d, using sm %d\n", offset, sm, gpio);
last_gpio = -1;
}

if (gpio != last_gpio) {
ws2812_program_init(pio, sm, offset, gpio, 800000.0, false);
printf("Initialized program at %d, using sm %d, gpio %d\n", offset, sm, gpio);
ws2812_program_init(pio, sm, offset, gpio, 800000.0, true);
last_gpio = gpio;
}

size_t size = info.size * info.itemsize;

if(size > UINT16_MAX) {
// rp1pio can only DMA in 32-bit blocks.
// Copy the data into a temporary vector, with redundant zeros at the end
// then byteswap it so that the data comes out in the right order.
uint8_t *data = reinterpret_cast<uint8_t *>(info.ptr);
std::vector<uint32_t> vec;
vec.resize((size + 3) / 4);
size_t data_size = vec.size() * 4;
memcpy(&vec[0], data, size);
for (auto &i : vec)
i = __builtin_bswap32(i);

if (data_size > UINT16_MAX) {
throw py::value_error("Too much data");
}

if (pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, size, 1)) {
throw std::runtime_error("pio_sm_config_xfer() failed");
if (data_size != last_size) {
if (pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, data_size, 1)) {
throw std::runtime_error("pio_sm_config_xfer() failed");
}
last_size = data_size;
}
if (pio_sm_xfer_data(pio, sm, PIO_DIR_TO_SM, size, info.ptr)) {
if (pio_sm_xfer_data(pio, sm, PIO_DIR_TO_SM, data_size, &vec[0])) {
throw std::runtime_error("pio_sm_xfer_data() failed");
}

}

static void free_pio(void) {
if (!pio) { return; }
if (offset <= 0) { pio_remove_program(pio, &ws2812_program, offset); };
if (!pio) {
return;
}
if (offset <= 0) {
pio_remove_program(pio, &ws2812_program, offset);
};
offset = -1;
if (sm >= 0) { pio_sm_unclaim(pio, sm); }
if (sm >= 0) {
pio_sm_unclaim(pio, sm);
}
sm = -1;
pio_close(pio);
pio = nullptr;
}

PYBIND11_MODULE(neopixel_write_pi5, m) {
PYBIND11_MODULE(adafruit_raspberry_pi5_neopixel_write, m) {
m.doc() = R"pbdoc(
neopixel_write for pi5
-----------------------
----------------------
.. currentmodule:: neopixel_write_pi5
.. currentmodule:: adafruit_raspberry_pi5_neopixel_write
.. autosummary::
:toctree: _generate
neopixel_write
free_pio
)pbdoc";

m.def("neopixel_write", &neopixel_write,
py::arg("gpio"),
py::arg("buf"),
R"pbdoc(NeoPixel writing function)pbdoc");
m.def("neopixel_write", &neopixel_write, py::arg("gpio"), py::arg("buf"),
R"pbdoc(NeoPixel writing function)pbdoc");

m.def("_free_pio", &free_pio,
R"pbdoc(Release any held PIO resource)pbdoc");
m.def("free_pio", &free_pio, R"pbdoc(Release any held PIO resource)pbdoc");

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
Expand Down

0 comments on commit 9b64b9b

Please sign in to comment.