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

Add Safe Abstractions for FuriStreamBuffer #177

Merged
merged 17 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions crates/flipperzero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ harness = false
name = "dialog"
required-features = ["alloc"]

[[example]]
name = "stream_buffer"
required-features = ["alloc"]

[[example]]
name = "threads"
required-features = ["alloc"]
85 changes: 85 additions & 0 deletions crates/flipperzero/examples/stream_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! Example showcasing the use of a stream buffer on multiple threads.

#![no_main]
#![no_std]

// Required for panic handler
extern crate flipperzero_rt;

// Required for allocator
extern crate flipperzero_alloc;

extern crate alloc;

use core::{ffi::CStr, num::NonZeroUsize, time::Duration};

use flipperzero::{furi, println};
use flipperzero_rt::{entry, manifest};

// Define the FAP Manifest for this application
manifest!(name = "Stream buffer example");

// Define the entry function
entry!(main);

// Entry point
fn main(_args: Option<&CStr>) -> i32 {
// Create a stream buffer pair
let size = NonZeroUsize::new(1024).unwrap();
let trigger_level = 16;
let (tx, rx) = furi::stream_buffer::stream_buffer(size, trigger_level);

// Stream buffer is empty
assert_eq!(tx.spaces_available(), size.into());
assert_eq!(rx.spaces_available(), size.into());
assert_eq!(tx.bytes_available(), 0);
assert_eq!(rx.bytes_available(), 0);

// Sending 4 bytes immediately
assert_eq!(tx.send(&[1, 2, 3, 4]), 4);
assert_eq!(tx.bytes_available(), 4);

// Receive bytes
let mut recv_buf = [0; 32];
assert_eq!(rx.recv(&mut recv_buf), 4);
assert_eq!(recv_buf[0..4], [1, 2, 3, 4]);

// Move sender to another thread
let tx_thread = furi::thread::spawn(move || {
// Wait 2 seconds before we send some bytes
furi::thread::sleep(Duration::from_secs(2));
assert_eq!(tx.send(&[5; 20]), 20);

// Send some bytes in a loop to see how the receiver handles them
for i in 4..20 {
furi::thread::sleep(Duration::from_millis(200));
tx.send(&[i as u8; 3]);
}

0
});

// Move receiver to another thread
let rx_thread = furi::thread::spawn(move || {
let mut buf = [0; 32];

// The sender waits 2 seconds, so we don't block and get no bytes
assert_eq!(rx.recv(&mut buf), 0);

// The sender sends 20 bytes after two seconds, that is more than the trigger, so we continue
assert_eq!(rx.recv_blocking(&mut buf), 20);

// Try to receive bytes as long as the sender is alive
while rx.is_sender_alive() {
let n = rx.recv_with_timeout(&mut buf, Duration::from_secs(2));
println!("got {} bytes: {:?}", n, buf[0..n]);
}

0
});

tx_thread.join();
rx_thread.join();

0
}
2 changes: 2 additions & 0 deletions crates/flipperzero/src/furi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub mod io;
pub mod log;
pub mod message_queue;
pub mod rng;
#[cfg(feature = "alloc")]
pub mod stream_buffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the following point, but I would prefer a separate stream-buffer feature which depends on alloc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels really bloated to a feature just because of the Arc I used. I could replace it with some custom counting logic using an AtomicUsize and sys::malloc to get a pointer but I'm not sure if that is better.

pub mod string;
pub mod sync;
pub mod thread;
Expand Down
Loading