mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
perf: avoid heap allocation in OLED drawString
This commit is contained in:
parent
c2aae65e79
commit
5e11362f1d
1 changed files with 22 additions and 11 deletions
|
|
@ -619,11 +619,8 @@ uint16_t OLEDDisplay::drawStringInternal(int16_t xMove, int16_t yMove, const cha
|
||||||
|
|
||||||
uint16_t OLEDDisplay::drawString(int16_t xMove, int16_t yMove, const String &strUser) {
|
uint16_t OLEDDisplay::drawString(int16_t xMove, int16_t yMove, const String &strUser) {
|
||||||
uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);
|
uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);
|
||||||
|
const char* text = strUser.c_str();
|
||||||
// char* text must be freed!
|
if (text == nullptr) {
|
||||||
char* text = strdup(strUser.c_str());
|
|
||||||
if (!text) {
|
|
||||||
DEBUG_OLEDDISPLAY("[OLEDDISPLAY][drawString] Can't allocate char array.\n");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -642,13 +639,27 @@ uint16_t OLEDDisplay::drawString(int16_t xMove, int16_t yMove, const String &str
|
||||||
|
|
||||||
uint16_t charDrawn = 0;
|
uint16_t charDrawn = 0;
|
||||||
uint16_t line = 0;
|
uint16_t line = 0;
|
||||||
char* textPart = strtok(text,"\n");
|
const char* lineStart = text;
|
||||||
while (textPart != NULL) {
|
while (true) {
|
||||||
uint16_t length = strlen(textPart);
|
const char* lineEnd = lineStart;
|
||||||
charDrawn += drawStringInternal(xMove, yMove - yOffset + (line++) * lineHeight, textPart, length, getStringWidth(textPart, length, true), true);
|
while (*lineEnd != 0 && *lineEnd != '\n') {
|
||||||
textPart = strtok(NULL, "\n");
|
lineEnd++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lineEnd == lineStart && *lineEnd == '\n') {
|
||||||
|
lineStart = lineEnd + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t length = lineEnd - lineStart;
|
||||||
|
charDrawn += drawStringInternal(xMove, yMove - yOffset + (line++) * lineHeight, lineStart, length,
|
||||||
|
getStringWidth(lineStart, length, true), true);
|
||||||
|
|
||||||
|
if (*lineEnd == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lineStart = lineEnd + 1;
|
||||||
}
|
}
|
||||||
free(text);
|
|
||||||
return charDrawn;
|
return charDrawn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue