cycle-menu

Crates.iocycle-menu
lib.rscycle-menu
version0.4.2
sourcesrc
created_at2024-01-28 20:50:38.552101
updated_at2024-05-22 11:42:57.789807
descriptionsimple no-std compatible library to create a menu that can be cycled through and shown with custom input/output
homepagehttps://github.com/MnlPhlp/cycle-menu
repositoryhttps://github.com/MnlPhlp/cycle-menu
max_upload_size
id1117843
size24,159
Manuel Philipp (MnlPhlp)

documentation

README

cycle-menu

Simple no-std compatible library to create a menu that can be cycled through and shown with custom input/output.

The menu object has two functions, next and ok, to controll it. When crating the menu a callback is given that is later used to display menu entries. Two different Menu-Items exist:

  • Action: Contains a callback that is executed on selection
  • Submenu: Contains a list of Menu-Items

Example

This example uses edge-executor to run two tasks that wait for button input and controll the menu. The Items are printed on the console

pub fn setup<P1: Pin, P2: Pin>(
    ex: &LocalExecutor,
    btn1: PinDriver<'static, P1, Input>,
    btn2: PinDriver<'static, P2, Input>,
) {
    let menu = Box::leak(Box::new(Mutex::new(create_menu())));
    // left button
    ex.spawn(btn_task(btn1, || {
        if let Ok(mut menu) = menu.try_lock() {
            menu.skip();
        } else {
            info!("Menu is locked");
        };
    }))
    .detach();

    // right button
    ex.spawn(btn_task(btn2, || {
        if let Ok(mut menu) = menu.try_lock() {
            menu.ok();
        } else {
            info!("Menu is locked");
        };
    }))
    .detach();
}

fn create_menu() -> Menu<'static> {
    let items = vec![
        Item::new_action("Test 1", || println!("Test activated")),
        Item::new_submenu(
            "Sub",
            vec![
                Item::new_action("Sub 1", || println!("Sub 1 activated")),
                Item::new_action("Sub 2", || println!("Sub 2 activated")),
            ],
        ),
    ];
    Menu::new(items, display)
}

fn display(text: &str) {
    info!("Menu: {text}")
}
Commit count: 15

cargo fmt