Skip to content

Commit

Permalink
examples: add cli support for uart-demo
Browse files Browse the repository at this point in the history
Signed-off-by: DongQing <[email protected]>
  • Loading branch information
Placebo27 committed Oct 19, 2024
1 parent f1d34b5 commit 878391f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/peripherals/uart-cli-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bouffalo-hal = { path = "../../../bouffalo-hal", features = ["bl808"] }
bouffalo-rt = { path = "../../../bouffalo-rt", features = ["bl808-dsp"] }
panic-halt = "0.2.0"
embedded-time = "0.12.1"
embedded-cli = "0.2.1"

[[bin]]
name = "uart-cli-demo"
Expand Down
63 changes: 58 additions & 5 deletions examples/peripherals/uart-cli-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@

use bouffalo_hal::prelude::*;
use bouffalo_rt::{entry, Clocks, Peripherals};
use embedded_cli::{cli::CliBuilder, Command};
use embedded_time::rate::*;
use panic_halt as _;

#[derive(Command)]
enum Base {
/// Print out 'Hello world!'.
Hello,
/// LED control command.
Led {
#[command(subcommand)]
command: Option<LedCommand>,
},
}

#[derive(Command)]
enum LedCommand {
/// Turn on LED.
On,
/// Turn off LED.
Off,
/// Switch LED state.
Switch,
}

#[entry]
fn main(p: Peripherals, c: Clocks) -> ! {
let tx = p.gpio.io14.into_uart();
Expand All @@ -20,13 +42,44 @@ fn main(p: Peripherals, c: Clocks) -> ! {

let (mut tx, mut rx) = serial.split();

let mut buf = [0u8; 1];
let mut led = p.gpio.io8.into_floating_output();
let mut led_state = PinState::Low;

writeln!(tx, "Welcome to embedded-cli example by bouffalo-hal🦀!").ok();
writeln!(tx, "For command helps, type 'help'.").ok();

writeln!(tx, "Hello world!").ok();
rx.read_exact(&mut buf).ok();
writeln!(tx, "Character: {}", buf[0]).ok();
let (command_buffer, history_buffer) = ([0; 128], [0; 128]);
let mut cli = CliBuilder::default()
.writer(tx)
.command_buffer(command_buffer)
.history_buffer(history_buffer)
.prompt("uart-cli-demo> ")
.build()
.unwrap();

loop {
// TODO: use embedded-cli to build a serial console
led.set_state(led_state).ok();
let mut slice = [0];
rx.read_exact(&mut slice).ok();
let _ = cli.process_byte::<Base, _>(
slice[0],
&mut Base::processor(|cli, command| {
match command {
Base::Hello => {
cli.writer().write_str("Hello world!").ok();
}
Base::Led { command } => match command {
Some(LedCommand::On) => led_state = PinState::Low,
Some(LedCommand::Off) => led_state = PinState::High,
Some(LedCommand::Switch) => led_state = !led_state,
None => match led_state {
PinState::High => cli.writer().write_str("LED state: High").unwrap(),
PinState::Low => cli.writer().write_str("LED state: Low").unwrap(),
},
},
}
Ok(())
}),
);
}
}

0 comments on commit 878391f

Please sign in to comment.