Chart geht schon fast

This commit is contained in:
rxf
2024-07-07 15:33:47 +02:00
parent 87eb2a30d4
commit b1be91b52f
6 changed files with 125 additions and 26 deletions

View File

@@ -1,15 +1,25 @@
// Grafik Routinen // Grafik Routinen
#define GRAFIK #define GRAFIK
#include "handlebme280.h"
#include "main.h"
#include "grafik.h" #include "grafik.h"
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8)) #define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4]; uint32_t draw_buf[DRAW_BUF_SIZE / 4];
static lv_obj_t * chart;
static lv_coord_t bmewert[MAXREADINGS];
static lv_coord_t bmewert_hum[MAXREADINGS];
void setup_grafik(void) { void setup_grafik(void) {
lv_init(); lv_init();
lv_display_t * disp; lv_display_t * disp;
disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf)); disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
for (int i = 0; i < MAXREADINGS; i++) {
bmewert[i] = 0;
bmewert_hum[i] = 25;
}
} }
void buildarc(ARC arc) { void buildarc(ARC arc) {
@@ -41,7 +51,7 @@ void buildarc(ARC arc) {
lv_obj_t * value_label = lv_label_create(arc.arc); lv_obj_t * value_label = lv_label_create(arc.arc);
char value[10]; char value[10];
sprintf(value, arc.valformat, arc.value); sprintf(value, arc.valformat, arc.value);
Serial.println(value); // Serial.println(value);
lv_label_set_text(value_label, value); lv_label_set_text(value_label, value);
lv_obj_align(value_label, LV_ALIGN_LEFT_MID, 38, -10); lv_obj_align(value_label, LV_ALIGN_LEFT_MID, 38, -10);
static lv_style_t style_value_label; static lv_style_t style_value_label;
@@ -66,6 +76,7 @@ void buildarc(ARC arc) {
lv_style_init(&style_unit_label); lv_style_init(&style_unit_label);
lv_style_set_text_font(&style_unit_label, &lv_font_montserrat_12); lv_style_set_text_font(&style_unit_label, &lv_font_montserrat_12);
lv_obj_add_style(unit_label, &style_unit_label, 0); lv_obj_add_style(unit_label, &style_unit_label, 0);
} }
void lv_create_main_gui(void) { void lv_create_main_gui(void) {
@@ -91,7 +102,11 @@ void lv_create_main_gui(void) {
lv_style_set_text_font(&style_time_label, &lv_font_montserrat_12); lv_style_set_text_font(&style_time_label, &lv_font_montserrat_12);
lv_obj_add_style(time_label, &style_time_label, 0); lv_obj_add_style(time_label, &style_time_label, 0);
// lv_obj_t * label = lv_label_create(lv_screen_active()); /* Refresh-Symbol anzeigen
lv_obj_t * refresh_label = lv_label_create(lv_screen_active());
lv_label_set_text(refresh_label, LV_SYMBOL_REFRESH);
lv_obj_align(refresh_label, LV_ALIGN_CENTER, 0, -45);
*/
/*Create an Arc*/ /*Create an Arc*/
lv_obj_t * aTemp = lv_arc_create(lv_screen_active()); lv_obj_t * aTemp = lv_arc_create(lv_screen_active());
@@ -100,39 +115,95 @@ void lv_create_main_gui(void) {
lv_obj_t * aHum = lv_arc_create(lv_screen_active()); lv_obj_t * aHum = lv_arc_create(lv_screen_active());
arcHum.arc = aHum; arcHum.arc = aHum;
buildarc(arcHum); buildarc(arcHum);
} }
void setValuetoArc(ARC arc, float value) { void setValuetoArc(ARC arc, float value) {
char s[60];
int r = arc.highvalue - arc.lowvalue; int r = arc.highvalue - arc.lowvalue;
int v = (value - arc.lowvalue) * 100 / r; int v = (value - arc.lowvalue) * 100 / r;
arc.value = v; arc.value = v;
sprintf(s,"high: %d, low: %d, value: %.1f, v: %d\n", arc.highvalue, arc.lowvalue, arc.value, v);
Serial.println(s);
lv_arc_set_value(arc.arc, arc.value); lv_arc_set_value(arc.arc, arc.value);
lv_obj_t * value_label = lv_obj_get_child(arc.arc, 1); lv_obj_t * value_label = lv_obj_get_child(arc.arc, 1);
char valuestr[10]; char valuestr[10];
sprintf(valuestr, arc.valformat, value); sprintf(valuestr, arc.valformat, value);
lv_label_set_text(value_label, valuestr); lv_label_set_text(value_label, valuestr);
sprintf(s,"name: %s, value: %.1f, green: %d, yellow: %d", arc.name, arc.value, arc.green, arc.yellow); if (value < arc.green)
Serial.println(s); {
static lv_style_t st_arc; lv_obj_set_style_arc_color(arc.arc, lv_palette_main(LV_PALETTE_GREEN), LV_PART_INDICATOR);
lv_style_init(&st_arc); }
if (arc.value < arc.green) { else if (value < arc.yellow)
lv_style_set_arc_color(&st_arc, lv_palette_main(LV_PALETTE_GREEN)); {
Serial.println("green"); lv_obj_set_style_arc_color(arc.arc, lv_palette_main(LV_PALETTE_YELLOW), LV_PART_INDICATOR);
} else if (arc.value < arc.yellow) { }
lv_style_set_arc_color(&st_arc, lv_palette_main(LV_PALETTE_YELLOW)); else
Serial.println("yellow"); {
} else { lv_obj_set_style_arc_color(arc.arc, lv_palette_main(LV_PALETTE_RED), LV_PART_INDICATOR);
lv_style_set_arc_color(&st_arc, lv_palette_main(LV_PALETTE_RED)); }
Serial.println("red");
} }
lv_obj_add_style(arc.arc, &st_arc, LV_PART_INDICATOR);
void drawChart() {
Serial.println("draw_chart ");
// Create a chart in lower third of screen
// lv_obj_t * chart;
chart = lv_chart_create(lv_screen_active());
lv_obj_set_size(chart, 300, 90);
lv_obj_align(chart, LV_ALIGN_BOTTOM_MID, 0, -5);
lv_chart_set_type(chart, LV_CHART_TYPE_LINE); /*Show lines and points too*/
lv_chart_set_point_count(chart, MAXREADINGS);
lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 10, 30);
lv_obj_set_style_size(chart, 0, 0, LV_PART_INDICATOR); // No points on line
/*Add data series*/
lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
lv_chart_set_point_count(chart, MAXREADINGS);
lv_chart_set_ext_y_array(chart, ser1, (lv_coord_t *)bmewert);
lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE), LV_CHART_AXIS_SECONDARY_Y);
lv_chart_set_point_count(chart, MAXREADINGS);
lv_chart_set_ext_y_array(chart, ser2, (lv_coord_t *)bmewert_hum);
lv_chart_refresh(chart); /*Required after direct set*/
// Axen
lv_obj_t * scale = lv_scale_create(chart);
lv_obj_set_size(scale, lv_pct(80), 70);
Serial.printf("lv_pct(80) %d\n", lv_pct(80));
lv_scale_set_mode(scale, LV_SCALE_MODE_VERTICAL_LEFT);
lv_obj_align(scale, LV_ALIGN_BOTTOM_LEFT, -215, 0);
lv_scale_set_label_show(scale, true);
lv_scale_set_total_tick_count(scale, 21);
lv_scale_set_major_tick_every(scale, 5);
lv_obj_set_style_length(scale, 3, LV_PART_ITEMS);
lv_obj_set_style_length(scale, 6, LV_PART_INDICATOR);
lv_obj_set_style_line_width(scale, 1, LV_PART_ITEMS);
lv_obj_set_style_line_width(scale, 1, LV_PART_INDICATOR);
lv_obj_set_style_line_width(scale, 1, LV_PART_MAIN);
lv_scale_set_range(scale, 10, 30);
static lv_style_t style_scale;
lv_style_init(&style_scale);
lv_style_set_text_font(&style_scale, &lv_font_montserrat_10);
lv_obj_add_style(scale, &style_scale, 0);
} }
void updatechartData(float value, float humvalue) {
int i = 0;
for (i = 0; i < MAXREADINGS - 1; i++) {
bmewert[i] = bmewert[i + 1];
bmewert_hum[i] = bmewert_hum[i + 1];
}
bmewert[MAXREADINGS - 1] = value;
bmewert_hum[MAXREADINGS - 1] = humvalue;
lv_chart_refresh(chart);
}

View File

@@ -40,7 +40,7 @@ typedef struct {
#ifdef MAIN #ifdef MAIN
ARC arcTemp = {NULL, "temp", 30, 0, 110, 30.0, "%.1f", "°C", 10, 30, 20, 25}; ARC arcTemp = {NULL, "temp", 30, 0, 110, 30.0, "%.1f", "°C", 10, 30, 20, 25};
ARC arcHum = {NULL, "humi", 180, 0, 110, 60.0, "%.0f", "%",0, 100, 50, 70}; ARC arcHum = {NULL, "humi", 180, 0, 110, 60.0, "%.0f", "%",0, 100, 65, 70};
#else #else
extern ARC arcTemp; extern ARC arcTemp;
extern ARC arcHum; extern ARC arcHum;
@@ -50,6 +50,9 @@ extern ARC arcHum;
void setup_grafik(void); void setup_grafik(void);
void lv_create_main_gui(void); void lv_create_main_gui(void);
void setValuetoArc(ARC arc, float value); void setValuetoArc(ARC arc, float value);
void drawChart();
void updatechartData(float value, float value_hum);
#endif #endif

View File

@@ -28,6 +28,11 @@
#include "grafik.h" #include "grafik.h"
#include "main.h" #include "main.h"
long repeatTime = 10000; // alle 1sec messen
long lastTime = 0;
BME280Data bmedata;
void setup() { void setup() {
// String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch(); // String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
@@ -47,15 +52,20 @@ void setup() {
Serial.println("Main GUI created"); Serial.println("Main GUI created");
setValuetoArc(arcTemp, 25.3); setValuetoArc(arcTemp, 25.3);
// setValuetoArc(arcHum, 45.0); setValuetoArc(arcHum, 45.0);
delay(1000); delay(1000);
// Start WiFi // Start WiFi
connectToWifi(); connectToWifi();
Serial.println("WiFi started");
// Start MQTT // Start MQTT
connectToMqtt(); connectToMqtt();
Serial.println("MQTT started"); Serial.println("MQTT started");
lastTime = millis();
drawChart();
/* /*
// Register print function for debugging // Register print function for debugging
lv_log_register_print_cb(log_print); lv_log_register_print_cb(log_print);
@@ -82,7 +92,13 @@ void setup() {
} }
void loop() { void loop() {
//readBME280(); if (millis() - lastTime > repeatTime) {
lastTime = millis();
bmedata = readBME280();
setValuetoArc(arcTemp, bmedata.temp);
setValuetoArc(arcHum, bmedata.hum);
updatechartData(bmedata.hum, bmedata.temp);
}
lv_task_handler(); // let the GUI do its work lv_task_handler(); // let the GUI do its work
lv_tick_inc(5); // tell LVGL how much time has passed lv_tick_inc(5); // tell LVGL how much time has passed
delay(5); // let this time pass delay(5); // let this time pass

View File

@@ -6,6 +6,8 @@ extern "C" {
#include "freertos/timers.h" #include "freertos/timers.h"
} }
#define MAXREADINGS 100
#ifdef MAIN #ifdef MAIN
TimerHandle_t mqttReconnectTimer; TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer; TimerHandle_t wifiReconnectTimer;

View File

@@ -7,8 +7,15 @@
void connectToWifi() { void connectToWifi() {
Serial.println("Connecting to Wi-Fi..."); Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.print("Connected to the WiFi with IP: ");
Serial.println(WiFi.localIP());
} }
/*
void WiFiEvent(WiFiEvent_t event) { void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event); Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) { switch(event) {
@@ -25,4 +32,4 @@ void WiFiEvent(WiFiEvent_t event) {
break; break;
} }
} }
*/

View File

@@ -13,7 +13,7 @@ const char* password = "RingNebelM57";
// #warning "You need to set the SSID and PASSWORD in weifi.h" // #warning "You need to set the SSID and PASSWORD in weifi.h"
void connectToWifi(); void connectToWifi();
void WiFiEvent(WiFiEvent_t event); // void WiFiEvent(WiFiEvent_t event);
#endif #endif