From 18bfc2d81a45589f810ab7afc20a25810b8fa7e3 Mon Sep 17 00:00:00 2001 From: Florent de Lamotte Date: Tue, 30 Sep 2025 09:21:12 +0200 Subject: [PATCH] DisplayDriver: introduce drawTextRightAlign and drawTextLeftAlign --- examples/companion_radio/ui-new/UITask.cpp | 31 +++++++--------------- src/helpers/ui/DisplayDriver.h | 9 +++++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index d124a494..c3da0643 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -257,40 +257,27 @@ public: } else if (_page == HomePage::GPS) { LocationProvider* nmea = sensors.getLocationProvider(); int y = 18; - display.setCursor(0, y); - display.print(_task->getGPSState() ? "gps on" : "gps off"); + display.drawTextLeftAlign(0, y, _task->getGPSState() ? "gps on" : "gps off"); if (nmea == NULL) { y = y + 12; - display.setCursor(0, y); - display.print("Can't access GPS"); + display.drawTextLeftAlign(0, y, "Can't access GPS"); } else { char buf[50]; strcpy(buf, nmea->isValid()?"fix":"no fix"); - display.setCursor( - display.width()-display.getTextWidth(buf)-1, y); - display.print(buf); + display.drawTextRightAlign(display.width()-1, y, buf); y = y + 12; - display.setCursor(0,y); - display.print("sat"); + display.drawTextLeftAlign(0, y, "sat"); sprintf(buf, "%d", nmea->satellitesCount()); - display.setCursor( - display.width()-display.getTextWidth(buf)-1, y); - display.print(buf); + display.drawTextRightAlign(display.width()-1, y, buf); y = y + 12; - display.setCursor(0,y); - display.print("pos"); + display.drawTextLeftAlign(0, y, "pos"); sprintf(buf, "%.4f %.4f", nmea->getLatitude()/1000000., nmea->getLongitude()/1000000.); - display.setCursor( - display.width()-display.getTextWidth(buf)-1, y); - display.print(buf); + display.drawTextRightAlign(display.width()-1, y, buf); y = y + 12; - display.setCursor(0,y); - display.print("alt"); + display.drawTextLeftAlign(0, y, "alt"); sprintf(buf, "%.2f", nmea->getAltitude()/1000.); - display.setCursor( - display.width()-display.getTextWidth(buf)-1, y); - display.print(buf); + display.drawTextRightAlign(display.width()-1, y, buf); y = y + 12; } #endif diff --git a/src/helpers/ui/DisplayDriver.h b/src/helpers/ui/DisplayDriver.h index 32839edc..ec63c191 100644 --- a/src/helpers/ui/DisplayDriver.h +++ b/src/helpers/ui/DisplayDriver.h @@ -32,6 +32,15 @@ public: setCursor(mid_x - w/2, y); print(str); } + virtual void drawTextRightAlign(int x_anch, int y, const char* str) { + int w = getTextWidth(str); + setCursor(x_anch - w, y); + print(str); + } + virtual void drawTextLeftAlign(int x_anch, int y, const char* str) { + setCursor(x_anch, y); + print(str); + } // convert UTF-8 characters to displayable block characters for compatibility virtual void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) {