Skip to content

Commit cee5566

Browse files
committed
Added drawLogBuffer(0,0) and display() at end of write()
Now print, println and printf "just work", while display not refreshed every character when a string of characters is written, only at the end.
1 parent 53a688e commit cee5566

File tree

2 files changed

+70
-61
lines changed

2 files changed

+70
-61
lines changed

src/OLEDDisplay.cpp

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
/*
3333
* TODO Helmut
3434
* - test/finish dislplay.printf() on mbed-os
35-
* - Finish _putc with drawLogBuffer when running display
3635
*/
3736

3837
#include "OLEDDisplay.h"
@@ -42,6 +41,7 @@ OLEDDisplay::OLEDDisplay() {
4241
displayWidth = 128;
4342
displayHeight = 64;
4443
displayBufferSize = displayWidth * displayHeight / 8;
44+
inhibitDrawLogBuffer = false;
4545
color = WHITE;
4646
geometry = GEOMETRY_128_64;
4747
textAlignment = TEXT_ALIGN_LEFT;
@@ -877,53 +877,71 @@ bool OLEDDisplay::setLogBuffer(uint16_t lines, uint16_t chars){
877877
}
878878

879879
size_t OLEDDisplay::write(uint8_t c) {
880-
if (this->logBufferSize > 0) {
881-
// Don't waste space on \r\n line endings, dropping \r
882-
if (c == 13) return 1;
883-
884-
// convert UTF-8 character to font table index
885-
c = (this->fontTableLookupFunction)(c);
886-
// drop unknown character
887-
if (c == 0) return 1;
888-
889-
bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
890-
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;
891-
892-
// Can we write to the buffer?
893-
if (bufferNotFull && maxLineNotReached) {
894-
this->logBuffer[logBufferFilled] = c;
895-
this->logBufferFilled++;
896-
// Keep track of lines written
897-
if (c == 10) this->logBufferLine++;
898-
} else {
899-
// Max line number is reached
900-
if (!maxLineNotReached) this->logBufferLine--;
901-
902-
// Find the end of the first line
903-
uint16_t firstLineEnd = 0;
904-
for (uint16_t i=0;i<this->logBufferFilled;i++) {
905-
if (this->logBuffer[i] == 10){
906-
// Include last char too
907-
firstLineEnd = i + 1;
908-
break;
909-
}
910-
}
911-
// If there was a line ending
912-
if (firstLineEnd > 0) {
913-
// Calculate the new logBufferFilled value
914-
this->logBufferFilled = logBufferFilled - firstLineEnd;
915-
// Now we move the lines infront of the buffer
916-
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
917-
} else {
918-
// Let's reuse the buffer if it was full
919-
if (!bufferNotFull) {
920-
this->logBufferFilled = 0;
921-
}// else {
922-
// Nothing to do here
923-
//}
880+
if (!fontData)
881+
return 1;
882+
883+
// Create a logBuffer if there isn't one
884+
if (!logBufferSize) {
885+
uint8_t textHeight = pgm_read_byte(fontData + HEIGHT_POS);
886+
uint16_t lines = this->displayHeight / textHeight;
887+
uint16_t chars = 2 * (this->displayWidth / textHeight);
888+
889+
if (this->displayHeight % textHeight)
890+
lines++;
891+
if (this->displayWidth % textHeight)
892+
chars++;
893+
setLogBuffer(lines, chars);
894+
}
895+
896+
// Don't waste space on \r\n line endings, dropping \r
897+
if (c == 13) return 1;
898+
899+
// convert UTF-8 character to font table index
900+
c = (this->fontTableLookupFunction)(c);
901+
// drop unknown character
902+
if (c == 0) return 1;
903+
904+
bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
905+
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;
906+
907+
// Can we write to the buffer?
908+
if (bufferNotFull && maxLineNotReached) {
909+
this->logBuffer[logBufferFilled] = c;
910+
this->logBufferFilled++;
911+
// Keep track of lines written
912+
if (c == 10) this->logBufferLine++;
913+
} else {
914+
// Max line number is reached
915+
if (!maxLineNotReached) this->logBufferLine--;
916+
917+
// Find the end of the first line
918+
uint16_t firstLineEnd = 0;
919+
for (uint16_t i=0;i<this->logBufferFilled;i++) {
920+
if (this->logBuffer[i] == 10){
921+
// Include last char too
922+
firstLineEnd = i + 1;
923+
break;
924924
}
925-
write(c);
926925
}
926+
// If there was a line ending
927+
if (firstLineEnd > 0) {
928+
// Calculate the new logBufferFilled value
929+
this->logBufferFilled = logBufferFilled - firstLineEnd;
930+
// Now we move the lines infront of the buffer
931+
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
932+
} else {
933+
// Let's reuse the buffer if it was full
934+
if (!bufferNotFull) {
935+
this->logBufferFilled = 0;
936+
}// else {
937+
// Nothing to do here
938+
//}
939+
}
940+
write(c);
941+
}
942+
if (!this->inhibitDrawLogBuffer) {
943+
drawLogBuffer(0, 0);
944+
display();
927945
}
928946
// We are always writing all uint8_t to the buffer
929947
return 1;
@@ -932,29 +950,19 @@ size_t OLEDDisplay::write(uint8_t c) {
932950
size_t OLEDDisplay::write(const char* str) {
933951
if (str == NULL) return 0;
934952
size_t length = strlen(str);
953+
// If we write a string, only do the drawLogBuffer at the end, not every time we write a char
954+
this->inhibitDrawLogBuffer = true;
935955
for (size_t i = 0; i < length; i++) {
936956
write(str[i]);
937957
}
958+
this->inhibitDrawLogBuffer = false;
959+
drawLogBuffer(0, 0);
960+
display();
938961
return length;
939962
}
940963

941964
#ifdef __MBED__
942965
int OLEDDisplay::_putc(int c) {
943-
944-
if (!fontData)
945-
return 1;
946-
if (!logBufferSize) {
947-
uint8_t textHeight = pgm_read_byte(fontData + HEIGHT_POS);
948-
uint16_t lines = this->displayHeight / textHeight;
949-
uint16_t chars = 2 * (this->displayWidth / textHeight);
950-
951-
if (this->displayHeight % textHeight)
952-
lines++;
953-
if (this->displayWidth % textHeight)
954-
chars++;
955-
setLogBuffer(lines, chars);
956-
}
957-
958966
return this->write((uint8_t)c);
959967
}
960968
#endif

src/OLEDDisplay.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ class OLEDDisplay : public Stream {
364364
uint16_t logBufferLine;
365365
uint16_t logBufferMaxLines;
366366
char *logBuffer;
367+
bool inhibitDrawLogBuffer;
367368

368369

369370
// the header size of the buffer used, e.g. for the SPI command header

0 commit comments

Comments
 (0)