-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_widgets.h
104 lines (93 loc) · 2.42 KB
/
data_widgets.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
#include <ArduinoJson.h>
#include <lvgl.h>
#include <HTTPClient.h>
class Subscriber {
public:
virtual void parseData(const char *payload) = 0;
};
template <typename T> class Widget : public Subscriber {
public:
virtual void parseData(const char *payload) {
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.println("parsing json failed");
} else {
data = doc["v"].as<T>();
redraw();
}
}
virtual void init(HTTPClient *client, const char* url){
client->begin(url);
int rc = client->GET();
Serial.print(url);
if(rc == 200){
parseData(client->getString().c_str());
Serial.println(" ok");
} else {
Serial.println(rc);
}
client->end();
}
virtual void redraw() = 0;
virtual void setData(const T d) {
data = d;
redraw();
}
virtual T getData(){ return data; }
virtual lv_obj_t *getLvObj() { return obj; }
protected:
T data;
lv_obj_t *obj;
};
//ACTIVE_PROFILE
class ActiveProfileWidget : public Widget<int> {
public:
ActiveProfileWidget(lv_obj_t *parent);
void redraw() { lv_label_set_text_fmt(obj, "%i", data); };
};
class TempWidget : public Widget<float> {
public:
TempWidget(lv_obj_t *parent);
void redraw() { lv_label_set_text_fmt(obj, "%.1f °C", data); };
};
class LedWidget : public Widget<bool> {
public:
LedWidget(lv_obj_t *, String, uint32_t);
void redraw() {
if(data){
lv_led_set_color(stateLed, lv_color_hex(activeColor));
lv_led_on(stateLed);
} else {
lv_led_set_color(stateLed, lv_color_hex(0x888888));
lv_led_off(stateLed);
}
};
virtual lv_obj_t *getLedObj() { return stateLed; }
private:
lv_obj_t *stateLed;
uint32_t activeColor;
};
class ImageWidget : public Widget<bool> {
public:
ImageWidget(lv_obj_t *, const lv_img_dsc_t*, uint32_t);
void redraw() {
if(data){
lv_obj_set_style_img_recolor(obj, lv_color_hex(activeColor), 0);
// lv_led_set_color(stateLed, lv_color_hex(activeColor));
// lv_led_on(stateLed);
} else {
lv_obj_set_style_img_recolor(obj, lv_palette_darken(LV_PALETTE_GREY, 3), 0);
}
};
virtual lv_obj_t *getLedObj() { return stateLed; }
private:
lv_obj_t *stateLed;
uint32_t activeColor;
};
class HumidityWidget : public Widget<float> {
public:
HumidityWidget(lv_obj_t *);
void redraw() { lv_label_set_text_fmt(obj, "%.0f %%", data); };
};