Skip to content

Commit 7b4e583

Browse files
committed
Add abstraction for hardware interface
1 parent 086e891 commit 7b4e583

File tree

9 files changed

+799
-488
lines changed

9 files changed

+799
-488
lines changed

SSD1306.cpp renamed to OLEDDisplay.cpp

Lines changed: 47 additions & 204 deletions
Large diffs are not rendered by default.

OLEDDisplay.h

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 by Daniel Eichhorn
5+
* Copyright (c) 2016 by Fabrice Weinberg
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
* Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
26+
*/
27+
28+
#ifndef OLEDDISPLAY_h
29+
#define OLEDDISPLAY_h
30+
31+
#include <Arduino.h>
32+
#include "OLEDDisplayFonts.h"
33+
34+
//#define DEBUG_OLEDDISPLAY(...) Serial.printf( __VA_ARGS__ )
35+
36+
#ifndef DEBUG_OLEDDISPLAY
37+
#define DEBUG_OLEDDISPLAY(...)
38+
#endif
39+
40+
// Use DOUBLE BUFFERING by default
41+
#ifndef OLEDDISPLAY_REDUCE_MEMORY
42+
#define OLEDDISPLAY_DOUBLE_BUFFER
43+
#endif
44+
45+
46+
// Display settings
47+
#define DISPLAY_WIDTH 128
48+
#define DISPLAY_HEIGHT 64
49+
#define DISPLAY_BUFFER_SIZE 1024
50+
51+
// Header Values
52+
#define JUMPTABLE_BYTES 4
53+
54+
#define JUMPTABLE_LSB 1
55+
#define JUMPTABLE_SIZE 2
56+
#define JUMPTABLE_WIDTH 3
57+
#define JUMPTABLE_START 4
58+
59+
#define WIDTH_POS 0
60+
#define HEIGHT_POS 1
61+
#define FIRST_CHAR_POS 2
62+
#define CHAR_NUM_POS 3
63+
64+
65+
// Display commands
66+
#define CHARGEPUMP 0x8D
67+
#define COLUMNADDR 0x21
68+
#define COMSCANDEC 0xC8
69+
#define COMSCANINC 0xC0
70+
#define DISPLAYALLON 0xA5
71+
#define DISPLAYALLON_RESUME 0xA4
72+
#define DISPLAYOFF 0xAE
73+
#define DISPLAYON 0xAF
74+
#define EXTERNALVCC 0x1
75+
#define INVERTDISPLAY 0xA7
76+
#define MEMORYMODE 0x20
77+
#define NORMALDISPLAY 0xA6
78+
#define PAGEADDR 0x22
79+
#define SEGREMAP 0xA0
80+
#define SETCOMPINS 0xDA
81+
#define SETCONTRAST 0x81
82+
#define SETDISPLAYCLOCKDIV 0xD5
83+
#define SETDISPLAYOFFSET 0xD3
84+
#define SETHIGHCOLUMN 0x10
85+
#define SETLOWCOLUMN 0x00
86+
#define SETMULTIPLEX 0xA8
87+
#define SETPRECHARGE 0xD9
88+
#define SETSEGMENTREMAP 0xA1
89+
#define SETSTARTLINE 0x40
90+
#define SETVCOMDETECT 0xDB
91+
#define SWITCHCAPVCC 0x2
92+
93+
#ifndef _swap_int16_t
94+
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
95+
#endif
96+
97+
enum OLEDDISPLAY_COLOR {
98+
BLACK = 0,
99+
WHITE = 1,
100+
INVERSE = 2
101+
};
102+
103+
enum OLEDDISPLAY_TEXT_ALIGNMENT {
104+
TEXT_ALIGN_LEFT = 0,
105+
TEXT_ALIGN_RIGHT = 1,
106+
TEXT_ALIGN_CENTER = 2,
107+
TEXT_ALIGN_CENTER_BOTH = 3
108+
};
109+
110+
111+
class OLEDDisplay {
112+
public:
113+
// Initialize the display
114+
bool init();
115+
116+
// Free the memory used by the display
117+
void end();
118+
119+
// Cycle through the initialization
120+
void resetDisplay(void);
121+
122+
/* Drawing functions */
123+
// Sets the color of all pixel operations
124+
void setColor(OLEDDISPLAY_COLOR color);
125+
126+
// Draw a pixel at given position
127+
void setPixel(int16_t x, int16_t y);
128+
129+
// Draw a line from position 0 to position 1
130+
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
131+
132+
// Draw the border of a rectangle at the given location
133+
void drawRect(int16_t x, int16_t y, int16_t width, int16_t height);
134+
135+
// Fill the rectangle
136+
void fillRect(int16_t x, int16_t y, int16_t width, int16_t height);
137+
138+
// Draw the border of a circle
139+
void drawCircle(int16_t x, int16_t y, int16_t radius);
140+
141+
// Fill circle
142+
void fillCircle(int16_t x, int16_t y, int16_t radius);
143+
144+
// Draw a line horizontally
145+
void drawHorizontalLine(int16_t x, int16_t y, int16_t length);
146+
147+
// Draw a lin vertically
148+
void drawVerticalLine(int16_t x, int16_t y, int16_t length);
149+
150+
/**
151+
* Draws a rounded progress bar with the outer dimensions given by width and height.
152+
*/
153+
void drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress);
154+
155+
// Draw a bitmap in the internal image format
156+
void drawFastImage(int16_t x, int16_t y, int16_t width, int16_t height, const char *image);
157+
158+
// Draw a XBM
159+
void drawXbm(int16_t x, int16_t y, int16_t width, int16_t height, const char *xbm);
160+
161+
/* Text functions */
162+
163+
// Draws a string at the given location
164+
void drawString(int16_t x, int16_t y, String text);
165+
166+
// Draws a String with a maximum width at the given location.
167+
// If the given String is wider than the specified width
168+
// The text will be wrapped to the next line at a space or dash
169+
void drawStringMaxWidth(int16_t x, int16_t y, uint16_t maxLineWidth, String text);
170+
171+
// Returns the width of the const char* with the current
172+
// font settings
173+
uint16_t getStringWidth(const char* text, uint16_t length);
174+
175+
// Convencience method for the const char version
176+
uint16_t getStringWidth(String text);
177+
178+
// Specifies relative to which anchor point
179+
// the text is rendered. Available constants:
180+
// TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER_BOTH
181+
void setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment);
182+
183+
// Sets the current font. Available default fonts
184+
// ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24
185+
void setFont(const char *fontData);
186+
187+
/* Display functions */
188+
189+
// Turn the display on
190+
void displayOn(void);
191+
192+
// Turn the display offs
193+
void displayOff(void);
194+
195+
// Inverted display mode
196+
void invertDisplay(void);
197+
198+
// Normal display mode
199+
void normalDisplay(void);
200+
201+
// Set display contrast
202+
void setContrast(char contrast);
203+
204+
// Turn the display upside down
205+
void flipScreenVertically();
206+
207+
// Write the buffer to the display memory
208+
virtual void display(void);
209+
210+
// Clear the local pixel buffer
211+
void clear(void);
212+
213+
uint8_t *buffer;
214+
215+
#ifdef OLEDDISPLAY_DOUBLE_BUFFER
216+
uint8_t *buffer_back;
217+
#endif
218+
219+
protected:
220+
OLEDDISPLAY_TEXT_ALIGNMENT textAlignment = TEXT_ALIGN_LEFT;
221+
OLEDDISPLAY_COLOR color = WHITE;
222+
223+
const char *fontData = ArialMT_Plain_10;
224+
225+
// Send a command to the display (low level function)
226+
virtual void sendCommand(uint8_t com);
227+
228+
// Connect to the display
229+
virtual bool connect();
230+
231+
// Send all the init commands
232+
void sendInitCommands();
233+
234+
// converts utf8 characters to extended ascii
235+
static char* utf8ascii(String s);
236+
static byte utf8ascii(byte ascii);
237+
238+
void inline drawInternal(int16_t xMove, int16_t yMove, int16_t width, int16_t height, const char *data, uint16_t offset, uint16_t bytesInData) __attribute__((always_inline));
239+
240+
void drawStringInternal(int16_t xMove, int16_t yMove, char* text, uint16_t textLength, uint16_t textWidth);
241+
242+
};
243+
244+
#endif

SSD1306Fonts.h renamed to OLEDDisplayFonts.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef OLEDDISPLAYFONTS_h
2+
#define OLEDDISPLAYFONTS_h
3+
14
const char ArialMT_Plain_10[] PROGMEM = {
25
0x0A, // Width: 10
36
0x0D, // Height: 13
@@ -1268,3 +1271,4 @@ const char ArialMT_Plain_24[] PROGMEM = {
12681271
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0xFF,0x07,0xE0,0xFF,0xFF,0x07,0x00,0x1C,0x18,0x00,0x00,0x06,0x30,0x00,0x00,0x06,0x30,0x00,0x00,0x06,0x30,0x00,0x00,0x0E,0x38,0x00,0x00,0x1C,0x1C,0x00,0x00,0xF8,0x0F,0x00,0x00,0xF0,0x03, // 254
12691272
0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x7E,0x00,0x06,0xC0,0xF0,0x01,0x06,0xC0,0x80,0x0F,0x07,0x00,0x00,0xFE,0x03,0x00,0x00,0xFC,0x00,0xC0,0xC0,0x1F,0x00,0xC0,0xF8,0x03,0x00,0x00,0x3E,0x00,0x00,0x00,0x06 // 255
12701273
};
1274+
#endif

0 commit comments

Comments
 (0)