Files
vfd/examples/blink.rs
Кобелев Андрей Андреевич 896670b62f Примеры и документация
2026-01-28 16:07:38 +05:00

63 lines
1.6 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use anyhow::Result;
use std::time::Duration;
use m::vfd::VfdConfig;
use m::worker::VfdWorker;
fn spaces(n: usize) -> String {
" ".repeat(n)
}
fn main() -> Result<()> {
// args:
// 1) port (default: /dev/cu.usbmodem101)
// 2) width (default: 20)
// 3) delay_ms (default: 400)
// 4) x (default: 1) 1-based
// 5) y (default: 1) 1-based line
// 6) text (default: "BLINK")
let mut args = std::env::args().skip(1);
let port_name = args.next().unwrap_or("/dev/cu.usbmodem101".into());
let width: usize = args.next().as_deref().unwrap_or("20").parse().unwrap_or(20);
let delay_ms: u64 = args
.next()
.as_deref()
.unwrap_or("400")
.parse()
.unwrap_or(400);
let x: u8 = args.next().as_deref().unwrap_or("1").parse().unwrap_or(1);
let y: u8 = args.next().as_deref().unwrap_or("1").parse().unwrap_or(1);
let text = args.next().unwrap_or("BLINK".into());
let text_len = text.chars().count();
let cfg = VfdConfig::new(port_name).with_width(width);
let worker = VfdWorker::start(cfg)?;
let vfd = worker.handle();
vfd.clear();
let _ = vfd.print_line_diff(1, "blink demo (print_at)");
if y != 1 {
let _ = vfd.print_at(1, y, ""); // просто чтобы “активировать” строку у некоторых дисплеев
}
let blank = spaces(text_len);
let mut on = false;
loop {
if on {
let _ = vfd.print_at(x, y, &blank);
} else {
let _ = vfd.print_at(x, y, &text);
}
on = !on;
std::thread::sleep(Duration::from_millis(delay_ms));
}
}