Skip to content

Commit

Permalink
add command executor
Browse files Browse the repository at this point in the history
  • Loading branch information
d-buckner committed Nov 11, 2024
1 parent 144df16 commit 77c18fd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/control_buffer.rs → src/command_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ impl fmt::Display for EOLReachedError {
}
}

pub struct ControlBuffer {
pub struct CommandBuffer {
buf: [u8; 256],
current_buf_idx: usize,
is_eol: bool,
}


impl ControlBuffer {
impl CommandBuffer {
pub fn new() -> Self {
ControlBuffer {
CommandBuffer {
buf: [0u8; 256],
current_buf_idx: 0,
is_eol: false,
Expand Down
29 changes: 29 additions & 0 deletions src/command_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use core::str::Split;

use crate::{
command_buffer::CommandBuffer, coordinate::PolarCoordinate, coordinate_queue,
transmission_channel,
};

const MOVE: &str = "MOVE";

pub async fn execute(command_buf: CommandBuffer) {
let mut args = command_buf.to_str().unwrap().split(' ');
let method = args.next().unwrap();

match method {
MOVE => execute_move(args).await,
_ => {}
};
}

async fn execute_move(mut args: Split<'_, char>) {
transmission_channel::send("MOVE ACK\r\n").await;
let theta_str = args.next().unwrap();
let rho_str = args.next().unwrap();
coordinate_queue::queue(PolarCoordinate {
theta: theta_str.parse::<f64>().unwrap(),
rho: rho_str.parse::<f64>().unwrap(),
})
.await;
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ mod arm;
mod coordinate;
mod stepper;
mod stepper_pair;
mod control_buffer;
mod command_buffer;
mod coordinate_queue;
mod uart_reader;
mod uart_writer;
mod transmission_channel;
mod command_executor;

bind_interrupts!(struct Irqs {
UART0_IRQ => uart::InterruptHandler<UART0>;
Expand Down
27 changes: 5 additions & 22 deletions src/uart_reader.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
use defmt::info;
use embassy_rp::{peripherals::UART0, uart::{Async, UartRx}};

use crate::{control_buffer::ControlBuffer, coordinate::PolarCoordinate, coordinate_queue, transmission_channel};

static MOVE: &str = "MOVE";
use crate::{command_buffer::CommandBuffer, command_executor};

#[embassy_executor::task]
pub async fn reader_task(mut rx: UartRx<'static, UART0, Async>) {
loop {
let mut control_buffer = ControlBuffer::new();
let mut command_buffer = CommandBuffer::new();
loop {
let mut char_buf = [0u8];
let _rr = rx.read(&mut char_buf).await;
let _br = control_buffer.add_char_buf(&char_buf);
if control_buffer.is_complete() {
let _br = command_buffer.add_char_buf(&char_buf);
if command_buffer.is_complete() {
break;
}
}

let input = control_buffer.to_str().unwrap();
let mut args = input.split(' ');
let command = args.next().unwrap();

if command == MOVE {
transmission_channel::send("MOVE ACK\r\n").await;
let theta_str = args.next().unwrap();
let rho_str = args.next().unwrap();
// TODO: parse rho
coordinate_queue::queue(PolarCoordinate {
theta: theta_str.parse::<f64>().unwrap(),
rho: 0.0,
}).await;
info!("Move to {}, {}", theta_str, rho_str);
}
command_executor::execute(command_buffer).await;
}
}

0 comments on commit 77c18fd

Please sign in to comment.