Improve logging
This commit is contained in:
parent
7a43ae7431
commit
1c6eefc004
27
src/strip.rs
27
src/strip.rs
@ -5,6 +5,7 @@ use common::{
|
||||
pattern::{self, Pattern},
|
||||
strip::Message,
|
||||
};
|
||||
use tracing::{error, info};
|
||||
use std::{
|
||||
cmp,
|
||||
ops::Add,
|
||||
@ -25,11 +26,11 @@ pub const MIN_TICK_TIME: u64 = 10;
|
||||
#[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)))]
|
||||
#[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,
|
||||
/// Number of lights to skip
|
||||
#[clap(
|
||||
short = 's',
|
||||
short,
|
||||
long,
|
||||
env,
|
||||
default_value_t = 0,
|
||||
@ -47,6 +48,10 @@ pub struct Config {
|
||||
/// 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,
|
||||
|
||||
/// The default adapter
|
||||
#[clap(long, default_value = "/dev/spidev0.0", help = "The serial interface")]
|
||||
pub serial_interface: String,
|
||||
}
|
||||
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
@ -59,8 +64,8 @@ pub struct LedStrip {
|
||||
impl LedStrip {
|
||||
pub fn new(config: Config) -> Result<Self, ProgramError> {
|
||||
let adapter = Box::new(
|
||||
Ws28xxSpiAdapter::new("/dev/spidev0.0")
|
||||
.map_err(|_| "Cannot start device /dev/spidev0.0!")?,
|
||||
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,
|
||||
@ -103,14 +108,17 @@ impl LedStrip {
|
||||
|
||||
fn set_num_lights(&mut self, num_lights: u16) {
|
||||
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;
|
||||
}
|
||||
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() {
|
||||
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() {
|
||||
self.pattern = pat;
|
||||
info!("Cleared lights");
|
||||
} else {
|
||||
let _result = message_tx.send(error::Message::String(format!(
|
||||
"Clearing light strip: {pat:?}"
|
||||
@ -140,10 +149,11 @@ impl LedStrip {
|
||||
Message::ChangePattern(mut pat) => match pat.init(self.config.num_lights) {
|
||||
Ok(()) => {
|
||||
if let Err(e) = self.pattern.cleanup() {
|
||||
eprintln!("Error cleaning up old pattern: {e:?}");
|
||||
error!("Error cleaning up old pattern: {e:?}");
|
||||
}
|
||||
|
||||
self.pattern = pat;
|
||||
info!("Changed pattern");
|
||||
}
|
||||
Err(e) => {
|
||||
let _result = message_tx.send(error::Message::String(format!(
|
||||
@ -161,6 +171,7 @@ impl LedStrip {
|
||||
)));
|
||||
}
|
||||
self.config.tick_time_ms = tick_time_ms;
|
||||
info!("Updated tick time to {}", self.config.tick_time_ms);
|
||||
}
|
||||
Message::Quit => {
|
||||
exit = true;
|
||||
@ -169,7 +180,7 @@ impl LedStrip {
|
||||
});
|
||||
if pat.init(self.config.num_lights).is_ok() {
|
||||
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);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user