2525#define OBS_DISPLAYS_H
2626
2727#include < Arduino.h>
28- #include < SSD1306 .h>
28+ #include < U8g2lib .h>
2929
3030#include " config.h"
3131#include " globals.h"
3232#include " gps.h"
3333#include " logo.h"
34+ #include " fonts/fonts.h"
3435#include " sensor.h"
3536
3637
37- extern const uint8_t Open_Sans_Regular_Plain_8[];
38- // // this font is part of OLEDDisplay::OLEDDisplay :/
39- // extern const uint8_t ArialMT_Plain_10[]; // :(
40- extern const uint8_t Open_Sans_Regular_Plain_10[];
41- extern const uint8_t Open_Sans_Regular_Plain_20[];
42- extern const uint8_t Open_Sans_Regular_Plain_34[];
43- extern const uint8_t Open_Sans_Regular_Plain_50[];
38+ #define BLACK 0
39+ #define WHITE 1
40+
4441extern const uint8_t BatterieLogo1[];
4542extern const uint8_t TempLogo[];
4643
47- #define TINY_FONT Open_Sans_Regular_Plain_8
48- #define SMALL_FONT Open_Sans_Regular_Plain_10
49- #define MEDIUM_FONT Open_Sans_Regular_Plain_20
50- #define LARGE_FONT Open_Sans_Regular_Plain_34
51- #define HUGE_FONT Open_Sans_Regular_Plain_50
44+ #define TINY_FONT OpenSans_Regular_6
45+ #define SMALL_FONT OpenSans_Regular_7
46+ #define MEDIUM_FONT OpenSans_Regular_15
47+ #define LARGE_FONT OpenSans_Regular_26
48+ #define HUGE_FONT OpenSans_Regular_37
5249
5350// Forward declare classes to build (because there is a cyclic dependency between sensor.h and displays.h)
5451class HCSR04SensorInfo ;
@@ -64,21 +61,24 @@ class DisplayDevice {
6461 private:
6562 void handleHighlight ();
6663 void displaySimple (uint16_t value);
67- SSD1306 * m_display;
64+ U8G2 * m_display = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); // original OBS display
6865 String gridText[ 4 ][ 6 ];
6966 uint8_t mLastProgress = 255 ;
7067 uint8_t mCurrentLine = 0 ;
7168 bool mInverted = false ;
69+ bool mFlipped = true ;
7270 uint32_t mHighlightTill = 0 ;
7371 bool mHighlighted = false ;
7472
7573 public:
7674 DisplayDevice () {
77- m_display = new SSD1306 (0x3c , 21 , 22 ); // ADDRESS, SDA, SCL
78- m_display->init ();
79- m_display->setBrightness (255 );
80- m_display->setTextAlignment (TEXT_ALIGN_LEFT);
81- m_display->display ();
75+ m_display->begin ();
76+ m_display->setFlipMode (mFlipped );
77+ m_display->setContrast (74 );
78+ m_display->setFontPosTop ();
79+ m_display->setFontMode (1 );
80+ m_display->setDrawColor (WHITE);
81+ m_display->updateDisplay ();
8282 }
8383
8484 ~DisplayDevice () {
@@ -96,20 +96,19 @@ class DisplayDevice {
9696 // ##############################################################
9797
9898 void invert () {
99- m_display->invertDisplay ();
100- m_display->display ();
10199 mInverted = true ;
100+ setInversion (mInverted );
102101 }
103102
104103 void normalDisplay () {
105- m_display->normalDisplay ();
106- m_display->display ();
107104 mInverted = false ;
105+ setInversion (mInverted );
108106 }
109107
110108 void flipScreen () {
111- m_display->flipScreenVertically ();
112- m_display->display ();
109+ mFlipped = !mFlipped ;
110+ m_display->setFlipMode (mFlipped );
111+ m_display->updateDisplay ();
113112 }
114113
115114 void clear () {
@@ -122,8 +121,8 @@ class DisplayDevice {
122121 // ##############################################################
123122
124123 void showLogo (bool val) {
125- m_display->drawXbm (0 , 0 , OBSLogo_width, OBSLogo_height, OBSLogo);
126- m_display->display ();
124+ m_display->drawXBM (0 , 0 , OBSLogo_width, OBSLogo_height, OBSLogo);
125+ m_display->updateDisplay ();
127126 }
128127
129128 // ##############################################################
@@ -132,18 +131,18 @@ class DisplayDevice {
132131
133132 void showGrid (bool val) {
134133 // Horizontal lines
135- m_display->drawHorizontalLine (0 , 2 , 128 );
136- m_display->drawHorizontalLine (0 , 12 , 128 );
137- m_display->drawHorizontalLine (0 , 22 , 128 );
138- m_display->drawHorizontalLine (0 , 32 , 128 );
139- m_display->drawHorizontalLine (0 , 42 , 128 );
140- m_display->drawHorizontalLine (0 , 52 , 128 );
141- m_display->drawHorizontalLine (0 , 62 , 128 );
134+ m_display->drawHLine (0 , 2 , 128 );
135+ m_display->drawHLine (0 , 12 , 128 );
136+ m_display->drawHLine (0 , 22 , 128 );
137+ m_display->drawHLine (0 , 32 , 128 );
138+ m_display->drawHLine (0 , 42 , 128 );
139+ m_display->drawHLine (0 , 52 , 128 );
140+ m_display->drawHLine (0 , 62 , 128 );
142141
143142 // Vertical lines
144- m_display->drawVerticalLine (32 , 0 , 64 );
145- m_display->drawVerticalLine (64 , 0 , 64 );
146- m_display->drawVerticalLine (96 , 0 , 64 );
143+ m_display->drawVLine (32 , 0 , 64 );
144+ m_display->drawVLine (64 , 0 , 64 );
145+ m_display->drawVLine (96 , 0 , 64 );
147146 }
148147
149148 // ##############################################################
@@ -166,7 +165,7 @@ class DisplayDevice {
166165
167166 void showTextOnGrid (int16_t x, int16_t y, String text, const uint8_t * font = SMALL_FONT, int8_t offset_x_ = 0 , int8_t offset_y_ = 0 ) {
168167 if (prepareTextOnGrid (x, y, text, font,offset_x_,offset_y_)) {
169- m_display->display ();
168+ m_display->updateDisplay ();
170169 }
171170 }
172171
@@ -186,7 +185,7 @@ class DisplayDevice {
186185 // 2 => 8 - (2*2) = 4
187186 // 3 => 8 - (3*2) = 2
188187 int x_offset = 8 - (x * 2 );
189- m_display->drawString (x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y]);
188+ m_display->drawStr (x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y]. c_str () );
190189 changed = true ;
191190 }
192191 return changed;
@@ -199,7 +198,7 @@ class DisplayDevice {
199198 gridText[x][y] = " " ;
200199 }
201200 }
202- m_display->display ();
201+ m_display->updateDisplay ();
203202 }
204203
205204 // Override the existing WHITE text with BLACK
@@ -208,23 +207,23 @@ class DisplayDevice {
208207 }
209208
210209 void cleanGridCell (int16_t x, int16_t y,int8_t offset_x_, int8_t offset_y_) {
211- m_display->setColor (BLACK);
210+ m_display->setDrawColor (BLACK);
212211 int x_offset = 8 - (x * 2 );
213- m_display->drawString (x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y]);
212+ m_display->drawStr (x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y]. c_str () );
214213 gridText[x][y] = " " ;
215- m_display->setColor (WHITE);
214+ m_display->setDrawColor (WHITE);
216215 }
217216
218217 void cleanBattery (int16_t x, int16_t y){
219- m_display->setColor (BLACK);
220- m_display->drawXbm (x, y, 8 , 9 , BatterieLogo1);
221- m_display->setColor (WHITE);
218+ m_display->setDrawColor (BLACK);
219+ m_display->drawXBM (x, y, 8 , 9 , BatterieLogo1);
220+ m_display->setDrawColor (WHITE);
222221 }
223222
224223 void cleanTemperatur (int16_t x, int16_t y){
225- m_display->setColor (BLACK);
226- m_display->drawXbm (x, y, 8 , 9 , TempLogo);
227- m_display->setColor (WHITE);
224+ m_display->setDrawColor (BLACK);
225+ m_display->drawXBM (x, y, 8 , 9 , TempLogo);
226+ m_display->setDrawColor (WHITE);
228227 }
229228
230229 void drawProgressBar (uint8_t y, uint32_t current, uint32_t target) {
@@ -242,34 +241,34 @@ class DisplayDevice {
242241 uint16_t rowOffset = y * 10 + 3 ;
243242
244243 if (mLastProgress != progress) {
245- m_display->drawRect (12 , rowOffset, 104 , 8 );
244+ m_display->drawFrame (12 , rowOffset, 104 , 8 );
246245 if (progress != 0 ) {
247- m_display->fillRect (14 , rowOffset + 2 , progress > 100 ? 100 : progress, 4 );
246+ m_display->drawBox (14 , rowOffset + 2 , progress > 100 ? 100 : progress, 4 );
248247 }
249- m_display->display ();
248+ m_display->updateDisplay ();
250249 mLastProgress = progress;
251250 }
252251 }
253252
254253 void drawWaitBar (uint8_t y, uint16_t tick) {
255254 uint16_t rowOffset = y * 10 + 3 ;
256- m_display->drawRect (12 , rowOffset, 104 , 8 );
257- m_display->setColor (BLACK);
258- m_display->fillRect (14 , rowOffset + 2 , 100 , 4 );
259- m_display->setColor (WHITE);
255+ m_display->drawFrame (12 , rowOffset, 104 , 8 );
256+ m_display->setDrawColor (BLACK);
257+ m_display->drawBox (14 , rowOffset + 2 , 100 , 4 );
258+ m_display->setDrawColor (WHITE);
260259
261260 int16_t pos = 45 + (45.0 * sin (tick / 10.0 ));
262- m_display->fillRect (14 + pos, rowOffset + 2 , 10 , 4 );
263- m_display->display ();
261+ m_display->drawBox (14 + pos, rowOffset + 2 , 10 , 4 );
262+ m_display->updateDisplay ();
264263 }
265264
266265 void clearProgressBar (uint8_t y) {
267266 if (UINT8_MAX != mLastProgress ) {
268267 clearTextLine (y);
269268 uint16_t rowOffset = y * 10 + 3 ;
270- m_display->setColor (BLACK);
271- m_display->fillRect (12 , rowOffset, 104 , 8 );
272- m_display->setColor (WHITE);
269+ m_display->setDrawColor (BLACK);
270+ m_display->drawBox (12 , rowOffset, 104 , 8 );
271+ m_display->setDrawColor (WHITE);
273272 m_display->display ();
274273 mLastProgress = UINT8_MAX;
275274 }
@@ -310,6 +309,15 @@ class DisplayDevice {
310309 uint16_t minDistanceToConfirm, int16_t batteryPercentage, int16_t TemperaturValue,
311310 int lastMeasurements, boolean insidePrivacyArea, double speed, uint8_t satellites);
312311
312+
313+ // ##############################################################
314+ // Internal methods
315+ // ##############################################################
316+
317+ private:
318+ void setInversion (bool enabled) {
319+ m_display->sendF (" c" , enabled ? 0xa7 : 0xa6 ); // set inversion of the SSD1306 OLED
320+ }
313321};
314322
315323#endif
0 commit comments