play over network

This commit is contained in:
Andy Killorin 2024-12-09 16:43:35 -05:00
parent 43fedd11bf
commit 860e49de9d
Signed by: ank
GPG key ID: B6241CA3B552BCA4
9 changed files with 123 additions and 8 deletions

33
common/Cargo.lock generated
View file

@ -11,10 +11,17 @@ dependencies = [
"generic-array",
]
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"heapless",
"serde",
"sha3",
]
@ -58,6 +65,26 @@ dependencies = [
"version_check",
]
[[package]]
name = "hash32"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"hash32",
"serde",
"stable_deref_trait",
]
[[package]]
name = "keccak"
version = "0.1.5"
@ -121,6 +148,12 @@ dependencies = [
"keccak",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "syn"
version = "2.0.90"

View file

@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
sha3 = {version="0.10.0", default-features=false}
serde = { version = "1.0.203", default-features = false, features = ["derive"] }
heapless = { version = "0.8.0", features = ["serde"] }

View file

@ -9,6 +9,7 @@ pub enum Request {
SetRecentBadge(Name),
SetBadge(Name, u64),
SetVolume(u8),
Play(heapless::String<32>),
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]

2
outside/Cargo.lock generated
View file

@ -264,6 +264,7 @@ dependencies = [
name = "common"
version = "0.1.0"
dependencies = [
"heapless 0.8.0",
"serde",
"sha3",
]
@ -1303,6 +1304,7 @@ checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"defmt",
"hash32 0.3.1",
"serde",
"stable_deref_trait",
]

View file

@ -83,7 +83,7 @@ pub enum MusicCommand {
Introduce(Name, bool),
/// 0-30
SetVolume(u8),
Play(&'static str),
Play(heapless::String<32>),
Button(),
}

View file

@ -72,6 +72,9 @@ pub async fn server_task(stack: embassy_net::Stack<'static>) {
Ok(Request::SetVolume(vol)) => {
COMMANDS.send(crate::music::MusicCommand::SetVolume(vol)).await;
}
Ok(Request::Play(song)) => {
COMMANDS.send(crate::music::MusicCommand::Play(song))
}
Err(e) => { info!("parse error: {e}") }
}
}

26
server/Cargo.lock generated
View file

@ -162,6 +162,7 @@ checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
name = "common"
version = "0.1.0"
dependencies = [
"heapless 0.8.0",
"serde",
"sha3",
]
@ -366,6 +367,15 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hash32"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.7.17"
@ -373,13 +383,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f"
dependencies = [
"atomic-polyfill",
"hash32",
"hash32 0.2.1",
"rustc_version",
"serde",
"spin",
"stable_deref_trait",
]
[[package]]
name = "heapless"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"hash32 0.3.1",
"serde",
"stable_deref_trait",
]
[[package]]
name = "http"
version = "1.2.0"
@ -660,7 +681,7 @@ dependencies = [
"cobs",
"embedded-io 0.4.0",
"embedded-io 0.6.1",
"heapless",
"heapless 0.7.17",
"serde",
]
@ -797,6 +818,7 @@ dependencies = [
"anyhow",
"axum",
"common",
"heapless 0.8.0",
"ping-rs",
"postcard",
"serde",

View file

@ -7,6 +7,7 @@ edition = "2021"
anyhow = "1.0.94"
axum = "0.7.9"
common = {path = "../common"}
heapless = { version = "0.8.0", features = ["serde"] }
ping-rs = "0.1.2"
postcard = {version = "1.0.0", features = ["alloc"]}
serde = "*"

View file

@ -1,5 +1,5 @@
use std::{collections::HashMap, net::Ipv4Addr, sync::Arc, time::Duration};
use std::{collections::HashMap, net::Ipv4Addr, str::FromStr, sync::Arc, time::Duration};
use axum::{extract::State, http::StatusCode, response::Html, routing::{get, post}, serve, Form, Router};
use common::{Name, Request};
@ -73,11 +73,12 @@ async fn main() -> Result<()>{
let app: Router = Router::new()
.route("/names", get(names))
.route("/soundboard", get(soundboard))
.route("/", get(root))
.route("/admin", get(control))
.route("/phil", get(phil))
.route("/recent", get(recent_badge))
.route("/setLast", post(set_last))
.route("/play", post(play))
.route("/openDoor", post(open_door))
.route("/open", get(open_door))
.route("/setVolume", post(set_volume))
@ -123,9 +124,9 @@ async fn main() -> Result<()>{
let door = Ipv4Addr::new(192,168,0,10).into();
let router = Ipv4Addr::new(192,168,0,50).into();
let (fish,door,router) = join!(
send_ping_async(&fish, Duration::from_millis(1500), Arc::new(&[255,127]), None),
send_ping_async(&door, Duration::from_millis(1500), Arc::new(&[255,127]), None),
send_ping_async(&router, Duration::from_millis(1500), Arc::new(&[255,127]), None)
send_ping_async(&fish, Duration::from_millis(3500), Arc::new(&[255,127]), None),
send_ping_async(&door, Duration::from_millis(3500), Arc::new(&[255,127]), None),
send_ping_async(&router, Duration::from_millis(3500), Arc::new(&[255,127]), None)
);
let status = NetStatus {
@ -204,6 +205,36 @@ async fn root() -> Html<String> {
"#);
Html::from(page)
}
async fn soundboard() -> Html<String> {
let mut page = format!(r#"
<h1>soundboard</h1>
"#);
for (name, sound) in [
("I'm John the Fish!", "b1"),
] {
page.push_str(&format!(r#"
<form target="output" style="display:inline; margin-right:10px; " action="setLast" method="post" id="myForm">
<button style="font-size:40px;" type="submit" name="name" value="{sound}" target="output">{name}</button>
</form>
"#));
}
page.push_str(&format!(r#"
<form target="output" style="margin-top:10px;" action="setLast" method="post" id="myForm">
<input style="font-size:40px;" name="name" placeholder="other" "type="text"></input>
<button style="font-size:40px;" type="submit" target="output">other</button>
</form>
<form target="output" action="setVolume" method="post" id="myForm">
<input type="range" min="0" max="30" value="0" name="volume" class="slider" id="volume">
<button style="font-size:30px;" type="submit" target="output">set volume</button>
</form>
<iframe name="output"></iframe>
"#));
Html::from(page)
}
async fn names(state: State<AppState>) -> Html<String> {
let last_badge_view = last_badge_view(&state).await;
@ -332,6 +363,27 @@ async fn set_volume(
Html::from(format!("<p>volume is now {}</p></br><a href=\"/\">back</a>", input.volume))
}
#[derive(serde::Deserialize)]
struct SongForm {
name: String,
}
async fn play(
state: State<AppState>,
Form(input): Form<SongForm>) -> Html<String> {
info!("playing {}", input.name);
let _fish = state.fish.acquire().await.unwrap();
let mut conn = TcpStream::connect(FISH).await.unwrap();
let name = heapless::String::from_str(&input.name).unwrap();
let req = Request::Play(name);
conn.write_all(&to_allocvec(&req).unwrap()).await.unwrap();
conn.flush().await.unwrap();
Html::from(format!("<p>playing {}</p>", input.name))
}
#[derive(serde::Deserialize)]
struct NameForm {
name: String,