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

View File

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

View File

@ -23,7 +23,7 @@ use embassy_time::Duration;
use embassy_time::Timer; use embassy_time::Timer;
use embedded_io_async::Write as _; use embedded_io_async::Write as _;
use heapless::{String, Vec}; use heapless::{String, Vec};
use log::{info, warn}; use log::{debug, info, warn};
use rand_core::RngCore; use rand_core::RngCore;
use static_cell::StaticCell; use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _}; 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 { let (code, res_type, res_content): (HttpResCode, &str, &[u8]) = match path {
"/" => (HttpResCode::Ok, include_bytes!("../web/index.html")), "/" => (HttpResCode::Ok, "html", include_bytes!("../web/index.html")),
"/htmx.min.js" => (HttpResCode::Ok, include_bytes!("../web/htmx.min.js")), "/htmx.min.js" => (
HttpResCode::Ok,
"javascript",
include_bytes!("../web/htmx.min.js"),
),
"/htmx.js" => (
HttpResCode::Ok,
"javascript",
include_bytes!("../web/htmx.js"),
),
p => 'res: { p => 'res: {
if p.starts_with("/ppp/cell") && p.len() == 10 { if p.starts_with("/ttt/cell") && p.len() == 10 {
let clicked_c: Cell = match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) { let clicked_c: Cell =
Ok(c) => c, match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
Err(_) => {break 'res (HttpResCode::NotFound, &[])} Ok(c) => c,
}; Err(_) => break 'res (HttpResCode::NotFound, "", &[]),
};
let mut board = board_atom.load(Ordering::Acquire); let mut board = board_atom.load(Ordering::Acquire);
if board & ((2^(clicked_c as u32)) + (2^(9+clicked_c as u32))) != 0 { if board
break 'res (HttpResCode::Forbidden, &[]) & ((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); board_atom.store(board, Ordering::Release);
info!("board : {}", board);
res_buf.clear(); res_buf.clear();
for c in 0..=8 { 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) Some(Team::Zero)
} else if board & (2^(9+c)) != 0 { } else if board & 2_u32.pow(9 + c) != 0 {
Some(Team::One) Some(Team::One)
} else {None}; } else {
None
};
info!("{} : {:?}", c, picked_by);
match picked_by { match picked_by {
Some(Team::Zero) => {res_buf.extend_from_slice(b"<div class=\"cell\" style=\"color:blue\"></div>").unwrap();}, Some(Team::Zero) => {
Some(Team::One) => {res_buf.extend_from_slice(b"<div class=\"cell\" style=\"color:red\"></div>").unwrap();}, res_buf
None => {write!( .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, &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 c
).unwrap();} ).unwrap();
}
}; };
} }
(HttpResCode::Ok, &res_buf) (HttpResCode::Ok, "html", &res_buf)
} else { } 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!( if let Err(e) = write!(
&mut res_head_buf, &mut res_head_buf,
"{}\r\n\ "{}\r\n\
Content-Type: text/html\r\n\ Content-Type: text/{}\r\n\
Content-Length: {}\r\n\r\n", Content-Length: {}\r\n\r\n",
Into::<&str>::into(code), Into::<&str>::into(code),
res_type,
res_content.len() res_content.len()
) { ) {
warn!("res buffer write error: {:?}", e); warn!("res buffer write error: {:?}", e);
@ -289,14 +329,14 @@ enum HttpRequestType {
enum HttpResCode { enum HttpResCode {
Ok, Ok,
NotFound, NotFound,
Forbidden Forbidden,
} }
impl Into<&str> for HttpResCode { impl Into<&str> for HttpResCode {
fn into(self) -> &'static str { fn into(self) -> &'static str {
match self { match self {
HttpResCode::Ok => "HTTP/1.1 200 OK", HttpResCode::Ok => "HTTP/1.1 200 OK",
HttpResCode::NotFound => "HTTP/1.1 404 NOT FOUND", 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 { impl From<bool> for Team {
fn from(value: bool) -> Self { fn from(value: bool) -> Self {
if value { if value { Team::One } else { Team::Zero }
Team::One
} else {
Team::Zero
}
} }
} }
@ -341,7 +377,7 @@ impl TryFrom<char> for Cell {
'6' => Cell::C6, '6' => Cell::C6,
'7' => Cell::C7, '7' => Cell::C7,
'8' => Cell::C8, '8' => Cell::C8,
_ => return Err(()) _ => return Err(()),
}) })
} }
} }
@ -358,7 +394,7 @@ impl TryFrom<u8> for Cell {
6 => Cell::C6, 6 => Cell::C6,
7 => Cell::C7, 7 => Cell::C7,
8 => Cell::C8, 8 => Cell::C8,
_ => return Err(()) _ => return Err(()),
}) })
} }
} }

View File

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