Skip to content

Commit

Permalink
Merge pull request #160 from card-io-ecg/menu_update
Browse files Browse the repository at this point in the history
Update embedded-menu
  • Loading branch information
bugadani authored Feb 25, 2024
2 parents 61e5cad + f7bebca commit eaf8e17
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ embedded-hal-bus = { version = "0.1.0", features = ["async"] }
embedded-nal-async = "0.7.0"
embedded-io = "0.6"
embedded-io-async = "0.6"
embedded-menu = "0.5.2"
embedded-menu = "0.6.0"
embedded-svc = { version = "0.27", default-features = false }
embassy-net = { version = "0.4", features = [
"tcp",
Expand Down
5 changes: 2 additions & 3 deletions gui/src/screens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ pub mod qr;
pub mod wifi_ap;

pub const fn menu_style<R>(
) -> MenuStyle<BinaryColor, AnimatedTriangle, SingleTouch, AnimatedPosition, R> {
) -> MenuStyle<AnimatedTriangle, SingleTouch, AnimatedPosition, R, BinaryColor> {
MenuStyle::new(BinaryColor::On)
.with_animated_selection_indicator(10)
.with_details_delay(300)
.with_selection_indicator(AnimatedTriangle::new(200))
.with_input_adapter(SingleTouch {
debounce_time: 1,
Expand All @@ -40,7 +39,7 @@ pub const fn menu_style<R>(

pub fn create_menu<T: AsRef<str>, R>(
title: T,
) -> MenuBuilder<T, SingleTouch, NoItems, R, BinaryColor, AnimatedPosition, AnimatedTriangle> {
) -> MenuBuilder<T, SingleTouch, NoItems, R, AnimatedPosition, AnimatedTriangle, BinaryColor> {
Menu::with_style(title, menu_style())
}

Expand Down
26 changes: 14 additions & 12 deletions gui/src/screens/wifi_ap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use embedded_graphics::{pixelcolor::BinaryColor, prelude::DrawTarget, Drawable};
use embedded_layout::{chain, object_chain::Chain};
use embedded_menu::{
interaction::single_touch::SingleTouch,
items::MenuItem,
selection_indicator::{style::animated_triangle::AnimatedTriangle, AnimatedPosition},
Menu,
};
Expand All @@ -17,26 +19,26 @@ pub enum ApMenuEvents {
Exit,
}

#[derive(Clone, Copy, PartialEq, Eq, Menu)]
#[menu(
title = "WiFi Config",
navigation(events = ApMenuEvents),
items = [
navigation(label = "Exit", event = ApMenuEvents::Exit)
]
)]
pub struct ApMenu {}

pub struct WifiApScreen {
pub menu: ApMenuMenuWrapper<SingleTouch, AnimatedPosition, AnimatedTriangle>,
pub menu: Menu<
&'static str,
SingleTouch,
chain! { MenuItem<&'static str, ApMenuEvents, (), true> },
ApMenuEvents,
AnimatedPosition,
AnimatedTriangle,
BinaryColor,
>,
pub state: WifiAccessPointState,
pub timeout: Option<u8>,
}

impl WifiApScreen {
pub fn new() -> Self {
Self {
menu: ApMenu {}.create_menu_with_style(menu_style()),
menu: Menu::with_style("WiFi Config", menu_style())
.add_item("Exit", (), |_| ApMenuEvents::Exit)
.build(),
state: WifiAccessPointState::NotConnected,
timeout: None,
}
Expand Down
11 changes: 6 additions & 5 deletions gui/src/widgets/battery_small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use embedded_graphics::{
};
use embedded_io_async::{Read, Write};
use embedded_layout::prelude::*;
use embedded_menu::items::select::SelectValue;
use embedded_menu::items::menu_item::SelectValue;
use norfs::storable::{LoadError, Loadable, Storable};
use ufmt::uwrite;

Expand Down Expand Up @@ -298,15 +298,16 @@ impl Drawable for Battery {
}

impl SelectValue for BatteryStyle {
fn next(&self) -> Self {
match self {
fn next(&mut self) {
*self = match self {
Self::MilliVolts => Self::Percentage,
Self::Percentage => Self::Icon,
Self::Icon => Self::LowIndicator,
Self::LowIndicator => Self::MilliVolts,
}
};
}
fn name(&self) -> &'static str {

fn marker(&self) -> &'static str {
match self {
Self::MilliVolts => "MilliVolts",
Self::Percentage => "Percentage",
Expand Down
10 changes: 8 additions & 2 deletions gui/src/widgets/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ where

fn at(&self, _idx: usize) -> &dyn View {
match self {
Self::Hidden(_) => unsafe { &EMPTY_VIEW_GROUP },
Self::Hidden(_) => unsafe {
#[allow(static_mut_ref)]
&EMPTY_VIEW_GROUP
},
Self::Visible(view) => view,
}
}

fn at_mut(&mut self, _idx: usize) -> &mut dyn View {
match self {
Self::Hidden(_) => unsafe { &mut EMPTY_VIEW_GROUP },
Self::Hidden(_) => unsafe {
#[allow(static_mut_ref)]
&mut EMPTY_VIEW_GROUP
},
Self::Visible(view) => view,
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/states/menu/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
uformat, AppState, SerialNumber,
};

use embedded_menu::items::NavigationItem;
use embedded_menu::items::menu_item::MenuItem;
use gui::screens::create_menu;

#[derive(Clone, Copy)]
Expand All @@ -28,16 +28,15 @@ struct AboutAppMenu;
type AboutMenuBuilder = impl AppMenuBuilder<AboutMenuEvents>;

fn about_menu_builder(context: &mut Context) -> AboutMenuBuilder {
let list_item = |label| NavigationItem::new(label, AboutMenuEvents::None);
let list_item =
|label| MenuItem::new(label, "").with_value_converter(|_| AboutMenuEvents::None);

let mut items = heapless::Vec::<_, 5>::new();
items.extend([
list_item(uformat!(20, "{}", env!("FW_VERSION_MENU_ITEM"))),
list_item(uformat!(20, "{}", env!("HW_VERSION_MENU_ITEM"))),
NavigationItem::new(
uformat!(20, "Serial {}", SerialNumber),
AboutMenuEvents::ToSerial,
),
list_item(uformat!(20, "Serial {}", SerialNumber))
.with_value_converter(|_| AboutMenuEvents::ToSerial),
list_item(match context.frontend.device_id() {
Some(id) => uformat!(20, "ADC {:?}", LeftPadAny(16, id)),
None => uformat!(20, "ADC Unknown"),
Expand All @@ -48,15 +47,15 @@ fn about_menu_builder(context: &mut Context) -> AboutMenuBuilder {
{
unwrap!(items
.push(
NavigationItem::new(uformat!(20, "Fuel gauge"), AboutMenuEvents::ToBatteryInfo)
.with_marker("MAX17055")
MenuItem::new(uformat!(20, "Fuel gauge"), "MAX17055")
.with_value_converter(|_| AboutMenuEvents::ToBatteryInfo)
)
.ok());
}

create_menu("Device info")
.add_items(items)
.add_item(NavigationItem::new("Back", AboutMenuEvents::Back))
.add_menu_items(items)
.add_item("Back", "<-", |_| AboutMenuEvents::Back)
}

impl MenuScreen for AboutAppMenu {
Expand Down
8 changes: 4 additions & 4 deletions src/states/menu/battery_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
uformat, AppState,
};
use embassy_time::Duration;
use embedded_menu::items::NavigationItem;
use embedded_menu::items::menu_item::MenuItem;
use gui::screens::create_menu;

#[derive(Clone, Copy)]
Expand All @@ -29,7 +29,7 @@ async fn battery_info_menu_builder(context: &mut Context) -> BatteryInfoMenuBuil

let mut list_item = |label| {
unwrap!(items
.push(NavigationItem::new(label, BatteryEvents::None))
.push(MenuItem::new(label, "").with_value_converter(|_| BatteryEvents::None))
.ok())
};

Expand Down Expand Up @@ -68,8 +68,8 @@ async fn battery_info_menu_builder(context: &mut Context) -> BatteryInfoMenuBuil
}

create_menu("Battery info")
.add_items(items)
.add_item(NavigationItem::new("Back", BatteryEvents::Back))
.add_menu_items(items)
.add_item("Back", "<-", |_| BatteryEvents::Back)
}

impl MenuScreen for BatteryInfoMenu {
Expand Down
18 changes: 10 additions & 8 deletions src/states/menu/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
states::menu::{AppMenu, AppMenuBuilder, MenuScreen},
AppState,
};
use embedded_menu::items::{NavigationItem, Select};
use gui::{screens::create_menu, widgets::battery_small::BatteryStyle};

pub async fn display_menu(context: &mut Context) -> AppState {
Expand Down Expand Up @@ -34,18 +33,21 @@ type DisplayMenuBuilder = impl AppMenuBuilder<DisplayMenuEvents>;
fn display_menu_builder(context: &mut Context) -> DisplayMenuBuilder {
create_menu("Display")
.add_item(
Select::new("Brightness", context.config.display_brightness)
.with_value_converter(DisplayMenuEvents::ChangeBrigtness),
"Brightness",
context.config.display_brightness,
DisplayMenuEvents::ChangeBrigtness,
)
.add_item(
Select::new("Battery", context.config.battery_display_style)
.with_value_converter(DisplayMenuEvents::ChangeBatteryStyle),
"Battery",
context.config.battery_display_style,
DisplayMenuEvents::ChangeBatteryStyle,
)
.add_item(
Select::new("EKG Filter", context.config.filter_strength)
.with_value_converter(DisplayMenuEvents::ChangeFilterStrength),
"EKG Filter",
context.config.filter_strength,
DisplayMenuEvents::ChangeFilterStrength,
)
.add_item(NavigationItem::new("Back", DisplayMenuEvents::Back))
.add_item("Back", "<-", |_| DisplayMenuEvents::Back)
}

impl MenuScreen for DisplayMenu {
Expand Down
36 changes: 22 additions & 14 deletions src/states/menu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::{
states::menu::{AppMenu, MenuScreen},
AppState,
};
use embedded_menu::items::NavigationItem;
use embedded_menu::items::menu_item::{MenuItem, SelectValue};
use gui::screens::create_menu;

use super::AppMenuBuilder;

#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
pub enum MainMenuEvents {
Measure,
Display,
Expand All @@ -22,6 +22,12 @@ pub enum MainMenuEvents {
Shutdown,
}

impl SelectValue for MainMenuEvents {
fn marker(&self) -> &'static str {
""
}
}

pub async fn main_menu(context: &mut Context) -> AppState {
info!("Free heap: {} bytes", ALLOCATOR.free());

Expand All @@ -37,29 +43,31 @@ type MainMenuBuilder = impl AppMenuBuilder<MainMenuEvents>;
fn main_menu_builder(context: &mut Context) -> MainMenuBuilder {
let mut optional_items = heapless::Vec::<_, 4>::new();

let mut optional_item =
|label, event| unwrap!(optional_items.push(NavigationItem::new(label, event)).ok());

if context.can_enable_wifi() {
optional_item("Wifi setup", MainMenuEvents::WifiSetup);
optional_item("Wifi networks", MainMenuEvents::WifiListVisible);

let mut optional_item = |label, event| {
unwrap!(optional_items
.push(MenuItem::new(label, event).with_value_converter(|evt| evt))
.ok())
};
let network_configured =
!context.config.backend_url.is_empty() && !context.config.known_networks.is_empty();

optional_item("Wifi setup", MainMenuEvents::WifiSetup);
optional_item("Wifi networks", MainMenuEvents::WifiListVisible);

if network_configured {
optional_item("Firmware update", MainMenuEvents::FirmwareUpdate);
optional_item("Speed test", MainMenuEvents::Throughput);
}
}

create_menu("Main menu")
.add_item(NavigationItem::new("Measure", MainMenuEvents::Measure))
.add_item(NavigationItem::new("Display", MainMenuEvents::Display))
.add_item(NavigationItem::new("Storage", MainMenuEvents::Storage))
.add_item(NavigationItem::new("Device info", MainMenuEvents::About))
.add_items(optional_items)
.add_item(NavigationItem::new("Shutdown", MainMenuEvents::Shutdown))
.add_item("Measure", MainMenuEvents::Measure, |evt| evt)
.add_item("Display", MainMenuEvents::Display, |evt| evt)
.add_item("Storage", MainMenuEvents::Storage, |evt| evt)
.add_item("Device info", MainMenuEvents::About, |evt| evt)
.add_menu_items(optional_items)
.add_item("Shutdown", MainMenuEvents::Shutdown, |evt| evt)
}

impl MenuScreen for MainMenu {
Expand Down
4 changes: 2 additions & 2 deletions src/states/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait AppMenuBuilder<E> {
}

impl<T, VG, E> AppMenuBuilder<E>
for MenuBuilder<T, SingleTouch, VG, E, BinaryColor, AnimatedPosition, AnimatedTriangle>
for MenuBuilder<T, SingleTouch, VG, E, AnimatedPosition, AnimatedTriangle, BinaryColor>
where
T: AsRef<str>,
VG: ViewGroup + MenuItemCollection<E>,
Expand All @@ -75,7 +75,7 @@ pub trait AppMenuT<E>: Drawable<Color = BinaryColor, Output = ()> {
}

impl<T, VG, E> AppMenuT<E>
for Menu<T, SingleTouch, VG, E, BinaryColor, AnimatedPosition, AnimatedTriangle>
for Menu<T, SingleTouch, VG, E, AnimatedPosition, AnimatedTriangle, BinaryColor>
where
T: AsRef<str>,
VG: ViewGroup + MenuItemCollection<E>,
Expand Down
Loading

0 comments on commit eaf8e17

Please sign in to comment.