feat(display): add air quality pages, history and display settings
This commit is contained in:
@@ -179,3 +179,66 @@ automatisch freigegeben.
|
||||
mit Portal `http://192.168.4.1` aktiv; keine STA-IP oder erfolgreichen
|
||||
Influx-Schreibvorgänge beobachtet. WLAN und bei Bedarf Organisation `Home` /
|
||||
Bucket `nanoPSX` im Portal einstellen; Tokens lokal eingeben.
|
||||
|
||||
## Displayseiten und Bedienung
|
||||
|
||||
Die vier EEZ-Seiten sind `Main` (bisherige Messwertansicht), `Page_2` (Raumluft),
|
||||
`History` (Verlauf) und `Device` (Gerät). Nach links zur nächsten, nach rechts zur
|
||||
vorherigen Seite wischen; an den äußeren Seiten gibt es keinen Umlauf. Vier kleine
|
||||
Markierungen am oberen Rand zeigen die aktuelle Seite. Tippen auf ein Statussymbol
|
||||
der Hauptseite öffnet die Geräteseite. Dropdowns und Helligkeitsregler verarbeiten
|
||||
ihre Berührungen selbst, ohne dabei versehentlich die Seite zu wechseln.
|
||||
|
||||
- Raumluft: CO₂ in ppm, VOC-/NOx-Index und ein kleiner CO₂-Verlauf. Nicht verfügbare
|
||||
Kanäle erscheinen als `--`; SEN54 erzeugt keine künstlichen CO₂-Werte.
|
||||
- Verlauf: Auswahl zwischen CO₂, PM2.5, Temperatur, Feuchte, VOC und NOx. Gezeigt
|
||||
werden 180 Zehn-Sekunden-Mittelwerte über die letzten 30 Minuten. Die angezeigte
|
||||
Wertespanne entspricht der automatisch angepassten Diagrammachse. Fehlende
|
||||
Zeitabschnitte bleiben Lücken. Der feste RAM-Ringpuffer wird bei Neustart geleert;
|
||||
SD-Karte, WLAN und InfluxDB sind für den lokalen Verlauf nicht erforderlich.
|
||||
- Gerätestatus: WLAN/IP, Alter der letzten bestätigten Influx-Übertragung (HTTP 204)
|
||||
und Sensoraktualität. Ein WLAN-Erfolg gilt nicht als erfolgreiche Datenübertragung.
|
||||
- Helligkeit: 20–100 %, Standard 100 %. Automatisches Abdunkeln: aus oder nach
|
||||
30/60/120 Sekunden, Standard 60 Sekunden. Abgedunkelt wird auf 15 %. Die erste
|
||||
Berührung weckt nur das Display und wird bis zum Loslassen verbraucht.
|
||||
- Display-Einstellungen werden erst nach Änderung gespeichert: Helligkeit beim
|
||||
Loslassen, Zeitwahl beim Auswählen. Eigener NVS-Namespace `ui_display`, keine
|
||||
WLAN-/Influx-Einstellungen. Beim ersten Start werden nur die Defaults gelesen.
|
||||
- Vor dem ersten Sensorpaket erscheint ein Starthinweis. Nach mehr als fünf
|
||||
Sekunden ohne erfolgreiches Paket werden die aktuellen Anzeigen als veraltet
|
||||
markiert und die Zahlen durch `--` ersetzt; der vorhandene Verlauf bleibt sichtbar.
|
||||
|
||||
## EEZ-Projekt und Firmware zusammen bearbeiten
|
||||
|
||||
`eez/nanopsx.eez-project` enthält Layout, Beschriftungen, Namen und Grundkonfiguration
|
||||
der Widgets. PlatformIO ruft vor jedem Build `tools/export_display.py` auf und hält
|
||||
`screens.c`/`screens.h` automatisch synchron. Kein manueller EEZ-Studio-Export nötig.
|
||||
Der projektspezifische Exporter unterstützt die hier verwendeten statischen Screen-,
|
||||
Panel-, Label-, Image-, Chart-, Slider- und Dropdown-Widgets und deren verwendete
|
||||
Stile. Nicht unterstützte Widgettypen/Stile brechen den Build ab, statt unbemerkt
|
||||
wegzufallen; bei Erweiterungen zuerst den Exporter ergänzen. EEZ Flow bleibt aus.
|
||||
|
||||
Navigation, dynamische Texte, Chart-Daten, Touch-Aufwecken und Einstellungen liegen
|
||||
außerhalb der generierten Dateien in `ui_navigation.c`, `ui_dashboard.c` und
|
||||
`ui_model.c`. Diese werden nach `ui_init()` unter dem LVGL-Lock initialisiert.
|
||||
Der Sensorproducer übergibt skalierte Zahlen an `ui_model_publish()` und ruft
|
||||
keine LVGL-Funktionen auf. Snapshot und begrenzter Ringpuffer sind auf dem ESP
|
||||
mit einem Spinlock geschützt. Die Diagramme werden im GUI-Task aktualisiert.
|
||||
|
||||
Prüfungen aus dem Repository-Hauptverzeichnis:
|
||||
|
||||
```bash
|
||||
python3 firmware/tools/export_display.py --check
|
||||
python3 firmware/test/ui/run_tests.py
|
||||
bash firmware/test/sensors/run.sh
|
||||
```
|
||||
|
||||
Die UI-Tests verwenden echte LVGL-Quellen und exportierte Displayobjekte bei
|
||||
48 KiB LVGL-Speicher. Sie prüfen vierseitige Navigation, Tippen, kurze/vertikale
|
||||
Gesten, äußere Grenzen, Kindobjekte, Dropdown-Isolation, Helligkeitsregler,
|
||||
Aufweckberührung, Abdunkeln, fehlende/veraltete Werte und Verlaufsmittelwerte,
|
||||
Lücken und Verdrängung alter Daten. Voraussetzung sind ein C-Compiler und die
|
||||
bereits aufgelöste LVGL-Komponente unter `firmware/managed_components`.
|
||||
Ein optionaler Verzeichnispfad hinter dem UI-Testbefehl speichert vier gerenderte
|
||||
PPM-Vorschauen mit simulierten Messwerten. Physische Darstellung und Fingerbedienung
|
||||
müssen zusätzlich am Gerät geprüft werden.
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"ui.c"
|
||||
"ui_navigation.c"
|
||||
"ui_dashboard.c"
|
||||
"ui_model.c"
|
||||
"images.c"
|
||||
"screens.c"
|
||||
"styles.c"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,15 +1,22 @@
|
||||
#ifndef EEZ_LVGL_UI_SCREENS_H
|
||||
#define EEZ_LVGL_UI_SCREENS_H
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct _objects_t {
|
||||
/* Generated by firmware/tools/export_display.py from the EEZ project. */
|
||||
#pragma once
|
||||
#include <lvgl.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef struct {
|
||||
lv_obj_t *main;
|
||||
lv_obj_t *page_2;
|
||||
lv_obj_t *history;
|
||||
lv_obj_t *device;
|
||||
lv_obj_t *obj0;
|
||||
lv_obj_t *obj5;
|
||||
lv_obj_t *obj6;
|
||||
lv_obj_t *obj7;
|
||||
lv_obj_t *obj8;
|
||||
lv_obj_t *obj9;
|
||||
lv_obj_t *obj10;
|
||||
lv_obj_t *obj11;
|
||||
lv_obj_t *obj1;
|
||||
lv_obj_t *obj2;
|
||||
lv_obj_t *obj3;
|
||||
@@ -20,32 +27,63 @@ typedef struct _objects_t {
|
||||
lv_obj_t *influx_off;
|
||||
lv_obj_t *mdns_ok;
|
||||
lv_obj_t *mdns_off;
|
||||
lv_obj_t *obj5;
|
||||
lv_obj_t *obj6;
|
||||
lv_obj_t *obj7;
|
||||
lv_obj_t *obj8;
|
||||
lv_obj_t *obj9;
|
||||
lv_obj_t *obj10;
|
||||
lv_obj_t *obj11;
|
||||
lv_obj_t *main_notice;
|
||||
lv_obj_t *page_dot_0_0;
|
||||
lv_obj_t *page_dot_0_1;
|
||||
lv_obj_t *page_dot_0_2;
|
||||
lv_obj_t *page_dot_0_3;
|
||||
lv_obj_t *page_2_title;
|
||||
lv_obj_t *room_state;
|
||||
lv_obj_t *room_co2;
|
||||
lv_obj_t *room_unit;
|
||||
lv_obj_t *room_voc;
|
||||
lv_obj_t *room_nox;
|
||||
lv_obj_t *room_chart;
|
||||
lv_obj_t *room_chart_caption;
|
||||
lv_obj_t *page_dot_1_0;
|
||||
lv_obj_t *page_dot_1_1;
|
||||
lv_obj_t *page_dot_1_2;
|
||||
lv_obj_t *page_dot_1_3;
|
||||
lv_obj_t *history_title;
|
||||
lv_obj_t *history_metric;
|
||||
lv_obj_t *history_range;
|
||||
lv_obj_t *history_chart;
|
||||
lv_obj_t *history_caption;
|
||||
lv_obj_t *page_dot_2_0;
|
||||
lv_obj_t *page_dot_2_1;
|
||||
lv_obj_t *page_dot_2_2;
|
||||
lv_obj_t *page_dot_2_3;
|
||||
lv_obj_t *device_title;
|
||||
lv_obj_t *device_wifi;
|
||||
lv_obj_t *device_influx;
|
||||
lv_obj_t *device_sensor;
|
||||
lv_obj_t *brightness_label;
|
||||
lv_obj_t *brightness;
|
||||
lv_obj_t *dim_label;
|
||||
lv_obj_t *dim_timeout;
|
||||
lv_obj_t *page_dot_3_0;
|
||||
lv_obj_t *page_dot_3_1;
|
||||
lv_obj_t *page_dot_3_2;
|
||||
lv_obj_t *page_dot_3_3;
|
||||
} objects_t;
|
||||
|
||||
extern objects_t objects;
|
||||
|
||||
enum ScreensEnum {
|
||||
SCREEN_ID_MAIN = 1,
|
||||
SCREEN_ID_PAGE_2 = 2,
|
||||
SCREEN_ID_HISTORY = 3,
|
||||
SCREEN_ID_DEVICE = 4,
|
||||
};
|
||||
|
||||
void create_screen_main();
|
||||
void tick_screen_main();
|
||||
|
||||
void tick_screen_by_id(enum ScreensEnum screenId);
|
||||
void create_screen_main(void);
|
||||
void tick_screen_main(void);
|
||||
void create_screen_page_2(void);
|
||||
void tick_screen_page_2(void);
|
||||
void create_screen_history(void);
|
||||
void tick_screen_history(void);
|
||||
void create_screen_device(void);
|
||||
void tick_screen_device(void);
|
||||
void create_screens(void);
|
||||
void tick_screen(int screen_index);
|
||||
|
||||
void create_screens();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*EEZ_LVGL_UI_SCREENS_H*/
|
||||
void tick_screen_by_id(enum ScreensEnum screen_id);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
#include "ui_dashboard.h"
|
||||
#include "ui_model.h"
|
||||
#include "ui.h"
|
||||
#include "fonts.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static ui_display_config_t config;
|
||||
static ui_brightness_cb_t set_brightness;
|
||||
static ui_save_config_cb_t save_config;
|
||||
static lv_chart_series_t *room_series, *history_series;
|
||||
static uint32_t last_activity;
|
||||
static bool initialized, dimmed, suppress_touch;
|
||||
static const uint16_t dim_seconds[] = {0, 30, 60, 120};
|
||||
static const char *units[] = {"ppm", "µg/m³", "°C", "%", "Index", "Index"};
|
||||
|
||||
static void update_brightness_label(void)
|
||||
{
|
||||
lv_label_set_text_fmt(objects.brightness_label, "Licht %u%%", config.brightness);
|
||||
}
|
||||
|
||||
static void settings_event(lv_event_t *event)
|
||||
{
|
||||
lv_obj_t *target = lv_event_get_target(event);
|
||||
if (target == objects.brightness) {
|
||||
config.brightness = (uint8_t)lv_slider_get_value(target);
|
||||
set_brightness(config.brightness);
|
||||
update_brightness_label();
|
||||
/* Dragging previews immediately, persistence happens only on release. */
|
||||
if (lv_event_get_code(event) == LV_EVENT_RELEASED && save_config) save_config(&config);
|
||||
} else if (target == objects.dim_timeout) {
|
||||
unsigned selected = lv_dropdown_get_selected(target);
|
||||
if (selected < sizeof(dim_seconds) / sizeof(dim_seconds[0])) {
|
||||
config.dim_after_s = dim_seconds[selected];
|
||||
if (save_config) save_config(&config);
|
||||
}
|
||||
}
|
||||
last_activity = lv_tick_get();
|
||||
}
|
||||
|
||||
static void status_clicked(lv_event_t *event)
|
||||
{
|
||||
(void)event;
|
||||
loadScreen(SCREEN_ID_DEVICE);
|
||||
}
|
||||
|
||||
void ui_dashboard_init(ui_display_config_t selected, ui_brightness_cb_t brightness,
|
||||
ui_save_config_cb_t save)
|
||||
{
|
||||
config = selected;
|
||||
if (config.brightness < 20 || config.brightness > 100) config.brightness = 100;
|
||||
unsigned dim_index = 2;
|
||||
for (unsigned i = 0; i < 4; ++i) if (config.dim_after_s == dim_seconds[i]) dim_index = i;
|
||||
config.dim_after_s = dim_seconds[dim_index];
|
||||
set_brightness = brightness;
|
||||
save_config = save;
|
||||
dimmed = suppress_touch = false;
|
||||
last_activity = lv_tick_get();
|
||||
initialized = true;
|
||||
lv_slider_set_value(objects.brightness, config.brightness, LV_ANIM_OFF);
|
||||
lv_obj_set_ext_click_area(objects.brightness, 12);
|
||||
lv_dropdown_set_selected(objects.dim_timeout, dim_index);
|
||||
update_brightness_label();
|
||||
/* Custom Ubuntu fonts do not contain LVGL's private-use arrow glyphs. */
|
||||
lv_obj_set_style_text_font(objects.history_metric, LV_FONT_DEFAULT, LV_PART_INDICATOR);
|
||||
lv_obj_set_style_text_font(objects.dim_timeout, LV_FONT_DEFAULT, LV_PART_INDICATOR);
|
||||
lv_obj_add_event_cb(objects.brightness, settings_event, LV_EVENT_VALUE_CHANGED, NULL);
|
||||
lv_obj_add_event_cb(objects.brightness, settings_event, LV_EVENT_RELEASED, NULL);
|
||||
lv_obj_add_event_cb(objects.dim_timeout, settings_event, LV_EVENT_VALUE_CHANGED, NULL);
|
||||
lv_obj_t *icons[] = {objects.wifi_ok, objects.wifi_off, objects.influx_ok,
|
||||
objects.influx_off, objects.mdns_ok, objects.mdns_off};
|
||||
for (unsigned i = 0; i < sizeof(icons) / sizeof(icons[0]); ++i) {
|
||||
lv_obj_add_flag(icons[i], LV_OBJ_FLAG_CLICKABLE);
|
||||
lv_obj_add_event_cb(icons[i], status_clicked, LV_EVENT_CLICKED, NULL);
|
||||
}
|
||||
lv_chart_set_point_count(objects.room_chart, UI_HISTORY_POINTS);
|
||||
lv_chart_set_point_count(objects.history_chart, UI_HISTORY_POINTS);
|
||||
room_series = lv_chart_add_series(objects.room_chart, lv_color_hex(0x62D6C5), LV_CHART_AXIS_PRIMARY_Y);
|
||||
history_series = lv_chart_add_series(objects.history_chart, lv_color_hex(0x62D6C5), LV_CHART_AXIS_PRIMARY_Y);
|
||||
lv_chart_set_all_value(objects.room_chart, room_series, LV_CHART_POINT_NONE);
|
||||
lv_chart_set_all_value(objects.history_chart, history_series, LV_CHART_POINT_NONE);
|
||||
lv_obj_set_style_size(objects.room_chart, 0, LV_PART_INDICATOR);
|
||||
lv_obj_set_style_size(objects.history_chart, 0, LV_PART_INDICATOR);
|
||||
lv_obj_set_style_line_width(objects.room_chart, 2, LV_PART_ITEMS);
|
||||
lv_obj_set_style_line_width(objects.history_chart, 2, LV_PART_ITEMS);
|
||||
set_brightness(config.brightness);
|
||||
}
|
||||
|
||||
void ui_dashboard_filter_touch(lv_indev_data_t *data)
|
||||
{
|
||||
if (!initialized) return;
|
||||
const bool pressed = data->state == LV_INDEV_STATE_PRESSED;
|
||||
if (pressed) {
|
||||
last_activity = lv_tick_get();
|
||||
if (dimmed) {
|
||||
dimmed = false;
|
||||
suppress_touch = true;
|
||||
set_brightness(config.brightness);
|
||||
}
|
||||
}
|
||||
if (suppress_touch) {
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
if (!pressed) suppress_touch = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void format_value(lv_obj_t *label, const char *prefix, float value, unsigned decimals)
|
||||
{
|
||||
if (!isfinite(value)) lv_label_set_text_fmt(label, "%s--", prefix);
|
||||
else {
|
||||
/* LVGL's compact printf is built without floating-point support. */
|
||||
char text[64];
|
||||
snprintf(text, sizeof(text), "%s%.*f", prefix, (int)decimals, (double)value);
|
||||
lv_label_set_text(label, text);
|
||||
}
|
||||
}
|
||||
|
||||
static void fill_chart(lv_obj_t *chart, lv_chart_series_t *series,
|
||||
ui_metric_t metric, uint64_t now_ms, lv_obj_t *range_label)
|
||||
{
|
||||
float values[UI_HISTORY_POINTS];
|
||||
ui_model_history(metric, now_ms, values);
|
||||
float low = INFINITY, high = -INFINITY;
|
||||
for (unsigned i = 0; i < UI_HISTORY_POINTS; ++i) {
|
||||
if (isfinite(values[i])) {
|
||||
if (values[i] < low) low = values[i];
|
||||
if (values[i] > high) high = values[i];
|
||||
}
|
||||
}
|
||||
/* Values use tenths in the plot. Never overflow LVGL's 16-bit coordinates. */
|
||||
const float scale = metric == UI_METRIC_CO2 ? 1.0f : 10.0f;
|
||||
int minimum = isfinite(low) ? (int)floorf(low * scale) : 0;
|
||||
int maximum = isfinite(high) ? (int)ceilf(high * scale) : 10;
|
||||
int margin = (maximum - minimum) / 10;
|
||||
if (margin < 5) margin = 5;
|
||||
minimum -= margin; maximum += margin;
|
||||
if (metric != UI_METRIC_TEMP && minimum < 0) minimum = 0;
|
||||
if (minimum < -32000) minimum = -32000;
|
||||
if (maximum > 32000) maximum = 32000;
|
||||
if (maximum <= minimum) maximum = minimum + 1;
|
||||
lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, minimum, maximum);
|
||||
for (unsigned i = 0; i < UI_HISTORY_POINTS; ++i) {
|
||||
float value = values[i] * scale;
|
||||
series->y_points[i] = !isfinite(value) ? LV_CHART_POINT_NONE :
|
||||
(lv_coord_t)lroundf(fminf(32000, fmaxf(-32000, value)));
|
||||
}
|
||||
lv_chart_refresh(chart);
|
||||
if (range_label) {
|
||||
if (isfinite(low)) {
|
||||
char text[64];
|
||||
snprintf(text, sizeof(text), "%.0f..%.0f %s", minimum / scale, maximum / scale, units[metric]);
|
||||
lv_label_set_text(range_label, text);
|
||||
}
|
||||
else lv_label_set_text(range_label, "Noch keine Daten");
|
||||
}
|
||||
}
|
||||
|
||||
void ui_dashboard_tick(uint64_t now_ms, const ui_device_status_t *status)
|
||||
{
|
||||
if (!initialized) return;
|
||||
lv_obj_t *on[] = {objects.wifi_ok, objects.influx_ok, objects.mdns_ok};
|
||||
lv_obj_t *off[] = {objects.wifi_off, objects.influx_off, objects.mdns_off};
|
||||
bool ok[] = {status->wifi_connected, status->influx_ok, status->mdns_ok};
|
||||
for (unsigned i = 0; i < 3; ++i) {
|
||||
lv_obj_add_flag(ok[i] ? off[i] : on[i], LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(ok[i] ? on[i] : off[i], LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
if (!dimmed && config.dim_after_s &&
|
||||
lv_tick_elaps(last_activity) >= (uint32_t)config.dim_after_s * 1000) {
|
||||
dimmed = true;
|
||||
set_brightness(15);
|
||||
}
|
||||
ui_sample_t sample;
|
||||
ui_model_snapshot(&sample);
|
||||
const uint64_t age = sample.available && now_ms >= sample.timestamp_ms ? now_ms - sample.timestamp_ms : 0;
|
||||
const bool fresh = sample.available && now_ms >= sample.timestamp_ms && age <= UI_SENSOR_STALE_MS;
|
||||
const char *state = !sample.available ? "Sensor startet ..." : fresh ? "Live / 1 Messung pro Sekunde" : "Messwerte veraltet";
|
||||
lv_label_set_text(objects.room_state, state);
|
||||
if (fresh) lv_obj_add_flag(objects.main_notice, LV_OBJ_FLAG_HIDDEN);
|
||||
else {
|
||||
lv_obj_clear_flag(objects.main_notice, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_label_set_text(objects.main_notice, state);
|
||||
lv_obj_t *values[] = {objects.obj5, objects.obj6, objects.obj7, objects.obj8,
|
||||
objects.obj9, objects.obj10, objects.obj11};
|
||||
for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); ++i) lv_label_set_text(values[i], "--");
|
||||
}
|
||||
format_value(objects.room_co2, "", fresh ? sample.values[UI_METRIC_CO2] : NAN, 0);
|
||||
format_value(objects.room_voc, "VOC-Index ", fresh ? sample.values[UI_METRIC_VOC] : NAN, 0);
|
||||
format_value(objects.room_nox, "NOx-Index ", fresh ? sample.values[UI_METRIC_NOX] : NAN, 0);
|
||||
if (fresh && !isfinite(sample.values[UI_METRIC_CO2]))
|
||||
lv_label_set_text(objects.room_state, "CO2-Kanal nicht verfügbar");
|
||||
fill_chart(objects.room_chart, room_series, UI_METRIC_CO2, now_ms, NULL);
|
||||
unsigned metric = lv_dropdown_get_selected(objects.history_metric);
|
||||
if (metric >= UI_METRIC_COUNT) metric = UI_METRIC_CO2;
|
||||
fill_chart(objects.history_chart, history_series, (ui_metric_t)metric, now_ms, objects.history_range);
|
||||
lv_label_set_text_fmt(objects.device_wifi, "WLAN: %s", status->wifi_connected ? status->ip : "nicht verbunden");
|
||||
if (status->last_influx_success_ms && now_ms >= status->last_influx_success_ms) {
|
||||
unsigned seconds = (unsigned)((now_ms - status->last_influx_success_ms) / 1000);
|
||||
if (seconds < 60) lv_label_set_text_fmt(objects.device_influx, "Influx: vor %u s / %s", seconds, status->influx_ok ? "OK" : "Fehler");
|
||||
else lv_label_set_text_fmt(objects.device_influx, "Influx: vor %u min / %s", seconds / 60, status->influx_ok ? "OK" : "Fehler");
|
||||
} else lv_label_set_text(objects.device_influx, "Influx: noch keine Übertragung");
|
||||
if (!sample.available) lv_label_set_text(objects.device_sensor, "Sensor: startet / wartet auf Daten");
|
||||
else lv_label_set_text_fmt(objects.device_sensor, "Sensor: %s / vor %u s", fresh ? "OK" : "veraltet", (unsigned)(age / 1000));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <lvgl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t brightness; /* 20..100 percent */
|
||||
uint16_t dim_after_s; /* 0, 30, 60, 120 */
|
||||
} ui_display_config_t;
|
||||
typedef struct {
|
||||
bool wifi_connected, influx_ok, mdns_ok;
|
||||
char ip[16];
|
||||
uint64_t last_influx_success_ms; /* zero until first successful HTTP 204 */
|
||||
} ui_device_status_t;
|
||||
typedef void (*ui_brightness_cb_t)(uint8_t percent);
|
||||
typedef void (*ui_save_config_cb_t)(const ui_display_config_t *config);
|
||||
|
||||
/* All calls run on the LVGL task or while its lock is held. */
|
||||
void ui_dashboard_init(ui_display_config_t config, ui_brightness_cb_t brightness,
|
||||
ui_save_config_cb_t save);
|
||||
void ui_dashboard_tick(uint64_t now_ms, const ui_device_status_t *status);
|
||||
/* Wrap the touch read callback, so the wake-up touch cannot activate controls. */
|
||||
void ui_dashboard_filter_touch(lv_indev_data_t *data);
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "ui_model.h"
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
static portMUX_TYPE model_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
#define LOCK() portENTER_CRITICAL(&model_mux)
|
||||
#define UNLOCK() portEXIT_CRITICAL(&model_mux)
|
||||
#else
|
||||
#define LOCK() ((void)0)
|
||||
#define UNLOCK() ((void)0)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint64_t bucket;
|
||||
float sum[UI_METRIC_COUNT];
|
||||
uint16_t count[UI_METRIC_COUNT];
|
||||
bool used;
|
||||
} history_bin_t;
|
||||
static ui_sample_t latest;
|
||||
static history_bin_t bins[UI_HISTORY_POINTS];
|
||||
|
||||
void ui_model_publish(const ui_sample_t *sample)
|
||||
{
|
||||
if (!sample || !sample->available) return;
|
||||
const uint64_t bucket = sample->timestamp_ms / UI_HISTORY_BUCKET_MS;
|
||||
LOCK();
|
||||
latest = *sample;
|
||||
history_bin_t *bin = &bins[bucket % UI_HISTORY_POINTS];
|
||||
if (!bin->used || bin->bucket != bucket) {
|
||||
*bin = (history_bin_t){.bucket = bucket, .used = true};
|
||||
}
|
||||
for (unsigned i = 0; i < UI_METRIC_COUNT; ++i) {
|
||||
if (isfinite(sample->values[i]) && bin->count[i] < UINT16_MAX) {
|
||||
bin->sum[i] += sample->values[i];
|
||||
++bin->count[i];
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
}
|
||||
|
||||
void ui_model_snapshot(ui_sample_t *sample)
|
||||
{
|
||||
LOCK();
|
||||
*sample = latest;
|
||||
UNLOCK();
|
||||
}
|
||||
|
||||
void ui_model_history(ui_metric_t metric, uint64_t now_ms, float *values)
|
||||
{
|
||||
const uint64_t last_bucket = now_ms / UI_HISTORY_BUCKET_MS;
|
||||
LOCK();
|
||||
for (unsigned i = 0; i < UI_HISTORY_POINTS; ++i) {
|
||||
const unsigned age = UI_HISTORY_POINTS - 1 - i;
|
||||
values[i] = NAN;
|
||||
if ((unsigned)metric >= UI_METRIC_COUNT || last_bucket < age) continue;
|
||||
const uint64_t bucket = last_bucket - age;
|
||||
const history_bin_t *bin = &bins[bucket % UI_HISTORY_POINTS];
|
||||
if (bin->used && bin->bucket == bucket && bin->count[metric]) {
|
||||
values[i] = bin->sum[metric] / bin->count[metric];
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define UI_HISTORY_POINTS 180
|
||||
#define UI_HISTORY_BUCKET_MS 10000
|
||||
#define UI_SENSOR_STALE_MS 5000
|
||||
|
||||
typedef enum {
|
||||
UI_METRIC_CO2, UI_METRIC_PM25, UI_METRIC_TEMP, UI_METRIC_RH,
|
||||
UI_METRIC_VOC, UI_METRIC_NOX, UI_METRIC_COUNT
|
||||
} ui_metric_t;
|
||||
typedef struct {
|
||||
uint64_t timestamp_ms;
|
||||
float values[UI_METRIC_COUNT]; /* ppm, ug/m3, degC, %, VOC index, NOx index */
|
||||
bool available;
|
||||
} ui_sample_t;
|
||||
|
||||
/* Producer submits scaled numbers, with NAN for unavailable channels. No LVGL calls. */
|
||||
void ui_model_publish(const ui_sample_t *sample);
|
||||
void ui_model_snapshot(ui_sample_t *sample);
|
||||
/* 180 fixed ten-second bins, oldest first; empty bins are NAN. RAM only. */
|
||||
void ui_model_history(ui_metric_t metric, uint64_t now_ms, float *values);
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "ui_navigation.h"
|
||||
#include "esp_log.h"
|
||||
#include "screens.h"
|
||||
#include "ui.h"
|
||||
|
||||
static void screen_gesture_cb(lv_event_t *event)
|
||||
{
|
||||
lv_indev_t *input = lv_indev_get_act();
|
||||
if (!input || lv_dropdown_is_open(objects.history_metric) ||
|
||||
lv_dropdown_is_open(objects.dim_timeout)) return;
|
||||
lv_obj_t *screen = lv_event_get_current_target(event);
|
||||
lv_obj_t *screens[] = {objects.main, objects.page_2, objects.history, objects.device};
|
||||
unsigned index = 0;
|
||||
while (index < 4 && screens[index] != screen) ++index;
|
||||
if (index >= 4) return;
|
||||
lv_dir_t direction = lv_indev_get_gesture_dir(input);
|
||||
unsigned next = index;
|
||||
if (direction == LV_DIR_LEFT && index < 3) ++next;
|
||||
else if (direction == LV_DIR_RIGHT && index > 0) --next;
|
||||
if (next == index) return;
|
||||
lv_indev_wait_release(input);
|
||||
loadScreen((enum ScreensEnum)(next + 1));
|
||||
ESP_LOGI("ui_navigation", "Swipe: page %u -> %u", index + 1, next + 1);
|
||||
}
|
||||
|
||||
static void prepare_gestures(lv_obj_t *object)
|
||||
{
|
||||
lv_obj_clear_flag(object, LV_OBJ_FLAG_SCROLLABLE);
|
||||
lv_obj_add_flag(object, LV_OBJ_FLAG_GESTURE_BUBBLE);
|
||||
for (uint32_t i = 0; i < lv_obj_get_child_cnt(object); ++i)
|
||||
prepare_gestures(lv_obj_get_child(object, i));
|
||||
}
|
||||
|
||||
void ui_navigation_init(void)
|
||||
{
|
||||
lv_obj_t *screens[] = {objects.main, objects.page_2, objects.history, objects.device};
|
||||
for (unsigned i = 0; i < 4; ++i) {
|
||||
prepare_gestures(screens[i]);
|
||||
lv_obj_clear_flag(screens[i], LV_OBJ_FLAG_GESTURE_BUBBLE);
|
||||
lv_obj_add_event_cb(screens[i], screen_gesture_cb, LV_EVENT_GESTURE, NULL);
|
||||
}
|
||||
/* Sliders/dropdowns own their gestures; changing brightness must not navigate. */
|
||||
lv_obj_clear_flag(objects.brightness, LV_OBJ_FLAG_GESTURE_BUBBLE);
|
||||
lv_obj_clear_flag(objects.history_metric, LV_OBJ_FLAG_GESTURE_BUBBLE);
|
||||
lv_obj_clear_flag(objects.dim_timeout, LV_OBJ_FLAG_GESTURE_BUBBLE);
|
||||
ESP_LOGI("ui_navigation", "Ready: 4 pages / measurements, air, history, device");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
/* Call once after ui_init(), with the LVGL lock held. Kept outside EEZ exports. */
|
||||
void ui_navigation_init(void);
|
||||
@@ -288,6 +288,7 @@ static esp_err_t net_influx_write_line(const char* line) {
|
||||
esp_http_client_cleanup(h);
|
||||
|
||||
ok = (err == ESP_OK && status == 204);
|
||||
if (ok) sys_status_record_influx_success();
|
||||
sys_status_set_influx_ok(ok);
|
||||
return ok ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
|
||||
@@ -18,4 +18,5 @@ idf_component_register(
|
||||
eez_gui
|
||||
storage
|
||||
hardware_config
|
||||
esp_timer
|
||||
)
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h> // NAN, isfinite, lround
|
||||
#include "vars.h"
|
||||
#include "ui_model.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
static const char* TAG = "air_sensor_app";
|
||||
|
||||
@@ -188,26 +190,28 @@ static void air_sensor_task_producer(void* arg) {
|
||||
// float noxf = (s.nox_idx == 0x7FFF) ? NAN : s.nox_idx / 10.0f; // bei Bedarf anzeigen
|
||||
|
||||
// Temperatur
|
||||
snprintf(buf, sizeof(buf), "%.1f", (double)s.temp_c);
|
||||
if (isfinite(s.temp_c)) snprintf(buf, sizeof(buf), "%.1f", (double)s.temp_c);
|
||||
else snprintf(buf, sizeof(buf), "--");
|
||||
set_var_temp(buf);
|
||||
|
||||
// Feuchte
|
||||
snprintf(buf, sizeof(buf), "%.1f", (double)s.rh_pct);
|
||||
if (isfinite(s.rh_pct)) snprintf(buf, sizeof(buf), "%.1f", (double)s.rh_pct);
|
||||
else snprintf(buf, sizeof(buf), "--");
|
||||
set_var_humidity(buf);
|
||||
|
||||
// VOC-Index (gerundet), falls NAN → "nan"
|
||||
if (!isfinite(vocf)) snprintf(buf, sizeof(buf), "nan");
|
||||
// VOC-Index (gerundet), nicht verfügbare Werte als "--"
|
||||
if (!isfinite(vocf)) snprintf(buf, sizeof(buf), "--");
|
||||
else snprintf(buf, sizeof(buf), "%d", (int)lround(vocf));
|
||||
set_var_voc_ind(buf);
|
||||
|
||||
// PM (µg/m³) mit einer Nachkommastelle
|
||||
if (!isfinite(pm10f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm10f);
|
||||
if (!isfinite(pm10f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm10f);
|
||||
set_var_pm10p0(buf);
|
||||
if (!isfinite(pm4f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm4f);
|
||||
if (!isfinite(pm4f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm4f);
|
||||
set_var_pm4p0(buf);
|
||||
if (!isfinite(pm2f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm2f);
|
||||
if (!isfinite(pm2f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm2f);
|
||||
set_var_pm2p5(buf);
|
||||
if (!isfinite(pm1f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm1f);
|
||||
if (!isfinite(pm1f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm1f);
|
||||
set_var_pm1p0(buf);
|
||||
|
||||
/* === Influx submit (generisch, variabel viele Felder) === */
|
||||
@@ -220,6 +224,15 @@ static void air_sensor_task_producer(void* arg) {
|
||||
const double temp = (double)s.temp_c;
|
||||
const double rh = (double)s.rh_pct;
|
||||
const double co2 = (co2_raw == UINT16_MAX) ? NAN : (double)co2_raw;
|
||||
#if NANOPSX_HAS_DISPLAY
|
||||
ui_sample_t display_sample = {
|
||||
.timestamp_ms = (uint64_t)(esp_timer_get_time() / 1000),
|
||||
.available = true,
|
||||
.values = {(float)co2, (float)pm2_5, (float)temp,
|
||||
(float)rh, (float)voc, (float)nox}
|
||||
};
|
||||
ui_model_publish(&display_sample);
|
||||
#endif
|
||||
static unsigned samples;
|
||||
if (++samples % 10 == 0) {
|
||||
ESP_LOGI(TAG, "%s sample: PM2.5=%.1f temp=%.2f rh=%.2f CO2=%.0f",
|
||||
|
||||
@@ -3,6 +3,6 @@ idf_component_register(
|
||||
"src/sys_status.c"
|
||||
INCLUDE_DIRS
|
||||
"include"
|
||||
REQUIRES
|
||||
REQUIRES esp_timer
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// File: firmware/components/sys_status/include/sys_status.h
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
@@ -25,6 +26,10 @@ static inline bool sys_status_get_wifi_ok(void) { return (xEventGroupGetBits(s
|
||||
static inline bool sys_status_get_influx_ok(void) { return (xEventGroupGetBits(sys_status_group()) & SYS_STATUS_INFLUX_OK) != 0; }
|
||||
static inline bool sys_status_get_mdns_ok(void) { return (xEventGroupGetBits(sys_status_group()) & SYS_STATUS_MDNS_OK) != 0; }
|
||||
|
||||
/* Monotonic timestamp of a confirmed HTTP 204, never merely a WLAN connection. */
|
||||
void sys_status_record_influx_success(void);
|
||||
uint64_t sys_status_last_influx_success_ms(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -21,3 +21,22 @@ EventGroupHandle_t sys_status_group(void)
|
||||
if (!s_status_group) sys_status_init();
|
||||
return s_status_group;
|
||||
}
|
||||
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/portmacro.h"
|
||||
static portMUX_TYPE influx_time_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
static uint64_t last_influx_success;
|
||||
void sys_status_record_influx_success(void)
|
||||
{
|
||||
uint64_t now = (uint64_t)(esp_timer_get_time() / 1000);
|
||||
portENTER_CRITICAL(&influx_time_mux);
|
||||
last_influx_success = now;
|
||||
portEXIT_CRITICAL(&influx_time_mux);
|
||||
}
|
||||
uint64_t sys_status_last_influx_success_ms(void)
|
||||
{
|
||||
portENTER_CRITICAL(&influx_time_mux);
|
||||
uint64_t result = last_influx_success;
|
||||
portEXIT_CRITICAL(&influx_time_mux);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ monitor_speed = 115200
|
||||
monitor_dtr = 0
|
||||
monitor_rts = 0
|
||||
board_build.partitions = partitions.csv
|
||||
; extra_scripts = scripts/enable_ccache.py
|
||||
extra_scripts = pre:tools/pio_display.py
|
||||
|
||||
+57
-58
@@ -35,6 +35,10 @@
|
||||
#include "demos/lv_demos.h"
|
||||
|
||||
#include "ui.h"
|
||||
#include "ui_navigation.h"
|
||||
#include "ui_dashboard.h"
|
||||
#include "nvs.h"
|
||||
#include "esp_netif.h"
|
||||
#include "i2cdev.h"
|
||||
|
||||
#include "sys_status.h"
|
||||
@@ -73,61 +77,6 @@ static void app_save_device_info(
|
||||
storage_save_influx_softap(org, bucket, token);
|
||||
}
|
||||
|
||||
// ---------- Status-Icons updaten: nutzt die in screens.c erzeugten Objekte ----------
|
||||
static void ui_update_status_icons(void)
|
||||
{
|
||||
static bool first = true;
|
||||
static bool pw=false, pi=false, pm=false;
|
||||
|
||||
bool w = sys_status_get_wifi_ok();
|
||||
bool i = sys_status_get_influx_ok();
|
||||
bool m = sys_status_get_mdns_ok();
|
||||
|
||||
// WiFi
|
||||
if (first || w != pw) {
|
||||
if (objects.wifi_ok && objects.wifi_off) {
|
||||
if (w) {
|
||||
lv_obj_clear_flag(objects.wifi_ok, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag (objects.wifi_off, LV_OBJ_FLAG_HIDDEN);
|
||||
} else {
|
||||
lv_obj_add_flag (objects.wifi_ok, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(objects.wifi_off, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
pw = w;
|
||||
}
|
||||
|
||||
// Influx
|
||||
if (first || i != pi) {
|
||||
if (objects.influx_ok && objects.influx_off) {
|
||||
if (i) {
|
||||
lv_obj_clear_flag(objects.influx_ok, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag (objects.influx_off, LV_OBJ_FLAG_HIDDEN);
|
||||
} else {
|
||||
lv_obj_add_flag (objects.influx_ok, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(objects.influx_off, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
pi = i;
|
||||
}
|
||||
|
||||
// mDNS
|
||||
if (first || m != pm) {
|
||||
if (objects.mdns_ok && objects.mdns_off) {
|
||||
if (m) {
|
||||
lv_obj_clear_flag(objects.mdns_ok, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag (objects.mdns_off, LV_OBJ_FLAG_HIDDEN);
|
||||
} else {
|
||||
lv_obj_add_flag (objects.mdns_ok, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(objects.mdns_off, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
pm = m;
|
||||
}
|
||||
|
||||
first = false;
|
||||
}
|
||||
|
||||
#define EXAMPLE_DISPLAY_ROTATION 270
|
||||
|
||||
#if EXAMPLE_DISPLAY_ROTATION == 90 || EXAMPLE_DISPLAY_ROTATION == 270
|
||||
@@ -236,12 +185,57 @@ static esp_err_t app_lvgl_init(void)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#if NANOPSX_HAS_DISPLAY
|
||||
static void (*original_touch_read)(lv_indev_drv_t *, lv_indev_data_t *);
|
||||
static void filtered_touch_read(lv_indev_drv_t *driver, lv_indev_data_t *data)
|
||||
{
|
||||
original_touch_read(driver, data);
|
||||
ui_dashboard_filter_touch(data);
|
||||
}
|
||||
|
||||
static ui_display_config_t load_display_config(void)
|
||||
{
|
||||
ui_display_config_t result = {.brightness = 100, .dim_after_s = 60};
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open("ui_display", NVS_READONLY, &handle) == ESP_OK) {
|
||||
nvs_get_u8(handle, "brightness", &result.brightness);
|
||||
nvs_get_u16(handle, "dim_after", &result.dim_after_s);
|
||||
nvs_close(handle);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void save_display_config(const ui_display_config_t *config)
|
||||
{
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err = nvs_open("ui_display", NVS_READWRITE, &handle);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_set_u8(handle, "brightness", config->brightness);
|
||||
if (err == ESP_OK) err = nvs_set_u16(handle, "dim_after", config->dim_after_s);
|
||||
if (err == ESP_OK) err = nvs_commit(handle);
|
||||
nvs_close(handle);
|
||||
}
|
||||
if (err != ESP_OK) ESP_LOGW("display", "Cannot save settings: %s", esp_err_to_name(err));
|
||||
}
|
||||
#endif
|
||||
|
||||
static void ui_tick_cb(lv_timer_t *t) {
|
||||
(void)t;
|
||||
app_update_mdns_status_from_nvs();
|
||||
if (lvgl_port_lock(0)) {
|
||||
ui_update_status_icons();
|
||||
tick_screen(0);
|
||||
ui_device_status_t status = {
|
||||
.wifi_connected = sys_status_get_wifi_ok(),
|
||||
.influx_ok = sys_status_get_influx_ok(),
|
||||
.mdns_ok = sys_status_get_mdns_ok(),
|
||||
.last_influx_success_ms = sys_status_last_influx_success_ms()
|
||||
};
|
||||
snprintf(status.ip, sizeof(status.ip), "verbunden");
|
||||
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
esp_netif_ip_info_t ip;
|
||||
if (netif && esp_netif_get_ip_info(netif, &ip) == ESP_OK && ip.ip.addr)
|
||||
snprintf(status.ip, sizeof(status.ip), IPSTR, IP2STR(&ip.ip));
|
||||
ui_dashboard_tick((uint64_t)(esp_timer_get_time() / 1000), &status);
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
}
|
||||
@@ -404,11 +398,16 @@ void app_main(void)
|
||||
|
||||
if (lvgl_port_lock(0)) {
|
||||
ui_init(); // erzeugt auch die Objekte in screens.c
|
||||
ui_dashboard_init(load_display_config(), bsp_display_set_brightness, save_display_config);
|
||||
ui_navigation_init();
|
||||
if (lvgl_touch_indev) {
|
||||
original_touch_read = lvgl_touch_indev->driver->read_cb;
|
||||
lvgl_touch_indev->driver->read_cb = filtered_touch_read;
|
||||
}
|
||||
lv_timer_create(ui_tick_cb, 1000, NULL);
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
|
||||
lv_timer_t *ui_timer = lv_timer_create(ui_tick_cb, 1000, NULL);
|
||||
(void)ui_timer;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run native UI integration tests against the project's actual LVGL sources."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import sys
|
||||
|
||||
firmware = Path(__file__).resolve().parents[2]
|
||||
lvgl = firmware / 'managed_components/lvgl__lvgl'
|
||||
ui = firmware / 'components/eez_gui'
|
||||
with tempfile.TemporaryDirectory(prefix='nanopsx-ui-') as directory:
|
||||
build = Path(directory)
|
||||
(build / 'esp_log.h').write_text('#pragma once\n#define ESP_LOGI(...) ((void)0)\n')
|
||||
sources = list((lvgl / 'src').rglob('*.c'))
|
||||
sources += [ui / name for name in ['ui.c', 'screens.c', 'ui_navigation.c', 'ui_dashboard.c', 'ui_model.c', 'ui_font_ubuntu_16.c',
|
||||
'ui_font_ubuntu_24.c', 'ui_font_pigiarniq_italic_34.c']]
|
||||
sources += list(ui.glob('ui_image_*.c'))
|
||||
sources += [Path(__file__).with_name('test_navigation.c')]
|
||||
subprocess.run(['cc', '-std=c99', '-O0', '-g', '-DLV_CONF_SKIP',
|
||||
'-DLV_MEM_SIZE=49152', '-I', str(build), '-I', str(lvgl),
|
||||
'-I', str(ui), *map(str, sources), '-lm',
|
||||
'-o', str(build / 'test_navigation')], check=True)
|
||||
subprocess.run([str(build / 'test_navigation'), *sys.argv[1:]], check=True)
|
||||
@@ -0,0 +1,175 @@
|
||||
/* Integration tests use the actual LVGL pointer pipeline, charts and EEZ exports. */
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "ui.h"
|
||||
#include "ui_navigation.h"
|
||||
#include "ui_dashboard.h"
|
||||
#include "ui_model.h"
|
||||
|
||||
static lv_indev_data_t sample;
|
||||
static uint32_t framebuffer[172][320];
|
||||
static unsigned brightness, saved;
|
||||
static ui_display_config_t saved_config;
|
||||
static void set_light(uint8_t value) { brightness = value; }
|
||||
static void save_settings(const ui_display_config_t *value) { saved_config = *value; ++saved; }
|
||||
static void read_pointer(lv_indev_drv_t *drv, lv_indev_data_t *data)
|
||||
{
|
||||
(void)drv; *data = sample;
|
||||
ui_dashboard_filter_touch(data);
|
||||
}
|
||||
static void flush(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *colors)
|
||||
{
|
||||
for (int y = area->y1; y <= area->y2; ++y)
|
||||
for (int x = area->x1; x <= area->x2; ++x) {
|
||||
if (x >= 0 && x < 320 && y >= 0 && y < 172) framebuffer[y][x] = lv_color_to32(*colors);
|
||||
++colors;
|
||||
}
|
||||
lv_disp_flush_ready(drv);
|
||||
}
|
||||
static void advance(unsigned ms)
|
||||
{
|
||||
for (unsigned i = 0; i < ms; i += 10) { lv_tick_inc(10); lv_timer_handler(); }
|
||||
}
|
||||
static void point(int x, int y, bool pressed)
|
||||
{
|
||||
sample.point.x = x; sample.point.y = y;
|
||||
sample.state = pressed ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
|
||||
advance(40);
|
||||
}
|
||||
static void swipe(int x, int y, int dx, int dy)
|
||||
{
|
||||
point(x, y, true);
|
||||
for (int i = 1; i <= 8; ++i) point(x + i * dx, y + i * dy, true);
|
||||
point(x + 8 * dx, y + 8 * dy, false);
|
||||
advance(300);
|
||||
ui_tick();
|
||||
}
|
||||
static void screenshot(const char *directory, unsigned page)
|
||||
{
|
||||
if (!directory) return;
|
||||
char path[1024]; snprintf(path,sizeof(path),"%s/page-%u.ppm",directory,page);
|
||||
FILE *file = fopen(path,"wb"); assert(file);
|
||||
fprintf(file,"P6\n320 172\n255\n");
|
||||
for (unsigned y=0;y<172;++y) for(unsigned x=0;x<320;++x) {
|
||||
uint32_t c=framebuffer[y][x];
|
||||
unsigned char rgb[]={c>>16,c>>8,c}; fwrite(rgb,1,3,file);
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
#define VALUE(name, text) const char *get_var_##name(void) { return text; }
|
||||
VALUE(pm1p0,"3.2") VALUE(pm2p5,"6.8") VALUE(pm4p0,"7.1") VALUE(pm10p0,"7.3")
|
||||
VALUE(voc_ind,"98") VALUE(humidity,"42.1") VALUE(temp,"23.4")
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
lv_init();
|
||||
static lv_color_t pixels[320 * 10];
|
||||
static lv_disp_draw_buf_t buffer;
|
||||
lv_disp_draw_buf_init(&buffer,pixels,NULL,320*10);
|
||||
lv_disp_drv_t display; lv_disp_drv_init(&display);
|
||||
display.hor_res=320; display.ver_res=172; display.draw_buf=&buffer; display.flush_cb=flush;
|
||||
lv_disp_drv_register(&display);
|
||||
lv_indev_drv_t pointer; lv_indev_drv_init(&pointer);
|
||||
pointer.type=LV_INDEV_TYPE_POINTER; pointer.read_cb=read_pointer;
|
||||
lv_indev_drv_register(&pointer);
|
||||
ui_init();
|
||||
ui_dashboard_init((ui_display_config_t){100,60},set_light,save_settings);
|
||||
ui_navigation_init(); advance(300);
|
||||
ui_device_status_t status={.wifi_connected=true,.influx_ok=true,.mdns_ok=true,
|
||||
.ip="192.168.50.86",.last_influx_success_ms=1790000};
|
||||
ui_dashboard_tick(1000,&status);
|
||||
assert(strstr(lv_label_get_text(objects.room_state),"startet"));
|
||||
assert(strcmp(lv_label_get_text(objects.room_co2),"--")==0);
|
||||
assert(!lv_obj_has_flag(objects.main_notice,LV_OBJ_FLAG_HIDDEN));
|
||||
ui_sample_t data={.available=true};
|
||||
/* Realistic 30-minute test data exercises the complete ring buffer and chart. */
|
||||
for (unsigned i=0;i<180;++i) {
|
||||
data.timestamp_ms=1000+i*10000;
|
||||
data.values[UI_METRIC_CO2]=900+150*sinf(i/15.0f);
|
||||
data.values[UI_METRIC_PM25]=6.8f+2*sinf(i/10.0f);
|
||||
data.values[UI_METRIC_TEMP]=23.4f;
|
||||
data.values[UI_METRIC_RH]=42.1f;
|
||||
data.values[UI_METRIC_VOC]=98;
|
||||
data.values[UI_METRIC_NOX]=1;
|
||||
ui_model_publish(&data);
|
||||
}
|
||||
ui_dashboard_tick(data.timestamp_ms,&status); tick_screen(0); advance(300);
|
||||
assert(lv_obj_has_flag(objects.main_notice,LV_OBJ_FLAG_HIDDEN));
|
||||
assert(atoi(lv_label_get_text(objects.room_co2))>700);
|
||||
const char *previews=argc>1?argv[1]:NULL;
|
||||
screenshot(previews,1);
|
||||
point(160,80,true); point(160,80,false); advance(300);
|
||||
assert(lv_scr_act()==objects.main);
|
||||
swipe(140,80,-1,0); assert(lv_scr_act()==objects.main);
|
||||
swipe(160,20,0,15); assert(lv_scr_act()==objects.main);
|
||||
swipe(40,80,25,0); assert(lv_scr_act()==objects.main);
|
||||
swipe(280,80,-25,0); assert(lv_scr_act()==objects.page_2);
|
||||
screenshot(previews,2);
|
||||
swipe(280,80,-25,0); assert(lv_scr_act()==objects.history);
|
||||
for(unsigned i=0;i<UI_METRIC_COUNT;++i) {
|
||||
lv_dropdown_set_selected(objects.history_metric,i);
|
||||
ui_dashboard_tick(data.timestamp_ms,&status);
|
||||
assert(strstr(lv_label_get_text(objects.history_range),"Noch")==NULL);
|
||||
assert(strstr(lv_label_get_text(objects.history_range),"..")!=NULL);
|
||||
assert(lv_label_get_text(objects.history_range)[0]!='f');
|
||||
}
|
||||
lv_dropdown_set_selected(objects.history_metric,0);
|
||||
ui_dashboard_tick(data.timestamp_ms,&status); advance(300);
|
||||
screenshot(previews,3);
|
||||
/* Popup owns its input, so a gesture on it cannot change the screen. */
|
||||
lv_dropdown_open(objects.history_metric); advance(100);
|
||||
swipe(280,20,-25,0); assert(lv_scr_act()==objects.history);
|
||||
lv_dropdown_close(objects.history_metric);
|
||||
swipe(280,20,-25,0); assert(lv_scr_act()==objects.device);
|
||||
screenshot(previews,4);
|
||||
swipe(280,20,-25,0); assert(lv_scr_act()==objects.device);
|
||||
/* Sliding the brightness control changes brightness, never the page. */
|
||||
swipe(270,111,-15,0); assert(lv_scr_act()==objects.device);
|
||||
assert(brightness>=20 && brightness<100 && saved>0);
|
||||
unsigned before=brightness, save_count=saved;
|
||||
advance(61000); ui_dashboard_tick(data.timestamp_ms,&status);
|
||||
assert(brightness==15);
|
||||
point(170,111,true); point(170,111,false);
|
||||
assert(brightness==before && saved==save_count); /* first touch wakes only */
|
||||
point(220,111,true); point(220,111,false);
|
||||
assert(brightness!=before && saved>save_count);
|
||||
lv_dropdown_set_selected(objects.dim_timeout,0);
|
||||
lv_event_send(objects.dim_timeout,LV_EVENT_VALUE_CHANGED,NULL);
|
||||
assert(saved_config.dim_after_s==0);
|
||||
advance(121000); ui_dashboard_tick(data.timestamp_ms,&status);
|
||||
assert(brightness==saved_config.brightness);
|
||||
/* Freshness is independent of network connectivity and missing channels. */
|
||||
ui_dashboard_tick(data.timestamp_ms+6000,&status);
|
||||
assert(strstr(lv_label_get_text(objects.room_state),"veraltet"));
|
||||
assert(strcmp(lv_label_get_text(objects.room_co2),"--")==0);
|
||||
data.values[UI_METRIC_CO2]=NAN;
|
||||
data.timestamp_ms+=10000; ui_model_publish(&data);
|
||||
status.wifi_connected=false; status.influx_ok=false;
|
||||
ui_dashboard_tick(data.timestamp_ms,&status);
|
||||
assert(strstr(lv_label_get_text(objects.room_state),"nicht verfügbar"));
|
||||
assert(strstr(lv_label_get_text(objects.device_wifi),"nicht verbunden"));
|
||||
assert(strstr(lv_label_get_text(objects.device_influx),"Fehler"));
|
||||
for(unsigned repeat=0;repeat<3;++repeat) {
|
||||
swipe(40,20,25,0); assert(lv_scr_act()==objects.history);
|
||||
swipe(40,20,25,0); assert(lv_scr_act()==objects.page_2);
|
||||
swipe(40,20,25,0); assert(lv_scr_act()==objects.main);
|
||||
swipe(280,20,-25,0); assert(lv_scr_act()==objects.page_2);
|
||||
swipe(280,20,-25,0); assert(lv_scr_act()==objects.history);
|
||||
swipe(280,20,-25,0); assert(lv_scr_act()==objects.device);
|
||||
}
|
||||
/* Ten-second means, gap preservation, missing-channel independence, eviction. */
|
||||
float history[UI_HISTORY_POINTS];
|
||||
uint64_t base=4000000;
|
||||
data.timestamp_ms=base; data.values[0]=800; ui_model_publish(&data);
|
||||
data.timestamp_ms=base+1000; data.values[0]=1000; ui_model_publish(&data);
|
||||
ui_model_history(UI_METRIC_CO2,base+1000,history);
|
||||
assert(fabsf(history[179]-900)<0.01f);
|
||||
ui_model_history(UI_METRIC_CO2,base+20000,history);
|
||||
assert(isnan(history[179]) && isnan(history[178]) && history[177]==900);
|
||||
ui_model_history(UI_METRIC_CO2,base+1810000,history);
|
||||
for(unsigned i=0;i<180;++i) assert(isnan(history[i]));
|
||||
puts("PASS: four-page navigation, controls, popup isolation, wake-only touch, dim settings, stale/missing data, six charts, history means/gaps/eviction");
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Export this project's static EEZ/LVGL screens without a desktop installation.
|
||||
|
||||
Supports the widget/style subset used here; rejects unsupported widget types.
|
||||
Application behavior stays in ui_dashboard/ui_navigation, outside generated files.
|
||||
"""
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
import json
|
||||
import re
|
||||
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
project = json.loads((root / 'eez/nanopsx.eez-project').read_text())
|
||||
out = root / 'firmware/components/eez_gui'
|
||||
|
||||
def name(value):
|
||||
return re.sub(r'[^a-z0-9_]', '_', value.lower())
|
||||
def string(value):
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
def walk(widget):
|
||||
yield widget
|
||||
for child in widget.get('children', []):
|
||||
yield from walk(child)
|
||||
|
||||
pages = project['userPages']
|
||||
roots = [name(page['name']) for page in pages]
|
||||
fields = list(roots)
|
||||
for page in pages:
|
||||
for widget in walk(page['components'][0]):
|
||||
identifier = widget.get('identifier', '').strip()
|
||||
if identifier:
|
||||
assert name(identifier) not in fields, identifier
|
||||
fields.append(name(identifier))
|
||||
header = ['/* Generated by firmware/tools/export_display.py from the EEZ project. */',
|
||||
'#pragma once', '#include <lvgl.h>', '#ifdef __cplusplus', 'extern "C" {', '#endif',
|
||||
'typedef struct {', *[f' lv_obj_t *{field};' for field in fields],
|
||||
'} objects_t;', 'extern objects_t objects;', 'enum ScreensEnum {',
|
||||
*[f' SCREEN_ID_{r.upper()} = {i+1},' for i,r in enumerate(roots)], '};']
|
||||
for r in roots:
|
||||
header += [f'void create_screen_{r}(void);', f'void tick_screen_{r}(void);']
|
||||
header += ['void create_screens(void);', 'void tick_screen(int screen_index);',
|
||||
'void tick_screen_by_id(enum ScreensEnum screen_id);', '#ifdef __cplusplus', '}', '#endif']
|
||||
source = ['/* Generated by firmware/tools/export_display.py from the EEZ project. */',
|
||||
'#include "screens.h"', '#include "fonts.h"', '#include "images.h"',
|
||||
'#include "vars.h"', '#include <string.h>',
|
||||
'objects_t objects;', 'lv_obj_t *tick_value_change_obj;', 'uint32_t active_theme_index;']
|
||||
creators = {'Screen':'lv_obj_create','Panel':'lv_obj_create','Label':'lv_label_create',
|
||||
'Image':'lv_img_create','Chart':'lv_chart_create','Slider':'lv_slider_create',
|
||||
'Dropdown':'lv_dropdown_create'}
|
||||
color_styles = {'bg_color','border_color','text_color','line_color'}
|
||||
scalar_styles = {'radius','border_width','pad_all','pad_left','pad_right','pad_top','pad_bottom',
|
||||
'bg_opa','line_width','line_rounded','text_letter_space','text_line_space'}
|
||||
for page,r in zip(pages, roots):
|
||||
bindings = []
|
||||
source.append(f'void create_screen_{r}(void) {{')
|
||||
for i,widget in enumerate(walk(page['components'][0])):
|
||||
kind = widget['type'].removeprefix('LVGL').removesuffix('Widget')
|
||||
assert kind in creators, f'Unsupported EEZ widget: {kind}'
|
||||
identifier = r if i == 0 else name(widget.get('identifier','').strip())
|
||||
# Resolve parent from the actual tree, avoiding flattened-index assumptions.
|
||||
def find_parent(node, target):
|
||||
for child in node.get('children',[]):
|
||||
if child is target: return node
|
||||
found = find_parent(child,target)
|
||||
if found is not None: return found
|
||||
tree = list(walk(page['components'][0]))
|
||||
parent = find_parent(tree[0], widget) if i else None
|
||||
parent_expr = f'w{next(j for j,node in enumerate(tree) if node is parent)}' if parent else 'NULL'
|
||||
v=f'w{i}'
|
||||
source.append(f' lv_obj_t *{v} = {creators[kind]}({parent_expr});')
|
||||
if identifier: source.append(f' objects.{identifier} = {v};')
|
||||
source.append(f' lv_obj_set_pos({v}, {widget["left"]}, {widget["top"]});')
|
||||
width = 'LV_SIZE_CONTENT' if widget.get('widthUnit')=='content' else widget['width']
|
||||
height = 'LV_SIZE_CONTENT' if widget.get('heightUnit')=='content' else widget['height']
|
||||
source.append(f' lv_obj_set_size({v}, {width}, {height});')
|
||||
source.append(f' lv_obj_clear_flag({v}, LV_OBJ_FLAG_SCROLLABLE);')
|
||||
if widget.get('clickableFlag'): source.append(f' lv_obj_add_flag({v}, LV_OBJ_FLAG_CLICKABLE);')
|
||||
if widget.get('hiddenFlag'): source.append(f' lv_obj_add_flag({v}, LV_OBJ_FLAG_HIDDEN);')
|
||||
for part,states in widget.get('localStyles',{}).get('definition',{}).items():
|
||||
for state,props in states.items():
|
||||
selector = f'LV_PART_{part} | LV_STATE_{state}'
|
||||
for prop,value in props.items():
|
||||
if prop in color_styles: cvalue=f'lv_color_hex(0x{value.lstrip("#")})'
|
||||
elif prop=='text_font': cvalue='&ui_font_'+name(value)
|
||||
elif prop in scalar_styles: cvalue=str(value).lower()
|
||||
else: raise ValueError(f'Unsupported EEZ style: {prop}')
|
||||
source.append(f' lv_obj_set_style_{prop}({v}, {cvalue}, {selector});')
|
||||
if kind=='Label':
|
||||
source.append(f' lv_label_set_long_mode({v}, LV_LABEL_LONG_{widget.get("longMode","WRAP")});')
|
||||
if widget.get('textType')=='expression':
|
||||
assert identifier
|
||||
binding=f'get_var_{name(widget["text"])}()'
|
||||
source.append(f' lv_label_set_text({v}, {binding});')
|
||||
bindings.append((identifier,binding))
|
||||
else: source.append(f' lv_label_set_text({v}, {string(widget.get("text",""))});')
|
||||
elif kind=='Image': source.append(f' lv_img_set_src({v}, &img_{name(widget["image"])});')
|
||||
elif kind=='Chart':
|
||||
source += [f' lv_chart_set_type({v}, LV_CHART_TYPE_LINE);',f' lv_chart_set_point_count({v}, {widget["pointCount"]});',f' lv_chart_set_div_line_count({v}, {widget["divLineCountH"]}, {widget["divLineCountV"]});']
|
||||
elif kind=='Slider':
|
||||
source += [f' lv_slider_set_range({v}, {widget["min"]}, {widget["max"]});',f' lv_slider_set_value({v}, {widget["value"]}, LV_ANIM_OFF);']
|
||||
elif kind=='Dropdown':
|
||||
source += [f' lv_dropdown_set_options({v}, {string(widget["options"])});',f' lv_dropdown_set_selected({v}, {widget["selected"]});',f' lv_dropdown_set_dir({v}, LV_DIR_{widget["direction"].upper()});']
|
||||
source += ['}',f'void tick_screen_{r}(void) {{']
|
||||
for identifier,binding in bindings:
|
||||
source += [f' if (strcmp(lv_label_get_text(objects.{identifier}), {binding}) != 0)', f' lv_label_set_text(objects.{identifier}, {binding});']
|
||||
source += ['}']
|
||||
source += ['void create_screens(void) {', ' lv_disp_t *display = lv_disp_get_default();',
|
||||
' lv_disp_set_theme(display, lv_theme_default_init(display, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), true, LV_FONT_DEFAULT));',
|
||||
*[f' create_screen_{r}();' for r in roots], '}',
|
||||
'void tick_screen(int index) {', ' switch (index) {',
|
||||
*[f' case {i}: tick_screen_{r}(); break;' for i,r in enumerate(roots)],
|
||||
' default: break;', ' }', '}',
|
||||
'void tick_screen_by_id(enum ScreensEnum id) { tick_screen(id - 1); }']
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--check', action='store_true', help='Fail if checked-in exports differ')
|
||||
args=parser.parse_args()
|
||||
for filename,lines in [('screens.h',header),('screens.c',source)]:
|
||||
data='\n'.join(lines)+'\n'
|
||||
if args.check:
|
||||
assert (out/filename).read_text()==data, f'Outdated EEZ export: {filename}'
|
||||
elif not (out/filename).exists() or (out/filename).read_text() != data:
|
||||
(out/filename).write_text(data)
|
||||
print('EEZ screen exports '+('verified' if args.check else 'generated'))
|
||||
@@ -0,0 +1,8 @@
|
||||
"""Keep EEZ screen exports in sync before PlatformIO configures the build."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
Import("env")
|
||||
script = Path(env.subst("$PROJECT_DIR")) / "tools" / "export_display.py"
|
||||
subprocess.run([sys.executable, str(script)], check=True)
|
||||
Reference in New Issue
Block a user