Compare commits

..

No commits in common. "8956acfcb4805e2ff4c9315c5e4f04a1548fdeae" and "868d7777081645676c3c19f32925626b9f4cd404" have entirely different histories.

5 changed files with 148 additions and 5369 deletions

57
Cargo.lock generated
View File

@ -219,7 +219,8 @@ dependencies = [
[[package]]
name = "cyw43"
version = "0.3.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c998ad980bbdc3947db4951bc763d14738ab873e20b5e2d87bb683011d7f9e8"
dependencies = [
"cortex-m",
"cortex-m-rt",
@ -236,7 +237,8 @@ dependencies = [
[[package]]
name = "cyw43-pio"
version = "0.4.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef20ba17ecf0730a1e71b6a6b9713fd5fe2c1f5e815fe2669069b3a6d115479a"
dependencies = [
"cyw43",
"embassy-rp",
@ -354,10 +356,10 @@ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "embassy-embedded-hal"
version = "0.3.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41fea5ef5bed4d3468dfd44f5c9fa4cda8f54c86d4fb4ae683eacf9d39e2ea12"
dependencies = [
"embassy-futures",
"embassy-hal-internal",
"embassy-sync",
"embassy-time",
"embedded-hal 0.2.7",
@ -371,7 +373,8 @@ dependencies = [
[[package]]
name = "embassy-executor"
version = "0.7.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90327bcc66333a507f89ecc4e2d911b265c45f5c9bc241f98eee076752d35ac6"
dependencies = [
"cortex-m",
"critical-section",
@ -383,7 +386,8 @@ dependencies = [
[[package]]
name = "embassy-executor-macros"
version = "0.6.2"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3577b1e9446f61381179a330fc5324b01d511624c55f25e3c66c9e3c626dbecf"
dependencies = [
"darling",
"proc-macro2",
@ -394,12 +398,14 @@ dependencies = [
[[package]]
name = "embassy-futures"
version = "0.1.1"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f878075b9794c1e4ac788c95b728f26aa6366d32eeb10c7051389f898f7d067"
[[package]]
name = "embassy-hal-internal"
version = "0.2.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef3bac31ec146321248a169e9c7b5799f1e0b3829c7a9b324cb4600a7438f59"
dependencies = [
"cortex-m",
"critical-section",
@ -409,8 +415,9 @@ dependencies = [
[[package]]
name = "embassy-net"
version = "0.7.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed041cc19a603d657124fddefdcbe5ef8bd60e77d972793ebb57de93394f5949"
dependencies = [
"defmt",
"document-features",
@ -427,7 +434,8 @@ dependencies = [
[[package]]
name = "embassy-net-driver"
version = "0.2.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524eb3c489760508f71360112bca70f6e53173e6fe48fc5f0efd0f5ab217751d"
dependencies = [
"defmt",
]
@ -435,7 +443,8 @@ dependencies = [
[[package]]
name = "embassy-net-driver-channel"
version = "0.3.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4818c32afec43e3cae234f324bad9a976c9aa7501022d26ff60a4017a1a006b7"
dependencies = [
"embassy-futures",
"embassy-net-driver",
@ -445,7 +454,8 @@ dependencies = [
[[package]]
name = "embassy-rp"
version = "0.4.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1a063d8baccdc5c7752840f4c7931f17bcd7de1ffe1efa2109e68113fe42612"
dependencies = [
"atomic-polyfill",
"cfg-if",
@ -483,7 +493,8 @@ dependencies = [
[[package]]
name = "embassy-sync"
version = "0.6.2"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d2c8cdff05a7a51ba0087489ea44b0b1d97a296ca6b1d6d1a33ea7423d34049"
dependencies = [
"cfg-if",
"critical-section",
@ -496,7 +507,8 @@ dependencies = [
[[package]]
name = "embassy-time"
version = "0.4.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f820157f198ada183ad62e0a66f554c610cdcd1a9f27d4b316358103ced7a1f8"
dependencies = [
"cfg-if",
"critical-section",
@ -511,7 +523,8 @@ dependencies = [
[[package]]
name = "embassy-time-driver"
version = "0.2.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d45f5d833b6d98bd2aab0c2de70b18bfaa10faf661a1578fd8e5dfb15eb7eba"
dependencies = [
"document-features",
]
@ -519,7 +532,8 @@ dependencies = [
[[package]]
name = "embassy-time-queue-utils"
version = "0.1.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc55c748d16908a65b166d09ce976575fb8852cf60ccd06174092b41064d8f83"
dependencies = [
"embassy-executor",
"heapless",
@ -528,7 +542,8 @@ dependencies = [
[[package]]
name = "embassy-usb"
version = "0.4.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e651b9b7b47b514e6e6d1940a6e2e300891a2c33641917130643602a0cb6386"
dependencies = [
"embassy-futures",
"embassy-net-driver-channel",
@ -542,7 +557,8 @@ dependencies = [
[[package]]
name = "embassy-usb-driver"
version = "0.1.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fc247028eae04174b6635104a35b1ed336aabef4654f5e87a8f32327d231970"
dependencies = [
"defmt",
]
@ -550,7 +566,8 @@ dependencies = [
[[package]]
name = "embassy-usb-logger"
version = "0.4.0"
source = "git+https://github.com/embassy-rs/embassy#cae954a87ec3c5ece520b6a44168b36e79f3f86a"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad40e3ec749d83f4b7ab0c0599c9bef0c51a73ce1c1087d8599ff2448d00c2aa"
dependencies = [
"embassy-futures",
"embassy-sync",

View File

@ -4,33 +4,31 @@ version = "0.1.0"
edition = "2024"
[dependencies]
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = [
embassy-executor = { version = "*", features = [
"defmt",
"nightly",
"arch-cortex-m",
"executor-thread",
"executor-interrupt",
] }
embassy-rp = { git = "https://github.com/embassy-rs/embassy", features = [
embassy-rp = { version = "*", features = [
"defmt",
"unstable-pac",
"rp2040",
"time-driver",
"critical-section-impl",
] }
embassy-time = {git = "https://github.com/embassy-rs/embassy"}
embassy-usb-logger = {git = "https://github.com/embassy-rs/embassy"}
embassy-net = { git = "https://github.com/embassy-rs/embassy", features = [
embassy-time = "*"
embassy-usb-logger = "*"
embassy-net = { version = "*", features = [
"defmt",
"proto-ipv4",
"tcp",
"dhcpv4",
"dns",
"icmp",
"packet-trace"
] }
cyw43-pio = {git = "https://github.com/embassy-rs/embassy"}
cyw43 = {git = "https://github.com/embassy-rs/embassy"}
cyw43-pio = "*"
cyw43 = "*"
embedded-io-async = "*"
defmt = "*"

View File

@ -23,7 +23,7 @@ use embassy_time::Duration;
use embassy_time::Timer;
use embedded_io_async::Write as _;
use heapless::{String, Vec};
use log::{debug, info, warn};
use log::{info, warn};
use rand_core::RngCore;
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};
@ -108,18 +108,15 @@ async fn main(spawner: Spawner) {
control.start_ap_wpa2("cyw43", "password", 5).await;
spawner.spawn(listen_task(stack, Team::Zero, 80)).unwrap();
spawner.spawn(listen_task(stack, Team::One, 81)).unwrap();
// spawner.spawn(listen_task(stack, Team::One, 81)).unwrap();
}
static TURN: AtomicBool = AtomicBool::new(false);
static current_turn: AtomicBool = AtomicBool::new(false);
// bits [0; 8] : player zero board / bits [9; 17] : player one board
static BOARD: AtomicU32 = AtomicU32::new(0);
static board_atom: AtomicU32 = AtomicU32::new(0);
#[embassy_executor::task(pool_size = 2)]
async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
loop {
info!("team:{:?}", team);
}
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut buf = [0; 4096];
@ -206,74 +203,41 @@ async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
}
};
let (code, res_type, res_content): (HttpResCode, &str, &[u8]) = match path {
"/" => (HttpResCode::Ok, "html", include_bytes!("../web/index.html")),
"/htmx.min.js" => (
HttpResCode::Ok,
"javascript",
include_bytes!("../web/htmx.min.js"),
),
"/htmx.js" => (
HttpResCode::Ok,
"javascript",
include_bytes!("../web/htmx.js"),
),
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 => 'res: {
if (p.starts_with("/ttt/cell") && p.len() == 10) || p == "/ttt/board" {
let mut board = BOARD.load(Ordering::Acquire);
if p.starts_with("/ttt/cell") {
let clicked_c: Cell =
match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
Ok(c) => c,
Err(_) => break 'res (HttpResCode::NotFound, "", &[]),
};
if board
& ((2_u32.pow(clicked_c as u32))
+ (2_u32.pow(9 + clicked_c as u32)))
!= 0
{
break 'res (HttpResCode::Forbidden, "", &[]);
}
board = board | 2_u32.pow((team as u32 * 9) + clicked_c as u32);
BOARD.store(board, Ordering::Release);
if p.starts_with("/ppp/cell") && p.len() == 10 {
let clicked_c: Cell = match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
Ok(c) => c,
Err(_) => {break 'res (HttpResCode::NotFound, &[])}
};
let mut board = board_atom.load(Ordering::Acquire);
if board & ((2^(clicked_c as u32)) + (2^(9+clicked_c as u32))) != 0 {
break 'res (HttpResCode::Forbidden, &[])
}
board = board | 2^((team as u32 * 9) + clicked_c as u32);
board_atom.store(board, Ordering::Release);
res_buf.clear();
for c in 0..=8 {
let picked_by = if board & 2_u32.pow(c) != 0 {
let picked_by = if board & (2^c) != 0 {
Some(Team::Zero)
} else if board & 2_u32.pow(9 + c) != 0 {
} else if board & (2^(9+c)) != 0 {
Some(Team::One)
} else {
None
};
} else {None};
match picked_by {
Some(Team::Zero) => {
res_buf
.extend_from_slice(
b"<div class=\"cell\" style=\"background-color:blue\"></div>",
)
.unwrap();
}
Some(Team::One) => {
res_buf.extend_from_slice(
b"<div class=\"cell\" style=\"background-color:red\"></div>",
)
.unwrap();
}
None => {
write!(
&mut res_buf,
"<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\"#grid\" hx-swap=\"innerHTML\"></button>",
c
).unwrap();
}
Some(Team::Zero) => {res_buf.extend_from_slice(b"<div class=\"cell\" style=\"color:blue\"></div>").unwrap();},
Some(Team::One) => {res_buf.extend_from_slice(b"<div class=\"cell\" style=\"color:red\"></div>").unwrap();},
None => {write!(
&mut res_buf,
"<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\".grid\" hx-swap=\"innerHTML\"></button>",
c
).unwrap();}
};
}
(HttpResCode::Ok, "html", &res_buf)
(HttpResCode::Ok, &res_buf)
} else {
(HttpResCode::NotFound, "", &[])
(HttpResCode::NotFound, &[])
}
}
};
@ -282,10 +246,9 @@ async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
if let Err(e) = write!(
&mut res_head_buf,
"{}\r\n\
Content-Type: text/{}\r\n\
Content-Type: text/html\r\n\
Content-Length: {}\r\n\r\n",
Into::<&str>::into(code),
res_type,
res_content.len()
) {
warn!("res buffer write error: {:?}", e);
@ -326,14 +289,14 @@ enum HttpRequestType {
enum HttpResCode {
Ok,
NotFound,
Forbidden,
Forbidden
}
impl Into<&str> for HttpResCode {
fn into(self) -> &'static str {
match self {
HttpResCode::Ok => "HTTP/1.1 200 OK",
HttpResCode::NotFound => "HTTP/1.1 404 NOT FOUND",
HttpResCode::Forbidden => "HTTP/1.1 403 FORBIDDEN",
HttpResCode::Forbidden => "HTTP/1.1 403 FORBIDDEN"
}
}
}
@ -345,7 +308,11 @@ enum Team {
}
impl From<bool> for Team {
fn from(value: bool) -> Self {
if value { Team::One } else { Team::Zero }
if value {
Team::One
} else {
Team::Zero
}
}
}
@ -374,7 +341,7 @@ impl TryFrom<char> for Cell {
'6' => Cell::C6,
'7' => Cell::C7,
'8' => Cell::C8,
_ => return Err(()),
_ => return Err(())
})
}
}
@ -391,7 +358,7 @@ impl TryFrom<u8> for Cell {
6 => Cell::C6,
7 => Cell::C7,
8 => Cell::C8,
_ => return Err(()),
_ => return Err(())
})
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
<!doctype html>
<head>
<script src="./htmx.js"></script>
<script src="./htmx.min.js"></script>
<style type="text/css">
body {
#grid {
@ -19,11 +19,69 @@
<html>
<body>
<h1>TicTacToe</h1>
<div
id="grid"
hx-post="/ttt/board"
hx-swap="innerHTML"
hx-trigger="load"
></div>
<div id="grid">
<button
class="cell"
hx-post="/ttt/cell0"
hx-target=".grid"
hx-swap="innerHTML"
></button>
<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>
</div>
</body>
</html>