sending messages to the tui chat

This commit is contained in:
Priec
2026-01-18 23:16:06 +01:00
parent ba2b3b188a
commit b729d3f23d
3 changed files with 10 additions and 16 deletions

View File

@@ -136,8 +136,8 @@ async fn main(spawner: Spawner) -> ! {
spawner.spawn(mpu::task::mpu_task(mpu_i2c)).expect("spawn mpu_task"); spawner.spawn(mpu::task::mpu_task(mpu_i2c)).expect("spawn mpu_task");
display::api::set_status("Booting...").await; display::api::set_status("Booting...").await;
mqtt_subscribe("esp32/topic").await; mqtt_subscribe("esp32/imu").await;
mqtt_publish("esp32/topic", b"online", QualityOfService::QoS1, false).await; mqtt_publish("esp32/imu", b"online", QualityOfService::QoS1, false).await;
display::api::set_status("Running").await; display::api::set_status("Running").await;
display::api::set_mqtt_status(true, 0).await; display::api::set_mqtt_status(true, 0).await;
@@ -171,8 +171,7 @@ async fn main(spawner: Spawner) -> ! {
} }
imu_reading_count += 1; imu_reading_count += 1;
// CRITICAL FIX: show_imu is now non-blocking (uses try_send internally) // Show_imu is now non-blocking (uses try_send internally)
// This prevents display channel backpressure from blocking the main loop
display::api::show_imu(reading); display::api::show_imu(reading);
if imu_reading_count % MQTT_PUBLISH_DIVIDER == 0 { if imu_reading_count % MQTT_PUBLISH_DIVIDER == 0 {
@@ -228,7 +227,6 @@ async fn core1_network_task(
// Signal core 0 that network is ready // Signal core 0 that network is ready
NETWORK_READY.signal(()); NETWORK_READY.signal(());
// Start MQTT on this core (it needs the stack)
spawner.spawn(mqtt_task(stack)).ok(); spawner.spawn(mqtt_task(stack)).ok();
} }
@@ -237,7 +235,7 @@ async fn handle_mqtt_message(msg: IncomingMsg) {
match txt { match txt {
"clear" => { display::api::clear().await; } "clear" => { display::api::clear().await; }
"status" => { mqtt_publish("esp32/status", b"running", QualityOfService::QoS1, false).await; } "status" => { mqtt_publish("esp32/status", b"running", QualityOfService::QoS1, false).await; }
_ => {} _ => { display::api::add_chat_message(txt).await; }
} }
} }
} }

View File

@@ -27,8 +27,6 @@ pub(crate) fn receiver() -> Receiver<'static, CriticalSectionRawMutex, DisplayCo
DISPLAY_CHANNEL.receiver() DISPLAY_CHANNEL.receiver()
} }
// ─────────────────────────────────────────────────────────────────────────────
/// Show IMU data - NON-BLOCKING to prevent backpressure deadlock /// Show IMU data - NON-BLOCKING to prevent backpressure deadlock
pub fn show_imu(reading: ImuReading) { pub fn show_imu(reading: ImuReading) {
// CRITICAL FIX: Use try_send instead of send().await // CRITICAL FIX: Use try_send instead of send().await

View File

@@ -59,7 +59,7 @@ enum Command {
Subscribe(String<TOPIC_MAX>), Subscribe(String<TOPIC_MAX>),
} }
// Standard command/info channels // Command/info channels
static CMD_CHAN: Channel<CriticalSectionRawMutex, Command, COMMAND_QUEUE> = Channel::new(); static CMD_CHAN: Channel<CriticalSectionRawMutex, Command, COMMAND_QUEUE> = Channel::new();
static EVT_CHAN: Channel<CriticalSectionRawMutex, IncomingMsg, EVENT_QUEUE> = Channel::new(); static EVT_CHAN: Channel<CriticalSectionRawMutex, IncomingMsg, EVENT_QUEUE> = Channel::new();
@@ -67,7 +67,7 @@ static EVT_CHAN: Channel<CriticalSectionRawMutex, IncomingMsg, EVENT_QUEUE> = Ch
static IMU_LATEST: Mutex<CriticalSectionRawMutex, Option<Vec<u8, PAYLOAD_MAX>>> = static IMU_LATEST: Mutex<CriticalSectionRawMutex, Option<Vec<u8, PAYLOAD_MAX>>> =
Mutex::new(None); Mutex::new(None);
/// ---------- Public API ---------- /// Public API
/// Blocking publish for command/response messages. /// Blocking publish for command/response messages.
pub async fn mqtt_publish(topic: &str, payload: &[u8], qos: QualityOfService, retain: bool) { pub async fn mqtt_publish(topic: &str, payload: &[u8], qos: QualityOfService, retain: bool) {
@@ -113,7 +113,7 @@ pub fn mqtt_events(
EVT_CHAN.receiver() EVT_CHAN.receiver()
} }
/// ---------- Internals ---------- /// Internals
fn truncate_str<const N: usize>(s: &str) -> String<N> { fn truncate_str<const N: usize>(s: &str) -> String<N> {
let mut h = String::new(); let mut h = String::new();
@@ -175,8 +175,6 @@ async fn handle_incoming(result: Result<(&str, &[u8]), ReasonCode>) -> Result<()
Ok(()) Ok(())
} }
/// ---------- Session Management ----------
async fn run_one_session( async fn run_one_session(
stack: Stack<'static>, stack: Stack<'static>,
tcp_rx: &mut [u8], tcp_rx: &mut [u8],
@@ -214,7 +212,7 @@ async fn run_one_session(
let mut ping_countdown: u32 = (KEEPALIVE_SECS * 10) as u32; // ticks until ping (100 ms each) let mut ping_countdown: u32 = (KEEPALIVE_SECS * 10) as u32; // ticks until ping (100 ms each)
loop { loop {
// 1. Send latest IMU payload if available // Send latest IMU payload if available
if let Ok(mut guard) = IMU_LATEST.try_lock() { if let Ok(mut guard) = IMU_LATEST.try_lock() {
if let Some(payload) = guard.take() { if let Some(payload) = guard.take() {
drop(guard); drop(guard);
@@ -244,12 +242,12 @@ async fn run_one_session(
} }
} }
// 2. Process any queued control commands // Process any queued control commands
while let Ok(cmd) = CMD_CHAN.try_receive() { while let Ok(cmd) = CMD_CHAN.try_receive() {
handle_command(&mut client, cmd).await.map_err(|_| ())?; handle_command(&mut client, cmd).await.map_err(|_| ())?;
} }
// 3. Check for incoming messages with timeout // Check for incoming messages with timeout
match with_timeout(Duration::from_millis(100), client.receive_message()).await { match with_timeout(Duration::from_millis(100), client.receive_message()).await {
Ok(Ok((topic, payload))) => { Ok(Ok((topic, payload))) => {
// Handle incoming message // Handle incoming message