From c1e36a11008dd0cfac006e5972b649dc22f0ffbe Mon Sep 17 00:00:00 2001
From: Rotzbua <Rotzbua@users.noreply.github.com>
Date: Sun, 18 Mar 2018 03:51:26 +0100
Subject: [PATCH] improve constructor

fix #181
---
 OLEDDisplay.cpp | 14 +++++++++++++-
 OLEDDisplay.h   |  3 +++
 SH1106Brzo.h    | 17 ++---------------
 SH1106Spi.h     | 17 ++---------------
 SH1106Wire.h    | 17 ++---------------
 SSD1306Brzo.h   | 17 ++---------------
 SSD1306Spi.h    | 17 ++---------------
 SSD1306Wire.h   | 17 ++---------------
 8 files changed, 28 insertions(+), 91 deletions(-)

diff --git a/OLEDDisplay.cpp b/OLEDDisplay.cpp
index 9caf84f..cc48374 100644
--- a/OLEDDisplay.cpp
+++ b/OLEDDisplay.cpp
@@ -714,6 +714,18 @@ size_t OLEDDisplay::write(const char* str) {
 }
 
 // Private functions
+void OLEDDisplay::setGeometry(OLEDDISPLAY_GEOMETRY g) {
+  this->geometry = g;
+  if (g == GEOMETRY_128_64) {
+    this->displayWidth                     = 128;
+    this->displayHeight                    = 64;
+  } else if (g == GEOMETRY_128_32) {
+    this->displayWidth                     = 128;
+    this->displayHeight                    = 32;
+  }
+  this->displayBufferSize                = displayWidth*displayHeight/8;
+}
+
 void OLEDDisplay::sendInitCommands(void) {
   sendCommand(DISPLAYOFF);
   sendCommand(SETDISPLAYCLOCKDIV);
@@ -854,4 +866,4 @@ char* OLEDDisplay::utf8ascii(String str) {
 
 void OLEDDisplay::setFontTableLookupFunction(FontTableLookupFunction function) {
   this->fontTableLookupFunction = function;
-}
\ No newline at end of file
+}
diff --git a/OLEDDisplay.h b/OLEDDisplay.h
index 9326323..3fd56dc 100644
--- a/OLEDDisplay.h
+++ b/OLEDDisplay.h
@@ -265,6 +265,9 @@ class OLEDDisplay : public Print {
     uint16_t  displayWidth                     = 128;
     uint16_t  displayHeight                    = 64;
     uint16_t  displayBufferSize                = 1024;
+    
+    // Set the correct height, width and buffer for the geometry
+    void setGeometry(OLEDDISPLAY_GEOMETRY g);
 
     OLEDDISPLAY_TEXT_ALIGNMENT   textAlignment = TEXT_ALIGN_LEFT;
     OLEDDISPLAY_COLOR            color         = WHITE;
diff --git a/SH1106Brzo.h b/SH1106Brzo.h
index 85d7372..a659063 100644
--- a/SH1106Brzo.h
+++ b/SH1106Brzo.h
@@ -44,21 +44,8 @@ class SH1106Brzo : public OLEDDisplay {
       uint8_t             _scl;
 
   public:
-    SH1106Brzo(uint8_t _address, uint8_t _sda, uint8_t _scl) : SH1106Brzo(GEOMETRY_128_64, _address, _sda, _scl) {
-
-    }
-
-    SH1106Brzo(OLEDDISPLAY_GEOMETRY g, uint8_t _address, uint8_t _sda, uint8_t _scl) {
-      this->geometry = g;
-      if (g == GEOMETRY_128_64) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 64;
-        this->displayBufferSize                = 1024;
-      } else if (g == GEOMETRY_128_32) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 32;
-        this->displayBufferSize                = 512;
-      }
+	SH1106Brzo(uint8_t _address, uint8_t _sda, uint8_t _scl, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) { 
+		setGeometry(g);
 
       this->_address = _address;
       this->_sda = _sda;
diff --git a/SH1106Spi.h b/SH1106Spi.h
index 45d9ccf..99ded8d 100644
--- a/SH1106Spi.h
+++ b/SH1106Spi.h
@@ -37,21 +37,8 @@ class SH1106Spi : public OLEDDisplay {
       uint8_t             _dc;
 
   public:
-    SH1106Spi(uint8_t _rst, uint8_t _dc) : SH1106Spi(GEOMETRY_128_64, _rst, _dc) {
-
-    }
-
-    SH1106Spi(OLEDDISPLAY_GEOMETRY g, uint8_t _rst, uint8_t _dc) {
-      this->geometry = g;
-      if (g == GEOMETRY_128_64) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 64;
-        this->displayBufferSize                = 1024;
-      } else if (g == GEOMETRY_128_32) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 32;
-        this->displayBufferSize                = 512;
-      }
+    SH1106Spi(uint8_t _rst, uint8_t _dc, uint8_t _cs, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
+        setGeometry(g);
 
       this->_rst = _rst;
       this->_dc  = _dc;
diff --git a/SH1106Wire.h b/SH1106Wire.h
index 6bb95e7..e168739 100644
--- a/SH1106Wire.h
+++ b/SH1106Wire.h
@@ -44,21 +44,8 @@ class SH1106Wire : public OLEDDisplay {
       uint8_t             _scl;
 
   public:
-    SH1106Wire(uint8_t _address, uint8_t _sda, uint8_t _scl) : SH1106Wire(GEOMETRY_128_64, _address, _sda, _scl) {
-
-    }
-
-    SH1106Wire(OLEDDISPLAY_GEOMETRY g, uint8_t _address, uint8_t _sda, uint8_t _scl) {
-      this->geometry = g;
-      if (g == GEOMETRY_128_64) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 64;
-        this->displayBufferSize                = 1024;
-      } else if (g == GEOMETRY_128_32) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 32;
-        this->displayBufferSize                = 512;
-      }
+    SH1106Wire(uint8_t _address, uint8_t _sda, uint8_t _scl, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
+      setGeometry(g);
 
       this->_address = _address;
       this->_sda = _sda;
diff --git a/SSD1306Brzo.h b/SSD1306Brzo.h
index edd3f53..3753a02 100644
--- a/SSD1306Brzo.h
+++ b/SSD1306Brzo.h
@@ -44,21 +44,8 @@ class SSD1306Brzo : public OLEDDisplay {
       uint8_t             _scl;
 
   public:
-    SSD1306Brzo(uint8_t _address, uint8_t _sda, uint8_t _scl) : SSD1306Brzo(GEOMETRY_128_64, _address, _sda, _scl) {
-
-    }
-
-    SSD1306Brzo(OLEDDISPLAY_GEOMETRY g, uint8_t _address, uint8_t _sda, uint8_t _scl) {
-      this->geometry = g;
-      if (g == GEOMETRY_128_64) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 64;
-        this->displayBufferSize                = 1024;
-      } else if (g == GEOMETRY_128_32) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 32;
-        this->displayBufferSize                = 512;
-      }
+    SSD1306Brzo(uint8_t _address, uint8_t _sda, uint8_t _scl, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
+      setGeometry(g);
 
       this->_address = _address;
       this->_sda = _sda;
diff --git a/SSD1306Spi.h b/SSD1306Spi.h
index 0984298..faf2344 100644
--- a/SSD1306Spi.h
+++ b/SSD1306Spi.h
@@ -44,21 +44,8 @@ class SSD1306Spi : public OLEDDisplay {
       uint8_t             _cs;
 
   public:
-    SSD1306Spi(uint8_t _rst, uint8_t _dc, uint8_t _cs) : SSD1306Spi(GEOMETRY_128_64, _rst, _dc, _cs) {
-
-    }
-
-    SSD1306Spi(OLEDDISPLAY_GEOMETRY g,  uint8_t _rst, uint8_t _dc, uint8_t _cs) {
-      this->geometry = g;
-      if (g == GEOMETRY_128_64) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 64;
-        this->displayBufferSize                = 1024;
-      } else if (g == GEOMETRY_128_32) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 32;
-        this->displayBufferSize                = 512;
-      }
+    SSD1306Spi(uint8_t _rst, uint8_t _dc, uint8_t _cs, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
+        setGeometry(g);
 
       this->_rst = _rst;
       this->_dc  = _dc;
diff --git a/SSD1306Wire.h b/SSD1306Wire.h
index 312bdea..4083139 100644
--- a/SSD1306Wire.h
+++ b/SSD1306Wire.h
@@ -39,21 +39,8 @@ class SSD1306Wire : public OLEDDisplay {
       bool                _doI2cAutoInit = false;
 
   public:
-    SSD1306Wire(uint8_t _address, uint8_t _sda, uint8_t _scl) : SSD1306Wire(GEOMETRY_128_64, _address, _sda, _scl) {
-
-    }
-
-    SSD1306Wire(OLEDDISPLAY_GEOMETRY g, uint8_t _address, uint8_t _sda, uint8_t _scl) {
-      this->geometry = g;
-      if (g == GEOMETRY_128_64) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 64;
-        this->displayBufferSize                = 1024;
-      } else if (g == GEOMETRY_128_32) {
-        this->displayWidth                     = 128;
-        this->displayHeight                    = 32;
-        this->displayBufferSize                = 512;
-      }
+    SSD1306Wire(uint8_t _address, uint8_t _sda, uint8_t _scl, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
+      setGeometry(g);
 
       this->_address = _address;
       this->_sda = _sda;
-- 
GitLab