MCPcopy Create free account
hub / github.com/PricelessToolkit/MailBoxGuard / drawString

Method drawString

Code/Arduino_libraries/SSD1306/OLEDDisplay.cpp:620–653  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

618
619
620uint16_t OLEDDisplay::drawString(int16_t xMove, int16_t yMove, const String &strUser) {
621 uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);
622
623 // char* text must be freed!
624 char* text = strdup(strUser.c_str());
625 if (!text) {
626 DEBUG_OLEDDISPLAY("[OLEDDISPLAY][drawString] Can't allocate char array.\n");
627 return 0;
628 }
629
630 uint16_t yOffset = 0;
631 // If the string should be centered vertically too
632 // we need to now how heigh the string is.
633 if (textAlignment == TEXT_ALIGN_CENTER_BOTH) {
634 uint16_t lb = 0;
635 // Find number of linebreaks in text
636 for (uint16_t i=0;text[i] != 0; i++) {
637 lb += (text[i] == 10);
638 }
639 // Calculate center
640 yOffset = (lb * lineHeight) / 2;
641 }
642
643 uint16_t charDrawn = 0;
644 uint16_t line = 0;
645 char* textPart = strtok(text,"\n");
646 while (textPart != NULL) {
647 uint16_t length = strlen(textPart);
648 charDrawn += drawStringInternal(xMove, yMove - yOffset + (line++) * lineHeight, textPart, length, getStringWidth(textPart, length, true), true);
649 textPart = strtok(NULL, "\n");
650 }
651 free(text);
652 return charDrawn;
653}
654
655void OLEDDisplay::drawStringf( int16_t x, int16_t y, char* buffer, String format, ... )
656{

Callers 1

LoadingDrawDefaultFunction · 0.45

Calls 2

pgm_read_byteFunction · 0.85
c_strMethod · 0.45

Tested by

no test coverage detected