Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added drawLogBuffer(0,0) and display() at end of write() #389

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -877,53 +877,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 @@ -932,29 +951,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
Loading