Made motor driver independent task
This commit is contained in:
@ -2,28 +2,85 @@
|
||||
|
||||
use log::*; //{trace, debug, info, warn, error}
|
||||
use anyhow::Result;
|
||||
use async_channel::{unbounded, Receiver, Sender};
|
||||
use std::panic;
|
||||
|
||||
pub trait MotorDriver: Send + Sync {
|
||||
fn start_up(&mut self) -> Result<()>;
|
||||
fn start_down(&mut self) -> Result<()>;
|
||||
fn stop(&mut self) -> Result<()>;
|
||||
use crate::dispatch;
|
||||
use crate::commands::Commands as Dispatch_Commands;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Commands {
|
||||
StartUp,
|
||||
StartDown,
|
||||
Stop
|
||||
}
|
||||
|
||||
pub struct MotorDriverDebug {}
|
||||
pub type SendQ = Sender<Commands>;
|
||||
pub type RecvQ = Receiver<Commands>;
|
||||
|
||||
impl MotorDriver for MotorDriverDebug {
|
||||
fn start_up(&mut self) -> Result<()> {
|
||||
pub struct MotorDriverDebug{
|
||||
endpoint: SendQ,
|
||||
recv_q: RecvQ,
|
||||
dispatch: dispatch::SendQ,
|
||||
}
|
||||
|
||||
/// Debug / example version of Motor Driver.
|
||||
impl MotorDriverDebug {
|
||||
pub fn new(send_q: dispatch::SendQ) -> Self {
|
||||
let (s,r) = unbounded();
|
||||
MotorDriverDebug {
|
||||
endpoint: s,
|
||||
recv_q: r,
|
||||
dispatch:send_q
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_endpoint(&self) -> SendQ {
|
||||
self.endpoint.clone()
|
||||
}
|
||||
|
||||
pub async fn run(&self) -> Result<()> {
|
||||
//TODO: Add error propagation to all loops
|
||||
loop {
|
||||
match self.recv_q.recv().await {
|
||||
Ok(cmd) => {
|
||||
match self.handle_cmd(cmd).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("Unable to send commands to motor! {:?}", e);
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Unable to receive commands for motor control! {:?}", e);
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_cmd(&self, cmd: Commands) -> Result<()> {
|
||||
match cmd {
|
||||
Commands::StartUp => {self.start_up().await?;Ok(())}
|
||||
Commands::StartDown => {self.start_down().await?;Ok(())}
|
||||
Commands::Stop => {self.stop().await?;Ok(())}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_up(&self) -> Result<()> {
|
||||
warn!("Starting motor, direction: Up");
|
||||
self.dispatch.send(Dispatch_Commands::NotifyMotorUp).await?;
|
||||
Ok(())
|
||||
}
|
||||
fn start_down(&mut self) -> Result<()> {
|
||||
pub async fn start_down(&self) -> Result<()> {
|
||||
warn!("Starting motor, direction: Down");
|
||||
self.dispatch.send(Dispatch_Commands::NotifyMotorDown).await?;
|
||||
Ok(())
|
||||
}
|
||||
fn stop(&mut self) ->Result<()> {
|
||||
pub async fn stop(&self) -> Result<()> {
|
||||
warn!("Stopping motor");
|
||||
self.dispatch.send(Dispatch_Commands::NotifyMotorStop).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
//unsafe impl Send for MotorDriverDebug {}
|
||||
Reference in New Issue
Block a user