buttons that button well

This commit is contained in:
Arkitu 2025-03-30 19:23:41 +02:00
parent 868d777708
commit 5b827c75f2
4 changed files with 107 additions and 86 deletions

57
Cargo.lock generated
View File

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

View File

@ -4,31 +4,33 @@ version = "0.1.0"
edition = "2024"
[dependencies]
embassy-executor = { version = "*", features = [
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = [
"defmt",
"nightly",
"arch-cortex-m",
"executor-thread",
"executor-interrupt",
] }
embassy-rp = { version = "*", features = [
embassy-rp = { git = "https://github.com/embassy-rs/embassy", features = [
"defmt",
"unstable-pac",
"rp2040",
"time-driver",
"critical-section-impl",
] }
embassy-time = "*"
embassy-usb-logger = "*"
embassy-net = { version = "*", features = [
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 = [
"defmt",
"proto-ipv4",
"tcp",
"dhcpv4",
"dns",
"icmp",
"packet-trace"
] }
cyw43-pio = "*"
cyw43 = "*"
cyw43-pio = {git = "https://github.com/embassy-rs/embassy"}
cyw43 = {git = "https://github.com/embassy-rs/embassy"}
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::{info, warn};
use log::{debug, info, warn};
use rand_core::RngCore;
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};
@ -203,41 +203,80 @@ async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
}
};
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")),
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"),
),
p => 'res: {
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, &[])}
};
if p.starts_with("/ttt/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, &[])
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^((team as u32 * 9) + clicked_c as u32);
info!("{:?}", clicked_c);
info!("{:?}", clicked_c as u32);
info!("{:?}", team as u32 * 9);
info!("{:?}", 2_u32.pow((team as u32 * 9) + clicked_c as u32));
info!(
"{:?}",
board | 2_u32.pow((team as u32 * 9) + clicked_c as u32)
);
board = board | 2_u32.pow((team as u32 * 9) + clicked_c as u32);
board_atom.store(board, Ordering::Release);
info!("board : {}", board);
res_buf.clear();
for c in 0..=8 {
let picked_by = if board & (2^c) != 0 {
let picked_by = if board & 2_u32.pow(c) != 0 {
Some(Team::Zero)
} else if board & (2^(9+c)) != 0 {
} else if board & 2_u32.pow(9 + c) != 0 {
Some(Team::One)
} else {None};
} else {
None
};
info!("{} : {:?}", c, picked_by);
match picked_by {
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!(
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>",
"<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\"#grid\" hx-swap=\"innerHTML\"></button>",
c
).unwrap();}
).unwrap();
}
};
}
(HttpResCode::Ok, &res_buf)
(HttpResCode::Ok, "html", &res_buf)
} else {
(HttpResCode::NotFound, &[])
(HttpResCode::NotFound, "", &[])
}
}
};
@ -246,9 +285,10 @@ 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/html\r\n\
Content-Type: text/{}\r\n\
Content-Length: {}\r\n\r\n",
Into::<&str>::into(code),
res_type,
res_content.len()
) {
warn!("res buffer write error: {:?}", e);
@ -289,14 +329,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",
}
}
}
@ -308,11 +348,7 @@ 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 }
}
}
@ -341,7 +377,7 @@ impl TryFrom<char> for Cell {
'6' => Cell::C6,
'7' => Cell::C7,
'8' => Cell::C8,
_ => return Err(())
_ => return Err(()),
})
}
}
@ -358,7 +394,7 @@ impl TryFrom<u8> for Cell {
6 => Cell::C6,
7 => Cell::C7,
8 => Cell::C8,
_ => return Err(())
_ => return Err(()),
})
}
}

View File

@ -1,6 +1,6 @@
<!doctype html>
<head>
<script src="./htmx.min.js"></script>
<script src="./htmx.js"></script>
<style type="text/css">
body {
#grid {
@ -23,63 +23,63 @@
<button
class="cell"
hx-post="/ttt/cell0"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell1"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell2"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell3"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell4"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell5"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell6"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell7"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell8"
hx-trigger="click"
hx-target=".grid"
hx-target="#grid"
hx-swap="innerHTML"
></button>
</div>