rpi_led_matrix_sys/
lib.rs

1//! Rust bindings into the C++ library `rpi-rgb-led-matrix`.
2//!
3//! # Features
4//!
5//! ## `c-stubs`
6//!
7//! Instead of linking to the C++ library, we make stub C functions ourselves with the same
8//! signature to enable limited testing on non-raspberry pi computers.
9//!
10//! ## `stdcpp-static-link`
11//!
12//! By default, we link dynamically to `libstdc++` as the underlying C++ library requires access
13//! to the C++ standard library. However, sometimes people want to statically link so everything
14//! is bundled in a single binary. Enabling this feature changes our build behavior to statically
15//! link to `libstdc++`.
16//!
17//! `libstdc++.a` must be "visible" to `rustc` when compiling. This means it is in the global linker
18//! search path, or you've passed it in manually, like:
19//! ```text
20//! RUSTFLAGS="-L /PATH/TO/LIBSTDC++/DIR/" cargo build --features="stdcpp-static-link"
21//! ```
22use libc::{c_char, c_int};
23
24#[cfg(feature = "c-stubs")]
25pub mod c_stubs;
26
27/// The C handle for `LedMatrix`.
28pub enum CLedMatrix {}
29
30/// The C handle for `LedCanvas`.
31pub enum CLedCanvas {}
32
33/// The C handle for `LedFont`.
34pub enum CLedFont {}
35
36/// The Rust representation of [`CLedMatrixOptions`], which contains parameters to specify your hardware setup.
37#[derive(Debug)]
38#[repr(C)]
39pub struct CLedMatrixOptions {
40    pub hardware_mapping: *mut c_char,
41    pub rows: c_int,
42    pub cols: c_int,
43    pub chain_length: c_int,
44    pub parallel: c_int,
45    pub pwm_bits: c_int,
46    pub pwm_lsb_nanoseconds: c_int,
47    pub pwm_dither_bits: c_int,
48    pub brightness: c_int,
49    pub scan_mode: c_int,
50    pub row_address_type: c_int,
51    pub multiplexing: c_int,
52    pub led_rgb_sequence: *mut c_char,
53    pub pixel_mapper_config: *mut c_char,
54    pub panel_type: *mut c_char,
55    pub disable_hardware_pulsing: c_char,
56    pub show_refresh_rate: c_char,
57    pub inverse_colors: c_char,
58    pub limit_refresh_rate_hz: c_int,
59}
60
61/// The Rust representation of [`CLedRuntimeOptions`], which contains parameters to specify
62/// how the library behaves at runtime.
63#[derive(Debug)]
64#[repr(C)]
65pub struct CLedRuntimeOptions {
66    pub gpio_slowdown: c_int,
67    pub daemon: c_int,
68    pub drop_privileges: c_int,
69    pub do_gpio_init: bool,
70}
71
72#[cfg_attr(not(feature = "c-stubs"), link(name = "rgbmatrixsys"))]
73extern "C" {
74    // unused C functions omitted
75    pub fn led_matrix_create_from_options_and_rt_options(
76        opts: *mut CLedMatrixOptions,
77        rt_opts: *mut CLedRuntimeOptions,
78    ) -> *mut CLedMatrix;
79    pub fn led_matrix_delete(matrix: *mut CLedMatrix);
80    pub fn led_matrix_get_canvas(matrix: *mut CLedMatrix) -> *mut CLedCanvas;
81    pub fn led_canvas_get_size(canvas: *const CLedCanvas, width: *mut c_int, height: *mut c_int);
82    pub fn led_canvas_set_pixel(canvas: *mut CLedCanvas, x: c_int, y: c_int, r: u8, g: u8, b: u8);
83    pub fn led_canvas_clear(canvas: *mut CLedCanvas);
84    pub fn led_canvas_fill(canvas: *mut CLedCanvas, r: u8, g: u8, b: u8);
85    pub fn led_matrix_create_offscreen_canvas(matrix: *mut CLedMatrix) -> *mut CLedCanvas;
86    pub fn led_matrix_swap_on_vsync(
87        matrix: *mut CLedMatrix,
88        canvas: *mut CLedCanvas,
89    ) -> *mut CLedCanvas;
90    pub fn load_font(bdf_font_file: *const c_char) -> *mut CLedFont;
91    pub fn delete_font(font: *mut CLedFont);
92    pub fn draw_text(
93        canvas: *mut CLedCanvas,
94        font: *const CLedFont,
95        x: c_int,
96        y: c_int,
97        r: u8,
98        g: u8,
99        b: u8,
100        utf8_text: *const c_char,
101        kerning_offset: c_int,
102    ) -> c_int;
103    pub fn vertical_draw_text(
104        canvas: *mut CLedCanvas,
105        font: *const CLedFont,
106        x: c_int,
107        y: c_int,
108        r: u8,
109        g: u8,
110        b: u8,
111        utf8_text: *const c_char,
112        kerning_offset: c_int,
113    ) -> c_int;
114    pub fn draw_circle(
115        canvas: *mut CLedCanvas,
116        x: c_int,
117        y: c_int,
118        radius: c_int,
119        r: u8,
120        g: u8,
121        b: u8,
122    );
123    pub fn draw_line(
124        canvas: *mut CLedCanvas,
125        x0: c_int,
126        y0: c_int,
127        x1: c_int,
128        y1: c_int,
129        r: u8,
130        g: u8,
131        b: u8,
132    );
133}