pub struct RGBMatrix { /* private fields */ }
Implementations§
Source§impl RGBMatrix
impl RGBMatrix
Sourcepub fn new(
config: RGBMatrixConfig,
requested_inputs: u32,
) -> Result<(Self, Box<Canvas>), MatrixCreationError>
pub fn new( config: RGBMatrixConfig, requested_inputs: u32, ) -> Result<(Self, Box<Canvas>), MatrixCreationError>
Create a new RGB matrix controller. This starts a new thread to update the matrix. Returns the controller and a canvas for drawing.
You can additionally request user readable GPIO bits which can later be received with
RGBMatrix::receive_new_inputs
. Only bits that are not already in use for reading or writing by the
matrix are allowed. Use RGBMatrix::enabled_input_bits
after calling this function to check which
bits were actually available.
Examples found in repository?
examples/image.rs (line 18)
14fn main() {
15 let config: RGBMatrixConfig = argh::from_env();
16 let rows = config.rows;
17 let cols = config.cols;
18 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20 let image_data = ImageRawBE::<Rgb888>::new(IMAGE_DATA, IMAGE_SIZE as u32);
21 let image = Image::new(
22 &image_data,
23 Point::new(
24 (cols / 2 - IMAGE_SIZE / 2) as i32,
25 (rows / 2 - IMAGE_SIZE / 2) as i32,
26 ),
27 );
28
29 for step in 0.. {
30 canvas.fill(0, 0, 0);
31 image.draw(canvas.as_mut()).unwrap();
32 canvas = matrix.update_on_vsync(canvas);
33
34 if step % 120 == 0 {
35 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
36 std::io::stdout().flush().unwrap();
37 }
38 }
39}
More examples
examples/rotating_square.rs (line 26)
22fn main() {
23 let config: RGBMatrixConfig = argh::from_env();
24 let rows = config.rows as isize;
25 let cols = config.cols as isize;
26 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
27
28 let [center_x, center_y] = [cols / 2, rows / 2];
29
30 let rotate_square = (rows.min(cols) as f64 * 1.41) as isize;
31 let min_rotate = center_x - rotate_square / 2;
32 let max_rotate = center_x + rotate_square / 2;
33
34 let display_square = (rows.min(cols) as f64 * 0.7) as isize;
35 let min_display = center_x - display_square / 2;
36 let max_display = center_x + display_square / 2;
37
38 for step in 0.. {
39 let rotation_deg = step as f64 / 2.0;
40 for x in min_rotate..max_rotate {
41 for y in min_rotate..max_rotate {
42 let [rot_x, rot_y] =
43 rotate([x - center_x, y - center_x], rotation_deg.to_radians());
44 let canvas_x = rot_x + center_x as f64;
45 let canvas_y = rot_y + center_y as f64;
46 if (min_display..max_display).contains(&x)
47 && (min_display..max_display).contains(&y)
48 {
49 canvas.set_pixel(
50 canvas_x as usize,
51 canvas_y as usize,
52 scale_col(x, min_display, max_display),
53 255 - scale_col(y, min_display, max_display),
54 scale_col(y, min_display, max_display),
55 )
56 } else {
57 canvas.set_pixel(canvas_x as usize, canvas_y as usize, 0, 0, 0)
58 }
59 }
60 }
61
62 canvas = matrix.update_on_vsync(canvas);
63
64 if step % 120 == 0 {
65 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
66 std::io::stdout().flush().unwrap();
67 }
68 }
69}
examples/drawing.rs (line 18)
13fn main() {
14 let config: RGBMatrixConfig = argh::from_env();
15 let rows = config.rows as i32;
16 let cols = config.cols as i32;
17
18 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20 let circle = {
21 let thin_stroke = PrimitiveStyle::with_stroke(Rgb888::CSS_GRAY, 1);
22 Circle::with_center(
23 Point::new(rows / 2 - 1, cols / 2 - 1),
24 rows.min(cols) as u32 - 2,
25 )
26 .into_styled(thin_stroke)
27 };
28 let top = Line::new(Point::new(0, 0), Point::new(cols - 1, 0))
29 .into_styled(PrimitiveStyle::with_stroke(Rgb888::GREEN, 1));
30 let bottom = Line::new(Point::new(0, rows - 1), Point::new(cols - 1, rows - 1))
31 .into_styled(PrimitiveStyle::with_stroke(Rgb888::CYAN, 1));
32 let left = Line::new(Point::new(0, 0), Point::new(0, rows - 1))
33 .into_styled(PrimitiveStyle::with_stroke(Rgb888::RED, 1));
34 let right = Line::new(Point::new(cols - 1, 0), Point::new(cols - 1, rows - 1))
35 .into_styled(PrimitiveStyle::with_stroke(Rgb888::BLUE, 1));
36 let diagonal1 = Line::new(Point::new(0, 0), Point::new(cols - 1, rows - 1))
37 .into_styled(PrimitiveStyle::with_stroke(Rgb888::YELLOW, 1));
38 let diagonal2 = Line::new(Point::new(cols - 1, 0), Point::new(0, rows - 1))
39 .into_styled(PrimitiveStyle::with_stroke(Rgb888::MAGENTA, 1));
40
41 let text = Text::with_alignment(
42 "Hello\nWorld",
43 Point::new(cols / 2, rows / 2),
44 MonoTextStyle::new(&FONT_6X10, Rgb888::WHITE),
45 Alignment::Center,
46 );
47
48 for step in 0.. {
49 canvas.fill(0, 0, 0);
50 circle.draw(canvas.as_mut()).unwrap();
51 [diagonal1, diagonal2, top, bottom, left, right]
52 .iter()
53 .for_each(|line| line.draw(canvas.as_mut()).unwrap());
54 if (step / 100) % 2 == 0 {
55 text.draw(canvas.as_mut()).unwrap();
56 }
57 canvas = matrix.update_on_vsync(canvas);
58
59 if step % 120 == 0 {
60 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
61 std::io::stdout().flush().unwrap();
62 }
63 }
64}
Sourcepub fn update_on_vsync(&mut self, canvas: Box<Canvas>) -> Box<Canvas>
pub fn update_on_vsync(&mut self, canvas: Box<Canvas>) -> Box<Canvas>
Updates the matrix with the new canvas. Blocks until the end of the current frame.
Examples found in repository?
examples/image.rs (line 32)
14fn main() {
15 let config: RGBMatrixConfig = argh::from_env();
16 let rows = config.rows;
17 let cols = config.cols;
18 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20 let image_data = ImageRawBE::<Rgb888>::new(IMAGE_DATA, IMAGE_SIZE as u32);
21 let image = Image::new(
22 &image_data,
23 Point::new(
24 (cols / 2 - IMAGE_SIZE / 2) as i32,
25 (rows / 2 - IMAGE_SIZE / 2) as i32,
26 ),
27 );
28
29 for step in 0.. {
30 canvas.fill(0, 0, 0);
31 image.draw(canvas.as_mut()).unwrap();
32 canvas = matrix.update_on_vsync(canvas);
33
34 if step % 120 == 0 {
35 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
36 std::io::stdout().flush().unwrap();
37 }
38 }
39}
More examples
examples/rotating_square.rs (line 62)
22fn main() {
23 let config: RGBMatrixConfig = argh::from_env();
24 let rows = config.rows as isize;
25 let cols = config.cols as isize;
26 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
27
28 let [center_x, center_y] = [cols / 2, rows / 2];
29
30 let rotate_square = (rows.min(cols) as f64 * 1.41) as isize;
31 let min_rotate = center_x - rotate_square / 2;
32 let max_rotate = center_x + rotate_square / 2;
33
34 let display_square = (rows.min(cols) as f64 * 0.7) as isize;
35 let min_display = center_x - display_square / 2;
36 let max_display = center_x + display_square / 2;
37
38 for step in 0.. {
39 let rotation_deg = step as f64 / 2.0;
40 for x in min_rotate..max_rotate {
41 for y in min_rotate..max_rotate {
42 let [rot_x, rot_y] =
43 rotate([x - center_x, y - center_x], rotation_deg.to_radians());
44 let canvas_x = rot_x + center_x as f64;
45 let canvas_y = rot_y + center_y as f64;
46 if (min_display..max_display).contains(&x)
47 && (min_display..max_display).contains(&y)
48 {
49 canvas.set_pixel(
50 canvas_x as usize,
51 canvas_y as usize,
52 scale_col(x, min_display, max_display),
53 255 - scale_col(y, min_display, max_display),
54 scale_col(y, min_display, max_display),
55 )
56 } else {
57 canvas.set_pixel(canvas_x as usize, canvas_y as usize, 0, 0, 0)
58 }
59 }
60 }
61
62 canvas = matrix.update_on_vsync(canvas);
63
64 if step % 120 == 0 {
65 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
66 std::io::stdout().flush().unwrap();
67 }
68 }
69}
examples/drawing.rs (line 57)
13fn main() {
14 let config: RGBMatrixConfig = argh::from_env();
15 let rows = config.rows as i32;
16 let cols = config.cols as i32;
17
18 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20 let circle = {
21 let thin_stroke = PrimitiveStyle::with_stroke(Rgb888::CSS_GRAY, 1);
22 Circle::with_center(
23 Point::new(rows / 2 - 1, cols / 2 - 1),
24 rows.min(cols) as u32 - 2,
25 )
26 .into_styled(thin_stroke)
27 };
28 let top = Line::new(Point::new(0, 0), Point::new(cols - 1, 0))
29 .into_styled(PrimitiveStyle::with_stroke(Rgb888::GREEN, 1));
30 let bottom = Line::new(Point::new(0, rows - 1), Point::new(cols - 1, rows - 1))
31 .into_styled(PrimitiveStyle::with_stroke(Rgb888::CYAN, 1));
32 let left = Line::new(Point::new(0, 0), Point::new(0, rows - 1))
33 .into_styled(PrimitiveStyle::with_stroke(Rgb888::RED, 1));
34 let right = Line::new(Point::new(cols - 1, 0), Point::new(cols - 1, rows - 1))
35 .into_styled(PrimitiveStyle::with_stroke(Rgb888::BLUE, 1));
36 let diagonal1 = Line::new(Point::new(0, 0), Point::new(cols - 1, rows - 1))
37 .into_styled(PrimitiveStyle::with_stroke(Rgb888::YELLOW, 1));
38 let diagonal2 = Line::new(Point::new(cols - 1, 0), Point::new(0, rows - 1))
39 .into_styled(PrimitiveStyle::with_stroke(Rgb888::MAGENTA, 1));
40
41 let text = Text::with_alignment(
42 "Hello\nWorld",
43 Point::new(cols / 2, rows / 2),
44 MonoTextStyle::new(&FONT_6X10, Rgb888::WHITE),
45 Alignment::Center,
46 );
47
48 for step in 0.. {
49 canvas.fill(0, 0, 0);
50 circle.draw(canvas.as_mut()).unwrap();
51 [diagonal1, diagonal2, top, bottom, left, right]
52 .iter()
53 .for_each(|line| line.draw(canvas.as_mut()).unwrap());
54 if (step / 100) % 2 == 0 {
55 text.draw(canvas.as_mut()).unwrap();
56 }
57 canvas = matrix.update_on_vsync(canvas);
58
59 if step % 120 == 0 {
60 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
61 std::io::stdout().flush().unwrap();
62 }
63 }
64}
Sourcepub fn enabled_input_bits(&self) -> u32
pub fn enabled_input_bits(&self) -> u32
Get the bits that were available for input.
Sourcepub fn receive_new_inputs(&mut self, timeout: Duration) -> Option<u32>
pub fn receive_new_inputs(&mut self, timeout: Duration) -> Option<u32>
Tries to receive a new GPIO input as specified with [RGBMatrix::request_enabled_inputs
].
Sourcepub fn get_framerate(&self) -> usize
pub fn get_framerate(&self) -> usize
Get the average frame rate over the last 60 frames.
Examples found in repository?
examples/image.rs (line 35)
14fn main() {
15 let config: RGBMatrixConfig = argh::from_env();
16 let rows = config.rows;
17 let cols = config.cols;
18 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20 let image_data = ImageRawBE::<Rgb888>::new(IMAGE_DATA, IMAGE_SIZE as u32);
21 let image = Image::new(
22 &image_data,
23 Point::new(
24 (cols / 2 - IMAGE_SIZE / 2) as i32,
25 (rows / 2 - IMAGE_SIZE / 2) as i32,
26 ),
27 );
28
29 for step in 0.. {
30 canvas.fill(0, 0, 0);
31 image.draw(canvas.as_mut()).unwrap();
32 canvas = matrix.update_on_vsync(canvas);
33
34 if step % 120 == 0 {
35 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
36 std::io::stdout().flush().unwrap();
37 }
38 }
39}
More examples
examples/rotating_square.rs (line 65)
22fn main() {
23 let config: RGBMatrixConfig = argh::from_env();
24 let rows = config.rows as isize;
25 let cols = config.cols as isize;
26 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
27
28 let [center_x, center_y] = [cols / 2, rows / 2];
29
30 let rotate_square = (rows.min(cols) as f64 * 1.41) as isize;
31 let min_rotate = center_x - rotate_square / 2;
32 let max_rotate = center_x + rotate_square / 2;
33
34 let display_square = (rows.min(cols) as f64 * 0.7) as isize;
35 let min_display = center_x - display_square / 2;
36 let max_display = center_x + display_square / 2;
37
38 for step in 0.. {
39 let rotation_deg = step as f64 / 2.0;
40 for x in min_rotate..max_rotate {
41 for y in min_rotate..max_rotate {
42 let [rot_x, rot_y] =
43 rotate([x - center_x, y - center_x], rotation_deg.to_radians());
44 let canvas_x = rot_x + center_x as f64;
45 let canvas_y = rot_y + center_y as f64;
46 if (min_display..max_display).contains(&x)
47 && (min_display..max_display).contains(&y)
48 {
49 canvas.set_pixel(
50 canvas_x as usize,
51 canvas_y as usize,
52 scale_col(x, min_display, max_display),
53 255 - scale_col(y, min_display, max_display),
54 scale_col(y, min_display, max_display),
55 )
56 } else {
57 canvas.set_pixel(canvas_x as usize, canvas_y as usize, 0, 0, 0)
58 }
59 }
60 }
61
62 canvas = matrix.update_on_vsync(canvas);
63
64 if step % 120 == 0 {
65 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
66 std::io::stdout().flush().unwrap();
67 }
68 }
69}
examples/drawing.rs (line 60)
13fn main() {
14 let config: RGBMatrixConfig = argh::from_env();
15 let rows = config.rows as i32;
16 let cols = config.cols as i32;
17
18 let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20 let circle = {
21 let thin_stroke = PrimitiveStyle::with_stroke(Rgb888::CSS_GRAY, 1);
22 Circle::with_center(
23 Point::new(rows / 2 - 1, cols / 2 - 1),
24 rows.min(cols) as u32 - 2,
25 )
26 .into_styled(thin_stroke)
27 };
28 let top = Line::new(Point::new(0, 0), Point::new(cols - 1, 0))
29 .into_styled(PrimitiveStyle::with_stroke(Rgb888::GREEN, 1));
30 let bottom = Line::new(Point::new(0, rows - 1), Point::new(cols - 1, rows - 1))
31 .into_styled(PrimitiveStyle::with_stroke(Rgb888::CYAN, 1));
32 let left = Line::new(Point::new(0, 0), Point::new(0, rows - 1))
33 .into_styled(PrimitiveStyle::with_stroke(Rgb888::RED, 1));
34 let right = Line::new(Point::new(cols - 1, 0), Point::new(cols - 1, rows - 1))
35 .into_styled(PrimitiveStyle::with_stroke(Rgb888::BLUE, 1));
36 let diagonal1 = Line::new(Point::new(0, 0), Point::new(cols - 1, rows - 1))
37 .into_styled(PrimitiveStyle::with_stroke(Rgb888::YELLOW, 1));
38 let diagonal2 = Line::new(Point::new(cols - 1, 0), Point::new(0, rows - 1))
39 .into_styled(PrimitiveStyle::with_stroke(Rgb888::MAGENTA, 1));
40
41 let text = Text::with_alignment(
42 "Hello\nWorld",
43 Point::new(cols / 2, rows / 2),
44 MonoTextStyle::new(&FONT_6X10, Rgb888::WHITE),
45 Alignment::Center,
46 );
47
48 for step in 0.. {
49 canvas.fill(0, 0, 0);
50 circle.draw(canvas.as_mut()).unwrap();
51 [diagonal1, diagonal2, top, bottom, left, right]
52 .iter()
53 .for_each(|line| line.draw(canvas.as_mut()).unwrap());
54 if (step / 100) % 2 == 0 {
55 text.draw(canvas.as_mut()).unwrap();
56 }
57 canvas = matrix.update_on_vsync(canvas);
58
59 if step % 120 == 0 {
60 print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
61 std::io::stdout().flush().unwrap();
62 }
63 }
64}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RGBMatrix
impl !RefUnwindSafe for RGBMatrix
impl Send for RGBMatrix
impl !Sync for RGBMatrix
impl Unpin for RGBMatrix
impl !UnwindSafe for RGBMatrix
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.