From b7de9736dc8a91e09c98f3759b95bf167c93ccf9 Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Fri, 10 Nov 2023 18:03:58 +0100
Subject: [PATCH] centered_text can now do two lines

---
 code/daisy-looper/helpers.h | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/code/daisy-looper/helpers.h b/code/daisy-looper/helpers.h
index 3148436..ef66325 100644
--- a/code/daisy-looper/helpers.h
+++ b/code/daisy-looper/helpers.h
@@ -5,13 +5,28 @@
 #include "Adafruit_GFX.h"
 extern Adafruit_SH1106G display;
 
-int centeredText(const char *buf, int x, int y, int color) {
+int centeredText(const char *buf, int x, int y, int color, int lineheight=8) {
   int16_t x1, y1;
   uint16_t w, h;
+  char *line_pointer = strchr(buf, '\n');
   display.setTextColor(color);
-  display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string
-  display.setCursor(x - (w / 2), y - (h / 2));
-  display.print(buf);
+  if (!line_pointer) {
+    display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string
+    display.setCursor(x - (w / 2), y - (h / 2));
+    display.print(buf);
+  }else {
+    char *tmp = strdup(buf);
+    char* d = strtok(tmp, "\n");
+    int line = 0;
+    while (d != NULL) {
+        display.getTextBounds(d, 0, 0, &x1, &y1, &w, &h); //calc width of new string
+        display.setCursor(x - (w / 2), y - (h / 2)-lineheight/2 + (line*lineheight));
+        display.print(d);
+        d = strtok(NULL, ",");
+        line++;
+    }
+    free(tmp);
+  }
   return w;
 }
 
-- 
GitLab