Skip to content

Commit 18bfc2d

Browse files
committed
DisplayDriver: introduce drawTextRightAlign and drawTextLeftAlign
1 parent db76351 commit 18bfc2d

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

examples/companion_radio/ui-new/UITask.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -257,40 +257,27 @@ class HomeScreen : public UIScreen {
257257
} else if (_page == HomePage::GPS) {
258258
LocationProvider* nmea = sensors.getLocationProvider();
259259
int y = 18;
260-
display.setCursor(0, y);
261-
display.print(_task->getGPSState() ? "gps on" : "gps off");
260+
display.drawTextLeftAlign(0, y, _task->getGPSState() ? "gps on" : "gps off");
262261
if (nmea == NULL) {
263262
y = y + 12;
264-
display.setCursor(0, y);
265-
display.print("Can't access GPS");
263+
display.drawTextLeftAlign(0, y, "Can't access GPS");
266264
} else {
267265
char buf[50];
268266
strcpy(buf, nmea->isValid()?"fix":"no fix");
269-
display.setCursor(
270-
display.width()-display.getTextWidth(buf)-1, y);
271-
display.print(buf);
267+
display.drawTextRightAlign(display.width()-1, y, buf);
272268
y = y + 12;
273-
display.setCursor(0,y);
274-
display.print("sat");
269+
display.drawTextLeftAlign(0, y, "sat");
275270
sprintf(buf, "%d", nmea->satellitesCount());
276-
display.setCursor(
277-
display.width()-display.getTextWidth(buf)-1, y);
278-
display.print(buf);
271+
display.drawTextRightAlign(display.width()-1, y, buf);
279272
y = y + 12;
280-
display.setCursor(0,y);
281-
display.print("pos");
273+
display.drawTextLeftAlign(0, y, "pos");
282274
sprintf(buf, "%.4f %.4f",
283275
nmea->getLatitude()/1000000., nmea->getLongitude()/1000000.);
284-
display.setCursor(
285-
display.width()-display.getTextWidth(buf)-1, y);
286-
display.print(buf);
276+
display.drawTextRightAlign(display.width()-1, y, buf);
287277
y = y + 12;
288-
display.setCursor(0,y);
289-
display.print("alt");
278+
display.drawTextLeftAlign(0, y, "alt");
290279
sprintf(buf, "%.2f", nmea->getAltitude()/1000.);
291-
display.setCursor(
292-
display.width()-display.getTextWidth(buf)-1, y);
293-
display.print(buf);
280+
display.drawTextRightAlign(display.width()-1, y, buf);
294281
y = y + 12;
295282
}
296283
#endif

src/helpers/ui/DisplayDriver.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ class DisplayDriver {
3232
setCursor(mid_x - w/2, y);
3333
print(str);
3434
}
35+
virtual void drawTextRightAlign(int x_anch, int y, const char* str) {
36+
int w = getTextWidth(str);
37+
setCursor(x_anch - w, y);
38+
print(str);
39+
}
40+
virtual void drawTextLeftAlign(int x_anch, int y, const char* str) {
41+
setCursor(x_anch, y);
42+
print(str);
43+
}
3544

3645
// convert UTF-8 characters to displayable block characters for compatibility
3746
virtual void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) {

0 commit comments

Comments
 (0)