Improve logging

This commit is contained in:
Austen Adler 2023-05-28 17:31:34 -04:00
parent 7a43ae7431
commit 1c6eefc004

View File

@ -5,6 +5,7 @@ use common::{
pattern::{self, Pattern}, pattern::{self, Pattern},
strip::Message, strip::Message,
}; };
use tracing::{error, info};
use std::{ use std::{
cmp, cmp,
ops::Add, ops::Add,
@ -25,11 +26,11 @@ pub const MIN_TICK_TIME: u64 = 10;
#[derive(Debug, Clone, Parser)] #[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)))] #[clap(short, 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( #[clap(
short = 's', short,
long, long,
env, env,
default_value_t = 0, default_value_t = 0,
@ -47,6 +48,10 @@ pub struct Config {
/// 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..))] #[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,
/// The default adapter
#[clap(long, default_value = "/dev/spidev0.0", help = "The serial interface")]
pub serial_interface: String,
} }
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
@ -59,8 +64,8 @@ pub struct LedStrip {
impl LedStrip { impl LedStrip {
pub fn new(config: Config) -> Result<Self, ProgramError> { pub fn new(config: Config) -> Result<Self, ProgramError> {
let adapter = Box::new( let adapter = Box::new(
Ws28xxSpiAdapter::new("/dev/spidev0.0") Ws28xxSpiAdapter::new(&config.serial_interface)
.map_err(|_| "Cannot start device /dev/spidev0.0!")?, .map_err(|_| format!("Cannot start device {}!", config.serial_interface))?,
); );
let pattern = Box::new(pattern::Solid::new(&pattern::SolidParams { let pattern = Box::new(pattern::Solid::new(&pattern::SolidParams {
color: color::BLACK, color: color::BLACK,
@ -103,14 +108,17 @@ impl LedStrip {
fn set_num_lights(&mut self, num_lights: u16) { fn set_num_lights(&mut self, num_lights: u16) {
if num_lights > MAX_NUM_LIGHTS { if num_lights > MAX_NUM_LIGHTS {
println!("Cannot set lights to {num_lights} as it exceeds max of {MAX_NUM_LIGHTS}"); error!("Cannot set lights to {num_lights} as it exceeds max of {MAX_NUM_LIGHTS}");
return; return;
} }
if let Err(e) = self.pattern.cleanup() { if let Err(e) = self.pattern.cleanup() {
eprintln!("Error cleaning up old pattern: {e:?}"); error!("Error cleaning up old pattern: {e:?}");
} }
if self.pattern.init(num_lights).is_ok() { if self.pattern.init(num_lights).is_ok() {
self.config.num_lights = num_lights; self.config.num_lights = num_lights;
info!("Updated num_lights to {}", self.config.tick_time_ms);
} else {
error!("Could not initialize pattern with new num_lights value {num_lights}");
} }
} }
@ -131,6 +139,7 @@ impl LedStrip {
})); }));
if pat.init(self.config.num_lights).is_ok() { if pat.init(self.config.num_lights).is_ok() {
self.pattern = pat; self.pattern = pat;
info!("Cleared lights");
} else { } else {
let _result = message_tx.send(error::Message::String(format!( let _result = message_tx.send(error::Message::String(format!(
"Clearing light strip: {pat:?}" "Clearing light strip: {pat:?}"
@ -140,10 +149,11 @@ impl LedStrip {
Message::ChangePattern(mut pat) => match pat.init(self.config.num_lights) { Message::ChangePattern(mut pat) => match pat.init(self.config.num_lights) {
Ok(()) => { Ok(()) => {
if let Err(e) = self.pattern.cleanup() { if let Err(e) = self.pattern.cleanup() {
eprintln!("Error cleaning up old pattern: {e:?}"); error!("Error cleaning up old pattern: {e:?}");
} }
self.pattern = pat; self.pattern = pat;
info!("Changed pattern");
} }
Err(e) => { Err(e) => {
let _result = message_tx.send(error::Message::String(format!( let _result = message_tx.send(error::Message::String(format!(
@ -161,6 +171,7 @@ impl LedStrip {
))); )));
} }
self.config.tick_time_ms = tick_time_ms; self.config.tick_time_ms = tick_time_ms;
info!("Updated tick time to {}", self.config.tick_time_ms);
} }
Message::Quit => { Message::Quit => {
exit = true; exit = true;
@ -169,7 +180,7 @@ impl LedStrip {
}); });
if pat.init(self.config.num_lights).is_ok() { if pat.init(self.config.num_lights).is_ok() {
if let Err(e) = self.pattern.cleanup() { if let Err(e) = self.pattern.cleanup() {
eprintln!("Error cleaning up old pattern: {e:?}"); error!("Error cleaning up old pattern: {e:?}");
} }
self.pattern = Box::new(pat); self.pattern = Box::new(pat);
} else { } else {