Skip to content
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
18 changes: 18 additions & 0 deletions cflib2/_rust.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,24 @@ class LinkContext:
# Returns
List of URIs found
"""
async def send_radio_broadcast(
self,
radio_nth: builtins.int,
channel: builtins.int,
address: typing.Sequence[builtins.int],
data: typing.Sequence[builtins.int],
) -> None:
r"""
Send a radio broadcast packet (no acknowledgement) on a specific radio and channel

This sends a raw packet without expecting an ack, useful for P2P communication.

# Arguments
* `radio_nth` - Radio dongle index (usually 0)
* `channel` - Radio channel number (0-125)
* `address` - 5-byte destination address
* `data` - Packet payload bytes
"""

class LinkError(CrazyflieError):
r"""
Expand Down
9 changes: 5 additions & 4 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ extension-module = ["pyo3/extension-module"]
pyo3 = { version = "0.26" }
pyo3-async-runtimes = { version = "0.26", features = ["tokio-runtime"] }
crazyflie-lib = "0.7.1"
crazyflie-link = "0.4.2"
crazyflie-link = "0.4.3"
crazyradio = "0.6.0"
tokio = { version = "1.48", features = ["full"] }
futures = "0.3.31"
pyo3-stub-gen-derive = "0.17.0"
Expand Down
34 changes: 32 additions & 2 deletions rust/src/link_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! Link context for scanning and discovering Crazyflies

use pyo3::prelude::*;
use pyo3::exceptions::PyRuntimeError;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3_stub_gen_derive::*;
use std::sync::Arc;

Expand Down Expand Up @@ -65,7 +65,7 @@ impl LinkContext {
// Default to E7E7E7E7E7 if no address provided
let addr = if let Some(addr_vec) = address {
if addr_vec.len() != 5 {
return Err(PyRuntimeError::new_err(
return Err(PyValueError::new_err(
"Address must be exactly 5 bytes"
));
}
Expand All @@ -83,4 +83,34 @@ impl LinkContext {
Ok(uris.into_iter().map(|uri| uri.to_string()).collect::<Vec<_>>())
})
}

/// Send a radio broadcast packet (no acknowledgement) on a specific radio and channel
///
/// This sends a raw packet without expecting an ack, useful for P2P communication.
///
/// # Arguments
/// * `radio_nth` - Radio dongle index (usually 0)
/// * `channel` - Radio channel number (0-125)
/// * `address` - 5-byte destination address
/// * `data` - Packet payload bytes
#[gen_stub(override_return_type(type_repr = "collections.abc.Coroutine[typing.Any, typing.Any, None]"))]
fn send_radio_broadcast<'py>(&self, py: Python<'py>, radio_nth: usize, channel: u8, address: Vec<u8>, data: Vec<u8>) -> PyResult<Bound<'py, PyAny>> {
Comment on lines +87 to +97
if address.len() != 5 {
return Err(PyValueError::new_err("Address must be exactly 5 bytes"));
}
let mut addr_array = [0u8; 5];
addr_array.copy_from_slice(&address);

let ch = crazyradio::Channel::from_number(channel)
.map_err(|_| PyValueError::new_err(format!("Invalid channel {}: must be 0-125", channel)))?;

let inner = self.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let mut radio = inner.get_radio(radio_nth).await
.map_err(|e| PyRuntimeError::new_err(format!("Failed to get radio: {:?}", e)))?;
radio.send_packet_no_ack_async(ch, addr_array, data).await
.map_err(|e| PyRuntimeError::new_err(format!("Broadcast failed: {:?}", e)))?;
Ok(())
Comment on lines +87 to +113
})
}
}
Loading