Skip to content

Commit

Permalink
Slightly refactor ui flow
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Sep 23, 2024
1 parent 628cf97 commit 6440ba7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async fn main(spawner: Spawner) {
// Cancel: start over again
Err(ui::Error::Cancel) => info!("User cancelled, starting over..."),
// Timeout: start over again
Err(ui::Error::Timeout) => info!("Timeout waiting for user, starting over..."),
Err(ui::Error::UserTimeout) => info!("Timeout waiting for user, starting over..."),
// TODO: Display error to user and start over again
Err(err) => panic!("Unhandled Error: {:?}", err),
}
Expand Down
57 changes: 31 additions & 26 deletions firmware/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub enum Error {
/// User cancel request
Cancel,
/// User interaction timeout
Timeout,
UserTimeout,
}

impl From<display::Error> for Error {
Expand All @@ -59,7 +59,7 @@ impl From<nfc::Error> for Error {

impl From<TimeoutError> for Error {
fn from(_err: TimeoutError) -> Self {
Self::Timeout
Self::UserTimeout
}
}

Expand Down Expand Up @@ -102,8 +102,32 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
Ok(())
}

/// Run the user interface flow
pub async fn run(&mut self) -> Result<(), Error> {
// Wait for id card and verify identification
let _uid = self.read_id_card().await?;
// Ask for number of drinks
let num_drinks = self.get_number_of_drinks().await?;
// Calculate total price. It's ok to cast num_drinks to f32 as it's always a small number.
#[allow(clippy::cast_precision_loss)]
let total_price = PRICE * num_drinks as f32;
// Show total price and ask for confirmation
self.confirm_purchase(num_drinks, total_price).await?;

// TODO: Process payment
let _ = screen::Success::new(num_drinks);
self.display
.screen(&screen::Failure::new("Not implemented yet"))
.await?;
let _ = self.buzzer.error().await;
let _key = self.keypad.read().await;
Ok(())
}
}

impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
/// Wait for id card and verify identification
pub async fn read_id_card(&mut self) -> Result<Uid, Error> {
async fn read_id_card(&mut self) -> Result<Uid, Error> {
info!("UI: Waiting for NFC card...");

let mut saving_power = false;
Expand Down Expand Up @@ -140,7 +164,7 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
}

/// Ask for number of drinks
pub async fn get_number_of_drinks(&mut self) -> Result<usize, Error> {
async fn get_number_of_drinks(&mut self) -> Result<usize, Error> {
info!("UI: Asking for number of drinks...");

self.display.screen(&screen::NumberOfDrinks).await?;
Expand All @@ -159,10 +183,10 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
}
}

/// Confirm purchase
pub async fn checkout(&mut self, num_drinks: usize, total_price: f32) -> Result<(), Error> {
/// Show total price and ask for confirmation
async fn confirm_purchase(&mut self, num_drinks: usize, total_price: f32) -> Result<(), Error> {
info!(
"UI: Asking for checkout of {} drinks, {:.02} EUR...",
"UI: Asking for purchase confirmation of {} drinks, {:.02} EUR...",
num_drinks, total_price
);

Expand All @@ -180,23 +204,4 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
}
}
}

/// Run the user interface flow
pub async fn run(&mut self) -> Result<(), Error> {
let _uid = self.read_id_card().await?;
let num_drinks = self.get_number_of_drinks().await?;
// It's ok to cast num_drinks to f32 as it's always a small number
#[allow(clippy::cast_precision_loss)]
let total_price = PRICE * num_drinks as f32;
self.checkout(num_drinks, total_price).await?;

// TODO: Process payment
let _ = screen::Success::new(num_drinks);
self.display
.screen(&screen::Failure::new("Not implemented yet"))
.await?;
let _ = self.buzzer.error().await;
let _key = self.keypad.read().await;
Ok(())
}
}

0 comments on commit 6440ba7

Please sign in to comment.