added spmc for the button to relay and led task
This commit is contained in:
parent
bb1349d903
commit
0e62f65a78
5 changed files with 41 additions and 25 deletions
33
src/main.rs
33
src/main.rs
|
@ -7,21 +7,14 @@ use button_relay::run::button::button_task;
|
||||||
use button_relay::run::relay::relay_task;
|
use button_relay::run::relay::relay_task;
|
||||||
use button_relay::run::shared::{Command, QUEUE_SIZE};
|
use button_relay::run::shared::{Command, QUEUE_SIZE};
|
||||||
|
|
||||||
// use embassy_executor::raw::Executor;
|
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
||||||
use embassy_sync::channel::Channel;
|
use embassy_sync::pubsub::{PubSubChannel, Publisher, Subscriber};
|
||||||
use esp_backtrace as _;
|
use esp_backtrace as _;
|
||||||
use esp_println::println;
|
use esp_println::println;
|
||||||
// use hal::embassy::executor::Executor;
|
|
||||||
use hal::gpio::{Input, Io, Level, Output, Pull};
|
use hal::gpio::{Input, Io, Level, Output, Pull};
|
||||||
use hal::{
|
use hal::{
|
||||||
clock::ClockControl,
|
clock::ClockControl, delay::Delay, peripherals::Peripherals, prelude::*, system::SystemControl,
|
||||||
delay::Delay,
|
|
||||||
// embassy,
|
|
||||||
peripherals::Peripherals,
|
|
||||||
prelude::*,
|
|
||||||
system::SystemControl,
|
|
||||||
};
|
};
|
||||||
use static_cell::make_static;
|
use static_cell::make_static;
|
||||||
|
|
||||||
|
@ -58,9 +51,17 @@ async fn main(spawner: Spawner) {
|
||||||
let led_pin = Output::new(io.pins.gpio13, Level::Low);
|
let led_pin = Output::new(io.pins.gpio13, Level::Low);
|
||||||
let relay_pin = Output::new(io.pins.gpio2, Level::Low);
|
let relay_pin = Output::new(io.pins.gpio2, Level::Low);
|
||||||
|
|
||||||
let encoder_channel: &'static mut Channel<NoopRawMutex, Command, QUEUE_SIZE> =
|
let channel: &'static mut PubSubChannel<
|
||||||
make_static!(Channel::new());
|
NoopRawMutex,
|
||||||
let encoder_senders = [encoder_channel.sender()];
|
Command,
|
||||||
|
QUEUE_SIZE,
|
||||||
|
QUEUE_SIZE,
|
||||||
|
QUEUE_SIZE,
|
||||||
|
> = make_static!(PubSubChannel::new());
|
||||||
|
|
||||||
|
let publisher = channel.publisher().unwrap();
|
||||||
|
let led_subscriber = channel.subscriber().unwrap();
|
||||||
|
let relay_subscriber = channel.subscriber().unwrap();
|
||||||
|
|
||||||
hal::interrupt::enable(
|
hal::interrupt::enable(
|
||||||
hal::peripherals::Interrupt::GPIO,
|
hal::peripherals::Interrupt::GPIO,
|
||||||
|
@ -82,10 +83,10 @@ async fn main(spawner: Spawner) {
|
||||||
|
|
||||||
println!("Starting embassy executor ...");
|
println!("Starting embassy executor ...");
|
||||||
|
|
||||||
let _ = spawner.spawn(blink_green(led_pin, encoder_channel.receiver()));
|
spawner.spawn(button_task(button_pin, publisher)).unwrap();
|
||||||
let _ = spawner.spawn(button_task(button_pin, encoder_senders));
|
spawner.spawn(blink_green(led_pin, led_subscriber)).unwrap();
|
||||||
let _ = spawner
|
spawner
|
||||||
.spawn(relay_task(relay_pin, encoder_channel.receiver()))
|
.spawn(relay_task(relay_pin, relay_subscriber))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
extern crate embassy_executor;
|
extern crate embassy_executor;
|
||||||
|
|
||||||
use crate::confg::Sens;
|
use crate::confg::Sens;
|
||||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Receiver};
|
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
|
||||||
use embassy_time::{Duration, Timer};
|
use embassy_time::{Duration, Timer};
|
||||||
use hal::gpio::{Gpio13, Output};
|
use hal::gpio::{Gpio13, Output};
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ use super::shared::{Command, QUEUE_SIZE};
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn blink_green(
|
pub async fn blink_green(
|
||||||
mut pin: Output<'static, Gpio13>,
|
mut pin: Output<'static, Gpio13>,
|
||||||
receiver: Receiver<'static, NoopRawMutex, Command, QUEUE_SIZE>,
|
mut subscriber: Subscriber<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
log::info!("{}", Sens("Loop..."));
|
log::info!("{}", Sens("Loop..."));
|
||||||
let command = receiver.receive().await;
|
let command = subscriber.next_message_pure().await;
|
||||||
// if command.is_on {
|
// if command.is_on {
|
||||||
if command.led_state {
|
if command.led_state {
|
||||||
pin.set_high();
|
pin.set_high();
|
||||||
|
|
|
@ -3,14 +3,14 @@ extern crate embassy_executor;
|
||||||
// use esp_println::println;
|
// use esp_println::println;
|
||||||
use hal::gpio::{Gpio12, Input};
|
use hal::gpio::{Gpio12, Input};
|
||||||
|
|
||||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Sender};
|
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Publisher};
|
||||||
|
|
||||||
use super::shared::{Command, QUEUE_SIZE};
|
use super::shared::{Command, QUEUE_SIZE};
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn button_task(
|
pub async fn button_task(
|
||||||
button_pin: Input<'static, Gpio12>,
|
button_pin: Input<'static, Gpio12>,
|
||||||
senders: [Sender<'static, NoopRawMutex, Command, QUEUE_SIZE>; 1],
|
publisher: Publisher<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||||
) {
|
) {
|
||||||
let _command_current_state = Command {
|
let _command_current_state = Command {
|
||||||
is_on: false,
|
is_on: false,
|
||||||
|
@ -36,7 +36,8 @@ pub async fn button_task(
|
||||||
};
|
};
|
||||||
|
|
||||||
// Send the payload through the channel
|
// Send the payload through the channel
|
||||||
senders[0].send(payload).await;
|
publisher.publish(payload).await;
|
||||||
|
|
||||||
esp_println::println!(
|
esp_println::println!(
|
||||||
"Button pressed, LED State: {}, Relay State: {}",
|
"Button pressed, LED State: {}, Relay State: {}",
|
||||||
led_state,
|
led_state,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
extern crate embassy_executor;
|
extern crate embassy_executor;
|
||||||
|
|
||||||
use crate::confg::Sens;
|
use crate::confg::Sens;
|
||||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Receiver};
|
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
|
||||||
use esp_println::println;
|
use esp_println::println;
|
||||||
use hal::gpio::{Gpio2, Output};
|
use hal::gpio::{Gpio2, Output};
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ use super::shared::{Command, QUEUE_SIZE};
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn relay_task(
|
pub async fn relay_task(
|
||||||
mut relay: Output<'static, Gpio2>,
|
mut relay: Output<'static, Gpio2>,
|
||||||
receiver: Receiver<'static, NoopRawMutex, Command, QUEUE_SIZE>,
|
mut subscriber: Subscriber<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
log::info!("{}", Sens("Loop..."));
|
log::info!("{}", Sens("Loop..."));
|
||||||
let command = receiver.receive().await;
|
let command = subscriber.next_message_pure().await;
|
||||||
// if command.is_on {
|
// if command.is_on {
|
||||||
if command.relay_state {
|
if command.relay_state {
|
||||||
relay.set_high();
|
relay.set_high();
|
||||||
|
|
|
@ -1,3 +1,17 @@
|
||||||
|
// use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
// use embassy_sync::blocking_mutex::Mutex;
|
||||||
|
// use static_cell::make_static;
|
||||||
|
|
||||||
|
// pub struct GlobalState {
|
||||||
|
// pub led_state: AtomicBool,
|
||||||
|
// pub relay_state: AtomicBool,
|
||||||
|
// }
|
||||||
|
|
||||||
|
// static GLOBAL_STATE: Mutex<GlobalState> = Mutex::new(GlobalState {
|
||||||
|
// led_state: AtomicBool::new(false),
|
||||||
|
// relay_state: AtomicBool::new(false),
|
||||||
|
// });
|
||||||
|
|
||||||
pub const QUEUE_SIZE: usize = 10;
|
pub const QUEUE_SIZE: usize = 10;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
|
Loading…
Reference in a new issue