diff --git a/examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino b/examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino index d684e7d..eb1d30f 100644 --- a/examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino +++ b/examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino @@ -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", @@ -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); } } diff --git a/src/OLEDDisplay.cpp b/src/OLEDDisplay.cpp index 6c4f332..9af0f9c 100644 --- a/src/OLEDDisplay.cpp +++ b/src/OLEDDisplay.cpp @@ -32,7 +32,6 @@ /* * TODO Helmut * - test/finish dislplay.printf() on mbed-os - * - Finish _putc with drawLogBuffer when running display */ #include "OLEDDisplay.h" @@ -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; @@ -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;ilogBufferFilled;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;ilogBufferFilled;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; @@ -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 diff --git a/src/OLEDDisplay.h b/src/OLEDDisplay.h index 12cc0c3..b7c302d 100644 --- a/src/OLEDDisplay.h +++ b/src/OLEDDisplay.h @@ -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