working tcp

This commit is contained in:
Daniel Bulant 2026-05-04 12:38:00 +02:00
parent e4e392de51
commit 7c51ee47b5
No known key found for this signature in database

View file

@ -2,6 +2,8 @@
#![no_main]
use arrayvec::ArrayVec;
use core::convert::Infallible;
use core::ops::Deref;
use core::str::FromStr;
use embassy_net::tcp::ConnectError;
use embedded_io::ReadReady;
@ -191,7 +193,7 @@ async fn main(spawner: Spawner) {
info!("Connected to {:?}", socket.remote_endpoint());
let delay = Duration::from_millis(100);
let mut buffer = ArrayVec::<u8, 512>::new();
let mut buffer = WriteBuffer::<1024>::new();
loop {
let in1 = in1.is_low();
let in2 = in2.is_low();
@ -201,7 +203,7 @@ async fn main(spawner: Spawner) {
{
use embedded_io::Write;
let _ = core::writeln!(
&mut buffer[..],
&mut buffer,
"in1={} in2={} in3={} in4={} angle={}",
in1,
in2,
@ -234,6 +236,45 @@ async fn main(spawner: Spawner) {
}
}
struct WriteBuffer<const CAP: usize>(ArrayVec<u8, CAP>);
impl<const CAP: usize> Deref for WriteBuffer<CAP> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<const CAP: usize> WriteBuffer<CAP> {
fn new() -> Self {
Self(ArrayVec::new())
}
}
impl<const CAP: usize> embedded_io::ErrorType for WriteBuffer<CAP> {
type Error = Infallible;
}
impl<const CAP: usize> embedded_io::Write for WriteBuffer<CAP> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let _ = self.0.try_extend_from_slice(buf); //silently fails!
Ok(buf.len())
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf)?;
Ok(())
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 {
loop {
if let Some(config) = stack.config_v4() {