Skip to content

Commit

Permalink
Introduce recv_blocking() method on PayloadReceiver
Browse files Browse the repository at this point in the history
Allows callers to wait for a frame without using futures.

Also bump async-std dependency to 1.12 (unfortunately still doesn't ensure recent-enough
`async-channel` - but I'll fix that in another PR).

Also update the stream example and READMEs with the new method, making it print any receive errors
(but not panic on them) and doing exactly 10 tries (instead of waiting for 10 success payloads).
  • Loading branch information
strohel committed Mar 14, 2023
1 parent 235672b commit 7162e21
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 65 deletions.
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,28 @@ camera.load_context().unwrap();
// Start streaming. Channel capacity is set to 3.
let payload_rx = camera.start_streaming(3).unwrap();

let mut payload_count = 0;
while payload_count < 10 {
match payload_rx.try_recv() {
Ok(payload) => {
println!(
"payload received! block_id: {:?}, timestamp: {:?}",
payload.id(),
payload.timestamp()
);
if let Some(image_info) = payload.image_info() {
println!("{:?}\n", image_info);
let image = payload.image();
// do something with the image.
// ...
}
payload_count += 1;

// Send back payload to streaming loop to reuse the buffer. This is optional.
payload_rx.send_back(payload);
}
Err(_err) => {
for _ in 0..10 {
let payload = match payload_rx.recv_blocking() {
Ok(payload) => payload,
Err(e) => {
println!("payload receive error: {e}");
continue;
}
};
println!(
"payload received! block_id: {:?}, timestamp: {:?}",
payload.id(),
payload.timestamp()
);
if let Some(image_info) = payload.image_info() {
println!("{:?}\n", image_info);
let image = payload.image();
// do something with the image.
// ...
}

// Send back payload to streaming loop to reuse the buffer. This is optional.
payload_rx.send_back(payload);
}

// Closes the camera.
Expand Down
2 changes: 1 addition & 1 deletion cameleon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ thiserror = "1.0.24"
semver = "1.0.0"
zip = "0.6.0"
sha-1 = "0.10.0"
async-std = { version = "1.9.0", features = ["unstable"] }
async-std = { version = "1.12.0", features = ["unstable"] }
tracing = "0.1.26"
auto_impl = "1.0.1"
cameleon-device = { path = "../device", version = "0.1.8" }
Expand Down
41 changes: 19 additions & 22 deletions cameleon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,28 @@ camera.load_context().unwrap();
// Start streaming. Channel capacity is set to 3.
let payload_rx = camera.start_streaming(3).unwrap();

let mut payload_count = 0;
while payload_count < 10 {
match payload_rx.try_recv() {
Ok(payload) => {
println!(
"payload received! block_id: {:?}, timestamp: {:?}",
payload.id(),
payload.timestamp()
);
if let Some(image_info) = payload.image_info() {
println!("{:?}\n", image_info);
let image = payload.image();
// do something with the image.
// ...
}
payload_count += 1;

// Send back payload to streaming loop to reuse the buffer. This is optional.
payload_rx.send_back(payload);
}
Err(_err) => {
for _ in 0..10 {
let payload = match payload_rx.recv_blocking() {
Ok(payload) => payload,
Err(e) => {
println!("payload receive error: {e}");
continue;
}
};
println!(
"payload received! block_id: {:?}, timestamp: {:?}",
payload.id(),
payload.timestamp()
);
if let Some(image_info) = payload.image_info() {
println!("{:?}\n", image_info);
let image = payload.image();
// do something with the image.
// ...
}

// Send back payload to streaming loop to reuse the buffer. This is optional.
payload_rx.send_back(payload);
}

// Closes the camera.
Expand Down Expand Up @@ -188,4 +186,3 @@ To start developing, please refer to [CONTRIBUTING.md][contributing].
This project is licenced under [MPL 2.0][license].

[license]: https://github.com/cameleon-rs/cameleon/blob/main/LICENSE

34 changes: 16 additions & 18 deletions cameleon/examples/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,25 @@ fn main() {
// Start streaming. Channel capacity is set to 3.
let payload_rx = camera.start_streaming(3).unwrap();

let mut payload_count = 0_usize;
while payload_count < 10 {
match payload_rx.try_recv() {
Ok(payload) => {
println!(
"payload received! block_id: {:?}, timestamp: {:?}",
payload.id(),
payload.timestamp()
);
if let Some(image_info) = payload.image_info() {
println!("{:?}\n", image_info);
}
payload_count += 1;

// Send back payload to streaming loop to reuse the buffer.
payload_rx.send_back(payload);
}
Err(_err) => {
for _ in 0..10 {
let payload = match payload_rx.recv_blocking() {
Ok(payload) => payload,
Err(e) => {
println!("payload receive error: {e}");
continue;
}
};
println!(
"payload received! block_id: {:?}, timestamp: {:?}",
payload.id(),
payload.timestamp()
);
if let Some(image_info) = payload.image_info() {
println!("{:?}\n", image_info);
}

// Send back payload to streaming loop to reuse the buffer.
payload_rx.send_back(payload);
}

camera.close().ok();
Expand Down
6 changes: 6 additions & 0 deletions cameleon/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ impl PayloadReceiver {
self.rx.try_recv()?
}

/// Receives [`Payload`] sent from the device.
/// If the channel is empty, this method blocks until the device produces the payload.
pub fn recv_blocking(&self) -> StreamResult<Payload> {
self.rx.recv_blocking()?
}

/// Sends back [`Payload`] to the device to reuse already allocated `payload`.
///
/// Sending back `payload` may improve performance of streaming, but not required to call this
Expand Down
5 changes: 2 additions & 3 deletions device/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ repository = "https://github.com/cameleon-rs/cameleon"
description = """
cameleon-device provides device specific protocol decoders and basic I/O operations for GenICam compatible devices.
"""
categories = ["computer-vision"]
categories = ["computer-vision"]
keywords = ["genicam", "camera", "usb3", "gige", "uvc"]

[dependencies]
thiserror = "1.0.24"
log = "0.4.14"
semver = "1.0.0"
async-std = { version = "1.9.0", features = ["unstable"] }
async-std = { version = "1.12.0", features = ["unstable"] }
const_format = "0.2.14"
futures = "0.3.14"
lazy_static = "1.4.0"
Expand All @@ -29,7 +29,6 @@ rusb = { version = "0.9.0", optional = true }
libusb1-sys = { version = "0.6.0", optional = true }
libc = { version = "0.2", optional = true }


[dev-dependencies]
trybuild = "1.0.42"

Expand Down

0 comments on commit 7162e21

Please sign in to comment.