Skip to content
Snippets Groups Projects
Commit b7de9736 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

centered_text can now do two lines

parent ee72acc1
No related branches found
No related tags found
No related merge requests found
...@@ -5,13 +5,28 @@ ...@@ -5,13 +5,28 @@
#include "Adafruit_GFX.h" #include "Adafruit_GFX.h"
extern Adafruit_SH1106G display; 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; int16_t x1, y1;
uint16_t w, h; uint16_t w, h;
char *line_pointer = strchr(buf, '\n');
display.setTextColor(color); display.setTextColor(color);
if (!line_pointer) {
display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string
display.setCursor(x - (w / 2), y - (h / 2)); display.setCursor(x - (w / 2), y - (h / 2));
display.print(buf); 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; return w;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment