@@ -870,11 +870,12 @@ void OLEDDisplay::cls() {
870
870
871
871
bool OLEDDisplay::setLogBuffer (uint16_t lines, uint16_t chars){
872
872
if (logBuffer != NULL ) free (logBuffer);
873
- uint16_t size = lines * chars;
873
+ uint16_t size = lines * ( chars + 1 ); // +1 is for \n
874
874
if (size > 0 ) {
875
875
this ->logBufferLine = 0 ; // Lines printed
876
876
this ->logBufferFilled = 0 ; // Nothing stored yet
877
877
this ->logBufferMaxLines = lines; // Lines max printable
878
+ this ->logBufferLineLen = chars; // Chars per line
878
879
this ->logBufferSize = size; // Total number of characters the buffer can hold
879
880
this ->logBuffer = (char *) malloc (size * sizeof (uint8_t ));
880
881
if (!this ->logBuffer ) {
@@ -893,12 +894,10 @@ size_t OLEDDisplay::write(uint8_t c) {
893
894
if (!logBufferSize) {
894
895
uint8_t textHeight = pgm_read_byte (fontData + HEIGHT_POS);
895
896
uint16_t lines = this ->displayHeight / textHeight;
896
- uint16_t chars = 3 * (this ->displayWidth / textHeight);
897
+ uint16_t chars = 5 * (this ->displayWidth / textHeight);
897
898
898
899
if (this ->displayHeight % textHeight)
899
900
lines++;
900
- if (this ->displayWidth % textHeight)
901
- chars++;
902
901
setLogBuffer (lines, chars);
903
902
}
904
903
@@ -910,50 +909,59 @@ size_t OLEDDisplay::write(uint8_t c) {
910
909
// drop unknown character
911
910
if (c == 0 ) return 1 ;
912
911
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 ;
915
914
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
927
918
uint16_t firstLineEnd = 0 ;
928
- for (uint16_t i= 0 ;i< this ->logBufferFilled ;i++) {
919
+ for (uint16_t i = 0 ; i < this ->logBufferFilled ; i++) {
929
920
if (this ->logBuffer [i] == 10 ){
930
921
// Include last char too
931
922
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 --;
932
929
break ;
933
930
}
934
931
}
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 ;
948
936
}
949
- write (c);
950
937
}
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
951
958
if (!this ->inhibitDrawLogBuffer ) {
952
959
clear ();
953
960
drawLogBuffer (0 , 0 );
954
961
display ();
955
962
}
956
- // We are always writing all uint8_t to the buffer
963
+
964
+ // We are always claim we printed it all
957
965
return 1 ;
958
966
}
959
967
0 commit comments