Hi all, still getting to grips with using ESPHome. I’m currently building out my M5Stack Basic with BTC v2.1 base (contains an SHT30).
The M5Stack Basic has a nice screen display and 3 buttons. I want to have multiple different pages showing different data and I’ve made a good start on that.
However, I really want some common elements on every page and I want some common formatting. I would really love to move those common parts to a separate, reusable file.
So can anyone share the best approach for this?
I’ve tried moving some code to an included .h
library file but that doesn’t get access to the it
object even if I include #include "esphome.h"
. Though I can, at least, use that file to define common variables such as the line spacing, header depths, etc.
Here is an example from one of the pages - only showing output from the local sensor and one other (via MQTT) at the moment. But you can see the start of my standard header (showing WiFi status and date/time) and footer (that will eventually show the button actions).
- id: page1
# Initial page. Header (Pg, status, time), Footer (buttons)
lambda: |-
/* ---- Header ---- */
auto nowtime = id(sntp_time).now();
auto wifiStatus = WiFi.status();
it.filled_rectangle(0,0, it.get_width(), HEADER_HT, id(COLOR_CSS_DEEPSKYBLUE));
if (WiFi.status() == WL_CONNECTED) {
it.print(TEXT_BLOCK_1, HEADER_Y, id(icon_font), id(COLOR_CSS_BLACK), TextAlign::LEFT, "");
} else {
it.print(TEXT_BLOCK_1, HEADER_Y, id(icon_font), id(COLOR_CSS_RED), TextAlign::LEFT, "");
}
if (nowtime.is_valid()) {
it.strftime(it.get_width()-1, HEADER_Y, id(print_font), id(COLOR_CSS_BLACK), TextAlign::TOP_RIGHT, "%Y-%m-%d %H:%M", nowtime);
} else {
it.print(it.get_width()-1, HEADER_Y, id(print_font), id(COLOR_CSS_BLACK), TextAlign::TOP_RIGHT, "----/--/-- --:--");
}
// ---- Footer ----
it.filled_rectangle(0,it.get_height()-FOOTER_HT, it.get_width(), FOOTER_HT, id(COLOR_CSS_LIGHTBLUE));
// ---- Main ----
// Here
it.print(TEXT_BLOCK_1, LINE_1, id(print_font), id(COLOR_CSS_WHITE), TextAlign::TOP_LEFT, "Here");
it.print(TEXT_BLOCK_1+6, LINE_2, id(icon_font), id(COLOR_CSS_WHITE), TextAlign::TOP_CENTER, "");
it.print(TEXT_BLOCK_1+6, LINE_3, id(icon_font), id(COLOR_CSS_WHITE), TextAlign::TOP_CENTER, "");
it.printf(TEXT_BLOCK_1+LINE_SIZE, LINE_2, id(print_font), id(COLOR_CSS_WHITE), TextAlign::LEFT, "%.1f°C", id(temperature).state);
it.printf(TEXT_BLOCK_1+LINE_SIZE, LINE_3, id(print_font), id(COLOR_CSS_WHITE), TextAlign::LEFT, "%.1f%%", id(humidity).state);
// Landing (D1M04)
double t_float = strToFloat( id(d1m04_temperature).state );
double h_float = strToFloat( id(d1m04_humidity).state );
it.print(TEXT_BLOCK_2, LINE_1, id(print_font), id(COLOR_CSS_WHITE), TextAlign::TOP_LEFT, "Landing");
it.print(TEXT_BLOCK_2+6, LINE_2, id(icon_font), id(COLOR_CSS_WHITE), TextAlign::TOP_CENTER, "");
it.print(TEXT_BLOCK_2+6, LINE_3, id(icon_font), id(COLOR_CSS_WHITE), TextAlign::TOP_CENTER, "");
it.printf(TEXT_BLOCK_2+LINE_SIZE, LINE_2, id(print_font), id(COLOR_CSS_WHITE), TextAlign::LEFT, "%.1f°C", t_float);
it.printf(TEXT_BLOCK_2+LINE_SIZE, LINE_3, id(print_font), id(COLOR_CSS_WHITE), TextAlign::LEFT, "%.1f%%", h_float);
// Bathroom
t_float = strToFloat( id(bathroom_temperature).state );
h_float = strToFloat( id(bathroom_humidity).state );
it.print(TEXT_BLOCK_3, LINE_1, id(print_font), id(COLOR_CSS_WHITE), TextAlign::TOP_LEFT, "Bathroom");
it.print(TEXT_BLOCK_3+6, LINE_2, id(icon_font), id(COLOR_CSS_WHITE), TextAlign::TOP_CENTER, "");
it.print(TEXT_BLOCK_3+6, LINE_3, id(icon_font), id(COLOR_CSS_WHITE), TextAlign::TOP_CENTER, "");
it.printf(TEXT_BLOCK_3+LINE_SIZE, LINE_2, id(print_font), id(COLOR_CSS_WHITE), TextAlign::LEFT, "%.1f°C", t_float);
it.printf(TEXT_BLOCK_3+LINE_SIZE, LINE_3, id(print_font), id(COLOR_CSS_WHITE), TextAlign::LEFT, "%.1f%%", h_float);