@@ -752,6 +752,8 @@ void OLEDDisplay::setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment) {
752
752
753
753
void OLEDDisplay::setFont (const uint8_t *fontData) {
754
754
this ->fontData = fontData;
755
+ // New font, so must recalculate. Whatever was there is gone at next print.
756
+ setLogBuffer ();
755
757
}
756
758
757
759
void OLEDDisplay::displayOn (void ) {
@@ -820,6 +822,10 @@ void OLEDDisplay::clear(void) {
820
822
}
821
823
822
824
void OLEDDisplay::drawLogBuffer (uint16_t xMove, uint16_t yMove) {
825
+ Serial.println (" [deprecated] Print functionality now handles buffer management automatically. This is a no-op." );
826
+ }
827
+
828
+ void OLEDDisplay::drawLogBuffer () {
823
829
uint16_t lineHeight = pgm_read_byte (fontData + HEIGHT_POS);
824
830
// Always align left
825
831
setTextAlignment (TEXT_ALIGN_LEFT);
@@ -832,15 +838,15 @@ void OLEDDisplay::drawLogBuffer(uint16_t xMove, uint16_t yMove) {
832
838
// If the lineHeight and the display height are not cleanly divisible, we need
833
839
// to start off the screen when the buffer has logBufferMaxLines so that the
834
840
// first line, and not the last line, drops off.
835
- uint16_t shiftUp = (this ->logBufferLine == this ->logBufferMaxLines ) ? displayHeight % lineHeight : 0 ;
841
+ uint16_t shiftUp = (this ->logBufferLine == this ->logBufferMaxLines ) ? (lineHeight - ( displayHeight % lineHeight)) % lineHeight : 0 ;
836
842
837
843
for (uint16_t i=0 ;i<this ->logBufferFilled ;i++){
838
844
length++;
839
845
// Everytime we have a \n print
840
846
if (this ->logBuffer [i] == 10 ) {
841
847
// Draw string on line `line` from lastPos to length
842
848
// Passing 0 as the lenght because we are in TEXT_ALIGN_LEFT
843
- drawStringInternal (xMove, yMove - shiftUp + (line++) * lineHeight, &this ->logBuffer [lastPos], length, 0 , false );
849
+ drawStringInternal (0 , 0 - shiftUp + (line++) * lineHeight, &this ->logBuffer [lastPos], length, 0 , false );
844
850
// Remember last pos
845
851
lastPos = i;
846
852
// Reset length
@@ -849,7 +855,7 @@ void OLEDDisplay::drawLogBuffer(uint16_t xMove, uint16_t yMove) {
849
855
}
850
856
// Draw the remaining string
851
857
if (length > 0 ) {
852
- drawStringInternal (xMove, yMove - shiftUp + line * lineHeight, &this ->logBuffer [lastPos], length, 0 , false );
858
+ drawStringInternal (0 , 0 - shiftUp + line * lineHeight, &this ->logBuffer [lastPos], length, 0 , false );
853
859
}
854
860
}
855
861
@@ -868,21 +874,44 @@ void OLEDDisplay::cls() {
868
874
display ();
869
875
}
870
876
871
- bool OLEDDisplay::setLogBuffer (uint16_t lines, uint16_t chars){
872
- if (logBuffer != NULL ) free (logBuffer);
877
+ bool OLEDDisplay::setLogBuffer (uint16_t lines, uint16_t chars) {
878
+ Serial.println (" [deprecated] Print functionality now handles buffer management automatically. This is a no-op." );
879
+ return true ;
880
+ }
881
+
882
+ bool OLEDDisplay::setLogBuffer (){
883
+ // don't know how big we need it without a font set.
884
+ if (!fontData)
885
+ return false ;
886
+
887
+ // we're always starting over
888
+ if (logBuffer != NULL )
889
+ free (logBuffer);
890
+
891
+ // figure out how big it needs to be
892
+ uint8_t textHeight = pgm_read_byte (fontData + HEIGHT_POS);
893
+ if (!textHeight)
894
+ return false ; // Prevent division by zero crashes
895
+ uint16_t lines = this ->displayHeight / textHeight + (this ->displayHeight % textHeight ? 1 : 0 );
896
+ uint16_t chars = 5 * (this ->displayWidth / textHeight);
873
897
uint16_t size = lines * (chars + 1 ); // +1 is for \n
874
- if (size > 0 ) {
875
- this ->logBufferLine = 0 ; // Lines printed
876
- this ->logBufferFilled = 0 ; // Nothing stored yet
877
- this ->logBufferMaxLines = lines; // Lines max printable
878
- this ->logBufferLineLen = chars; // Chars per line
879
- this ->logBufferSize = size; // Total number of characters the buffer can hold
880
- this ->logBuffer = (char *) malloc (size * sizeof (uint8_t ));
881
- if (!this ->logBuffer ) {
882
- DEBUG_OLEDDISPLAY (" [OLEDDISPLAY][setLogBuffer] Not enough memory to create log buffer\n " );
883
- return false ;
884
- }
898
+
899
+ // Something weird must have happened
900
+ if (size == 0 )
901
+ return false ;
902
+
903
+ // All good, initialize logBuffer
904
+ this ->logBufferLine = 0 ; // Lines printed
905
+ this ->logBufferFilled = 0 ; // Nothing stored yet
906
+ this ->logBufferMaxLines = lines; // Lines max printable
907
+ this ->logBufferLineLen = chars; // Chars per line
908
+ this ->logBufferSize = size; // Total number of characters the buffer can hold
909
+ this ->logBuffer = (char *) malloc (size * sizeof (uint8_t ));
910
+ if (!this ->logBuffer ) {
911
+ DEBUG_OLEDDISPLAY (" [OLEDDISPLAY][setLogBuffer] Not enough memory to create log buffer\n " );
912
+ return false ;
885
913
}
914
+
886
915
return true ;
887
916
}
888
917
@@ -892,13 +921,9 @@ size_t OLEDDisplay::write(uint8_t c) {
892
921
893
922
// Create a logBuffer if there isn't one
894
923
if (!logBufferSize) {
895
- uint8_t textHeight = pgm_read_byte (fontData + HEIGHT_POS);
896
- uint16_t lines = this ->displayHeight / textHeight;
897
- uint16_t chars = 5 * (this ->displayWidth / textHeight);
898
-
899
- if (this ->displayHeight % textHeight)
900
- lines++;
901
- setLogBuffer (lines, chars);
924
+ // Give up if we can't create a logBuffer somehow
925
+ if (!setLogBuffer ())
926
+ return 1 ;
902
927
}
903
928
904
929
// Don't waste space on \r\n line endings, dropping \r
@@ -957,11 +982,11 @@ size_t OLEDDisplay::write(uint8_t c) {
957
982
// Draw to screen unless we're writing a whole string at a time
958
983
if (!this ->inhibitDrawLogBuffer ) {
959
984
clear ();
960
- drawLogBuffer (0 , 0 );
985
+ drawLogBuffer ();
961
986
display ();
962
987
}
963
988
964
- // We are always claim we printed it all
989
+ // We always claim we printed it all
965
990
return 1 ;
966
991
}
967
992
@@ -975,7 +1000,7 @@ size_t OLEDDisplay::write(const char* str) {
975
1000
}
976
1001
this ->inhibitDrawLogBuffer = false ;
977
1002
clear ();
978
- drawLogBuffer (0 , 0 );
1003
+ drawLogBuffer ();
979
1004
display ();
980
1005
return length;
981
1006
}
0 commit comments