Adafruit OLED - how to center text.

Hello. I have 2,42" OLED display with Adafruit library. Its possible to set function like "setCenter" for X,Y coordinates? (One time I set X,Y , then only "setCenter" for X,Y.

For example, now I have 6 different text:
ONE
TWO
THREE
FOUR
FIVE
SIX

some of them have different number of lettetrs, and I need to put different X,Y coordinates (6 times) to have all text in one position, center.

Google "Adafruit getTextBounds()"

Write helper functions for Centered text, Right aligned text, ...
e.g. calculate width of string. calculate the correct Cursor position. set Cursor and print the string.

David.

"calculate the correct Cursor position. set Cursor and print the string."

but this I need make for all object six times. Not one time:

if (Input == 1)
  {
    display.setCursor(13, 12);
    display.print("ONE");
  }
  if (Input == 2)
  {
    display.setCursor(13, 12);
    display.print("TWO");
  }
  if (Input == 3)
  {
    display.setCursor(11, 12);
    display.print("THREE");
  }
  if (Input == 4)
  {
    display.setCursor(12, 12);
    display.print("FOUR");
  }
  if (Input == 5)
  {
    display.setCursor(12, 12);
    display.print("FIVE");
  }
  if (Input == 6)
  {
    display.setCursor(13, 12);
    display.print("SIX");
  }

Im looking for options like this:

Center = (12,10); //X,Y
if (Input == 1)
  {
    display.print.Center("ONE");
  }
  if (Input == 2)
  {
    display.print.Center("TWO");
  }
  if (Input == 3)
  {
    display.print.Center("THREE");
  }
  if (Input == 4)
  {
    display.print.Center("FOUR");
  }
  if (Input == 5)
  {
    display.print.Center("FIVE");
  }
  if (Input == 6)
  {
    display.print.Center("SIX");
  }

Did you look at getTextBounds() ?

Yes, it is fairly complicated. Which is why you write a helper function once and use many times.

A function written in C is simple.
You require a lot of study to learn how to write your own Center class in C++

You could also think about designing a neater code sequence.

But learning how to write a helper function is a useful exercise. You can re-use it in different projects.
Ask if you want some help.

David.

Yes, I have found some topics with this function, but as You wrote, its fairly complicated :smiley:

Now I dont know how to make correct function with this getTexBounds()

This function allow me to set maximum area where letters can be displayed with "center"?

void drawCentreString(const char *buf, int x, int y)
{
    int16_t x1, y1;
    uint16_t w, h;
    display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
    display.setCursor(x - w / 2, y);
    display.print(buf);
}

call with:

    if (Input == 1) drawCentreString("ONE", 13, 12);
    else if (Input == 2) drawCentreString("TWO", 13, 12);
    else if (Input == 3) drawCentreString("THREE", 13, 12);
    ...

Untested. Helper expects a char array. If you use String class, you would write the equivalent function but with a String argument. e.g.

void drawCentreString(const String &buf, int x, int y)
{
    int16_t x1, y1;
    uint16_t w, h;
    display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
    display.setCursor(x - w / 2, y);
    display.print(buf);
}
3 Likes

Thanks a lot :slight_smile:

One last question.

With this library: GitHub - jarzebski/Arduino-DS3231: DS3231 Real-Time-Clock

how can I use drawCentreString ??

28.10.2019 vs 2.2.19 (different width, DD.MM.YYYY_

same situation with time:
1:05 vs 11:05

Which display ?
What library(s) ?
Which Font ?

The GitHub examples appear to format the dates properly for the Serial Terminal.
i.e. use the library dateFormat()

Bog-standard Arduino print() makes horrible format e.g. 2.2.19

Centred text, Overwritten text, ... is very simple with standard Adafruit 7x5 Font

It gets fairly messy with variable-width Fonts that can only be printed in Transparent mode.
Yes, you can write helper functions once. Use many times in other projects.

David.