implement back button
This commit is contained in:
@@ -9,7 +9,7 @@ use embedded_graphics::pixelcolor::Rgb565;
|
|||||||
use embedded_graphics::prelude::RgbColor;
|
use embedded_graphics::prelude::RgbColor;
|
||||||
use embedded_graphics_core::draw_target::DrawTarget;
|
use embedded_graphics_core::draw_target::DrawTarget;
|
||||||
|
|
||||||
use super::state::Menu;
|
use super::state::ScreenItem;
|
||||||
|
|
||||||
pub const MAX_SELECTED: i32 = 3;
|
pub const MAX_SELECTED: i32 = 3;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ impl super::navigation::Navigable for MainMenu {
|
|||||||
fn handle_input(
|
fn handle_input(
|
||||||
&self,
|
&self,
|
||||||
input: super::navigation::Action,
|
input: super::navigation::Action,
|
||||||
) -> impl core::future::Future<Output = Menu> + Send {
|
) -> impl core::future::Future<Output = ScreenItem> + Send {
|
||||||
let new_menu = match input {
|
let new_menu = match input {
|
||||||
super::navigation::Action::Up => {
|
super::navigation::Action::Up => {
|
||||||
let new_selected = if (self.selected - 1) < 0 {
|
let new_selected = if (self.selected - 1) < 0 {
|
||||||
@@ -43,21 +43,21 @@ impl super::navigation::Navigable for MainMenu {
|
|||||||
} else {
|
} else {
|
||||||
self.selected - 1
|
self.selected - 1
|
||||||
};
|
};
|
||||||
Menu::Main(MainMenu {
|
ScreenItem::Main(MainMenu {
|
||||||
selected: new_selected,
|
selected: new_selected,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
super::navigation::Action::Down => {
|
super::navigation::Action::Down => {
|
||||||
let new_selected = self.selected.wrapping_add(1) % MAX_SELECTED;
|
let new_selected = self.selected.wrapping_add(1) % MAX_SELECTED;
|
||||||
Menu::Main(MainMenu {
|
ScreenItem::Main(MainMenu {
|
||||||
selected: new_selected,
|
selected: new_selected,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
super::navigation::Action::Ok => match self.selected {
|
super::navigation::Action::Ok => match self.selected {
|
||||||
2 => Menu::Settings(SettingsMenu { selected: 0 }),
|
2 => ScreenItem::Settings(SettingsMenu { selected: 0 }),
|
||||||
_ => Menu::Main(self.clone()),
|
_ => ScreenItem::Main(self.clone()),
|
||||||
},
|
},
|
||||||
_ => Menu::Main(self.clone()),
|
_ => ScreenItem::Main(self.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
future::ready(new_menu)
|
future::ready(new_menu)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::navigation::scan_menu::ScanMenu;
|
use crate::navigation::scan_menu::ScanMenu;
|
||||||
use crate::navigation::state::{Menu, NavigationState};
|
use crate::navigation::state::{NavigationState, ScreenItem};
|
||||||
use crate::{display::primary_lcd::PrimaryLcdDisplay, navigation::inputs::Inputs};
|
use crate::{display::primary_lcd::PrimaryLcdDisplay, navigation::inputs::Inputs};
|
||||||
|
|
||||||
use embassy_futures::select::Either6;
|
use embassy_futures::select::Either6;
|
||||||
@@ -23,15 +23,16 @@ pub trait Navigable {
|
|||||||
&self,
|
&self,
|
||||||
display: &mut PrimaryLcdDisplay<'static>,
|
display: &mut PrimaryLcdDisplay<'static>,
|
||||||
) -> impl core::future::Future<Output = ()> + Send;
|
) -> impl core::future::Future<Output = ()> + Send;
|
||||||
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = Menu> + Send;
|
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = ScreenItem> + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn run(mut inputs: Inputs, display: &'static mut PrimaryLcdDisplay<'static>) {
|
pub async fn run(mut inputs: Inputs, display: &'static mut PrimaryLcdDisplay<'static>) {
|
||||||
let mut state = NavigationState::default();
|
|
||||||
// state.current_screen = Menu::Scan(ScanMenu { progress: (100 * 30 / 255) as u8 });
|
|
||||||
|
|
||||||
log::info!("starting navigation");
|
log::info!("starting navigation");
|
||||||
|
|
||||||
|
let mut state = NavigationState::new();
|
||||||
|
state.screens.last().unwrap().display(display).await;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let selection = select6(
|
let selection = select6(
|
||||||
inputs.up.wait_for_low(),
|
inputs.up.wait_for_low(),
|
||||||
@@ -53,8 +54,21 @@ pub async fn run(mut inputs: Inputs, display: &'static mut PrimaryLcdDisplay<'st
|
|||||||
};
|
};
|
||||||
|
|
||||||
log::info!("Action: {:?}", action);
|
log::info!("Action: {:?}", action);
|
||||||
state.current_screen = state.current_screen.handle_input(action).await;
|
|
||||||
state.current_screen.display(display).await;
|
let screen = match action {
|
||||||
|
Action::Back => state.back(),
|
||||||
|
_ => {
|
||||||
|
state
|
||||||
|
.screens
|
||||||
|
.last()
|
||||||
|
.expect("no screen on stack")
|
||||||
|
.handle_input(action)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
screen.display(display).await;
|
||||||
|
state.screens.push(screen);
|
||||||
|
|
||||||
Timer::after(Duration::from_millis(DEBOUNCE_DURATION_MILLIS)).await;
|
Timer::after(Duration::from_millis(DEBOUNCE_DURATION_MILLIS)).await;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ use embedded_graphics::text::TextStyleBuilder;
|
|||||||
use embedded_graphics_core::draw_target::DrawTarget;
|
use embedded_graphics_core::draw_target::DrawTarget;
|
||||||
|
|
||||||
use super::navigation::Action;
|
use super::navigation::Action;
|
||||||
use super::state::Menu;
|
use super::state::ScreenItem;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ScanMenu {
|
pub struct ScanMenu {
|
||||||
@@ -82,14 +82,14 @@ impl super::navigation::Navigable for ScanMenu {
|
|||||||
core::future::ready(())
|
core::future::ready(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = Menu> + Send {
|
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = ScreenItem> + Send {
|
||||||
async move {
|
async move {
|
||||||
let new_progress = match input {
|
let new_progress = match input {
|
||||||
Action::Up => self.progress.wrapping_add(5),
|
Action::Up => self.progress.wrapping_add(5),
|
||||||
_ => self.progress,
|
_ => self.progress,
|
||||||
};
|
};
|
||||||
|
|
||||||
Menu::Scan(ScanMenu {
|
ScreenItem::Scan(ScanMenu {
|
||||||
progress: new_progress,
|
progress: new_progress,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::navigation::menu_item;
|
use crate::navigation::menu_item;
|
||||||
use crate::navigation::state::Menu;
|
use crate::navigation::state::ScreenItem;
|
||||||
use crate::{display::primary_lcd::PrimaryLcdDisplay, navigation::inputs::Inputs};
|
use crate::{display::primary_lcd::PrimaryLcdDisplay, navigation::inputs::Inputs};
|
||||||
|
|
||||||
use embedded_graphics::pixelcolor::Rgb565;
|
use embedded_graphics::pixelcolor::Rgb565;
|
||||||
@@ -27,7 +27,7 @@ impl super::navigation::Navigable for SettingsMenu {
|
|||||||
fn handle_input(
|
fn handle_input(
|
||||||
&self,
|
&self,
|
||||||
input: super::navigation::Action,
|
input: super::navigation::Action,
|
||||||
) -> impl core::future::Future<Output = super::state::Menu> + Send {
|
) -> impl core::future::Future<Output = ScreenItem> + Send {
|
||||||
async move { Menu::Settings(SettingsMenu { selected: 0 }) }
|
async move { ScreenItem::Settings(SettingsMenu { selected: 0 }) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-10
@@ -1,24 +1,26 @@
|
|||||||
use crate::navigation::{
|
use crate::navigation::{
|
||||||
main_menu::MainMenu, navigation::Navigable, scan_menu::ScanMenu, settings_menu::SettingsMenu,
|
main_menu::MainMenu, navigation::Navigable, scan_menu::ScanMenu, settings_menu::SettingsMenu,
|
||||||
};
|
};
|
||||||
|
use alloc::vec;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Menu {
|
pub enum ScreenItem {
|
||||||
Main(MainMenu),
|
Main(MainMenu),
|
||||||
Scan(ScanMenu),
|
Scan(ScanMenu),
|
||||||
Settings(SettingsMenu),
|
Settings(SettingsMenu),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Navigable for Menu {
|
impl Navigable for ScreenItem {
|
||||||
fn display(
|
fn display(
|
||||||
&self,
|
&self,
|
||||||
display: &mut crate::display::primary_lcd::PrimaryLcdDisplay<'static>,
|
display: &mut crate::display::primary_lcd::PrimaryLcdDisplay<'static>,
|
||||||
) -> impl core::future::Future<Output = ()> + Send {
|
) -> impl core::future::Future<Output = ()> + Send {
|
||||||
async move {
|
async move {
|
||||||
match self {
|
match self {
|
||||||
Menu::Main(main_menu) => main_menu.display(display).await,
|
ScreenItem::Main(main_menu) => main_menu.display(display).await,
|
||||||
Menu::Scan(scan_menu) => scan_menu.display(display).await,
|
ScreenItem::Scan(scan_menu) => scan_menu.display(display).await,
|
||||||
Menu::Settings(settings_menu) => settings_menu.display(display).await,
|
ScreenItem::Settings(settings_menu) => settings_menu.display(display).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,9 +31,9 @@ impl Navigable for Menu {
|
|||||||
) -> impl core::future::Future<Output = Self> + Send {
|
) -> impl core::future::Future<Output = Self> + Send {
|
||||||
async move {
|
async move {
|
||||||
match self {
|
match self {
|
||||||
Menu::Main(main_menu) => main_menu.handle_input(input).await,
|
ScreenItem::Main(main_menu) => main_menu.handle_input(input).await,
|
||||||
Menu::Scan(scan_menu) => scan_menu.handle_input(input).await,
|
ScreenItem::Scan(scan_menu) => scan_menu.handle_input(input).await,
|
||||||
Menu::Settings(settings_menu) => settings_menu.handle_input(input).await,
|
ScreenItem::Settings(settings_menu) => settings_menu.handle_input(input).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,13 +41,13 @@ impl Navigable for Menu {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct NavigationState {
|
pub struct NavigationState {
|
||||||
pub current_screen: Menu,
|
pub screens: Vec<ScreenItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for NavigationState {
|
impl Default for NavigationState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
current_screen: Menu::Main(MainMenu { selected: 0 }),
|
screens: vec![ScreenItem::Main(MainMenu { selected: 0 })],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,4 +56,13 @@ impl NavigationState {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn back(&mut self) -> ScreenItem {
|
||||||
|
let screen = self.screens.pop().unwrap();
|
||||||
|
if self.screens.is_empty() {
|
||||||
|
log::info!("last screen");
|
||||||
|
return screen;
|
||||||
|
}
|
||||||
|
self.screens.pop().unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user