Skip to content

Instantly share code, notes, and snippets.

@mcorrigan
Last active September 13, 2023 03:45
Show Gist options
  • Save mcorrigan/f944aa3549354f285aefcb638b302f77 to your computer and use it in GitHub Desktop.
Save mcorrigan/f944aa3549354f285aefcb638b302f77 to your computer and use it in GitHub Desktop.
Position text oled SSD1306 Arduino
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