Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/engines/MqttDataEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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; }
}
Expand Down
4 changes: 4 additions & 0 deletions src/engines/MqttDataEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> m_topicsIdx{0};
// "<SCREEN TITLE> 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<uint32_t> m_configGen{0};

// Task lifecycle.
Expand Down
39 changes: 39 additions & 0 deletions src/engines/mqttdata/DataSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/engines/mqttdata/DataSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<SCREEN TITLE> 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.
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/engines/mqttdata/PanelText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/engines/mqttdata/PanelText.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/native/test_native_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading