add rgb check and example
This commit is contained in:
16
examples/embassy-rgb/Cargo.toml
Normal file
16
examples/embassy-rgb/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "embassy-rgb"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
cortex-m.workspace = true
|
||||
cortex-m-rt.workspace = true
|
||||
defmt.workspace = true
|
||||
defmt-rtt.workspace = true
|
||||
embassy-executor = { workspace = true, features = ["defmt", "executor-thread", "platform-cortex-m"] }
|
||||
embassy-stm32 = { workspace = true, features = ["defmt", "stm32f103c8", "time-driver-tim3"] }
|
||||
embassy-time.workspace = true
|
||||
panic-probe.workspace = true
|
||||
9
examples/embassy-rgb/build.rs
Normal file
9
examples/embassy-rgb/build.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=../../memory.x");
|
||||
println!(
|
||||
"cargo:rustc-link-search={}",
|
||||
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
.join("../..")
|
||||
.display()
|
||||
);
|
||||
}
|
||||
143
examples/embassy-rgb/src/main.rs
Normal file
143
examples/embassy-rgb/src/main.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
#![deny(unsafe_code)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use cortex_m as _;
|
||||
use defmt::info;
|
||||
use defmt_rtt as _;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embassy_time::Timer;
|
||||
use panic_probe as _;
|
||||
|
||||
const SMOOTH_STEPS: u16 = 64;
|
||||
const SMOOTH_STEP_MS: u64 = 30;
|
||||
const HARD_HOLD_MS: u64 = 1000;
|
||||
const PWM_FRAME_US: u64 = 10_000;
|
||||
const PWM_SLICES: u16 = 100;
|
||||
const PWM_SLICE_US: u64 = PWM_FRAME_US / PWM_SLICES as u64;
|
||||
|
||||
// mix two 8-bit values across a fixed number of steps.
|
||||
fn blend(start_value: u8, end_value: u8, step: u16, steps: u16) -> u8 {
|
||||
let start_value = u16::from(start_value);
|
||||
let end_value = u16::from(end_value);
|
||||
let step = step.min(steps);
|
||||
let left = steps - step;
|
||||
(((start_value * left) + (end_value * step)) / steps) as u8
|
||||
}
|
||||
|
||||
// enerate one software PWM frame on the RGB GPIO pins
|
||||
async fn pwm_frame(
|
||||
red: &mut Output<'_>,
|
||||
green: &mut Output<'_>,
|
||||
blue: &mut Output<'_>,
|
||||
red_pct: u8,
|
||||
green_pct: u8,
|
||||
blue_pct: u8,
|
||||
) {
|
||||
for slice in 0..PWM_SLICES {
|
||||
if slice < u16::from(red_pct) {
|
||||
red.set_high();
|
||||
} else {
|
||||
red.set_low();
|
||||
}
|
||||
|
||||
if slice < u16::from(green_pct) {
|
||||
green.set_high();
|
||||
} else {
|
||||
green.set_low();
|
||||
}
|
||||
|
||||
if slice < u16::from(blue_pct) {
|
||||
blue.set_high();
|
||||
} else {
|
||||
blue.set_low();
|
||||
}
|
||||
|
||||
Timer::after_micros(PWM_SLICE_US).await;
|
||||
}
|
||||
}
|
||||
|
||||
// hold one RGB color for a fixed amount of time
|
||||
async fn show_color(
|
||||
red: &mut Output<'_>,
|
||||
green: &mut Output<'_>,
|
||||
blue: &mut Output<'_>,
|
||||
red_pct: u8,
|
||||
green_pct: u8,
|
||||
blue_pct: u8,
|
||||
hold_ms: u64,
|
||||
) {
|
||||
let frames = hold_ms / (PWM_FRAME_US / 1000);
|
||||
|
||||
for _ in 0..frames {
|
||||
pwm_frame(red, green, blue, red_pct, green_pct, blue_pct).await;
|
||||
}
|
||||
}
|
||||
|
||||
// fade the RGB output through a soft color loop
|
||||
async fn smooth_cycle(red: &mut Output<'_>, green: &mut Output<'_>, blue: &mut Output<'_>) {
|
||||
const COLORS: [(u8, u8, u8); 4] = [(100, 0, 0), (0, 100, 0), (0, 0, 100), (100, 0, 0)];
|
||||
|
||||
for pair in COLORS.windows(2) {
|
||||
let (r0, g0, b0) = pair[0];
|
||||
let (r1, g1, b1) = pair[1];
|
||||
|
||||
info!(
|
||||
"fade step: {}% {}% {}% -> {}% {}% {}%",
|
||||
r0, g0, b0, r1, g1, b1
|
||||
);
|
||||
|
||||
for step in 0..=SMOOTH_STEPS {
|
||||
show_color(
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
blend(r0, r1, step, SMOOTH_STEPS),
|
||||
blend(g0, g1, step, SMOOTH_STEPS),
|
||||
blend(b0, b1, step, SMOOTH_STEPS),
|
||||
SMOOTH_STEP_MS,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// switch the RGB output through fixed hard colors.
|
||||
async fn hard_cycle(red: &mut Output<'_>, green: &mut Output<'_>, blue: &mut Output<'_>) {
|
||||
const COLORS: [((u8, u8, u8), &str); 8] = [
|
||||
((100, 0, 0), "red"),
|
||||
((0, 100, 0), "green"),
|
||||
((0, 0, 100), "blue"),
|
||||
((100, 100, 0), "yellow"),
|
||||
((0, 100, 100), "cyan"),
|
||||
((100, 0, 100), "magenta"),
|
||||
((100, 100, 100), "white"),
|
||||
((0, 0, 0), "off"),
|
||||
];
|
||||
|
||||
for &((r, g, b), name) in &COLORS {
|
||||
info!("hard step: {}", name);
|
||||
show_color(red, green, blue, r, g, b, HARD_HOLD_MS).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
let _gnd = Output::new(p.PA0, Level::Low, Speed::Low);
|
||||
let mut led = Output::new(p.PC13, Level::High, Speed::Low);
|
||||
let mut red = Output::new(p.PA1, Level::Low, Speed::Low);
|
||||
let mut green = Output::new(p.PA2, Level::Low, Speed::Low);
|
||||
let mut blue = Output::new(p.PA3, Level::Low, Speed::Low);
|
||||
|
||||
loop {
|
||||
led.toggle();
|
||||
info!("phase: smooth");
|
||||
smooth_cycle(&mut red, &mut green, &mut blue).await;
|
||||
|
||||
led.toggle();
|
||||
info!("phase: hard");
|
||||
hard_cycle(&mut red, &mut green, &mut blue).await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user