Skip to content

Commit

Permalink
Show random greetings
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Nov 9, 2024
1 parent c40780f commit 05b8a70
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions firmware/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Show random greetings to user

## 0.1.0 - 2024-10-30

- Showcased on general meeting
Expand Down
1 change: 1 addition & 0 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ async fn main(spawner: Spawner) {

// Create UI
let mut ui = ui::Ui::new(
rng,
&mut display,
&mut keypad,
&mut nfc,
Expand Down
19 changes: 16 additions & 3 deletions firmware/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use embedded_graphics::draw_target::DrawTarget;
use embedded_graphics::image::{Image, ImageRaw};
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
use rand_core::RngCore;
use u8g2_fonts::types::{FontColor, HorizontalAlignment, VerticalPosition};
use u8g2_fonts::{fonts, FontRenderer};

Expand Down Expand Up @@ -173,12 +174,16 @@ impl Screen for ScanId {

/// Prompt to ask for number of drinks
pub struct NumberOfDrinks<N> {
greeting: u32,
name: N,
}

impl<N: fmt::Display> NumberOfDrinks<N> {
pub fn new(name: N) -> Self {
Self { name }
pub fn new<RNG: RngCore>(rng: &mut RNG, name: N) -> Self {
Self {
greeting: rng.next_u32(),
name,
}
}
}

Expand All @@ -187,8 +192,16 @@ impl<N: fmt::Display> Screen for NumberOfDrinks<N> {
&self,
target: &mut D,
) -> Result<(), Error<D::Error>> {
static GREETINGS: [&str; 9] = [
"Hallo", "Hi", "Hey", "Tach", "Servus", "Moin", "Hej", "Olá", "Ciao",
];

MEDIUM_FONT.render_aligned(
format_args!("Hallo {}", self.name),
format_args!(
"{} {}",
GREETINGS[self.greeting as usize % GREETINGS.len()],
self.name
),
Point::new(63, 8),
VerticalPosition::Baseline,
HorizontalAlignment::Center,
Expand Down
12 changes: 8 additions & 4 deletions firmware/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use embassy_time::{with_timeout, Duration, TimeoutError, Timer};
use embedded_hal_async::digital::Wait;
use embedded_hal_async::i2c::I2c;
use log::info;
use rand_core::RngCore;

/// How long to show the splash screen if no key is pressed
const SPLASH_TIMEOUT: Duration = Duration::from_secs(5);
Expand All @@ -36,7 +37,8 @@ const IDLE_TIMEOUT: Duration = Duration::from_secs(300);
const IDLE_TIMEOUT: Duration = Duration::from_secs(10);

/// User interface
pub struct Ui<'a, I2C, IRQ> {
pub struct Ui<'a, RNG, I2C, IRQ> {
rng: RNG,
display: &'a mut Display<I2C>,
keypad: &'a mut Keypad<'a, 3, 4>,
nfc: &'a mut Nfc<I2C, IRQ>,
Expand All @@ -47,10 +49,11 @@ pub struct Ui<'a, I2C, IRQ> {
users: &'a mut Users,
}

impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
impl<'a, RNG: RngCore, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, RNG, I2C, IRQ> {
/// Create user interface with given human interface devices
#[allow(clippy::too_many_arguments)]
pub fn new(
rng: RNG,
display: &'a mut Display<I2C>,
keypad: &'a mut Keypad<'a, 3, 4>,
nfc: &'a mut Nfc<I2C, IRQ>,
Expand All @@ -61,6 +64,7 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
users: &'a mut Users,
) -> Self {
Self {
rng,
display,
keypad,
nfc,
Expand Down Expand Up @@ -213,7 +217,7 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
}
}

impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
impl<'a, RNG: RngCore, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, RNG, I2C, IRQ> {
/// Authentication: wait for id card, read it and look up the associated user. On idle timeout,
/// enter power saving (turn off display). Any key pressed leaves power saving (turn on
/// display).
Expand Down Expand Up @@ -265,7 +269,7 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
info!("UI: Asking for number of drinks...");

self.display
.screen(&screen::NumberOfDrinks::new(name))
.screen(&screen::NumberOfDrinks::new(&mut self.rng, name))
.await?;
loop {
#[allow(clippy::match_same_arms)]
Expand Down

0 comments on commit 05b8a70

Please sign in to comment.