1
Fork 0

recv control input on northbridge

This commit is contained in:
Andy Killorin 2025-02-03 14:09:20 -05:00
parent e717238727
commit b012a83ee0
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 28 additions and 2 deletions

View file

@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
pub const BAUDRATE: u32 = 115200;
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Command {
/// Forward, clockwise
Twist(f32,f32),

View file

@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use common::{Command, Response, BAUDRATE};
use framed_codec::FramedCodec;
use futures::{SinkExt, StreamExt};
use tokio::sync::broadcast;
use tokio::{io::AsyncReadExt, net::{TcpListener, TcpSocket}, sync::{broadcast, watch::{self, Sender}}};
use tokio_serial::SerialPortBuilderExt;
use tokio_util::codec::Framed;
@ -24,6 +24,10 @@ async fn main() -> Result<()> {
Ok(postcard::from_bytes::<Response>(&data.ok().context("decode err")?)?)
});
let (send, commands) = watch::channel(Command::Stop);
tokio::spawn(control(send));
println!("starting");
write.send(Command::Enable).await?;
@ -41,7 +45,29 @@ async fn main() -> Result<()> {
if let Some(data) = data.sensor_data {
let _ = sensor_sender.send(data);
}
write.send(commands.borrow().clone()).await?;
}
Ok(())
}
async fn control(sender: Sender<Command>) -> Result<()> {
let listener = TcpListener::bind("0.0.0.0:3322").await?;
let (mut stream, addr) = listener.accept().await?;
println!("connected to {addr:?}");
loop {
let len = stream.read_u32().await?;
let mut buf = vec![0; len as usize];
stream.read_exact(&mut buf).await?;
let cmd: Command = postcard::from_bytes(&buf)?;
println!("recv {cmd:?}");
sender.send(cmd)?;
}
}