Initial rough-in of motor controller

This commit is contained in:
2024-08-17 15:10:06 -04:00
parent 3633f636c6
commit 3185746213
11 changed files with 653 additions and 62 deletions

View File

@ -23,96 +23,95 @@ use ::{
//time::Duration, // could also use core::time::Duration?
},
core::time::Duration,
log::{info, warn},
};
use crate::commands::Commands;
use async_channel::Sender;
use log::*; //{trace, debug, info, warn, error}
#[derive(Command)]
pub enum Menu<'a> {
/// Send a button press: Up
ButtonUp,
/// Simulate the PIC controller sending aus n Up character
PicRecvUp,
/// Send a button press: Down
ButtonDown,
/// Simulate the PIC controller sending us a Down character
PicRecvDown,
/// Send a button press: Auto toggle on/off
ButtonAuto,
/// Send a button press: Pair/forget
ButtonPair,
/// Simulate the PIC controller sending us a Stop character
PicRecvStop,
/// 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 },
Log { level: u8 },
}
pub fn process_menu(
cli: &mut CliHandle<'_, SimpleWriter, Infallible>,
command: Menu<'_>,
dispatch: &Sender<Commands>,
) -> Result<(), Infallible> {
match command {
Menu::ButtonUp => {
cli.writer().write_str("TODO: simulate button")?;
// We ignore sending errors throughout because the Cli interface is only for
// testing and debugging.
Menu::PicRecvUp => {
cli.writer().write_str("Sending PicButtonUp Received command")?;
let _ = dispatch.send_blocking(Commands::PicRecvUp);
}
Menu::ButtonDown => {
cli.writer().write_str("TODO: simulate button")?;
Menu::PicRecvDown => {
cli.writer().write_str("Sending PicButtonDown command")?;
let _ = dispatch.send_blocking(Commands::PicRecvDown);
}
Menu::ButtonAuto => {
cli.writer().write_str("TODO: simulate button")?;
}
Menu::ButtonPair => {
cli.writer().write_str("TODO: simulate button")?;
Menu::PicRecvStop => {
cli.writer().write_str("Sending PicButtonStop command")?;
let _ = dispatch.send_blocking(Commands::PicRecvStop);
}
Menu::BluetoothUp { data } => {
cli.writer()
.write_str("TODO: simulate bluetooth characteristic change")?;
let _ = data;
.write_str("Sending BluetoothUp")?;
let _ = dispatch.send_blocking(Commands::BluetoothUp { data: data });
}
Menu::BluetoothDown { data } => {
cli.writer()
.write_str("TODO: simulate bluetooth characteristic change")?;
let _ = data;
.write_str("Sending BluetoothDown")?;
let _ = dispatch.send_blocking(Commands::BluetoothDown { data: data });
}
Menu::BluetoothStop { data } => {
cli.writer()
.write_str("TODO: simulate bluetooth characteristic change")?;
let _ = data;
.write_str("SendingBluetoothStop")?;
let _ = dispatch.send_blocking(Commands::BluetoothStop { _data: data });
}
Menu::BluetoothLearn { data } => {
cli.writer()
@ -144,6 +143,39 @@ pub fn process_menu(
.write_str("TODO: simulate bluetooth characteristic change {}")?;
let _ = wifipass;
}
Menu::Log { level } => {
match level {
0 => {
cli.writer().write_str("Log set to Off")?;
log::set_max_level(log::LevelFilter::Off);
}
1 => {
cli.writer().write_str("Log set to Error")?;
log::set_max_level(log::LevelFilter::Error);
}
2 => {
cli.writer().write_str("Log set to Warn")?;
log::set_max_level(log::LevelFilter::Warn);
}
3 => {
cli.writer().write_str("Log set to Info")?;
log::set_max_level(log::LevelFilter::Info);
}
4 => {
cli.writer().write_str("Log set to Debug")?;
log::set_max_level(log::LevelFilter::Debug);
}
_ => {
cli.writer().write_str("Log set to Trace")?;
log::set_max_level(log::LevelFilter::Trace);
}
}
error!("error test");
warn!("warn test");
info!("info test");
debug!("debug test");
trace!("trace test");
}
}
Ok(())
}
@ -157,19 +189,19 @@ impl embedded_io::ErrorType for SimpleWriter {
impl embedded_io::Write for SimpleWriter {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
stdout().write(&buf).unwrap(); //TODO: handle errors?
stdout().write(&buf).unwrap();
Ok(buf.len())
}
fn flush(&mut self) -> Result<(), Self::Error> {
let _ = stdout().flush(); //TODO currently ignoring this error
let _ = stdout().flush();
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");
pub async fn start_cli(dispatch: Sender<Commands>) -> anyhow::Result<()> {
let timer_service = EspTaskTimerService::new()?;
info!("Setting up command line listener and processor");
let writer = SimpleWriter {};
let mut cli = CliBuilder::default()
.writer(writer)
@ -184,7 +216,11 @@ pub async fn start_cli() -> anyhow::Result<()> {
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))?;
//cli.process_byte::<Menu, _>(buf[0], &mut Menu::processor(process_menu))?;
cli.process_byte::<Menu, _>(buf[0], &mut Menu::processor(
|cli, command| {
process_menu(cli, command, &dispatch)
}))?;
}
Err(e) => match e.kind() {
std::io::ErrorKind::WouldBlock => {} // This is expected if there is no input