Use clap to parse arguments
This commit is contained in:
parent
0a13264096
commit
7a43ae7431
@ -20,6 +20,8 @@ ws2818-rgb-led-spi-driver = { path = "./lib-ws2818-rgb-led-spi-driver" }
|
|||||||
common = { path = "./common" }
|
common = { path = "./common" }
|
||||||
webui = { path = "./webui" }
|
webui = { path = "./webui" }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
|
clap = { version = "4.3.0", features = ["derive", "env"] }
|
||||||
|
tracing = "0.1.37"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
23
src/main.rs
23
src/main.rs
@ -33,6 +33,7 @@
|
|||||||
// mod pattern;
|
// mod pattern;
|
||||||
mod strip;
|
mod strip;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
use clap::Parser;
|
||||||
use common::error::{self, ProgramError, ProgramResult};
|
use common::error::{self, ProgramError, ProgramResult};
|
||||||
use std::{
|
use std::{
|
||||||
io,
|
io,
|
||||||
@ -47,31 +48,17 @@ fn main() -> ProgramResult<()> {
|
|||||||
// Initialize any config
|
// Initialize any config
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
|
|
||||||
|
// Use clap to parse the configuration
|
||||||
|
let config = strip::Config::parse();
|
||||||
|
|
||||||
// Strip control transmitter and receiver
|
// Strip control transmitter and receiver
|
||||||
let (strip_tx, strip_rx) = channel::<common::strip::Message>();
|
let (strip_tx, strip_rx) = channel::<common::strip::Message>();
|
||||||
let (console_strip_tx, webui_strip_tx) = (strip_tx.clone(), strip_tx);
|
let (console_strip_tx, webui_strip_tx) = (strip_tx.clone(), strip_tx);
|
||||||
|
|
||||||
let (message_tx, message_rx) = channel::<error::Message>();
|
let (message_tx, message_rx) = channel::<error::Message>();
|
||||||
|
|
||||||
let num_lights: u16 = std::env::var("NUM_LIGHTS")
|
|
||||||
.as_ref()
|
|
||||||
.map(String::as_str)
|
|
||||||
.unwrap_or("89")
|
|
||||||
.parse()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
eprintln!("Number of lights: {num_lights}");
|
|
||||||
|
|
||||||
make_child(message_tx.clone(), move |message_tx| -> ProgramResult<()> {
|
make_child(message_tx.clone(), move |message_tx| -> ProgramResult<()> {
|
||||||
let mut strip = LedStrip::new(strip::Config {
|
let mut strip = LedStrip::new(config)?;
|
||||||
// I have 89 right now, but start off with 20
|
|
||||||
num_lights,
|
|
||||||
// Skip 14 lights
|
|
||||||
shift_lights: 14,
|
|
||||||
// Scaling factor (scale 0..255)
|
|
||||||
global_brightness_max: 255,
|
|
||||||
tick_time_ms: strip::DEFAULT_TICK_TIME_MS,
|
|
||||||
})?;
|
|
||||||
strip.strip_loop(message_tx, &strip_rx)
|
strip.strip_loop(message_tx, &strip_rx)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
18
src/strip.rs
18
src/strip.rs
@ -1,3 +1,4 @@
|
|||||||
|
use clap::Parser;
|
||||||
use common::{
|
use common::{
|
||||||
color, error,
|
color, error,
|
||||||
error::ProgramError,
|
error::ProgramError,
|
||||||
@ -21,15 +22,30 @@ pub const DEFAULT_TICK_TIME_MS: u64 = 10;
|
|||||||
/// Minimum time per tick before strip breaks
|
/// Minimum time per tick before strip breaks
|
||||||
pub const MIN_TICK_TIME: u64 = 10;
|
pub const MIN_TICK_TIME: u64 = 10;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Number of lights
|
/// Number of lights
|
||||||
|
#[clap(short = 'c', long, help = "Number of lights in the strip", value_parser = clap::value_parser!(u16).range(1..=(MAX_NUM_LIGHTS as i64)))]
|
||||||
pub num_lights: u16,
|
pub num_lights: u16,
|
||||||
/// Number of lights to skip
|
/// Number of lights to skip
|
||||||
|
#[clap(
|
||||||
|
short = 's',
|
||||||
|
long,
|
||||||
|
env,
|
||||||
|
default_value_t = 0,
|
||||||
|
help = "Number of lights to skip in the beginning"
|
||||||
|
)]
|
||||||
pub shift_lights: u16,
|
pub shift_lights: u16,
|
||||||
/// Global brightness multiplier
|
/// Global brightness multiplier
|
||||||
|
#[clap(
|
||||||
|
long,
|
||||||
|
env,
|
||||||
|
default_value_t = 255,
|
||||||
|
help = "The max brightness (clamped)"
|
||||||
|
)]
|
||||||
pub global_brightness_max: u8,
|
pub global_brightness_max: u8,
|
||||||
/// Time per tick
|
/// Time per tick
|
||||||
|
#[clap(long = "tick_time", env, default_value_t = DEFAULT_TICK_TIME_MS, help = "Tick time in milliseconds", value_parser = clap::value_parser!(u64).range(MIN_TICK_TIME..))]
|
||||||
pub tick_time_ms: u64,
|
pub tick_time_ms: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user