pub struct DDPConnection {
pub pixel_config: PixelConfig,
pub id: ID,
pub receiver_packet: Receiver<Packet>,
/* private fields */
}Expand description
A connection to a DDP display device.
This is the main type for sending pixel data to LED strips and other DDP-compatible displays. It handles packet assembly, sequencing, and automatic chunking of large data arrays.
§Examples
§Basic usage
use ddp_rs::connection::DDPConnection;
use ddp_rs::protocol::{PixelConfig, ID};
use std::net::UdpSocket;
let mut conn = DDPConnection::try_new(
"192.168.1.40:4048",
PixelConfig::default(),
ID::Default,
UdpSocket::bind("0.0.0.0:4048")?
)?;
// Send RGB data for 3 pixels
conn.write(&[
255, 0, 0, // Red
0, 255, 0, // Green
0, 0, 255, // Blue
])?;§Using offsets to update part of the strip
// Update pixels starting at byte offset 300 (pixel 100 in RGB)
conn.write_offset(&[255, 128, 64], 300)?;Fields§
§pixel_config: PixelConfigPixel format configuration (RGB, RGBW, etc.)
id: IDProtocol ID for this connection
receiver_packet: Receiver<Packet>Receiver for packets coming from the display (responses)
Implementations§
Source§impl DDPConnection
impl DDPConnection
Sourcepub fn write(&mut self, data: &[u8]) -> Result<usize, DDPError>
pub fn write(&mut self, data: &[u8]) -> Result<usize, DDPError>
Writes pixel data to the display starting at offset 0.
Large data arrays are automatically split into multiple packets. Each packet can contain up to 1440 bytes (480 RGB pixels).
§Arguments
data- Raw pixel data bytes. For RGB, this should be groups of 3 bytes (R,G,B). For RGBW, groups of 4 bytes (R,G,B,W).
§Returns
The total number of bytes sent across all packets.
§Examples
// Set first 3 pixels to red, green, blue
conn.write(&[255, 0, 0, 0, 255, 0, 0, 0, 255])?;Sourcepub fn write_offset(
&mut self,
data: &[u8],
offset: u32,
) -> Result<usize, DDPError>
pub fn write_offset( &mut self, data: &[u8], offset: u32, ) -> Result<usize, DDPError>
Writes pixel data to the display starting at a specific byte offset.
This is useful for updating only a portion of your LED strip without resending all the data.
§Arguments
data- Raw pixel data bytes to sendoffset- Starting byte offset (not pixel offset). For RGB, offset 3 = pixel 1.
§Examples
// Update pixel 10 (offset = 10 * 3 = 30) to white
conn.write_offset(&[255, 255, 255], 30)?;Sourcepub fn write_message(&mut self, msg: Message) -> Result<usize, DDPError>
pub fn write_message(&mut self, msg: Message) -> Result<usize, DDPError>
Sends a JSON control message to the display.
This is useful for things like setting brightness, changing display modes, or querying configuration.
§Arguments
msg- Aprotocol::message::Message(typed or untyped JSON)
§Examples
// Send a control message
let json_value = serde_json::json!({"brightness": 128});
conn.write_message(Message::Parsed((ID::Control, json_value)))?;Sourcepub fn get_incoming(&self) -> Result<Packet, DDPError>
pub fn get_incoming(&self) -> Result<Packet, DDPError>
Attempts to retrieve a packet from the display (non-blocking).
Checks if any response packets have been received from the display.
§Returns
Ok(Packet)- A packet was availableErr(DDPError::NothingToReceive)- No packets waitingErr(DDPError::CrossBeamError)- Channel error
Sourcepub fn try_new<A>(
addr: A,
pixel_config: PixelConfig,
id: ID,
socket: UdpSocket,
) -> Result<DDPConnection, DDPError>where
A: ToSocketAddrs,
pub fn try_new<A>(
addr: A,
pixel_config: PixelConfig,
id: ID,
socket: UdpSocket,
) -> Result<DDPConnection, DDPError>where
A: ToSocketAddrs,
Creates a new DDP connection to a display.
§Arguments
addr- The display address (IP:port). DDP standard port is 4048.pixel_config- Pixel format configuration (RGB, RGBW, etc.)id- Protocol ID to use for this connectionsocket- A bound UDP socket for sending/receiving data
§Returns
Ok(DDPConnection)- Connection created successfullyErr(DDPError)- Failed to resolve address or create connection
§Examples
use ddp_rs::connection::DDPConnection;
use ddp_rs::protocol::{PixelConfig, ID};
use std::net::UdpSocket;
let conn = DDPConnection::try_new(
"192.168.1.40:4048",
PixelConfig::default(),
ID::Default,
UdpSocket::bind("0.0.0.0:4048")?
)?;