Initial working exampel of test console
This commit is contained in:
195
gem-remotes-esp32/src/test_console.rs
Normal file
195
gem-remotes-esp32/src/test_console.rs
Normal file
@ -0,0 +1,195 @@
|
||||
// === Constants ==============================================================
|
||||
const MAX_COMMAND_SIZE: usize = 64; // in bytes; we want to test wifi ssid which
|
||||
// can be 32 bytes.
|
||||
const MAX_HISTORY_SIZE: usize = 128; // in bytes
|
||||
const SLEEP_TIME_MS: u64 = 10; // should be > 1/CONFIG_FREERTOS_HZ in sdkconfig
|
||||
// currently this is 1ms, which is also about as
|
||||
// long as it would take uart buffers to overflow
|
||||
// at maximum rate. But this is intended for
|
||||
// human input only. It's noticeably laggy at 50.
|
||||
|
||||
// ============================================================================
|
||||
|
||||
use ::{
|
||||
anyhow::Result,
|
||||
embedded_cli::{
|
||||
cli::{CliBuilder, CliHandle},
|
||||
Command,
|
||||
},
|
||||
esp_idf_svc::timer::EspTaskTimerService,
|
||||
std::{
|
||||
convert::Infallible,
|
||||
io::{stdin, stdout, Read, Write},
|
||||
//time::Duration, // could also use core::time::Duration?
|
||||
},
|
||||
core::time::Duration,
|
||||
log::{info, warn},
|
||||
};
|
||||
|
||||
#[derive(Command)]
|
||||
pub enum Menu<'a> {
|
||||
/// Send a button press: Up
|
||||
ButtonUp,
|
||||
|
||||
/// Send a button press: Down
|
||||
ButtonDown,
|
||||
|
||||
/// Send a button press: Auto toggle on/off
|
||||
ButtonAuto,
|
||||
|
||||
/// Send a button press: Pair/forget
|
||||
ButtonPair,
|
||||
|
||||
/// Send a bluetooth characteristic: Up
|
||||
BluetoothUp {
|
||||
/// 0 for not pressed, 1 for pressed
|
||||
data: u8,
|
||||
},
|
||||
|
||||
/// Send a bluetooth characteristic: Down
|
||||
BluetoothDown {
|
||||
/// 0 for not pressed, 1 for pressed
|
||||
data: u8,
|
||||
},
|
||||
|
||||
/// Send a bluetooth characteristic: Stop
|
||||
BluetoothStop {
|
||||
/// 0 for not pressed, 1 for pressed
|
||||
data: u8,
|
||||
},
|
||||
|
||||
/// Send a bluetooth characteristic: Learn
|
||||
BluetoothLearn {
|
||||
/// 0 for not pressed, 1 for pressed
|
||||
data: u8,
|
||||
},
|
||||
|
||||
/// Send a bluetooth characteristic: Auto
|
||||
BluetoothAuto {
|
||||
/// 0 for not pressed, 1 for pressed
|
||||
data: u8,
|
||||
},
|
||||
|
||||
/// Send a bluetooth characteristic: Limits
|
||||
BluetoothTopLimit { data: u8 },
|
||||
|
||||
/// Send a bluetooth characteristic: Limits
|
||||
BluetoothBottomLimit { data: u8 },
|
||||
/// Send a bluetooth characteristic: Wifi SSID
|
||||
BluetoothWifiSsid { ssid: &'a str },
|
||||
|
||||
/// Send a bluetooth characteristic: Wifi Password
|
||||
BluetoothWifiPassword { wifipass: &'a str },
|
||||
}
|
||||
|
||||
pub fn process_menu(
|
||||
cli: &mut CliHandle<'_, SimpleWriter, Infallible>,
|
||||
command: Menu<'_>,
|
||||
) -> Result<(), Infallible> {
|
||||
match command {
|
||||
Menu::ButtonUp => {
|
||||
cli.writer().write_str("TODO: simulate button")?;
|
||||
}
|
||||
Menu::ButtonDown => {
|
||||
cli.writer().write_str("TODO: simulate button")?;
|
||||
}
|
||||
Menu::ButtonAuto => {
|
||||
cli.writer().write_str("TODO: simulate button")?;
|
||||
}
|
||||
Menu::ButtonPair => {
|
||||
cli.writer().write_str("TODO: simulate button")?;
|
||||
}
|
||||
Menu::BluetoothUp { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothDown { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothStop { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothLearn { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothAuto { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothTopLimit { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothBottomLimit { data } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = data;
|
||||
}
|
||||
Menu::BluetoothWifiSsid { ssid } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change")?;
|
||||
let _ = ssid;
|
||||
}
|
||||
Menu::BluetoothWifiPassword { wifipass } => {
|
||||
cli.writer()
|
||||
.write_str("TODO: simulate bluetooth characteristic change {}")?;
|
||||
let _ = wifipass;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// === SimpleWriter ===========================================================
|
||||
pub struct SimpleWriter {}
|
||||
|
||||
impl embedded_io::ErrorType for SimpleWriter {
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
impl embedded_io::Write for SimpleWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||
stdout().write(&buf).unwrap(); //TODO: handle errors?
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
let _ = stdout().flush(); //TODO currently ignoring this error
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_cli() -> anyhow::Result<()> {
|
||||
let timer_service = EspTaskTimerService::new().unwrap(); //TODO: handle this error
|
||||
info!("Setting up command line listern and processor");
|
||||
let writer = SimpleWriter {};
|
||||
let mut cli = CliBuilder::default()
|
||||
.writer(writer)
|
||||
.command_buffer([0_u8; MAX_COMMAND_SIZE])
|
||||
.history_buffer([0_u8; MAX_HISTORY_SIZE])
|
||||
.prompt("$> ")
|
||||
.build()?;
|
||||
let mut reader = stdin();
|
||||
let mut buf = [0_u8; 1];
|
||||
let mut async_timer = timer_service.timer_async()?;
|
||||
loop {
|
||||
async_timer.after(Duration::from_millis(SLEEP_TIME_MS)).await?;
|
||||
match reader.read_exact(&mut buf) {
|
||||
Ok(_) => {
|
||||
cli.process_byte::<Menu, _>(buf[0], &mut Menu::processor(process_menu))?;
|
||||
}
|
||||
Err(e) => match e.kind() {
|
||||
std::io::ErrorKind::WouldBlock => {} // This is expected if there is no input
|
||||
_ => {warn!("Error waiting for input: {}", e)}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user