diff --git a/src/engines/MqttDataEngine.cpp b/src/engines/MqttDataEngine.cpp index e11c8d20..b9c2d9e4 100644 --- a/src/engines/MqttDataEngine.cpp +++ b/src/engines/MqttDataEngine.cpp @@ -59,6 +59,8 @@ void MqttDataEngine::onConfigChanged(const EngineConfig* cfg) { uint8_t tooLong = 0; m_topicCount[spare] = mqd::parseTopics(list.c_str(), m_topics[spare], limits.maxTopics, &tooLong); if (tooLong) LOGW("MqttData", "%u topic(s) longer than %u characters ignored", tooLong, (unsigned)(mqd::TOPIC_LEN - 1)); + String title = cfg->getString("screen_title", ""); + mqd::makeWaitingLabel(title.c_str(), m_waitingLabel[spare]); // Every settings save notifies every engine; reconnect only when this instance's topics changed. bool changed = m_topicCount[spare] != m_topicCount[cur]; for (uint8_t i = 0; !changed && i < m_topicCount[spare]; i++) changed = strcmp(m_topics[spare][i], m_topics[cur][i]) != 0; @@ -68,6 +70,9 @@ void MqttDataEngine::onConfigChanged(const EngineConfig* cfg) { m_cycle = mqd::Cycle(); m_cycle.sinceMs = millis(); m_viewTopic = -1; + } else if (strcmp(m_waitingLabel[spare], m_waitingLabel[cur]) != 0) { + // Only the title changed: publish the spare slot (same topics), no reconnect. + m_topicsIdx.store(spare, std::memory_order_release); } requestRedraw(); } @@ -290,7 +295,12 @@ void MqttDataEngine::draw(mqd::Session& s) { void MqttDataEngine::drawNotice(Notice n) { if (n == Notice::NoData) { panel_text::drawNoData(m_surface); return; } - if (n == Notice::Connecting) { panel_text::drawNotice(m_surface, panel_text::Message::Connecting); return; } + if (n == Notice::Connecting) { + const char* label = m_waitingLabel[m_topicsIdx.load(std::memory_order_acquire)]; + if (*label) panel_text::drawWaitingLabel(m_surface, label); + else panel_text::drawNotice(m_surface, panel_text::Message::Connecting); + return; + } if (n == Notice::NoConnection) { panel_text::drawNotice(m_surface, panel_text::Message::NoConnection); return; } if (n == Notice::Unsupported) { panel_text::drawNotice(m_surface, panel_text::Message::Unsupported); return; } } diff --git a/src/engines/MqttDataEngine.h b/src/engines/MqttDataEngine.h index 5e8f7893..7194c3d5 100644 --- a/src/engines/MqttDataEngine.h +++ b/src/engines/MqttDataEngine.h @@ -59,6 +59,10 @@ class MqttDataEngine : public IEngine { char m_topics[2][mqd::MAX_TOPICS][mqd::TOPIC_LEN] = {}; uint8_t m_topicCount[2] = {0, 0}; std::atomic m_topicsIdx{0}; + // " INCOMING..." shown instead of CONNECTING (empty = default notice). Double- + // buffered with m_topics: written to the spare slot on config apply, published by the + // m_topicsIdx release store, read on Core 1. + char m_waitingLabel[2][mqd::WAIT_LABEL_LEN] = {}; std::atomic m_configGen{0}; // Task lifecycle. diff --git a/src/engines/mqttdata/DataSession.cpp b/src/engines/mqttdata/DataSession.cpp index 00ebcbbc..bb365dfc 100644 --- a/src/engines/mqttdata/DataSession.cpp +++ b/src/engines/mqttdata/DataSession.cpp @@ -33,6 +33,45 @@ uint8_t parseTopics(const char* list, char out[][TOPIC_LEN], uint8_t max, uint8_ return n; } +namespace { +// Latin-1 letters U+00C0..U+00FF folded to an uppercase ASCII base letter (0 = drop). +char foldLatin1(uint32_t cp) { + static const char kFold[] = + "AAAAAAACEEEEIIII" // C0..CF + "DNOOOOO\0OUUUUY\0S" // D0..DF (D7 x and DE thorn dropped) + "AAAAAAACEEEEIIII" // E0..EF + "DNOOOOO\0OUUUUY\0Y"; // F0..FF (F7 division and FE thorn dropped) + return (cp >= 0xC0 && cp <= 0xFF) ? kFold[cp - 0xC0] : 0; +} +} // namespace + +void makeWaitingLabel(const char* screenTitle, char out[WAIT_LABEL_LEN]) { + out[0] = '\0'; + const uint8_t* p = (const uint8_t*)(screenTitle ? screenTitle : ""); + char t[WAIT_TITLE_MAX + 1]; + size_t n = 0; + while (*p && n < WAIT_TITLE_MAX) { + uint32_t cp = *p++; + if (cp >= 0x80) { // UTF-8 lead byte: decode, skip the rest + int extra = (cp & 0xE0) == 0xC0 ? 1 : (cp & 0xF0) == 0xE0 ? 2 : (cp & 0xF8) == 0xF0 ? 3 : 0; + cp &= extra == 1 ? 0x1F : extra == 2 ? 0x0F : 0x07; + for (; extra > 0 && (*p & 0xC0) == 0x80; extra--) cp = (cp << 6) | (*p++ & 0x3F); + char c = foldLatin1(cp); + if (c) t[n++] = c; + continue; + } + if (cp == ' ' || cp == '\t' || cp == '\r' || cp == '\n') { + if (n > 0) t[n++] = ' '; // leading whitespace dropped + continue; + } + t[n++] = (cp >= 'a' && cp <= 'z') ? (char)(cp - 32) : (char)cp; + } + while (n > 0 && t[n - 1] == ' ') n--; // trailing whitespace dropped + t[n] = '\0'; + if (n == 0 || strcmp(t, "MQTT DATA") == 0 || strcmp(t, "DONNEES MQTT") == 0 || strcmp(t, "DATOS MQTT") == 0) return; + snprintf(out, WAIT_LABEL_LEN, "%s INCOMING...", t); +} + uint8_t subPageCount(const View& v) { if (v.kind != feed::Kind::Weather || !v.weather.valid) return 1; uint8_t days = v.weather.dayCount < MAX_WEATHER_DAYS ? v.weather.dayCount : MAX_WEATHER_DAYS; diff --git a/src/engines/mqttdata/DataSession.h b/src/engines/mqttdata/DataSession.h index 58a5ebae..b1e6b428 100644 --- a/src/engines/mqttdata/DataSession.h +++ b/src/engines/mqttdata/DataSession.h @@ -23,6 +23,10 @@ namespace mqd { static constexpr uint8_t MAX_TOPICS = 6; static constexpr size_t TOPIC_LEN = 96; +/// Size (with the NUL) of the " INCOMING..." label shown while connecting. +static constexpr size_t WAIT_LABEL_LEN = 32; +/// Screen-title characters kept for that label (19 + " INCOMING..." + NUL = 31 bytes). +static constexpr size_t WAIT_TITLE_MAX = 19; static constexpr uint8_t MAX_WEATHER_DAYS = 5; /// Memory budget of one activation. @@ -41,6 +45,12 @@ Limits limitsFor(bool hasPsram); /// for not fitting TOPIC_LEN. uint8_t parseTopics(const char* list, char out[][TOPIC_LEN], uint8_t max, uint8_t* tooLong = nullptr); +/// Builds the label shown instead of CONNECTING from the screen's `screen_title`: trimmed, +/// uppercased (Latin-1 accents folded to their base letter, other non-ASCII dropped), cut to +/// WAIT_TITLE_MAX characters, plus " INCOMING...". `out` is left empty when there is no title +/// or it is the engine's default name (MQTT DATA / DONNEES MQTT / DATOS MQTT). No allocation. +void makeWaitingLabel(const char* screenTitle, char out[WAIT_LABEL_LEN]); + struct Allocator { void* (*alloc)(size_t bytes); void (*release)(void* p); diff --git a/src/engines/mqttdata/PanelText.cpp b/src/engines/mqttdata/PanelText.cpp index d542a392..e7c961b5 100644 --- a/src/engines/mqttdata/PanelText.cpp +++ b/src/engines/mqttdata/PanelText.cpp @@ -110,4 +110,12 @@ void drawNotice(IDrawingSurface* m, Message msg) { } } +void drawWaitingLabel(IDrawingSurface* m, const char* text) { + if (!m || !text || !*text) return; + const uint16_t grey = m->color565(0x78, 0x78, 0x78); + if (width(text, 1) <= m->width()) { drawCentred(m, text, grey); return; } + const int x = (m->width() - width(text, TINY)) / 2; + print(m, x > 0 ? x : 0, (m->height() - height(TINY)) / 2, text, TINY, grey, m->width()); +} + } // namespace panel_text diff --git a/src/engines/mqttdata/PanelText.h b/src/engines/mqttdata/PanelText.h index 004be2cc..45bbef80 100644 --- a/src/engines/mqttdata/PanelText.h +++ b/src/engines/mqttdata/PanelText.h @@ -43,4 +43,8 @@ enum class Message : uint8_t { Connecting, NoConnection, Unsupported }; /// NON PRIS EN CHARGE / NO COMPATIBLE in #C08000. void drawNotice(IDrawingSurface* m, Message msg); +/// The connecting notice with the screen's own label ("WEATHER INCOMING..."), #787878, placed like +/// drawNotice; drawn in the tiny font when it is too wide for G1 on this panel. +void drawWaitingLabel(IDrawingSurface* m, const char* text); + } // namespace panel_text diff --git a/test/native/test_native_core.cpp b/test/native/test_native_core.cpp index b783d0a6..3fd8256c 100644 --- a/test/native/test_native_core.cpp +++ b/test/native/test_native_core.cpp @@ -838,6 +838,29 @@ void test_mqttdata_limits_topics_and_parsing(void) { TEST_ASSERT_EQUAL_STRING("t/4", s.topic(3)); } +void test_mqttdata_waiting_label_from_screen_title(void) { + char out[mqd::WAIT_LABEL_LEN]; + mqd::makeWaitingLabel("Weather", out); + TEST_ASSERT_EQUAL_STRING("WEATHER INCOMING...", out); + mqd::makeWaitingLabel(" back yard ", out); + TEST_ASSERT_EQUAL_STRING("BACK YARD INCOMING...", out); + mqd::makeWaitingLabel("M\xC3\xA9t\xC3\xA9o", out); // "Météo": accents folded + TEST_ASSERT_EQUAL_STRING("METEO INCOMING...", out); + // Cut to 19 title characters: fits WAIT_LABEL_LEN with the suffix. + mqd::makeWaitingLabel("A very long screen title here", out); + TEST_ASSERT_EQUAL_STRING("A VERY LONG SCREEN INCOMING...", out); + mqd::makeWaitingLabel("0123456789012345678901234", out); + TEST_ASSERT_EQUAL_STRING("0123456789012345678 INCOMING...", out); + TEST_ASSERT_EQUAL(31, (int)strlen(out)); + // No title, or still the engine's default name: plain CONNECTING. + const char* none[] = {"", " ", nullptr, "MQTT Data", "mqtt data", "Donn\xC3\xA9" "es MQTT", "Donnees MQTT", "Datos MQTT"}; + for (const char* t : none) { + out[0] = 'x'; + mqd::makeWaitingLabel(t, out); + TEST_ASSERT_EQUAL_STRING("", out); + } +} + void test_mqttdata_session_pages_types_and_handoff(void) { mqd::Session s; uint8_t n = mqd::parseTopics("ha/g,ha/v,ha/w", g_topics, mqd::MAX_TOPICS); @@ -5013,6 +5036,7 @@ int main(int argc, char** argv) { RUN_TEST(test_feed_weather_payload_and_conditions); RUN_TEST(test_mqttdata_session_allocations_balance_over_activations); RUN_TEST(test_mqttdata_limits_topics_and_parsing); + RUN_TEST(test_mqttdata_waiting_label_from_screen_title); RUN_TEST(test_mqttdata_session_pages_types_and_handoff); RUN_TEST(test_mqttdata_page_cycle); RUN_TEST(test_mqttdata_blueprint_payloads);