-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-FileCopyrightText: The djio authors | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
use std::{future::Future, time::Duration}; | ||
|
||
use discro::{new_pubsub, Publisher, Subscriber}; | ||
|
||
use crate::{BlinkingLedOutput, BlinkingLedTicker}; | ||
|
||
#[allow(clippy::manual_async_fn)] // Explicit return type to to enforce the trait bounds | ||
pub fn blinking_led_task( | ||
period: Duration, | ||
publisher: Publisher<BlinkingLedOutput>, | ||
) -> impl Future<Output = ()> + Send + 'static { | ||
async move { | ||
let mut ticker = BlinkingLedTicker::default(); | ||
let mut interval = tokio::time::interval(period); | ||
loop { | ||
interval.tick().await; | ||
let output = ticker.tick(); | ||
publisher.write(output); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "spawn-blinking-led-task")] | ||
#[must_use] | ||
pub fn spawn_blinking_led_task(period: Duration) -> Subscriber<BlinkingLedOutput> { | ||
let (publisher, subscriber) = new_pubsub(BlinkingLedOutput::ON); | ||
let task = blinking_led_task(period, publisher); | ||
tokio::spawn(task); | ||
subscriber | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters