Просмотр исходного кода

DisplayDriver: introduce drawTextRightAlign and drawTextLeftAlign

Florent de Lamotte 10 месяцев назад
Родитель
Сommit
18bfc2d81a
2 измененных файлов с 18 добавлено и 22 удалено
  1. 9 22
      examples/companion_radio/ui-new/UITask.cpp
  2. 9 0
      src/helpers/ui/DisplayDriver.h

+ 9 - 22
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

+ 9 - 0
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) {