Skip to content

Commit

Permalink
Better handling of startup initialization and error
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Oct 27, 2024
1 parent 3fb6948 commit 25a6d60
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
20 changes: 13 additions & 7 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,17 @@ async fn main(spawner: Spawner) {
// Show splash screen for a while, ignore any error
let _ = ui.show_splash().await;

// Wait for network to become available (if not already), ignore any error
let _ = ui.wait_network_up().await;

// Refresh articles, ignore any error
let _ = ui.refresh_articles().await;
loop {
match ui.init().await {
// Success or cancel: continue
Ok(()) | Err(error::Error::Cancel) => break,
// Display error to user and try again
Err(err) => {
error!("Initialization error: {:?}", err);
let _ = ui.show_error(&err, false).await;
}
}
}

loop {
match ui.run().await {
Expand All @@ -241,8 +247,8 @@ async fn main(spawner: Spawner) {
Err(error::Error::UserTimeout) => info!("Timeout waiting for user, starting over..."),
// Display error to user and start over again
Err(err) => {
error!("Unhandled Error: {:?}", err);
let _ = ui.show_error(&err).await;
error!("Error: {:?}", err);
let _ = ui.show_error(&err, true).await;
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions firmware/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
}

/// Show error screen and wait for keypress or timeout
pub async fn show_error<M: fmt::Display>(&mut self, message: M) -> Result<(), Error> {
pub async fn show_error<M: fmt::Display>(
&mut self,
message: M,
interactive: bool,
) -> Result<(), Error> {
info!("UI: Displaying error: {}", message);

self.display.screen(&screen::Failure::new(message)).await?;
let _ = self.buzzer.error().await;
if interactive {
let _ = self.buzzer.error().await;
}

// Wait at least 1s without responding to keypad
let min_time = Duration::from_secs(1);
Expand Down Expand Up @@ -134,6 +140,7 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {

/// Refresh article names and prices
pub async fn refresh_articles(&mut self) -> Result<(), Error> {
// Wait for network to become available (if not already)
self.wait_network_up().await?;

info!("UI: Refreshing articles...");
Expand All @@ -153,6 +160,17 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
Ok(())
}

/// Initialize user interface
pub async fn init(&mut self) -> Result<(), Error> {
// Wait for network to become available (if not already)
self.wait_network_up().await?;

// Refresh article names and prices
self.refresh_articles().await?;

Ok(())
}

/// Run the user interface flow
pub async fn run(&mut self) -> Result<(), Error> {
// Wait for id card and verify identification
Expand All @@ -169,7 +187,7 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {

// TODO: Process payment
let _ = screen::Success::new(num_drinks);
let _ = self.show_error("Not implemented yet").await;
let _ = self.show_error("Not implemented yet", true).await;
let _key = self.keypad.read().await;
Ok(())
}
Expand Down

0 comments on commit 25a6d60

Please sign in to comment.