-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
5a8a3ba
commit bc84ee6
Showing
3 changed files
with
87 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
use crate::tasks::mqtt_send_and_recv::SEND_CHANNEL; | ||
use core::str::FromStr; | ||
use defmt::{debug, error, info, warn}; | ||
use heapless::String; | ||
use hyped_core::{log_types::LogLevel, mqtt::MqttMessage, mqtt_topics::MqttTopics}; | ||
|
||
/// Log a message to the console and send it to the MQTT broker | ||
pub async fn log(level: LogLevel, message: &str) { | ||
match level { | ||
LogLevel::Info => info!("{}", message), | ||
LogLevel::Warn => warn!("{}", message), | ||
LogLevel::Error => error!("{}", message), | ||
LogLevel::Debug => debug!("{}", message), | ||
} | ||
SEND_CHANNEL | ||
.send(MqttMessage { | ||
topic: MqttTopics::to_string(&MqttTopics::Logs), | ||
payload: String::<512>::from_str(message).unwrap(), | ||
}) | ||
.await; | ||
} | ||
use crate::tasks::mqtt_send_and_recv::SEND_CHANNEL; | ||
use core::str::FromStr; | ||
use defmt::{debug, error, info, warn}; | ||
use heapless::String; | ||
use hyped_core::{log_types::LogLevel, mqtt::MqttMessage, mqtt_topics::MqttTopics}; | ||
|
||
/// Log a message to the console and send it to the MQTT broker | ||
pub async fn log(level: LogLevel, message: &str) { | ||
match level { | ||
LogLevel::Info => info!("{}", message), | ||
LogLevel::Warn => warn!("{}", message), | ||
LogLevel::Error => error!("{}", message), | ||
LogLevel::Debug => debug!("{}", message), | ||
} | ||
SEND_CHANNEL | ||
.send(MqttMessage { | ||
topic: MqttTopics::to_string(&MqttTopics::Logs), | ||
payload: String::<512>::from_str(message).unwrap(), | ||
}) | ||
.await; | ||
} |
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 |
---|---|---|
@@ -1,65 +1,65 @@ | ||
use crate::log::log; | ||
use core::str::FromStr; | ||
use embassy_stm32::gpio::{AnyPin, Input, Pull}; | ||
use embassy_time::{Duration, Timer}; | ||
use heapless::String; | ||
use hyped_core::{log_types::LogLevel, mqtt::MqttMessage, mqtt_topics::MqttTopics}; | ||
use serde::{Deserialize, Serialize}; | ||
use typenum::consts::*; | ||
|
||
use super::mqtt_send_and_recv::SEND_CHANNEL; | ||
|
||
use {defmt_rtt as _, panic_probe as _}; | ||
|
||
/// Struct to hold the state of the button | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ButtonMqttMessage { | ||
pub task_id: u8, | ||
pub status: bool, | ||
} | ||
|
||
/// Sends the state of the button over MQTT | ||
#[embassy_executor::task] | ||
pub async fn button_task(pin: AnyPin) { | ||
let button = Input::new(pin, Pull::Down); | ||
loop { | ||
SEND_CHANNEL | ||
.send(MqttMessage { | ||
topic: MqttTopics::to_string(&MqttTopics::Debug), | ||
payload: String::<512>::from_str( | ||
serde_json_core::to_string::<U512, ButtonMqttMessage>(&ButtonMqttMessage { | ||
task_id: 0, | ||
status: button.is_high(), | ||
}) | ||
.unwrap() | ||
.as_str(), | ||
) | ||
.unwrap(), | ||
}) | ||
.await; | ||
Timer::after(Duration::from_millis(100)).await; | ||
} | ||
} | ||
|
||
/// Sends a ping MQTT message every five seconds | ||
#[embassy_executor::task] | ||
pub async fn five_seconds_task() { | ||
loop { | ||
log(LogLevel::Info, "Ping from five second loop").await; | ||
SEND_CHANNEL | ||
.send(MqttMessage { | ||
topic: MqttTopics::to_string(&MqttTopics::Debug), | ||
payload: String::<512>::from_str( | ||
serde_json_core::to_string::<U512, ButtonMqttMessage>(&ButtonMqttMessage { | ||
task_id: 2, | ||
status: false, | ||
}) | ||
.unwrap() | ||
.as_str(), | ||
) | ||
.unwrap(), | ||
}) | ||
.await; | ||
Timer::after(Duration::from_secs(5)).await; | ||
} | ||
} | ||
use crate::log::log; | ||
use core::str::FromStr; | ||
use embassy_stm32::gpio::{AnyPin, Input, Pull}; | ||
use embassy_time::{Duration, Timer}; | ||
use heapless::String; | ||
use hyped_core::{log_types::LogLevel, mqtt::MqttMessage, mqtt_topics::MqttTopics}; | ||
use serde::{Deserialize, Serialize}; | ||
use typenum::consts::*; | ||
|
||
use super::mqtt_send_and_recv::SEND_CHANNEL; | ||
|
||
use {defmt_rtt as _, panic_probe as _}; | ||
|
||
/// Struct to hold the state of the button | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ButtonMqttMessage { | ||
pub task_id: u8, | ||
pub status: bool, | ||
} | ||
|
||
/// Sends the state of the button over MQTT | ||
#[embassy_executor::task] | ||
pub async fn button_task(pin: AnyPin) { | ||
let button = Input::new(pin, Pull::Down); | ||
loop { | ||
SEND_CHANNEL | ||
.send(MqttMessage { | ||
topic: MqttTopics::to_string(&MqttTopics::Debug), | ||
payload: String::<512>::from_str( | ||
serde_json_core::to_string::<U512, ButtonMqttMessage>(&ButtonMqttMessage { | ||
task_id: 0, | ||
status: button.is_high(), | ||
}) | ||
.unwrap() | ||
.as_str(), | ||
) | ||
.unwrap(), | ||
}) | ||
.await; | ||
Timer::after(Duration::from_millis(100)).await; | ||
} | ||
} | ||
|
||
/// Sends a ping MQTT message every five seconds | ||
#[embassy_executor::task] | ||
pub async fn five_seconds_task() { | ||
loop { | ||
log(LogLevel::Info, "Ping from five second loop").await; | ||
SEND_CHANNEL | ||
.send(MqttMessage { | ||
topic: MqttTopics::to_string(&MqttTopics::Debug), | ||
payload: String::<512>::from_str( | ||
serde_json_core::to_string::<U512, ButtonMqttMessage>(&ButtonMqttMessage { | ||
task_id: 2, | ||
status: false, | ||
}) | ||
.unwrap() | ||
.as_str(), | ||
) | ||
.unwrap(), | ||
}) | ||
.await; | ||
Timer::after(Duration::from_secs(5)).await; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ pub enum MqttTopics { | |
Velocity, | ||
Acceleration, | ||
Logs, | ||
Debug | ||
Debug, | ||
} | ||
|
||
impl MqttTopics { | ||
|