small refactor
This commit is contained in:
parent
fb05e624b7
commit
21ae0e4968
5 changed files with 87 additions and 66 deletions
|
|
@ -2,19 +2,28 @@ import type {
|
|||
PartySocketEvent,
|
||||
PartyState,
|
||||
} from "../api/src/party-types";
|
||||
let ws: WebSocket | null = new WebSocket("ws://localhost:3000/api/dev-socket/ws");
|
||||
|
||||
let last_ep_port: null|number = null
|
||||
let last_ep_addr: null|string = null
|
||||
|
||||
const socket = await Bun.udpSocket({
|
||||
port: 7070,
|
||||
socket: {
|
||||
data(socket, buf, port, addr) {
|
||||
console.log(`message from ${addr}:${port}:`);
|
||||
console.log(buf.toString());
|
||||
// console.log(`message from ${addr}:${port}:`);
|
||||
last_ep_addr = addr
|
||||
last_ep_port = port
|
||||
const str = buf.toString();
|
||||
console.log(str);
|
||||
const opts = str.split(" ").map(t => t.trim().split("="))
|
||||
const parsedOpts = Object.fromEntries(opts)
|
||||
const { in1, in2, in3, in4, angle } = parsedOpts
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
let ws: WebSocket | null = new WebSocket("ws://localhost:4000/api/dev-socket/ws");
|
||||
|
||||
ws.onerror = e => {
|
||||
console.error(e)
|
||||
|
|
@ -36,8 +45,9 @@ ws.onmessage = e => {
|
|||
const { currentQuestion } = partyData
|
||||
console.log(currentQuestion)
|
||||
let text = currentQuestion?.text
|
||||
if (text) {
|
||||
ws?.send(text)
|
||||
if (text && last_ep_port !== null && last_ep_addr !== null) {
|
||||
socket.send(text, last_ep_port, last_ep_addr)
|
||||
// ws?.send(text)
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
57
pico/src/buffer.rs
Normal file
57
pico/src/buffer.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use core::{convert::Infallible, ops::Deref};
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use embassy_futures::yield_now;
|
||||
use embassy_net::Stack;
|
||||
|
||||
pub 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> {
|
||||
pub fn new() -> Self {
|
||||
Self(ArrayVec::new())
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.0.clear();
|
||||
}
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 {
|
||||
loop {
|
||||
if let Some(config) = stack.config_v4() {
|
||||
return config.clone();
|
||||
}
|
||||
yield_now().await;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +1,17 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use core::convert::Infallible;
|
||||
use core::net::SocketAddrV4;
|
||||
use core::ops::Deref;
|
||||
use core::str::FromStr;
|
||||
use embassy_net::tcp::ConnectError;
|
||||
use embassy_net::udp::{PacketMetadata, UdpMetadata, UdpSocket};
|
||||
use embedded_io::ReadReady;
|
||||
|
||||
use crate::buffer::{WriteBuffer, wait_for_config};
|
||||
use ag_lcd::{Blink, Cursor, Display, LcdDisplay};
|
||||
use as5600::As5600;
|
||||
use cyw43::{JoinOptions, aligned_bytes};
|
||||
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::yield_now;
|
||||
use embassy_net::tcp::client::{TcpClient, TcpClientState};
|
||||
use embassy_net::{Stack, StackResources};
|
||||
use embassy_rp::clocks::RoscRng;
|
||||
use embassy_rp::gpio::{Input, Level, Output};
|
||||
|
|
@ -29,8 +23,11 @@ use embassy_rp::{peripherals::USB, usb};
|
|||
use embassy_time::{Delay, Duration, Timer};
|
||||
use static_cell::StaticCell;
|
||||
use ufmt::{uWrite, uwrite};
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
mod buffer;
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
PIO0_IRQ_0 => InterruptHandler<PIO0>;
|
||||
DMA_IRQ_0 => dma::InterruptHandler<DMA_CH0>;
|
||||
|
|
@ -55,7 +52,7 @@ async fn net_task(mut runner: embassy_net::Runner<'static, cyw43::NetDriver<'sta
|
|||
runner.run().await
|
||||
}
|
||||
|
||||
const WIFI_NETWORK: &str = "aura";
|
||||
const WIFI_NETWORK: &str = "flamme";
|
||||
const WIFI_PASSWORD: &str = "12345678";
|
||||
|
||||
#[embassy_executor::main]
|
||||
|
|
@ -181,7 +178,7 @@ async fn main(spawner: Spawner) {
|
|||
);
|
||||
socket.bind(7070).unwrap();
|
||||
|
||||
let host_addr = embassy_net::Ipv4Address::from_str("192.168.12.1").unwrap();
|
||||
let host_addr = embassy_net::Ipv4Address::from_str("84.238.32.253").unwrap();
|
||||
let target = UdpMetadata::from(SocketAddrV4::new(host_addr, 7070));
|
||||
uwrite!(lcd, "udp.");
|
||||
|
||||
|
|
@ -207,7 +204,7 @@ async fn main(spawner: Spawner) {
|
|||
}
|
||||
{
|
||||
let _ = socket.send_to(&*buffer, target).await;
|
||||
buffer.0.clear();
|
||||
buffer.clear();
|
||||
}
|
||||
if socket.may_recv() {
|
||||
let mut rx_buffer = [0; 4096];
|
||||
|
|
@ -228,51 +225,3 @@ async fn main(spawner: Spawner) {
|
|||
Timer::after(delay).await;
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
return config.clone();
|
||||
}
|
||||
yield_now().await;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { treaty } from "@elysiajs/eden";
|
||||
import type { App } from "../../../api/src/index";
|
||||
|
||||
export const client = treaty<App>("127.0.0.1:3000", {});
|
||||
export const client = treaty<App>("aura.rpi1.danbulant.cloud", {});
|
||||
|
|
|
|||
|
|
@ -11,14 +11,19 @@ const config = defineConfig({
|
|||
plugins: [
|
||||
devtools(),
|
||||
tailwindcss(),
|
||||
tanstackStart(),
|
||||
tanstackStart({
|
||||
spa: {
|
||||
enabled: true
|
||||
}
|
||||
}),
|
||||
viteReact({
|
||||
babel: {
|
||||
plugins: ["babel-plugin-react-compiler"],
|
||||
},
|
||||
}),
|
||||
],
|
||||
server: {
|
||||
server: {
|
||||
allowedHosts: ["aura.rpi1.danbulant.cloud"],
|
||||
proxy: {
|
||||
"/api": {
|
||||
target: "http://localhost:4000",
|
||||
|
|
|
|||
Loading…
Reference in a new issue