@@ -870,11 +870,12 @@ void OLEDDisplay::cls() {
870870
871871bool OLEDDisplay::setLogBuffer (uint16_t lines, uint16_t chars){
872872 if (logBuffer != NULL ) free (logBuffer);
873- uint16_t size = lines * chars;
873+ uint16_t size = lines * ( chars + 1 ); // +1 is for \n
874874 if (size > 0 ) {
875875 this ->logBufferLine = 0 ; // Lines printed
876876 this ->logBufferFilled = 0 ; // Nothing stored yet
877877 this ->logBufferMaxLines = lines; // Lines max printable
878+ this ->logBufferLineLen = chars; // Chars per line
878879 this ->logBufferSize = size; // Total number of characters the buffer can hold
879880 this ->logBuffer = (char *) malloc (size * sizeof (uint8_t ));
880881 if (!this ->logBuffer ) {
@@ -893,12 +894,10 @@ size_t OLEDDisplay::write(uint8_t c) {
893894 if (!logBufferSize) {
894895 uint8_t textHeight = pgm_read_byte (fontData + HEIGHT_POS);
895896 uint16_t lines = this ->displayHeight / textHeight;
896- uint16_t chars = 3 * (this ->displayWidth / textHeight);
897+ uint16_t chars = 5 * (this ->displayWidth / textHeight);
897898
898899 if (this ->displayHeight % textHeight)
899900 lines++;
900- if (this ->displayWidth % textHeight)
901- chars++;
902901 setLogBuffer (lines, chars);
903902 }
904903
@@ -910,50 +909,59 @@ size_t OLEDDisplay::write(uint8_t c) {
910909 // drop unknown character
911910 if (c == 0 ) return 1 ;
912911
913- bool maxLineNotReached = this ->logBufferLine < this ->logBufferMaxLines ;
914- bool bufferNotFull = this ->logBufferFilled < this ->logBufferSize ;
912+ bool maxLineReached = this ->logBufferLine >= this ->logBufferMaxLines ;
913+ bool bufferFull = this ->logBufferFilled >= this ->logBufferSize ;
915914
916- // Can we write to the buffer?
917- if (bufferNotFull && maxLineNotReached) {
918- this ->logBuffer [logBufferFilled] = c;
919- this ->logBufferFilled ++;
920- // Keep track of lines written
921- if (c == 10 ) this ->logBufferLine ++;
922- } else {
923- // Max line number is reached
924- if (!maxLineNotReached) this ->logBufferLine --;
925-
926- // Find the end of the first line
915+ // Can we write to the buffer? If not, make space.
916+ if (bufferFull || maxLineReached) {
917+ // See if we can chop off the first line
927918 uint16_t firstLineEnd = 0 ;
928- for (uint16_t i= 0 ;i< this ->logBufferFilled ;i++) {
919+ for (uint16_t i = 0 ; i < this ->logBufferFilled ; i++) {
929920 if (this ->logBuffer [i] == 10 ){
930921 // Include last char too
931922 firstLineEnd = i + 1 ;
923+ // Calculate the new logBufferFilled value
924+ this ->logBufferFilled = logBufferFilled - firstLineEnd;
925+ // Now move other lines to front of the buffer
926+ memcpy (this ->logBuffer , &this ->logBuffer [firstLineEnd], logBufferFilled);
927+ // And voila, buffer one line shorter
928+ this ->logBufferLine --;
932929 break ;
933930 }
934931 }
935- // If there was a line ending
936- if (firstLineEnd > 0 ) {
937- // Calculate the new logBufferFilled value
938- this ->logBufferFilled = logBufferFilled - firstLineEnd;
939- // Now we move the lines infront of the buffer
940- memcpy (this ->logBuffer , &this ->logBuffer [firstLineEnd], logBufferFilled);
941- } else {
942- // Let's reuse the buffer if it was full
943- if (!bufferNotFull) {
944- this ->logBufferFilled = 0 ;
945- }// else {
946- // Nothing to do here
947- // }
932+ // In we can't take off first line, we just empty the buffer
933+ if (!firstLineEnd) {
934+ this ->logBufferFilled = 0 ;
935+ this ->logBufferLine = 0 ;
948936 }
949- write (c);
950937 }
938+
939+ // So now we know for sure we have space in the buffer
940+
941+ // Find the length of the last line
942+ uint16_t lastLineLen= 0 ;
943+ for (uint16_t i = 0 ; i < this ->logBufferFilled ; i++) {
944+ lastLineLen++;
945+ if (this ->logBuffer [i] == 10 ) lastLineLen = 0 ;
946+ }
947+ // if last line is max length, ignore anything but linebreaks
948+ if (lastLineLen >= this ->logBufferLineLen ) {
949+ if (c != 10 ) return 1 ;
950+ }
951+
952+ // Write to buffer
953+ this ->logBuffer [this ->logBufferFilled ++] = c;
954+ // Keep track of lines written
955+ if (c == 10 ) this ->logBufferLine ++;
956+
957+ // Draw to screen unless we're writing a whole string at a time
951958 if (!this ->inhibitDrawLogBuffer ) {
952959 clear ();
953960 drawLogBuffer (0 , 0 );
954961 display ();
955962 }
956- // We are always writing all uint8_t to the buffer
963+
964+ // We are always claim we printed it all
957965 return 1 ;
958966}
959967
0 commit comments