32
32
/*
33
33
* TODO Helmut
34
34
* - test/finish dislplay.printf() on mbed-os
35
- * - Finish _putc with drawLogBuffer when running display
36
35
*/
37
36
38
37
#include " OLEDDisplay.h"
@@ -42,6 +41,7 @@ OLEDDisplay::OLEDDisplay() {
42
41
displayWidth = 128 ;
43
42
displayHeight = 64 ;
44
43
displayBufferSize = displayWidth * displayHeight / 8 ;
44
+ inhibitDrawLogBuffer = false ;
45
45
color = WHITE;
46
46
geometry = GEOMETRY_128_64;
47
47
textAlignment = TEXT_ALIGN_LEFT;
@@ -877,53 +877,71 @@ bool OLEDDisplay::setLogBuffer(uint16_t lines, uint16_t chars){
877
877
}
878
878
879
879
size_t OLEDDisplay::write (uint8_t c) {
880
- if (this -> logBufferSize > 0 ) {
881
- // Don't waste space on \r\n line endings, dropping \r
882
- if (c == 13 ) return 1 ;
883
-
884
- // convert UTF-8 character to font table index
885
- c = ( this -> fontTableLookupFunction )(c );
886
- // drop unknown character
887
- if (c == 0 ) return 1 ;
888
-
889
- bool maxLineNotReached = this ->logBufferLine < this -> logBufferMaxLines ;
890
- bool bufferNotFull = this -> logBufferFilled < this -> logBufferSize ;
891
-
892
- // Can we write to the buffer?
893
- if (bufferNotFull && maxLineNotReached) {
894
- this -> logBuffer [logBufferFilled] = c;
895
- this -> logBufferFilled ++;
896
- // Keep track of lines written
897
- if (c == 10 ) this -> logBufferLine ++ ;
898
- } else {
899
- // Max line number is reached
900
- if (!maxLineNotReached) this ->logBufferLine -- ;
901
-
902
- // Find the end of the first line
903
- uint16_t firstLineEnd = 0 ;
904
- for ( uint16_t i= 0 ;i< this ->logBufferFilled ;i++) {
905
- if ( this ->logBuffer [i] == 10 ){
906
- // Include last char too
907
- firstLineEnd = i + 1 ;
908
- break ;
909
- }
910
- }
911
- // If there was a line ending
912
- if (firstLineEnd > 0 ) {
913
- // Calculate the new logBufferFilled value
914
- this -> logBufferFilled = logBufferFilled - firstLineEnd;
915
- // Now we move the lines infront of the buffer
916
- memcpy ( this -> logBuffer , & this -> logBuffer [firstLineEnd], logBufferFilled);
917
- } else {
918
- // Let's reuse the buffer if it was full
919
- if (!bufferNotFull ) {
920
- this ->logBufferFilled = 0 ;
921
- } // else {
922
- // Nothing to do here
923
- // }
880
+ if (!fontData)
881
+ return 1 ;
882
+
883
+ // Create a logBuffer if there isn't one
884
+ if (!logBufferSize) {
885
+ uint8_t textHeight = pgm_read_byte (fontData + HEIGHT_POS );
886
+ uint16_t lines = this -> displayHeight / textHeight;
887
+ uint16_t chars = 2 * ( this -> displayWidth / textHeight) ;
888
+
889
+ if ( this ->displayHeight % textHeight)
890
+ lines++ ;
891
+ if ( this -> displayWidth % textHeight)
892
+ chars++;
893
+ setLogBuffer (lines, chars);
894
+ }
895
+
896
+ // Don't waste space on \r\n line endings, dropping \r
897
+ if (c == 13 ) return 1 ;
898
+
899
+ // convert UTF-8 character to font table index
900
+ c = ( this ->fontTableLookupFunction )(c) ;
901
+ // drop unknown character
902
+ if (c == 0 ) return 1 ;
903
+
904
+ bool maxLineNotReached = this -> logBufferLine < this ->logBufferMaxLines ;
905
+ bool bufferNotFull = this ->logBufferFilled < this -> logBufferSize ;
906
+
907
+ // Can we write to the buffer?
908
+ if (bufferNotFull && maxLineNotReached) {
909
+ this -> logBuffer [logBufferFilled] = c;
910
+ this -> logBufferFilled ++;
911
+ // Keep track of lines written
912
+ if (c == 10 ) this -> logBufferLine ++;
913
+ } else {
914
+ // Max line number is reached
915
+ if (!maxLineNotReached) this -> logBufferLine --;
916
+
917
+ // Find the end of the first line
918
+ uint16_t firstLineEnd = 0 ;
919
+ for ( uint16_t i= 0 ;i< this -> logBufferFilled ;i++ ) {
920
+ if ( this ->logBuffer [i] == 10 ){
921
+ // Include last char too
922
+ firstLineEnd = i + 1 ;
923
+ break ;
924
924
}
925
- write (c);
926
925
}
926
+ // If there was a line ending
927
+ if (firstLineEnd > 0 ) {
928
+ // Calculate the new logBufferFilled value
929
+ this ->logBufferFilled = logBufferFilled - firstLineEnd;
930
+ // Now we move the lines infront of the buffer
931
+ memcpy (this ->logBuffer , &this ->logBuffer [firstLineEnd], logBufferFilled);
932
+ } else {
933
+ // Let's reuse the buffer if it was full
934
+ if (!bufferNotFull) {
935
+ this ->logBufferFilled = 0 ;
936
+ }// else {
937
+ // Nothing to do here
938
+ // }
939
+ }
940
+ write (c);
941
+ }
942
+ if (!this ->inhibitDrawLogBuffer ) {
943
+ drawLogBuffer (0 , 0 );
944
+ display ();
927
945
}
928
946
// We are always writing all uint8_t to the buffer
929
947
return 1 ;
@@ -932,29 +950,19 @@ size_t OLEDDisplay::write(uint8_t c) {
932
950
size_t OLEDDisplay::write (const char * str) {
933
951
if (str == NULL ) return 0 ;
934
952
size_t length = strlen (str);
953
+ // If we write a string, only do the drawLogBuffer at the end, not every time we write a char
954
+ this ->inhibitDrawLogBuffer = true ;
935
955
for (size_t i = 0 ; i < length; i++) {
936
956
write (str[i]);
937
957
}
958
+ this ->inhibitDrawLogBuffer = false ;
959
+ drawLogBuffer (0 , 0 );
960
+ display ();
938
961
return length;
939
962
}
940
963
941
964
#ifdef __MBED__
942
965
int OLEDDisplay::_putc (int c) {
943
-
944
- if (!fontData)
945
- return 1 ;
946
- if (!logBufferSize) {
947
- uint8_t textHeight = pgm_read_byte (fontData + HEIGHT_POS);
948
- uint16_t lines = this ->displayHeight / textHeight;
949
- uint16_t chars = 2 * (this ->displayWidth / textHeight);
950
-
951
- if (this ->displayHeight % textHeight)
952
- lines++;
953
- if (this ->displayWidth % textHeight)
954
- chars++;
955
- setLogBuffer (lines, chars);
956
- }
957
-
958
966
return this ->write ((uint8_t )c);
959
967
}
960
968
#endif
0 commit comments