Skip to content

Commit 2d65122

Browse files
committed
ui: sensors page
1 parent 6fae950 commit 2d65122

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

examples/companion_radio/ui-new/UITask.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class HomeScreen : public UIScreen {
7575
RADIO,
7676
BLUETOOTH,
7777
ADVERT,
78+
SENSORS,
7879
SHUTDOWN,
7980
Count // keep as last
8081
};
@@ -113,9 +114,32 @@ class HomeScreen : public UIScreen {
113114
display.fillRect(iconX + 2, iconY + 2, fillWidth, iconHeight - 4);
114115
}
115116

117+
DynamicJsonDocument _sensors_doc;
118+
JsonArray _sensors_arr;
119+
bool scroll = false;
120+
int scroll_offset = 0;
121+
int next_sensors_refresh = 0;
122+
123+
void refresh_sensors() {
124+
CayenneLPP lpp(200);
125+
if (millis() > next_sensors_refresh) {
126+
lpp.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
127+
sensors.querySensors(0xFF, lpp);
128+
_sensors_arr.clear();
129+
lpp.decode(lpp.getBuffer(), lpp.getSize(), _sensors_arr);
130+
scroll = _sensors_arr.size() > UI_RECENT_LIST_SIZE; // there is a status line
131+
#if AUTO_OFF_MILLIS > 0
132+
next_sensors_refresh = millis() + 5000; // refresh sensor values every 5 sec
133+
#else
134+
next_sensors_refresh = millis() + 60000; // refresh sensor values every 1 min
135+
#endif
136+
}
137+
}
138+
116139
public:
117140
HomeScreen(UITask* task, mesh::RTCClock* rtc, SensorManager* sensors, NodePrefs* node_prefs)
118-
: _task(task), _rtc(rtc), _sensors(sensors), _node_prefs(node_prefs), _page(0), _shutdown_init(false) { }
141+
: _task(task), _rtc(rtc), _sensors(sensors), _node_prefs(node_prefs), _page(0),
142+
_shutdown_init(false), _sensors_doc(2048) { _sensors_arr=_sensors_doc.to<JsonArray>(); }
119143

120144
void poll() override {
121145
if (_shutdown_init && !_task->isButtonPressed()) { // must wait for USR button to be released
@@ -211,6 +235,34 @@ class HomeScreen : public UIScreen {
211235
display.setColor(DisplayDriver::GREEN);
212236
display.drawXbm((display.width() - 32) / 2, 18, advert_icon, 32, 32);
213237
display.drawTextCentered(display.width() / 2, 64 - 11, "advert: " PRESS_LABEL);
238+
} else if (_page == HomePage::SENSORS) {
239+
int y = 18;
240+
refresh_sensors();
241+
char buf[100];
242+
int s_size = _sensors_arr.size();
243+
for (int i = 0; i < (scroll?UI_RECENT_LIST_SIZE:s_size); i++) {
244+
JsonObject v = _sensors_arr[(i+scroll_offset)%s_size];
245+
display.setCursor(0, y);
246+
switch (v["type"].as<int>()) {
247+
case 136: // GPS
248+
sprintf(buf, "%.4f %.4f",
249+
v["value"]["latitude"].as<float>(),
250+
v["value"]["longitude"].as<float>());
251+
break;
252+
default: // will be a float for now
253+
sprintf(buf, "%.02f",
254+
v["value"].as<float>());
255+
}
256+
display.setCursor(0, y);
257+
display.print(v["name"].as<JsonString>().c_str());
258+
display.setCursor(
259+
display.width()-display.getTextWidth(buf)-1, y
260+
);
261+
display.print(buf);
262+
y = y + 12;
263+
}
264+
if (scroll) scroll_offset = (scroll_offset+1)%s_size;
265+
else scroll_offset = 0;
214266
} else if (_page == HomePage::SHUTDOWN) {
215267
display.setColor(DisplayDriver::GREEN);
216268
display.setTextSize(1);

0 commit comments

Comments
 (0)