working two sockets + website
This commit is contained in:
parent
ef8d06b516
commit
8f79c33b6e
24
Cargo.toml
24
Cargo.toml
@ -4,11 +4,29 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
embassy-executor = {version="*", features = ["defmt", "task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt"]}
|
embassy-executor = { version = "*", features = [
|
||||||
embassy-rp = {version ="*", features = ["defmt", "unstable-pac", "rp2040", "time-driver", "critical-section-impl"] }
|
"defmt",
|
||||||
|
"nightly",
|
||||||
|
"arch-cortex-m",
|
||||||
|
"executor-thread",
|
||||||
|
"executor-interrupt",
|
||||||
|
] }
|
||||||
|
embassy-rp = { version = "*", features = [
|
||||||
|
"defmt",
|
||||||
|
"unstable-pac",
|
||||||
|
"rp2040",
|
||||||
|
"time-driver",
|
||||||
|
"critical-section-impl",
|
||||||
|
] }
|
||||||
embassy-time = "*"
|
embassy-time = "*"
|
||||||
embassy-usb-logger = "*"
|
embassy-usb-logger = "*"
|
||||||
embassy-net = {version = "*", features = ["defmt", "proto-ipv4", "tcp", "dhcpv4", "dns"]}
|
embassy-net = { version = "*", features = [
|
||||||
|
"defmt",
|
||||||
|
"proto-ipv4",
|
||||||
|
"tcp",
|
||||||
|
"dhcpv4",
|
||||||
|
"dns",
|
||||||
|
] }
|
||||||
cyw43-pio = "*"
|
cyw43-pio = "*"
|
||||||
cyw43 = "*"
|
cyw43 = "*"
|
||||||
|
|
||||||
|
125
src/main.rs
125
src/main.rs
@ -1,15 +1,15 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![allow(async_fn_in_trait)]
|
#![allow(async_fn_in_trait)]
|
||||||
|
#![feature(impl_trait_in_assoc_type)]
|
||||||
#![feature(slice_split_once)]
|
#![feature(slice_split_once)]
|
||||||
|
|
||||||
use core::fmt::{Debug, Write};
|
use core::fmt::{Debug, Write};
|
||||||
use core::str::from_utf8;
|
use core::str::from_utf8;
|
||||||
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
||||||
use defmt::println;
|
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::tcp::TcpSocket;
|
use embassy_net::tcp::TcpSocket;
|
||||||
use embassy_net::{Config, StackResources};
|
use embassy_net::{Config, Stack, StackResources};
|
||||||
use embassy_rp::bind_interrupts;
|
use embassy_rp::bind_interrupts;
|
||||||
use embassy_rp::clocks::RoscRng;
|
use embassy_rp::clocks::RoscRng;
|
||||||
use embassy_rp::gpio::{Level, Output};
|
use embassy_rp::gpio::{Level, Output};
|
||||||
@ -105,26 +105,40 @@ async fn main(spawner: Spawner) {
|
|||||||
//control.start_ap_open("cyw43", 5).await;
|
//control.start_ap_open("cyw43", 5).await;
|
||||||
control.start_ap_wpa2("cyw43", "password", 5).await;
|
control.start_ap_wpa2("cyw43", "password", 5).await;
|
||||||
|
|
||||||
|
// loop {
|
||||||
|
// info!("test");
|
||||||
|
// Timer::after_secs(0).await;
|
||||||
|
// }
|
||||||
|
|
||||||
|
spawner.spawn(listen_task(stack, Team::Zero, 80)).unwrap();
|
||||||
|
spawner.spawn(listen_task(stack, Team::One, 81)).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task(pool_size = 2)]
|
||||||
|
async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
|
||||||
let mut rx_buffer = [0; 4096];
|
let mut rx_buffer = [0; 4096];
|
||||||
let mut tx_buffer = [0; 4096];
|
let mut tx_buffer = [0; 4096];
|
||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
let mut res_buf = Vec::<u8, 4096>::new();
|
let mut res_head_buf = Vec::<u8, 4096>::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
Timer::after_secs(0).await;
|
||||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
socket.set_timeout(Some(Duration::from_secs(30)));
|
socket.set_timeout(Some(Duration::from_secs(30)));
|
||||||
|
|
||||||
control.gpio_set(0, false).await;
|
info!("Socket {:?}: Listening on TCP:{}...", team, port);
|
||||||
info!("Listening on TCP:1234...");
|
if let Err(e) = socket.accept(port).await {
|
||||||
if let Err(e) = socket.accept(1234).await {
|
|
||||||
warn!("accept error: {:?}", e);
|
warn!("accept error: {:?}", e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Received connection from {:?}", socket.remote_endpoint());
|
info!(
|
||||||
control.gpio_set(0, true).await;
|
"Socket {:?}: Received connection from {:?}",
|
||||||
|
team,
|
||||||
|
socket.remote_endpoint()
|
||||||
|
);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
Timer::after_secs(0).await;
|
||||||
let n = match socket.read(&mut buf).await {
|
let n = match socket.read(&mut buf).await {
|
||||||
Ok(0) => {
|
Ok(0) => {
|
||||||
warn!("read EOF");
|
warn!("read EOF");
|
||||||
@ -132,12 +146,17 @@ async fn main(spawner: Spawner) {
|
|||||||
}
|
}
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("read error: {:?}", e);
|
warn!("Socket {:?}: read error: {:?}", team, e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("request :\n{}", from_utf8(&buf[..n]).unwrap());
|
info!(
|
||||||
|
"Socket {:?}: request :\n{}",
|
||||||
|
team,
|
||||||
|
from_utf8(&buf[..n]).unwrap()
|
||||||
|
);
|
||||||
|
Timer::after_secs(0).await;
|
||||||
|
|
||||||
let mut headers: &[u8] = &buf[..n];
|
let mut headers: &[u8] = &buf[..n];
|
||||||
let mut content: &[u8] = &[];
|
let mut content: &[u8] = &[];
|
||||||
@ -172,7 +191,7 @@ async fn main(spawner: Spawner) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
match l1.next() {
|
match l1.next() {
|
||||||
Some(path) => path,
|
Some(path) => from_utf8(path).unwrap(),
|
||||||
None => {
|
None => {
|
||||||
warn!("No path");
|
warn!("No path");
|
||||||
break;
|
break;
|
||||||
@ -182,50 +201,47 @@ async fn main(spawner: Spawner) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (code, res): (HttpResCode, &str) = match path {
|
let (code, res_content): (HttpResCode, &[u8]) = match path {
|
||||||
b"/" => (
|
"/" => (HttpResCode::Ok, include_bytes!("../web/index.html")),
|
||||||
HttpResCode::Ok,
|
"/htmx.min.js" => (HttpResCode::Ok, include_bytes!("../web/htmx.min.js")),
|
||||||
"<!DOCTYPE html>\r\n\
|
p => {
|
||||||
<html>\r\n\
|
// if p.starts_with("/ppp/cell") {
|
||||||
<body>\r\n\
|
// let clicked_c = match p[9] {
|
||||||
<h1>Titre</h1>\r\n\
|
// '0' =>
|
||||||
<p>contenu</p>\r\n\
|
// };
|
||||||
</body>\r\n\
|
// }
|
||||||
</html>",
|
(HttpResCode::NotFound, &[])
|
||||||
),
|
}
|
||||||
_ => (HttpResCode::NotFound, ""),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//b"HTTP/1.1 200 OK\r\n\
|
res_head_buf.clear();
|
||||||
// Content-Type: text/html\r\n\
|
|
||||||
// Content-Length: 81\r\n\r\n\
|
|
||||||
// <!DOCTYPE html>\r\n\
|
|
||||||
// <html>\r\n\
|
|
||||||
// <body>\r\n\
|
|
||||||
// <h1>Titre</h1>\r\n\
|
|
||||||
// <p>contenu</p>\r\n\
|
|
||||||
// </body>\r\n\
|
|
||||||
// </html>"
|
|
||||||
|
|
||||||
// Write response to buf
|
|
||||||
res_buf.clear();
|
|
||||||
if let Err(e) = write!(
|
if let Err(e) = write!(
|
||||||
&mut res_buf,
|
&mut res_head_buf,
|
||||||
"{}\r\n\
|
"{}\r\n\
|
||||||
Content-Type: text/html\r\n\
|
Content-Type: text/html\r\n\
|
||||||
Content-Length: {}\r\n\r\n\
|
Content-Length: {}\r\n\r\n",
|
||||||
{}",
|
|
||||||
Into::<&str>::into(code),
|
Into::<&str>::into(code),
|
||||||
res.len(),
|
res_content.len()
|
||||||
res
|
|
||||||
) {
|
) {
|
||||||
warn!("write error: {:?}", e);
|
warn!("res buffer write error: {:?}", e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("response :\n{}", from_utf8(&res_buf).unwrap());
|
info!(
|
||||||
|
"Socket {:?}: response :\n{}",
|
||||||
|
team,
|
||||||
|
from_utf8(&res_head_buf).unwrap()
|
||||||
|
);
|
||||||
|
Timer::after_secs(0).await;
|
||||||
|
|
||||||
match socket.write_all(&res_buf).await {
|
match socket.write_all(&res_head_buf).await {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("write error: {:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match socket.write_all(&res_content).await {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("write error: {:?}", e);
|
warn!("write error: {:?}", e);
|
||||||
@ -254,3 +270,22 @@ impl Into<&str> for HttpResCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum Team {
|
||||||
|
Zero,
|
||||||
|
One,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum Cell {
|
||||||
|
C0,
|
||||||
|
C1,
|
||||||
|
C2,
|
||||||
|
C3,
|
||||||
|
C4,
|
||||||
|
C5,
|
||||||
|
C6,
|
||||||
|
C7,
|
||||||
|
C8,
|
||||||
|
}
|
||||||
|
1
web/htmx.min.js
vendored
Normal file
1
web/htmx.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
88
web/index.html
Normal file
88
web/index.html
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<head>
|
||||||
|
<script src="./htmx.min.js"></script>
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
#grid {
|
||||||
|
.cell {
|
||||||
|
border: 1px dotted black;
|
||||||
|
padding: 33%;
|
||||||
|
}
|
||||||
|
display: grid;
|
||||||
|
border: 1px solid black;
|
||||||
|
grid-template-rows: 1fr 1fr 1fr;
|
||||||
|
grid-template-columns: 1fr 1fr 1fr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>TicTacToe</h1>
|
||||||
|
<div id="grid">
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell1"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell2"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell3"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell4"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell5"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell6"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell7"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell8"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell9"
|
||||||
|
hx-trigger="click"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user