Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(output-net/wled): fix sending #32

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions examples/wled-demo.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
#![allow(unused_variables)]

use anyhow::Result;
use palette::{Hsl, Srgb};
use palette::Hsl;

use photonic::attr::AsFixedAttr;
use photonic::Scene;
use photonic_effects::nodes::{Brightness, Raindrops};
use photonic_output_null::Null;
use photonic_effects::nodes::Raindrops;
use photonic_output_net::wled;

#[tokio::main]
async fn main() -> Result<()> {
let mut scene = Scene::new();

let base = scene.node("raindrops", Raindrops {
let raindrops = scene.node("raindrops", Raindrops {
rate: 0.3.fixed(),
decay: (0.96, 0.98).fixed(),
color: (Hsl::new(187.5, 0.25, 0.5), Hsl::new(223.92, 0.5, 0.5)).fixed(),
color: (Hsl::new(27.5, 0.25, 0.5), Hsl::new(79.0, 0.5, 0.5)).fixed(),
})?;

let brightness = scene.node("brightness", Brightness {
value: 1.0.fixed(),
source: base,
range: None,
})?;

// let (scene, introspection) = scene.run(brightness, Terminal {
// waterfall: true,
// })?;
let output = wled::WledSender {
mode: Default::default(),
size: 50,
target: "192.168.0.29:21324".parse()?,
};

let scene = scene.run(brightness, Null::<Srgb>::default()).await?;
let scene = scene.run(raindrops, output).await?;

return scene.run(60).await;
}
14 changes: 12 additions & 2 deletions output-net/src/wled.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use byteorder::{BigEndian, WriteBytesExt};
use std::net::SocketAddr;

use anyhow::Result;
use byteorder::{BigEndian, WriteBytesExt};
use palette::rgb::Rgb;

use photonic::{BufferReader, Output, OutputDecl, WhiteMode};
Expand Down Expand Up @@ -32,15 +33,18 @@ impl Mode {
}
}

#[derive(Default)]
pub struct WledSender {
pub mode: Mode,
pub size: usize,

pub target: SocketAddr,
}

pub struct WledSenderOutput {
mode: Mode,
size: usize,

socket: tokio::net::UdpSocket,
}

impl OutputDecl for WledSender {
Expand All @@ -50,9 +54,13 @@ impl OutputDecl for WledSender {

async fn materialize(self) -> Result<Self::Output>
where Self::Output: Sized {
let socket = tokio::net::UdpSocket::bind("[::]:0").await?;
socket.connect(self.target).await?;

return Ok(Self::Output {
mode: self.mode,
size: self.size,
socket,
});
}
}
Expand Down Expand Up @@ -110,6 +118,8 @@ impl Output for WledSenderOutput {
}
}

self.socket.send(&buffer).await?;

return Ok(());
}

Expand Down
Loading