$ show jgarff/rpi_ws281x
This repository provides a userspace C library for controlling WS2812/SK6812 RGB(RGBW) LEDs on Raspberry Pi single-board computers. The core architecture uses Raspberry Pi's DMA (Direct Memory Access) controller to feed data to the PWM, PCM, or SPI peripheral, generating the precise timing signals required by the LED protocol without burdening the CPU. The library is structured as a static/shared C library with a clean API (ws2811_init, ws2811_render, ws2811_fini) that abstracts hardware details such as GPIO configuration, clock setup, and memory allocation via the VideoCore mailbox interface. The code is well-documented with register-level header files (pwm.h, pcm.h, dma.h, clk.h, gpio.h) that could be reused in other Pi projects. Building is supported via both SCons and CMake, and Debian packaging files are included. The library is the foundational component for numerous language bindings (Python, Rust, Java, etc.) and is widely used in LED projects like unicorn-hat and pixel-art displays.
The LLM identified 2 files worth highlighting for reuse:
Provides inline functions for setting GPIO function select, output level, and output mode by directly manipulating the BCM283x GPIO registers. The abstraction is minimal yet effective, requiring only a volatile pointer to the gpio register map. This pattern of hardware register mapping and inline manipulation is clean, efficient, and can be extracted for other Raspberry Pi bare-metal or userspace GPIO projects without modification.
Implements automatic detection of Raspberry Pi hardware revision by parsing /proc/cpuinfo (or /proc/device-tree on ARM64) to identify the model, peripheral base address, and video core base address. The detection code uses a lookup table of known revision codes and returns a const structure with hardware parameters. This is a self-contained utility that can be reused in any Pi C/C++ project that needs to adapt to different Pi generations (Pi 1/2/3/4/400/Zero/CM).