Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Last active December 24, 2022 16:47
Show Gist options
  • Save cat-in-136/87a65d095811a1220351d58cb8e03610 to your computer and use it in GitHub Desktop.
Save cat-in-136/87a65d095811a1220351d58cb8e03610 to your computer and use it in GitHub Desktop.
Xmas illumination using M5Atom Matrix 5x5 NeoPixel LED matrix powered by rustlang

Xmas illumination using M5Atom Matrix 5x5 NeoPixel LED matrix powered by rustlang

ref. https://github.com/cat-in-136/ws2812-esp32-rmt-driver

  • In advance, prepare ESP Rust environment (using espup command).
  • Copy .cargo, build.rs, rust-toolchain.toml, sdkconfig.defaults from ws2812-esp32-rmt-driver path.
  • build binary with cargo +esp build
[package]
name = "xmas-m5atom-matrix-rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
esp-idf-sys = { version = "0.32", features = ["binstart"] }
smart-leds = "*"
embedded-graphics = "*"
ws2812-esp32-rmt-driver = { version = "0.5", features = ["embedded-graphics-core", "unstable"] }
[build-dependencies]
embuild = "*"
anyhow = "1"
[profile.release]
strip = true
opt-level = "s"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
strip = true
opt-level = "z"
use embedded_graphics::pixelcolor::Rgb888;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{PrimitiveStyle, Rectangle, Triangle};
use esp_idf_sys::esp_random;
use std::thread::sleep;
use std::time::Duration;
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelMatrix, Ws2812DrawTarget};
fn main() -> ! {
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
// or else some patches to the runtime implemented by esp-idf-sys might not link properly.
esp_idf_sys::link_patches();
let mut draw = Ws2812DrawTarget::<LedPixelMatrix<5, 5>>::new(0, 27).unwrap();
draw.set_brightness(40);
println!("Start Ws2812DrawTarget!");
let mut t = 0u32;
let mut ornaments = [(Point::new(-1, -1), Rgb888::BLACK, 0); 6];
loop {
draw.clear_with_black().unwrap();
let tree = Triangle::new(Point::new(2, 0), Point::new(-1, 3), Point::new(5, 3));
for (p, c, t_start) in ornaments.iter_mut() {
if (!tree.contains(p.clone())) || *t_start + 20 < t {
let mut rand_value = unsafe { esp_random() } as u32;
let new_p = Point::new(
(((rand_value >> 0) & 0xF) % 5) as i32,
(((rand_value >> 4) & 0xF) % 5) as i32,
);
if tree.contains(new_p) {
let new_c = match ((rand_value >> 8) & 0xF) % 5 {
0 => Rgb888::RED,
1 => Rgb888::WHITE,
2 => Rgb888::BLUE,
3 => Rgb888::CSS_ORANGE,
4 => Rgb888::CSS_YELLOW,
_ => unreachable!(),
};
*p = new_p;
*c = new_c;
*t_start = t;
} else {
// try next loop
}
}
}
tree.into_styled(PrimitiveStyle::with_fill(Rgb888::GREEN))
.draw(&mut draw)
.unwrap();
for (p, c, _) in &ornaments {
Rectangle::new(p.clone(), Size::new(1, 1))
.into_styled(PrimitiveStyle::with_fill(c.clone()))
.draw(&mut draw)
.unwrap();
}
Rectangle::new(Point::new(2, 4), Size::new(1, 1))
.into_styled(PrimitiveStyle::with_fill(Rgb888::CSS_SADDLE_BROWN))
.draw(&mut draw)
.unwrap();
draw.flush().unwrap();
sleep(Duration::from_millis(100));
t = t.saturating_add(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment