added spmc for the button to relay and led task

This commit is contained in:
aOK 2024-06-09 15:42:37 +03:00
parent bb1349d903
commit 0e62f65a78
5 changed files with 41 additions and 25 deletions

View file

@ -7,21 +7,14 @@ use button_relay::run::button::button_task;
use button_relay::run::relay::relay_task;
use button_relay::run::shared::{Command, QUEUE_SIZE};
// use embassy_executor::raw::Executor;
use embassy_executor::Spawner;
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_println::println;
// use hal::embassy::executor::Executor;
use hal::gpio::{Input, Io, Level, Output, Pull};
use hal::{
clock::ClockControl,
delay::Delay,
// embassy,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
clock::ClockControl, delay::Delay, peripherals::Peripherals, prelude::*, system::SystemControl,
};
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 relay_pin = Output::new(io.pins.gpio2, Level::Low);
let encoder_channel: &'static mut Channel<NoopRawMutex, Command, QUEUE_SIZE> =
make_static!(Channel::new());
let encoder_senders = [encoder_channel.sender()];
let channel: &'static mut PubSubChannel<
NoopRawMutex,
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::peripherals::Interrupt::GPIO,
@ -82,10 +83,10 @@ async fn main(spawner: Spawner) {
println!("Starting embassy executor ...");
let _ = spawner.spawn(blink_green(led_pin, encoder_channel.receiver()));
let _ = spawner.spawn(button_task(button_pin, encoder_senders));
let _ = spawner
.spawn(relay_task(relay_pin, encoder_channel.receiver()))
spawner.spawn(button_task(button_pin, publisher)).unwrap();
spawner.spawn(blink_green(led_pin, led_subscriber)).unwrap();
spawner
.spawn(relay_task(relay_pin, relay_subscriber))
.unwrap();
loop {

View file

@ -1,7 +1,7 @@
extern crate embassy_executor;
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 hal::gpio::{Gpio13, Output};
@ -10,11 +10,11 @@ use super::shared::{Command, QUEUE_SIZE};
#[embassy_executor::task]
pub async fn blink_green(
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 {
log::info!("{}", Sens("Loop..."));
let command = receiver.receive().await;
let command = subscriber.next_message_pure().await;
// if command.is_on {
if command.led_state {
pin.set_high();

View file

@ -3,14 +3,14 @@ extern crate embassy_executor;
// use esp_println::println;
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};
#[embassy_executor::task]
pub async fn button_task(
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 {
is_on: false,
@ -36,7 +36,8 @@ pub async fn button_task(
};
// Send the payload through the channel
senders[0].send(payload).await;
publisher.publish(payload).await;
esp_println::println!(
"Button pressed, LED State: {}, Relay State: {}",
led_state,

View file

@ -1,7 +1,7 @@
extern crate embassy_executor;
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 hal::gpio::{Gpio2, Output};
@ -10,11 +10,11 @@ use super::shared::{Command, QUEUE_SIZE};
#[embassy_executor::task]
pub async fn relay_task(
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 {
log::info!("{}", Sens("Loop..."));
let command = receiver.receive().await;
let command = subscriber.next_message_pure().await;
// if command.is_on {
if command.relay_state {
relay.set_high();

View file

@ -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;
#[derive(Clone, Debug)]