Last active
September 13, 2023 03:45
-
-
Save mcorrigan/f944aa3549354f285aefcb638b302f77 to your computer and use it in GitHub Desktop.
Position text oled SSD1306 Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void displayPositionedText(char* buf, int textSize, Position vpos, Position hpos) { | |
display.setTextSize(textSize); | |
int16_t x1, y1; | |
uint16_t w, h; | |
display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); | |
int _vpos = 0, _hpos = 0; | |
switch (vpos) { | |
case CENTER: | |
_vpos = (SCREEN_HEIGHT - h) / 2; | |
break; | |
case TOP: | |
_vpos = 0; | |
break; | |
case BOTTOM: | |
_vpos = SCREEN_HEIGHT - h; | |
break; | |
default: | |
Serial.print(vpos); | |
Serial.println(" is not a valid Vertical Position"); | |
return; | |
} | |
switch (hpos) { | |
case CENTER: | |
_hpos = (SCREEN_WIDTH - w) / 2; | |
break; | |
case RIGHT: | |
_hpos = SCREEN_WIDTH - w; | |
break; | |
case LEFT: | |
_hpos = 0; | |
break; | |
default: | |
Serial.print(hpos); | |
Serial.println(" is not a valid Horizontal Position"); | |
return; | |
} | |
display.setCursor(_hpos, _vpos); | |
display.print(buf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment