NetBurner 3.5.6
PDF Version
qwiic_grssd1306.h
1// qwiic_gr1306.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//
13//
14// Written by Kirk Benell @ SparkFun Electronics, March 2022
15//
16// This library configures and draws graphics to OLED boards that use the
17// SSD1306 display hardware. The library only supports I2C.
18//
19// Repository:
20// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
21//
22// Documentation:
23// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
24//
25//
26// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
27//
28// SPDX-License-Identifier: MIT
29//
30// The MIT License (MIT)
31//
32// Copyright (c) 2022 SparkFun Electronics
33// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
34// associated documentation files (the "Software"), to deal in the Software without restriction,
35// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
36// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to
37// do so, subject to the following conditions:
38// The above copyright notice and this permission notice shall be included in all copies or substantial
39// portions of the Software.
40// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
41// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
43// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46/*
47 * Header file for the SSD1306 bitmap graphics driver device.
48 */
49
50#pragma once
51
52#include "qwiic_grbuffer.h"
53#include "qwiic_i2c.h"
54#include "res/qwiic_resdef.h"
55
57// Device Config
59//
60// Defaults
61// Each device can have a different Hardware pin configuration, which must
62// be set in the device. These are the pins that connect the display to
63// the SSD1306.
64//
65#define kDefaultPinConfig 0x12
66#define kDefaultPreCharge 0xF1
67#define kDefaultVCOMDeselect 0x40
68#define kDefaultContrast 0x8F
69
71// The graphics Raster Operator functions (ROPS)
73// - Copy - copy the pixel value in to the buffer (default)
74// - Not Copy - copy the not of the pixel value to buffer
75// - Not - Set the buffer value to not it's current value
76// - XOR - XOR of color and current pixel value
77// - Black - Set value to always be black
78// - White - set value to always be white
79
80typedef enum gr_op_funcs_
81{
82 grROPCopy = 0,
83 grROPNotCopy = 1,
84 grROPNot = 2,
85 grROPXOR = 3,
86 grROPBlack = 4,
87 grROPWhite = 5
88} grRasterOp_t;
89
91// Flags for scrolling
93
94#define SCROLL_VERTICAL 0x01
95#define SCROLL_RIGHT 0x02
96#define SCROLL_LEFT 0x04
97#define SCROLL_VERT_RIGHT SCROLL_VERTICAL | SCROLL_RIGHT
98#define SCROLL_VERT_LEFT SCROLL_VERTICAL | SCROLL_LEFT
99
100#define SCROLL_INTERVAL_5_FRAMES 0x00
101#define SCROLL_INTERVAL_64_FRAMES 0x01
102#define SCROLL_INTERVAL_128_FRAMES 0x02
103#define SCROLL_INTERVAL_256_FRAMES 0x03
104#define SCROLL_INTERVAL_3_FRAMES 0x04
105#define SCROLL_INTERVAL_4_FRAMES 0x05
106#define SCROLL_INTERVAL_25_FRAMES 0x06
107#define SCROLL_INTERVAL_2_FRAMES 0x07
108
110// Buffer Management
112//
113// The memory/back buffer of the SSD1306 is based on the concept of pages -
114// each page is a stream of bytes, and defined as follows:
115//
116// - X pixel position is an offset in a byte array
117// - Y pixel position is a bit in a byte, so a page can have 8 Y locations
118//
119// A pixel value of 1, turn on the corresponding pixel, 0 turns it off.
120//
121// The device has different data transfer modes - see the data sheet - mostly
122// outline how received a recieved byte is placed in the device framebuffer and the
123// next update locaton set.
124//
125// This implementation uses the Page mode for buffer transfer. This is defined by:
126// - A start position is set - a page number and column in that page.
127// - As data is transferred, it is written to the screenbuffer, based on this start
128// position
129// - If the end of the page is reached, the next entry location is the start of that page
130//
131// >> Implementation <<
132//
133// This implementation uses the concept of "dirty rects" at the page level to minimize data
134// transfers to the device. The min and max x locations set for each page is recorded as
135// graphics are draw to the graphics buffer. When the transfering the display buffer to
136// the devices screen buffer, the following takes place:
137//
138// For each page:
139// - if page is dirty
140// - Set the screen buffer current location to this page, xmin dirty value
141// - Write buffer bytes to the device - starting at xmin for the page, ending at xmax
142// - Mark the buffer as "clean"
143//
144//
145// Define variables to manage page state
146
147#define kMaxPageNumber 8
148
149typedef struct
150{
151 int16_t xmin;
152 int16_t xmax;
153} pageState_t;
154
156// QwGrSSD1306
157// A buffer graphics device to support the SSD1306 graphics hardware
158
159class QwGrSSD1306 : public QwGrBufferDevice
160{
161 private:
162 void setupDefaults(void);
163
164 public:
165 QwGrSSD1306()
166 {
167 setupDefaults(); // default constructor - always called
168 }
169 QwGrSSD1306(uint8_t width, uint8_t height) : QwGrSSD1306(0, 0, width, height){};
170
171 // call super class
172 QwGrSSD1306(uint8_t x0, uint8_t y0, uint8_t width, uint8_t height) : QwGrBufferDevice(x0, y0, width, height)
173 {
174 setupDefaults();
175 };
176
177 // Public draw methods
178 void display(void); // send graphics buffer to the devices screen buffer
179 void erase(void);
180
181 // Device setup
182 virtual bool init(void);
183
184 bool isInitialized(void)
185 {
186 return m_isInitialized;
187 };
188 bool reset(bool clearDisplay = true);
189
190 // method to set the communication bus this object should use
191 void setCommBus(QwI2C &theBus, uint8_t id_bus);
192
193 // Set the current color/pixel write operation
194 void setColor(uint8_t color);
195
196 // Settings/operational methods
197 void setContrast(uint8_t);
198
199 // default address of the device - expect the sub to fill in.
200 uint8_t default_address;
201
202 void setRasterOp(grRasterOp_t rop)
203 {
204 m_rop = rop;
205 }
206
207 grRasterOp_t rasterOp(void)
208 {
209 return m_rop;
210 }
211 // screen control
212 void invert(bool);
213 void flipVert(bool);
214 void flipHorz(bool);
215
216 // screen scrolling
217 void stopScroll(void);
218 void scroll(uint16_t scroll_type, uint8_t start, uint8_t stop, uint8_t interval = SCROLL_INTERVAL_2_FRAMES);
219
220 void displayPower(bool enable = true);
221
222 protected:
223 // Subclasses of this class define the specifics of the device, including size.
224 // Subclass needs to define the graphics buffer array - stack based - and pass in
225 void setBuffer(uint8_t *pBuffer);
226
228 // Internal, fast draw routines - this are used in the overall
229 // draw interface (_QwIDraw) for this object/device/system.
230 //
231 // >> Pixels <<
232 void drawPixel(uint8_t x, uint8_t y, uint8_t clr);
233
234 // >> Fast Lines <<
235 void drawLineHorz(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t clr);
236 void drawLineVert(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t clr);
237
238 // fast rect fill
239 void drawRectFilled(uint8_t x0, uint8_t y0, uint8_t width, uint8_t height, uint8_t clr);
240
241 // >> Fast Bitmap <<
242 void drawBitmap(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t *pBitmap, uint8_t bmp_width,
243 uint8_t bmp_height);
244
246 // configuration methods for sub-classes. Settings unique to a device
247 void setCommPins(uint8_t);
248 void setPreCharge(uint8_t);
249 void setVcomDeselect(uint8_t);
250
251 private:
252 // Internal buffer management methods
253 bool setScreenBufferAddress(uint8_t page, uint8_t column);
254 void initBuffers(void); // clear graphics and screen buffer
255 void clearScreenBuffer(void);
256 void resendGraphics(void);
257 void setupOLEDDevice(bool clearDisplay = true);
258
259 // device communication methods
260 void sendDevCommand(uint8_t command);
261 void sendDevCommand(uint8_t command, uint8_t value);
262 void sendDevCommand(uint8_t *commands, uint8_t n);
263 void sendDevData(uint8_t *pData, uint8_t nData);
264
266 // instance vars
267
268 // Buffer variables
269 uint8_t *m_pBuffer; // Pointer to the graphics buffer
270 uint8_t m_nPages; // number of pages for current device
271 pageState_t m_pageState[kMaxPageNumber]; // page state descriptors
272 pageState_t m_pageErase[kMaxPageNumber]; // keep track of erase boundaries
273 bool m_pendingErase;
274
275 // display variables
276 uint8_t m_color; // current color (really 0 or 1)
277 grRasterOp_t m_rop; // current raster operation code
278
279 // I2C things
280 QwI2C *m_i2cBus; // pointer to our i2c bus object
281 uint8_t m_i2cAddress; // address of the device
282
283 // Stash values for settings that are unique to each device.
284 uint8_t m_initHWComPins;
285 uint8_t m_initPreCharge;
286 uint8_t m_initVCOMDeselect;
287 uint8_t m_initContrast;
288
289 bool m_isInitialized; // general init flag
290};