From 2fed645d22a0462392bd281bfbf981689eb47a61 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sun, 28 May 2023 18:47:45 -0400 Subject: [PATCH] Allow pattern initialization --- Cargo.toml | 1 + README.adoc | 7 +++++++ common/Cargo.toml | 1 + common/src/pattern.rs | 2 +- common/src/pattern/car_rainbow.rs | 1 + common/src/strip.rs | 2 +- src/main.rs | 4 ++++ src/strip.rs | 30 ++++++++++++++++++++++++++---- src/ui.rs | 2 +- 9 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49edca9..c5e84d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ webui = { path = "./webui" } dotenv = "0.15.0" clap = { version = "4.3.0", features = ["derive", "env"] } tracing = "0.1.37" +env_logger = "0.10.0" [profile.release] lto = true diff --git a/README.adoc b/README.adoc index d3426db..9f79683 100644 --- a/README.adoc +++ b/README.adoc @@ -37,3 +37,10 @@ xbps-install -y cross-armv7l-linux-gnueabihf rustup target add armv7-unknown-linux-gnueabihf make deploy ---- + +== Notes + +---- +Car: 102 +House: 89; skip 14 +---- diff --git a/common/Cargo.toml b/common/Cargo.toml index 9217e39..cdd1f8d 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -10,3 +10,4 @@ serde_json = "1" parking_lot = "0.12" crossbeam-channel = "0.5.6" strum = { version = "0.24.1", features = ["derive"] } +tracing = "0.1.37" diff --git a/common/src/pattern.rs b/common/src/pattern.rs index b36013d..0f7cc87 100644 --- a/common/src/pattern.rs +++ b/common/src/pattern.rs @@ -135,7 +135,7 @@ pub enum Parameters { impl Default for Parameters { fn default() -> Self { - Self::Visualizer(VisualizerParams::default()) + Self::MovingRainbow(Default::default()) } } diff --git a/common/src/pattern/car_rainbow.rs b/common/src/pattern/car_rainbow.rs index 7ec99ce..2e21dfd 100644 --- a/common/src/pattern/car_rainbow.rs +++ b/common/src/pattern/car_rainbow.rs @@ -2,6 +2,7 @@ use super::{ColorIterator, FormRender, InputRender, Pattern, PatternError, Patte use crate::color::{self, Rgb, RAINBOW}; use serde::{Deserialize, Serialize}; use std::{collections::VecDeque, convert::TryFrom, iter}; +use tracing::info; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct CarRainbowParams { diff --git a/common/src/strip.rs b/common/src/strip.rs index d6f226a..83e4fb4 100644 --- a/common/src/strip.rs +++ b/common/src/strip.rs @@ -1,7 +1,7 @@ use crate::pattern::Pattern; pub enum Message { ClearLights, - ChangePattern(Box), + ChangePattern(Box), SetNumLights(u16), SetTickTime(u64), Quit, diff --git a/src/main.rs b/src/main.rs index 26874bb..32a3beb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ use ui::console_ui_loop; fn main() -> ProgramResult<()> { // Initialize any config dotenv::dotenv().ok(); + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); // Use clap to parse the configuration let config = strip::Config::parse(); @@ -57,15 +58,18 @@ fn main() -> ProgramResult<()> { let (message_tx, message_rx) = channel::(); + // The strip itself make_child(message_tx.clone(), move |message_tx| -> ProgramResult<()> { let mut strip = LedStrip::new(config)?; strip.strip_loop(message_tx, &strip_rx) }); + // Text user-interface make_child(message_tx.clone(), move |message_tx| -> ProgramResult<()> { console_ui_loop(message_tx, &console_strip_tx) }); + // Webui user-interface make_child(message_tx, move |message_tx| -> ProgramResult<()> { webui::start(message_tx.clone(), webui_strip_tx).map_err(ProgramError::IoError) }); diff --git a/src/strip.rs b/src/strip.rs index 21f1b5f..8a385f2 100644 --- a/src/strip.rs +++ b/src/strip.rs @@ -9,6 +9,7 @@ use std::{ cmp, ops::Add, process, + str::FromStr, sync::mpsc::{Receiver, Sender}, thread, time::{Duration, Instant}, @@ -52,13 +53,17 @@ pub struct Config { /// The default adapter #[clap(long, default_value = "/dev/spidev0.0", help = "The serial interface")] pub serial_interface: String, + + /// The initial pattern + #[clap(short, long, help = "The name of the initial pattern")] + pub initial_pattern: Option, } #[allow(clippy::module_name_repetitions)] pub struct LedStrip { pub adapter: Box, pub config: Config, - pub pattern: Box, + pub pattern: Box, } impl LedStrip { @@ -67,16 +72,33 @@ impl LedStrip { Ws28xxSpiAdapter::new(&config.serial_interface) .map_err(|_| format!("Cannot start device {}!", config.serial_interface))?, ); - let pattern = Box::new(pattern::Solid::new(&pattern::SolidParams { - color: color::BLACK, - })); let num_lights = config.num_lights; + + // Initialize the pattern if they requested one; if there are any issues, initialize with black instead + let pattern = config + .initial_pattern + .as_ref() + .and_then(|ip| { + pattern::Parameters::from_str(ip) + .as_ref() + .map(pattern::Parameters::to_pattern) + .map_err(|e| error!("Could not initialize with requested pattern {ip}: {e:?}")) + .ok() + }) + .unwrap_or_else(|| { + info!("Using default black pattern"); + Box::new(pattern::Solid::new(&pattern::SolidParams { + color: color::BLACK, + })) + }); + let mut ret = Self { adapter, config, pattern, }; ret.set_num_lights(num_lights); + Ok(ret) } diff --git a/src/ui.rs b/src/ui.rs index 8531361..7663d3e 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -123,7 +123,7 @@ fn parse_color(r: &str, g: &str, b: &str) -> Result { fn change_pattern( strip_tx: &Sender, - pat: Box, + pat: Box, ) -> Result<(), String> { strip_tx .send(strip::Message::ChangePattern(pat))