Skip to content

Commit

Permalink
Added drawLogBuffer(0,0) and display() at end of write() (#389)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ropg committed Mar 10, 2024
1 parent 7782bc3 commit 1da41d9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 71 deletions.
11 changes: 1 addition & 10 deletions examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ void drawCircle(void) {
}

void printBuffer(void) {
// Initialize the log buffer
// allocate memory to store 8 lines of text and 30 chars per line.
display.setLogBuffer(5, 30);

// Some test data
const char* test[] = {
"Hello",
Expand All @@ -189,15 +185,10 @@ void printBuffer(void) {
"scrolling is",
"working"
};

display.clear();
for (uint8_t i = 0; i < 11; i++) {
display.clear();
// Print to the screen
display.println(test[i]);
// Draw it to the internal screen buffer
display.drawLogBuffer(0, 0);
// Display it on the screen
display.display();
delay(500);
}
}
Expand Down
132 changes: 71 additions & 61 deletions src/OLEDDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
/*
* TODO Helmut
* - test/finish dislplay.printf() on mbed-os
* - Finish _putc with drawLogBuffer when running display
*/

#include "OLEDDisplay.h"
Expand All @@ -42,6 +41,7 @@ OLEDDisplay::OLEDDisplay() {
displayWidth = 128;
displayHeight = 64;
displayBufferSize = displayWidth * displayHeight / 8;
inhibitDrawLogBuffer = false;
color = WHITE;
geometry = GEOMETRY_128_64;
textAlignment = TEXT_ALIGN_LEFT;
Expand Down Expand Up @@ -879,53 +879,72 @@ bool OLEDDisplay::setLogBuffer(uint16_t lines, uint16_t chars){
}

size_t OLEDDisplay::write(uint8_t c) {
if (this->logBufferSize > 0) {
// Don't waste space on \r\n line endings, dropping \r
if (c == 13) return 1;

// convert UTF-8 character to font table index
c = (this->fontTableLookupFunction)(c);
// drop unknown character
if (c == 0) return 1;

bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;

// Can we write to the buffer?
if (bufferNotFull && maxLineNotReached) {
this->logBuffer[logBufferFilled] = c;
this->logBufferFilled++;
// Keep track of lines written
if (c == 10) this->logBufferLine++;
} else {
// Max line number is reached
if (!maxLineNotReached) this->logBufferLine--;

// Find the end of the first line
uint16_t firstLineEnd = 0;
for (uint16_t i=0;i<this->logBufferFilled;i++) {
if (this->logBuffer[i] == 10){
// Include last char too
firstLineEnd = i + 1;
break;
}
}
// If there was a line ending
if (firstLineEnd > 0) {
// Calculate the new logBufferFilled value
this->logBufferFilled = logBufferFilled - firstLineEnd;
// Now we move the lines infront of the buffer
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
} else {
// Let's reuse the buffer if it was full
if (!bufferNotFull) {
this->logBufferFilled = 0;
}// else {
// Nothing to do here
//}
if (!fontData)
return 1;

// Create a logBuffer if there isn't one
if (!logBufferSize) {
uint8_t textHeight = pgm_read_byte(fontData + HEIGHT_POS);
uint16_t lines = this->displayHeight / textHeight;
uint16_t chars = 3 * (this->displayWidth / textHeight);

if (this->displayHeight % textHeight)
lines++;
if (this->displayWidth % textHeight)
chars++;
setLogBuffer(lines, chars);
}

// Don't waste space on \r\n line endings, dropping \r
if (c == 13) return 1;

// convert UTF-8 character to font table index
c = (this->fontTableLookupFunction)(c);
// drop unknown character
if (c == 0) return 1;

bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;

// Can we write to the buffer?
if (bufferNotFull && maxLineNotReached) {
this->logBuffer[logBufferFilled] = c;
this->logBufferFilled++;
// Keep track of lines written
if (c == 10) this->logBufferLine++;
} else {
// Max line number is reached
if (!maxLineNotReached) this->logBufferLine--;

// Find the end of the first line
uint16_t firstLineEnd = 0;
for (uint16_t i=0;i<this->logBufferFilled;i++) {
if (this->logBuffer[i] == 10){
// Include last char too
firstLineEnd = i + 1;
break;
}
write(c);
}
// If there was a line ending
if (firstLineEnd > 0) {
// Calculate the new logBufferFilled value
this->logBufferFilled = logBufferFilled - firstLineEnd;
// Now we move the lines infront of the buffer
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
} else {
// Let's reuse the buffer if it was full
if (!bufferNotFull) {
this->logBufferFilled = 0;
}// else {
// Nothing to do here
//}
}
write(c);
}
if (!this->inhibitDrawLogBuffer) {
clear();
drawLogBuffer(0, 0);
display();
}
// We are always writing all uint8_t to the buffer
return 1;
Expand All @@ -934,29 +953,20 @@ size_t OLEDDisplay::write(uint8_t c) {
size_t OLEDDisplay::write(const char* str) {
if (str == NULL) return 0;
size_t length = strlen(str);
// If we write a string, only do the drawLogBuffer at the end, not every time we write a char
this->inhibitDrawLogBuffer = true;
for (size_t i = 0; i < length; i++) {
write(str[i]);
}
this->inhibitDrawLogBuffer = false;
clear();
drawLogBuffer(0, 0);
display();
return length;
}

#ifdef __MBED__
int OLEDDisplay::_putc(int c) {

if (!fontData)
return 1;
if (!logBufferSize) {
uint8_t textHeight = pgm_read_byte(fontData + HEIGHT_POS);
uint16_t lines = this->displayHeight / textHeight;
uint16_t chars = 2 * (this->displayWidth / textHeight);

if (this->displayHeight % textHeight)
lines++;
if (this->displayWidth % textHeight)
chars++;
setLogBuffer(lines, chars);
}

return this->write((uint8_t)c);
}
#endif
Expand Down
1 change: 1 addition & 0 deletions src/OLEDDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class OLEDDisplay : public Stream {
uint16_t logBufferLine;
uint16_t logBufferMaxLines;
char *logBuffer;
bool inhibitDrawLogBuffer;


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

0 comments on commit 1da41d9

Please sign in to comment.