Handle errors properly

This commit is contained in:
Austen Adler 2021-08-07 13:58:38 -04:00
parent 409f1d65a5
commit 3a1faf7edc
2 changed files with 13 additions and 11 deletions

View File

@ -39,7 +39,7 @@ use ui::console_ui_loop;
fn main() -> Result<(), ProgramError> {
let (tx, rx) = channel::<Message>();
let handle = thread::spawn(move || -> Result<(), ProgramError> {
let strip_handle = thread::spawn(move || -> Result<(), ProgramError> {
let mut strip = LEDStrip::new(strip::Config {
// I have 89 right now, but start off with 20
num_lights: 20,
@ -49,13 +49,12 @@ fn main() -> Result<(), ProgramError> {
global_brightness_max: 20,
tick_time_ms: strip::DEFAULT_TICK_TIME_MS,
})?;
strip.strip_loop(&rx);
Err(ProgramError::General(String::from(
"Dead strip thread. Terminating",
)))
strip.strip_loop(&rx)
});
console_ui_loop(&tx)?;
// I do not care if the console ui crashes
let _console_ui_handle =
thread::spawn(move || -> Result<(), ProgramError> { console_ui_loop(&tx) });
handle.join()?
strip_handle.join()?
}

View File

@ -100,7 +100,7 @@ impl LEDStrip {
}
}
pub fn strip_loop(&mut self, rx: &Receiver<Message>) {
pub fn strip_loop(&mut self, rx: &Receiver<Message>) -> Result<(), ProgramError> {
let mut exit = false;
loop {
let target_time = Instant::now().add(Duration::from_millis(self.config.tick_time_ms));
@ -145,9 +145,12 @@ impl LEDStrip {
}
// TODO: Handle error properly
let result = self.pattern.step();
if let Ok(true) = result {
self.write_buf_from_pattern();
if self
.pattern
.step()
.map_err(|_| ProgramError::General(String::from("Pattern step failure")))?
{
self.write_buf_from_pattern()?;
}
if exit {