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
881881size_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) {
934953size_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__
944969int 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
0 commit comments