@@ -123,7 +123,7 @@ void setup()
123123 if (myOLED.begin (wirePort) == false )
124124 {
125125 Serial.println (" OLED Device begin failed. Freezing..." );
126- writeCenteredStringToDisplay (" OLED Failure" );
126+ writeCenteredStringToDisplay (" OLED Failure" , true );
127127 while (true )
128128 ;
129129 }
@@ -141,12 +141,12 @@ void setup()
141141 {
142142 Serial.println (
143143 " BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing..." );
144- writeCenteredStringToDisplay (" BMV080 Failure" );
144+ writeCenteredStringToDisplay (" BMV080 Failure" , true );
145145 while (1 )
146146 ;
147147 }
148148 Serial.println (" BMV080 found!" );
149- writeCenteredStringToDisplay (" BMV080 Found" );
149+ writeCenteredStringToDisplay (" BMV080 Found" , true );
150150
151151 // wirePort.setClock(400000); //Increase I2C data rate to 400kHz
152152
@@ -157,19 +157,19 @@ void setup()
157157 if (bmv080.setMode (SFE_BMV080_MODE_CONTINUOUS) == true )
158158 {
159159 Serial.println (" BMV080 set to continuous mode" );
160- writeCenteredStringToDisplay (" Continuous Mode Set" );
160+ writeCenteredStringToDisplay (" Continuous Mode Set" , true );
161161 }
162162 else
163163 {
164164 Serial.println (" Error setting BMV080 mode" );
165- writeCenteredStringToDisplay (" BMV080 Mode Error" );
165+ writeCenteredStringToDisplay (" BMV080 Mode Error" , true );
166166 }
167167
168168 // Set up the MAX17048 LiPo fuel gauge:
169169 if (lipo.begin () == false ) // Connect to the MAX17043 using the default wire port
170170 {
171171 Serial.println (F (" MAX17048 not detected. Please check wiring. Freezing." ));
172- writeCenteredStringToDisplay (" MAX17048 Failure" );
172+ writeCenteredStringToDisplay (" MAX17048 Failure" , true );
173173 while (1 )
174174 ;
175175 }
@@ -187,25 +187,25 @@ void loop()
187187 pm25Value = bmv080.getPM25 ();
188188 pm1Value = bmv080.getPM1 ();
189189
190+ Serial.print (pm1Value);
191+ Serial.print (" \t " );
192+ Serial.print (pm25Value);
193+ writeStaticDisplayItems ();
194+ writeValuesToDisplay ();
195+ // lipo.getSOC() returns the estimated state of charge (e.g. 79%)
196+ soc = lipo.getSOC ();
197+ Serial.print (" \t Batt: " );
198+ Serial.print (soc); // Print the battery state of charge
199+ Serial.print (" %" );
200+ writeBatteryLevelToDisplay ();
201+ myOLED.display (); // actually command the display to show the scene
202+
190203 if (bmv080.getIsObstructed () == true )
191204 {
192205 Serial.print (" \t Obstructed" );
193- writeCenteredStringToDisplay (" Obstructed" );
194- }
195- else
196- {
197- Serial.print (pm1Value);
198- Serial.print (" \t " );
199- Serial.print (pm25Value);
200- writeStaticDisplayItems ();
201- writeValuesToDisplay ();
202- // lipo.getSOC() returns the estimated state of charge (e.g. 79%)
203- soc = lipo.getSOC ();
204- Serial.print (" \t Batt: " );
205- Serial.print (soc); // Print the battery state of charge
206- Serial.print (" %" );
207- writeBatteryLevelToDisplay ();
208- myOLED.display (); // actually command the display to show the scene
206+ writeObstructedBoarder ();
207+ writeCenteredStringToDisplay (" Obstructed" , false ); // don't clear the screen
208+ // so the obstructed message is displayed on top of the PM values
209209 }
210210
211211 Serial.println ();
@@ -240,7 +240,7 @@ void writeStaticDisplayItems()
240240 // clear the screen
241241 myOLED.erase ();
242242
243- myOLED.setFont (&QW_FONT_5X7 );
243+ myOLED.setFont (&QW_FONT_8X16 );
244244
245245 // draw the PM1 static text label
246246 // calculate the x position of the PM1 label
@@ -274,21 +274,28 @@ void writeValuesToDisplay()
274274 // calculate the x position of the PM1 value
275275 // we want it to be centered in the left half of the screen
276276 int xPosPM1Value = (myOLED.getWidth () / 4 ) - (myOLED.getStringWidth (toPrint) / 2 );
277- myOLED.text (xPosPM1Value, 10 , toPrint, 1 );
277+
278+ // calculate the y start position of the PM1 value
279+ // we want the bottom of the text to align with the bottom of the screen
280+ int yPosPM1Value = myOLED.getHeight () - myOLED.getStringHeight (toPrint);
281+ myOLED.text (xPosPM1Value, yPosPM1Value, toPrint, 1 );
278282
279283 // draw the PM2.5 value
280284 // calculate the x position of the PM2.5 value
281285 // we want it to be centered in the right half of the screen
282286 int xPosPM25Value = (myOLED.getWidth () / 4 ) * 3 - (myOLED.getStringWidth (toPrint) / 2 );
283287 toPrint = String (int (pm25Value));
284- myOLED.text (xPosPM25Value, 10 , toPrint, 1 );
288+ myOLED.text (xPosPM25Value, yPosPM1Value , toPrint, 1 ); // same y position as the PM1 value
285289}
286290
287291// Write a string to the display that is centered horizontally and vertically
288- void writeCenteredStringToDisplay (String toPrint)
292+ void writeCenteredStringToDisplay (String toPrint, bool clearScreen )
289293{
290294 // clear the screen
291- myOLED.erase ();
295+ if (clearScreen)
296+ {
297+ myOLED.erase ();
298+ }
292299
293300 // set the font to the 8x16 font
294301 myOLED.setFont (&QW_FONT_5X7);
@@ -418,6 +425,18 @@ void writeBatteryLevelToDisplay()
418425 {
419426 myOLED.rectangle (xPosRightBlock, yPosBatteryLevelBlocks, 2 , 2 , 1 );
420427 }
428+ }
429+
430+ void writeObstructedBoarder ()
431+ {
432+ // set fort to 5x7
433+ myOLED.setFont (&QW_FONT_5X7);
421434
435+ int xPosObstructed = (myOLED.getWidth () / 2 ) - (myOLED.getStringWidth (" Obstructed" ) / 2 ) - 2 ;
436+ int yPosObstructed = (myOLED.getHeight () / 2 ) - (myOLED.getStringHeight (" Obstructed" ) / 2 ) - 2 ;
437+ int widthObstructed = myOLED.getStringWidth (" Obstructed" ) + 4 ;
438+ int heightObstructed = myOLED.getStringHeight (" Obstructed" ) + 4 ;
422439
440+ // draw the black filled rectangle
441+ myOLED.rectangleFill (xPosObstructed, yPosObstructed, widthObstructed, heightObstructed, 0 );
423442}
0 commit comments