Skip to content

Commit 40db6ce

Browse files
authored
Merge pull request #12 from sparkfun/Add_QwiicCustomOLED
Add QwiicCustomOLED
2 parents edca24b + f5fcd38 commit 40db6ce

File tree

5 files changed

+216
-2
lines changed

5 files changed

+216
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
3+
Example-09_CustomOLED.ino
4+
5+
This demo shows the basic setup of the OLED library, generating simple graphics and displaying
6+
the results on the target device.
7+
8+
Micro OLED https://www.sparkfun.com/products/14532
9+
Transparent OLED https://www.sparkfun.com/products/15173
10+
"Narrow" OLED https://www.sparkfun.com/products/17153
11+
12+
Written by Kirk Benell @ SparkFun Electronics, March 2022
13+
14+
Repository:
15+
https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
16+
17+
Documentation:
18+
https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
19+
20+
SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
21+
*/
22+
23+
#include <SparkFun_Qwiic_OLED.h> //http://librarymanager/All#SparkFun_Qwiic_OLED
24+
25+
26+
// This demo shows how to use the QwiicCustomOLED class,
27+
// allowing the display width, height etc. to be set manually.
28+
29+
QwiicCustomOLED myOLED;
30+
31+
32+
void setup()
33+
{
34+
delay(1000);
35+
36+
Serial.begin(115200);
37+
Serial.println("Running OLED example");
38+
39+
Wire.begin();
40+
41+
// If desired, we can customize the OLED before we begin it.
42+
// 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)
51+
52+
// Initalize the OLED device and related graphics system
53+
if (myOLED.begin(Wire, 0x3D) == false) // The TwoWire port and I2C address are set here
54+
{
55+
Serial.println("Device begin failed. Freezing...");
56+
while (true)
57+
;
58+
}
59+
Serial.println("Begin success");
60+
61+
// Do a simple test - fill a rectangle on the screen and then print hello!
62+
63+
// Fill a rectangle on the screen that has a 4 pixel board
64+
myOLED.rectangleFill(4, 4, myOLED.getWidth() - 8, myOLED.getHeight() - 8);
65+
66+
String hello = "hello"; // our message
67+
68+
// Center our message on the screen. Get the screen size of the "hello" string,
69+
// calling the getStringWidth() and getStringHeight() methods on the oled
70+
71+
// starting x position - screen width minus string width / 2
72+
int x0 = (myOLED.getWidth() - myOLED.getStringWidth(hello)) / 2;
73+
74+
// starting y position - screen height minus string height / 2
75+
int y0 = (myOLED.getHeight() - myOLED.getStringHeight(hello)) / 2;
76+
77+
// Draw the text - color of black (0)
78+
myOLED.text(x0, y0, hello, 0);
79+
80+
// There's nothing on the screen yet - Now send the graphics to the device
81+
myOLED.display();
82+
83+
// That's it - HELLO!
84+
}
85+
86+
void loop()
87+
{
88+
delay(1000); // Do nothing
89+
}

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ QwiicTransparentOLED KEYWORD1
1010
QwiicNarrowOLED KEYWORD1
1111
QwiicMicroOLED KEYWORD1
1212
Qwiic1in3OLED KEYWORD1
13+
QwiicCustomOLED KEYWORD1
1314
QwiicFont KEYWORD1
1415
grRasterOp_t KEYWORD1
1516

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun Qwiic OLED Arduino Library
2-
version=1.0.8
2+
version=1.0.9
33
author=SparkFun Electronics <[email protected]>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for SparkFun SSD1306 based OLED display products.

src/SparkFun_Qwiic_OLED.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "qwiic_olednarrow.h"
5454
#include "qwiic_oledtransp.h"
5555
#include "qwiic_oled_1in3.h"
56+
#include "qwiic_oled_custom.h"
5657

5758
#include <Arduino.h>
5859
#include <Wire.h>
@@ -84,9 +85,10 @@ typedef QwBitmap QwiicBitmap;
8485

8586
template <typename SSD1306DeviceType>
8687
class QwiicOLEDBaseClass : public Print { // NOTE: implementing Arduino Print
87-
private:
88+
public:
8889
// our device driver
8990
SSD1306DeviceType m_device;
91+
private:
9092
QwI2C m_i2cBus; // our i2c object
9193

9294
// for the Aruduino print functionaliyt
@@ -836,3 +838,7 @@ class QwiicTransparentOLED : public QwiicOLEDBaseClass<QwOLEDTransparent> {
836838
class Qwiic1in3OLED : public QwiicOLEDBaseClass<QwOLED1in3> {
837839
// nothing here - see above
838840
};
841+
842+
class QwiicCustomOLED : public QwiicOLEDBaseClass<QwOLEDCustom> {
843+
// nothing here - see above
844+
};

src/qwiic_oled_custom.h

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// qwiic_oled_custom.h
2+
//
3+
// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306.
4+
//
5+
// SparkFun sells these at its website: www.sparkfun.com
6+
//
7+
// Do you like this library? Help support SparkFun. Buy a board!
8+
//
9+
// Micro OLED https://www.sparkfun.com/products/14532
10+
// Transparent OLED https://www.sparkfun.com/products/15173
11+
// "Narrow" OLED https://www.sparkfun.com/products/17153
12+
// 1.3" OLED https://www.sparkfun.com/products/23453
13+
//
14+
//
15+
// Written by Kirk Benell @ SparkFun Electronics, March 2022
16+
//
17+
// This library configures and draws graphics to OLED boards that use the
18+
// SSD1306 display hardware. The library only supports I2C.
19+
//
20+
// Repository:
21+
// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
22+
//
23+
// Documentation:
24+
// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
25+
//
26+
//
27+
// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
28+
//
29+
// SPDX-License-Identifier: MIT
30+
//
31+
// The MIT License (MIT)
32+
//
33+
// Copyright (c) 2022 SparkFun Electronics
34+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
35+
// associated documentation files (the "Software"), to deal in the Software without restriction,
36+
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
37+
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to
38+
// do so, subject to the following conditions:
39+
// The above copyright notice and this permission notice shall be included in all copies or substantial
40+
// portions of the Software.
41+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
42+
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
44+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46+
47+
// Implementation for the 1.3" OLED device
48+
49+
#pragma once
50+
51+
#include "qwiic_grssd1306.h"
52+
53+
//////////////////////////////////////////////////////////////////
54+
// Set the defaults for the SparkFun Qwiic MicroOLED
55+
56+
#define kOLEDCustomDefaultWidth 128
57+
#define kOLEDCustomDefaultHeight 64
58+
59+
#define kOLEDCustomDefaultXOffset 0
60+
#define kOLEDCustomDefaultYOffset 0
61+
62+
// Parameters for this device
63+
#define kOLEDCustomDefaultPinConfig 0x12
64+
#define kOLEDCustomDefaultPreCharge 0xF1
65+
#define kOLEDCustomDefaultVCOM 0x40
66+
#define kOLEDCustomDefaultContrast 0xCF
67+
68+
#define kOLEDCustomDefaultDefaultAddress 0x3D
69+
#define kOLEDCustomDefaultAltAddress 0x3C
70+
71+
class QwOLEDCustom : public QwGrSSD1306 {
72+
73+
public:
74+
// Constructor - setup the viewport and default address for this device.
75+
QwOLEDCustom()
76+
: QwGrSSD1306(kOLEDCustomDefaultXOffset, kOLEDCustomDefaultYOffset, kOLEDCustomDefaultWidth, kOLEDCustomDefaultHeight)
77+
{
78+
default_address = kOLEDCustomDefaultDefaultAddress;
79+
};
80+
81+
~QwOLEDCustom()
82+
{
83+
if (m_graphicsBuffer != nullptr)
84+
delete[] m_graphicsBuffer;
85+
};
86+
87+
// set up the specific device settings
88+
bool init(void)
89+
{
90+
setViewport(xOffset, yOffset, displayWidth, displayHeight);
91+
92+
setCommPins(pinConfig);
93+
setPreCharge(preCharge);
94+
setVcomDeselect(vcomDeselect);
95+
setContrast(contrast);
96+
97+
if (m_graphicsBuffer != nullptr)
98+
delete[] m_graphicsBuffer;
99+
m_graphicsBuffer = new uint8_t[(uint16_t)displayWidth * (uint16_t)displayHeight / 8];
100+
setBuffer(m_graphicsBuffer); // The buffer to use
101+
102+
// Call the super class to do all the work
103+
return this->QwGrSSD1306::init();
104+
};
105+
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;
114+
115+
protected:
116+
// Graphics buffer for this device.
117+
uint8_t *m_graphicsBuffer = nullptr;
118+
};

0 commit comments

Comments
 (0)