Use clap to parse arguments

This commit is contained in:
Austen Adler 2023-05-28 17:23:55 -04:00
parent 0a13264096
commit 7a43ae7431
3 changed files with 24 additions and 19 deletions

View File

@ -20,6 +20,8 @@ ws2818-rgb-led-spi-driver = { path = "./lib-ws2818-rgb-led-spi-driver" }
common = { path = "./common" }
webui = { path = "./webui" }
dotenv = "0.15.0"
clap = { version = "4.3.0", features = ["derive", "env"] }
tracing = "0.1.37"
[profile.release]
lto = true

View File

@ -33,6 +33,7 @@
// mod pattern;
mod strip;
mod ui;
use clap::Parser;
use common::error::{self, ProgramError, ProgramResult};
use std::{
io,
@ -47,31 +48,17 @@ fn main() -> ProgramResult<()> {
// Initialize any config
dotenv::dotenv().ok();
// Use clap to parse the configuration
let config = strip::Config::parse();
// Strip control transmitter and receiver
let (strip_tx, strip_rx) = channel::<common::strip::Message>();
let (console_strip_tx, webui_strip_tx) = (strip_tx.clone(), strip_tx);
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<()> {
let mut strip = LedStrip::new(strip::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,
})?;
let mut strip = LedStrip::new(config)?;
strip.strip_loop(message_tx, &strip_rx)
});

View File

@ -1,3 +1,4 @@
use clap::Parser;
use common::{
color, error,
error::ProgramError,
@ -21,15 +22,30 @@ pub const DEFAULT_TICK_TIME_MS: u64 = 10;
/// Minimum time per tick before strip breaks
pub const MIN_TICK_TIME: u64 = 10;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Parser)]
pub struct Config {
/// 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,
/// 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,
/// Global brightness multiplier
#[clap(
long,
env,
default_value_t = 255,
help = "The max brightness (clamped)"
)]
pub global_brightness_max: u8,
/// 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,
}