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"
|
||||
|
||||
[dependencies]
|
||||
embassy-executor = {version="*", features = ["defmt", "task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt"]}
|
||||
embassy-rp = {version ="*", features = ["defmt", "unstable-pac", "rp2040", "time-driver", "critical-section-impl"] }
|
||||
embassy-executor = { version = "*", features = [
|
||||
"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-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 = "*"
|
||||
|
||||
|
125
src/main.rs
125
src/main.rs
@ -1,15 +1,15 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(async_fn_in_trait)]
|
||||
#![feature(impl_trait_in_assoc_type)]
|
||||
#![feature(slice_split_once)]
|
||||
|
||||
use core::fmt::{Debug, Write};
|
||||
use core::str::from_utf8;
|
||||
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
||||
use defmt::println;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::tcp::TcpSocket;
|
||||
use embassy_net::{Config, StackResources};
|
||||
use embassy_net::{Config, Stack, StackResources};
|
||||
use embassy_rp::bind_interrupts;
|
||||
use embassy_rp::clocks::RoscRng;
|
||||
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_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 tx_buffer = [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 {
|
||||
Timer::after_secs(0).await;
|
||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
socket.set_timeout(Some(Duration::from_secs(30)));
|
||||
|
||||
control.gpio_set(0, false).await;
|
||||
info!("Listening on TCP:1234...");
|
||||
if let Err(e) = socket.accept(1234).await {
|
||||
info!("Socket {:?}: Listening on TCP:{}...", team, port);
|
||||
if let Err(e) = socket.accept(port).await {
|
||||
warn!("accept error: {:?}", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
info!("Received connection from {:?}", socket.remote_endpoint());
|
||||
control.gpio_set(0, true).await;
|
||||
info!(
|
||||
"Socket {:?}: Received connection from {:?}",
|
||||
team,
|
||||
socket.remote_endpoint()
|
||||
);
|
||||
|
||||
loop {
|
||||
Timer::after_secs(0).await;
|
||||
let n = match socket.read(&mut buf).await {
|
||||
Ok(0) => {
|
||||
warn!("read EOF");
|
||||
@ -132,12 +146,17 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
warn!("read error: {:?}", e);
|
||||
warn!("Socket {:?}: read error: {:?}", team, e);
|
||||
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 content: &[u8] = &[];
|
||||
@ -172,7 +191,7 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
},
|
||||
match l1.next() {
|
||||
Some(path) => path,
|
||||
Some(path) => from_utf8(path).unwrap(),
|
||||
None => {
|
||||
warn!("No path");
|
||||
break;
|
||||
@ -182,50 +201,47 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
};
|
||||
|
||||
let (code, res): (HttpResCode, &str) = match path {
|
||||
b"/" => (
|
||||
HttpResCode::Ok,
|
||||
"<!DOCTYPE html>\r\n\
|
||||
<html>\r\n\
|
||||
<body>\r\n\
|
||||
<h1>Titre</h1>\r\n\
|
||||
<p>contenu</p>\r\n\
|
||||
</body>\r\n\
|
||||
</html>",
|
||||
),
|
||||
_ => (HttpResCode::NotFound, ""),
|
||||
let (code, res_content): (HttpResCode, &[u8]) = match path {
|
||||
"/" => (HttpResCode::Ok, include_bytes!("../web/index.html")),
|
||||
"/htmx.min.js" => (HttpResCode::Ok, include_bytes!("../web/htmx.min.js")),
|
||||
p => {
|
||||
// if p.starts_with("/ppp/cell") {
|
||||
// let clicked_c = match p[9] {
|
||||
// '0' =>
|
||||
// };
|
||||
// }
|
||||
(HttpResCode::NotFound, &[])
|
||||
}
|
||||
};
|
||||
|
||||
//b"HTTP/1.1 200 OK\r\n\
|
||||
// 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();
|
||||
res_head_buf.clear();
|
||||
if let Err(e) = write!(
|
||||
&mut res_buf,
|
||||
&mut res_head_buf,
|
||||
"{}\r\n\
|
||||
Content-Type: text/html\r\n\
|
||||
Content-Length: {}\r\n\r\n\
|
||||
{}",
|
||||
Content-Length: {}\r\n\r\n",
|
||||
Into::<&str>::into(code),
|
||||
res.len(),
|
||||
res
|
||||
res_content.len()
|
||||
) {
|
||||
warn!("write error: {:?}", e);
|
||||
warn!("res buffer write error: {:?}", e);
|
||||
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(()) => {}
|
||||
Err(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