adafruit / Adafruit_SSD1306

Arduino library for SSD1306 monochrome 128x64 and 128x32 OLEDs
http://www.adafruit.com/category/63_98
Other
1.74k stars 963 forks source link

Strange behaviour when using display.getTextBounds #234

Open crazynightgriffin opened 2 years ago

crazynightgriffin commented 2 years ago

Hi When using

If the text is over 10 characters it does not centre. code a simple example.

`#include

include

include

include <Fonts/FreeSans9pt7b.h>

include <Fonts/FreeSans12pt7b.h>

include <Fonts/FreeSerif12pt7b.h>

define SCREEN_WIDTH 128 // OLED display width, in pixels

define SCREEN_HEIGHT 64 // OLED display height, in pixels

define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {

Serial.begin(9600); // put your setup code here, to run once: display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.display(); delay(2000); }

void loop() { // put your main code here, to run repeatedly:

display.setTextSize(1);
display.clearDisplay();
display.setTextColor(WHITE);
drawCentreText("aaaaaaaaaaa", 70, 20);   //writes 2nd row of text
display.display();
delay(5000);

} // this is a helper loop and centers the text void drawCentreText(const char *buf, int x, int y) { int16_t x1; int16_t y1; uint16_t w; uint16_t h; display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string display.setCursor(x - w / 2, y); display.print(buf); }`

electroman00 commented 2 years ago

[OLED Display Module 12864] https://www.amazon.com/gp/product/B0837DLWVH/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

drawCentreText("1234567890123456789012",62 ,32 ); //Center on Display

will center 123456789012345678901

// 21 Characters w/setTextSize(1) // 10 Characters w/setTextSize(2) // 7 Characters w/setTextSize(3) // 5 Characters w/setTextSize(4)

display.getTextBounds(buf, 0, y, &x1, &y1, &w, &h); //calc width of new string

hth