Skip to content

Commit 1da41d9

Browse files
authored
Added drawLogBuffer(0,0) and display() at end of write() (#389)
* 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. * The assumption that chars are at most twice as high as they're wide was too optimistic * Remove the now unnecessary commands from DrawingDemo
1 parent 7782bc3 commit 1da41d9

File tree

3 files changed

+73
-71
lines changed

3 files changed

+73
-71
lines changed

examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ void drawCircle(void) {
171171
}
172172

173173
void printBuffer(void) {
174-
// Initialize the log buffer
175-
// allocate memory to store 8 lines of text and 30 chars per line.
176-
display.setLogBuffer(5, 30);
177-
178174
// Some test data
179175
const char* test[] = {
180176
"Hello",
@@ -189,15 +185,10 @@ void printBuffer(void) {
189185
"scrolling is",
190186
"working"
191187
};
192-
188+
display.clear();
193189
for (uint8_t i = 0; i < 11; i++) {
194-
display.clear();
195190
// Print to the screen
196191
display.println(test[i]);
197-
// Draw it to the internal screen buffer
198-
display.drawLogBuffer(0, 0);
199-
// Display it on the screen
200-
display.display();
201192
delay(500);
202193
}
203194
}

src/OLEDDisplay.cpp

Lines changed: 71 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;
@@ -879,53 +879,72 @@ bool OLEDDisplay::setLogBuffer(uint16_t lines, uint16_t chars){
879879
}
880880

881881
size_t OLEDDisplay::write(uint8_t c) {
882-
if (this->logBufferSize > 0) {
883-
// Don't waste space on \r\n line endings, dropping \r
884-
if (c == 13) return 1;
885-
886-
// convert UTF-8 character to font table index
887-
c = (this->fontTableLookupFunction)(c);
888-
// drop unknown character
889-
if (c == 0) return 1;
890-
891-
bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
892-
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;
893-
894-
// Can we write to the buffer?
895-
if (bufferNotFull && maxLineNotReached) {
896-
this->logBuffer[logBufferFilled] = c;
897-
this->logBufferFilled++;
898-
// Keep track of lines written
899-
if (c == 10) this->logBufferLine++;
900-
} else {
901-
// Max line number is reached
902-
if (!maxLineNotReached) this->logBufferLine--;
903-
904-
// Find the end of the first line
905-
uint16_t firstLineEnd = 0;
906-
for (uint16_t i=0;i<this->logBufferFilled;i++) {
907-
if (this->logBuffer[i] == 10){
908-
// Include last char too
909-
firstLineEnd = i + 1;
910-
break;
911-
}
912-
}
913-
// If there was a line ending
914-
if (firstLineEnd > 0) {
915-
// Calculate the new logBufferFilled value
916-
this->logBufferFilled = logBufferFilled - firstLineEnd;
917-
// Now we move the lines infront of the buffer
918-
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
919-
} else {
920-
// Let's reuse the buffer if it was full
921-
if (!bufferNotFull) {
922-
this->logBufferFilled = 0;
923-
}// else {
924-
// Nothing to do here
925-
//}
882+
if (!fontData)
883+
return 1;
884+
885+
// Create a logBuffer if there isn't one
886+
if (!logBufferSize) {
887+
uint8_t textHeight = pgm_read_byte(fontData + HEIGHT_POS);
888+
uint16_t lines = this->displayHeight / textHeight;
889+
uint16_t chars = 3 * (this->displayWidth / textHeight);
890+
891+
if (this->displayHeight % textHeight)
892+
lines++;
893+
if (this->displayWidth % textHeight)
894+
chars++;
895+
setLogBuffer(lines, chars);
896+
}
897+
898+
// Don't waste space on \r\n line endings, dropping \r
899+
if (c == 13) return 1;
900+
901+
// convert UTF-8 character to font table index
902+
c = (this->fontTableLookupFunction)(c);
903+
// drop unknown character
904+
if (c == 0) return 1;
905+
906+
bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
907+
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;
908+
909+
// Can we write to the buffer?
910+
if (bufferNotFull && maxLineNotReached) {
911+
this->logBuffer[logBufferFilled] = c;
912+
this->logBufferFilled++;
913+
// Keep track of lines written
914+
if (c == 10) this->logBufferLine++;
915+
} else {
916+
// Max line number is reached
917+
if (!maxLineNotReached) this->logBufferLine--;
918+
919+
// Find the end of the first line
920+
uint16_t firstLineEnd = 0;
921+
for (uint16_t i=0;i<this->logBufferFilled;i++) {
922+
if (this->logBuffer[i] == 10){
923+
// Include last char too
924+
firstLineEnd = i + 1;
925+
break;
926926
}
927-
write(c);
928927
}
928+
// If there was a line ending
929+
if (firstLineEnd > 0) {
930+
// Calculate the new logBufferFilled value
931+
this->logBufferFilled = logBufferFilled - firstLineEnd;
932+
// Now we move the lines infront of the buffer
933+
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
934+
} else {
935+
// Let's reuse the buffer if it was full
936+
if (!bufferNotFull) {
937+
this->logBufferFilled = 0;
938+
}// else {
939+
// Nothing to do here
940+
//}
941+
}
942+
write(c);
943+
}
944+
if (!this->inhibitDrawLogBuffer) {
945+
clear();
946+
drawLogBuffer(0, 0);
947+
display();
929948
}
930949
// We are always writing all uint8_t to the buffer
931950
return 1;
@@ -934,29 +953,20 @@ size_t OLEDDisplay::write(uint8_t c) {
934953
size_t OLEDDisplay::write(const char* str) {
935954
if (str == NULL) return 0;
936955
size_t length = strlen(str);
956+
// If we write a string, only do the drawLogBuffer at the end, not every time we write a char
957+
this->inhibitDrawLogBuffer = true;
937958
for (size_t i = 0; i < length; i++) {
938959
write(str[i]);
939960
}
961+
this->inhibitDrawLogBuffer = false;
962+
clear();
963+
drawLogBuffer(0, 0);
964+
display();
940965
return length;
941966
}
942967

943968
#ifdef __MBED__
944969
int OLEDDisplay::_putc(int c) {
945-
946-
if (!fontData)
947-
return 1;
948-
if (!logBufferSize) {
949-
uint8_t textHeight = pgm_read_byte(fontData + HEIGHT_POS);
950-
uint16_t lines = this->displayHeight / textHeight;
951-
uint16_t chars = 2 * (this->displayWidth / textHeight);
952-
953-
if (this->displayHeight % textHeight)
954-
lines++;
955-
if (this->displayWidth % textHeight)
956-
chars++;
957-
setLogBuffer(lines, chars);
958-
}
959-
960970
return this->write((uint8_t)c);
961971
}
962972
#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)