@@ -52,7 +52,6 @@ int nFONTS = sizeof(demoFonts) / sizeof(demoFonts[0]);
5252int iFont = 0 ;
5353
5454// Some vars for the title.
55- int xTitle, yTitle;
5655String strTitle = " <<Font>>" ;
5756QwiicFont *pFntTitle = &QW_FONT_5X7;
5857
@@ -72,25 +71,19 @@ void setup()
7271 }
7372 Serial.println (" Begin success" );
7473
75- // Position to use for the time/banner displayed before each font
76-
77- // starting x position - width minus string length (font width * number of characters) / 2
78- xTitle = (myOLED.getWidth () - (pFntTitle->width + 1 ) * strTitle.length ()) / 2 ;
79-
80- yTitle = (myOLED.getHeight () - pFntTitle->height ) / 2 ;
8174}
8275
8376void loop ()
8477{
85- // Write out a title
86- writeTitle ();
87-
88- delay (1000 );
89-
9078 // next font for display
9179 iFont = (iFont + 1 ) % nFONTS;
9280 myOLED.setFont (demoFonts[iFont]);
9381
82+ // Write font name to screen
83+ writeTitle ();
84+
85+ delay (1000 );
86+
9487 // Write out the full font char set
9588 writeFontChars ();
9689
@@ -101,16 +94,16 @@ void loop()
10194void writeFontChars ()
10295{
10396 // get the font
104- QwiicFont *pFont = myOLED.getFont ();
97+ QwiicFont * currFont = myOLED.getFont ();
10598
10699 // how many chars can a screen handle? (x * y)
107- uint16_t screenChars = myOLED.getWidth () / (pFont ->width + 1 ); // X
108- uint8_t nY = myOLED.getHeight () / pFont ->height ; // Y
100+ uint16_t screenChars = myOLED.getWidth () / (currFont ->width + 1 ); // X
101+ uint8_t nY = myOLED.getHeight () / currFont ->height ; // Y
109102
110103 screenChars *= (nY == 0 ? 1 : nY); // need at least 1 row
111104
112105 // Loop over the characters in the font.
113- for (int i = 0 ; i < pFont ->n_chars ; i++)
106+ for (int i = 0 ; i < currFont ->n_chars ; i++)
114107 {
115108
116109 if (i % screenChars == 0 )
@@ -122,7 +115,7 @@ void writeFontChars()
122115
123116 // if the character is a carriage return, send a blank - otherwise the
124117 // write routine will perform a CR and lead to a confusing display.
125- myOLED.write ((i + pFont ->start != ' \n ' ) ? i + pFont ->start : ' ' );
118+ myOLED.write ((i + currFont ->start != ' \n ' ) ? i + currFont ->start : ' ' );
126119
127120 myOLED.display (); // show the added char
128121
@@ -133,14 +126,35 @@ void writeFontChars()
133126// Simple title for a font
134127void writeTitle ()
135128{
129+
130+ // Get the current font name
131+ String strTitle = myOLED.getFontName ();
132+
133+ // save our current font, then switch to font for title
134+ QwiicFont * currFont = myOLED.getFont ();
135+
136136 // Set title font font
137137 myOLED.setFont (pFntTitle);
138138
139+ // Position to use for the time/banner displayed before each font
140+
141+ // Get the width of the title in screen size
142+ int width = myOLED.getStringWidth (strTitle);
143+
144+ // if the string is wider than the screen, set x at 0, otherwise center text
145+
146+ int xTitle = (width >= myOLED.getWidth () ? 0 : (myOLED.getWidth () - width) / 2 );
147+
148+ // starting y position - width minus string height / 2. No need to chech height, 5x7 font fits everything
149+ int yTitle = (myOLED.getHeight () - myOLED.getStringHeight (strTitle)) / 2 ;
150+
139151 myOLED.erase ();
140152
141153 // Draw the text
142154 myOLED.text (xTitle, yTitle, strTitle);
143155
144156 // There's nothing on the screen yet - Now send the graphics to the device
145157 myOLED.display ();
158+
159+ myOLED.setFont (currFont);
146160}
0 commit comments