Skip to content

Commit 206a98e

Browse files
authored
Merge pull request #13 from sparkfun/Add_QwiicCustomOLED
Adding object oriented syntactical sugar
2 parents 40db6ce + d63c712 commit 206a98e

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

examples/Example-09_CustomOLED/Example-09_CustomOLED.ino

+8-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
QwiicCustomOLED myOLED;
3030

31-
3231
void setup()
3332
{
3433
delay(1000);
@@ -40,14 +39,14 @@ void setup()
4039

4140
// If desired, we can customize the OLED before we begin it.
4241
// Otherwise it will default to 128x64 (1.3" OLED).
43-
myOLED.m_device.xOffset = 0; // Set the active area X offset. For the Micro 64x48, set this to 2
44-
myOLED.m_device.yOffset = 0; // Set the active area Y offset
45-
myOLED.m_device.displayWidth = 128; // Set the active area width
46-
myOLED.m_device.displayHeight = 64; // Set the active area height
47-
myOLED.m_device.pinConfig = 0x12; // Set COM Pins Hardware Configuration (DAh)
48-
myOLED.m_device.preCharge = 0xF1; // Set Pre-charge Period (D9h)
49-
myOLED.m_device.vcomDeselect = 0x40; // Set VCOMH Deselect Level (DBh)
50-
myOLED.m_device.contrast = 0xCF; // Set Contrast Control for BANK0 (81h)
42+
myOLED.setXOffset(0); // Set the active area X offset. For the Micro 64x48, set this to 2
43+
myOLED.setYOffset(0); // Set the active area Y offset
44+
myOLED.setDisplayWidth(128); // Set the active area width. For the Micro 64x48, set this to 64
45+
myOLED.setDisplayHeight(64); // Set the active area height. For the Micro 64x48, set this to 48
46+
myOLED.setPinConfig(0x12); // Set COM Pins Hardware Configuration (DAh)
47+
myOLED.setPreCharge(0xF1); // Set Pre-charge Period (D9h)
48+
myOLED.setVcomDeselect(0x40); // Set VCOMH Deselect Level (DBh)
49+
myOLED.setContrast(0xCF); // Set Contrast Control for BANK0 (81h). For the Micro 64x48, set this to 0x8F
5150

5251
// Initalize the OLED device and related graphics system
5352
if (myOLED.begin(Wire, 0x3D) == false) // The TwoWire port and I2C address are set here

src/SparkFun_Qwiic_OLED.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ typedef QwBitmap QwiicBitmap;
8585

8686
template <typename SSD1306DeviceType>
8787
class QwiicOLEDBaseClass : public Print { // NOTE: implementing Arduino Print
88-
public:
88+
protected:
8989
// our device driver
9090
SSD1306DeviceType m_device;
9191
private:
@@ -840,5 +840,13 @@ class Qwiic1in3OLED : public QwiicOLEDBaseClass<QwOLED1in3> {
840840
};
841841

842842
class QwiicCustomOLED : public QwiicOLEDBaseClass<QwOLEDCustom> {
843-
// nothing here - see above
843+
public:
844+
void setXOffset(uint8_t xOffset){ m_device.setXOffset(xOffset); }
845+
void setYOffset(uint8_t yOffset){ m_device.setYOffset(yOffset); }
846+
void setDisplayWidth(uint8_t displayWidth){ m_device.setDisplayWidth(displayWidth); }
847+
void setDisplayHeight(uint8_t displayHeight){ m_device.setDisplayHeight(displayHeight); }
848+
void setPinConfig(uint8_t pinConfig){ m_device.setPinConfig(pinConfig); }
849+
void setPreCharge(uint8_t preCharge){ m_device.setPreCharge(preCharge); }
850+
void setVcomDeselect(uint8_t vcomDeselect){ m_device.setVcomDeselect(vcomDeselect); }
851+
void setContrast(uint8_t contrast){ m_device.setContrast(contrast); }
844852
};

src/qwiic_oled_custom.h

+28-16
Original file line numberDiff line numberDiff line change
@@ -81,38 +81,50 @@ class QwOLEDCustom : public QwGrSSD1306 {
8181
~QwOLEDCustom()
8282
{
8383
if (m_graphicsBuffer != nullptr)
84+
{
8485
delete[] m_graphicsBuffer;
86+
m_graphicsBuffer = nullptr;
87+
}
8588
};
8689

8790
// set up the specific device settings
8891
bool init(void)
8992
{
90-
setViewport(xOffset, yOffset, displayWidth, displayHeight);
93+
this->QwGrSSD1306::setViewport(m_xOffset, m_yOffset, m_displayWidth, m_displayHeight);
9194

92-
setCommPins(pinConfig);
93-
setPreCharge(preCharge);
94-
setVcomDeselect(vcomDeselect);
95-
setContrast(contrast);
95+
this->QwGrSSD1306::setCommPins(m_pinConfig);
96+
this->QwGrSSD1306::setPreCharge(m_preCharge);
97+
this->QwGrSSD1306::setVcomDeselect(m_vcomDeselect);
98+
this->QwGrSSD1306::setContrast(m_contrast);
9699

97100
if (m_graphicsBuffer != nullptr)
98101
delete[] m_graphicsBuffer;
99-
m_graphicsBuffer = new uint8_t[(uint16_t)displayWidth * (uint16_t)displayHeight / 8];
100-
setBuffer(m_graphicsBuffer); // The buffer to use
102+
m_graphicsBuffer = new uint8_t[(uint16_t)m_displayWidth * (uint16_t)m_displayHeight / 8];
103+
this->QwGrSSD1306::setBuffer(m_graphicsBuffer); // The buffer to use
101104

102105
// Call the super class to do all the work
103106
return this->QwGrSSD1306::init();
104107
};
105108

106-
uint8_t xOffset = kOLEDCustomDefaultXOffset;
107-
uint8_t yOffset = kOLEDCustomDefaultYOffset;
108-
uint8_t displayWidth = kOLEDCustomDefaultWidth;
109-
uint8_t displayHeight = kOLEDCustomDefaultHeight;
110-
uint8_t pinConfig = kOLEDCustomDefaultPinConfig;
111-
uint8_t preCharge = kOLEDCustomDefaultPreCharge;
112-
uint8_t vcomDeselect = kOLEDCustomDefaultVCOM;
113-
uint8_t contrast = kOLEDCustomDefaultContrast;
109+
void setXOffset(uint8_t xOffset){ m_xOffset = xOffset; }
110+
void setYOffset(uint8_t yOffset){ m_yOffset = yOffset; }
111+
void setDisplayWidth(uint8_t displayWidth){ m_displayWidth = displayWidth; }
112+
void setDisplayHeight(uint8_t displayHeight){ m_displayHeight = displayHeight; }
113+
void setPinConfig(uint8_t pinConfig){ m_pinConfig = pinConfig; }
114+
void setPreCharge(uint8_t preCharge){ m_preCharge = preCharge; }
115+
void setVcomDeselect(uint8_t vcomDeselect){ m_vcomDeselect = vcomDeselect; }
116+
void setContrast(uint8_t contrast){ m_contrast = contrast; }
117+
118+
private:
119+
uint8_t m_xOffset = kOLEDCustomDefaultXOffset;
120+
uint8_t m_yOffset = kOLEDCustomDefaultYOffset;
121+
uint8_t m_displayWidth = kOLEDCustomDefaultWidth;
122+
uint8_t m_displayHeight = kOLEDCustomDefaultHeight;
123+
uint8_t m_pinConfig = kOLEDCustomDefaultPinConfig;
124+
uint8_t m_preCharge = kOLEDCustomDefaultPreCharge;
125+
uint8_t m_vcomDeselect = kOLEDCustomDefaultVCOM;
126+
uint8_t m_contrast = kOLEDCustomDefaultContrast;
114127

115-
protected:
116128
// Graphics buffer for this device.
117129
uint8_t *m_graphicsBuffer = nullptr;
118130
};

0 commit comments

Comments
 (0)