Skip to content

Commit bfdd195

Browse files
committed
Various improvements
1 parent 94bebd3 commit bfdd195

File tree

8 files changed

+2237
-1887
lines changed

8 files changed

+2237
-1887
lines changed

SSD1306.cpp

+470-222
Large diffs are not rendered by default.

SSD1306.h

+158-102
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,69 @@
1-
/**The MIT License (MIT)
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+
*/
227

3-
Copyright (c) 2015 by Daniel Eichhorn
28+
#pragma once
429

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
30+
#include <Arduino.h>
31+
#include <Wire.h>
1132

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
33+
#include "SSD1306Fonts.h"
1434

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
35+
//#define DEBUG_SSD1306(...) Serial.printf( __VA_ARGS__ )
2236

23-
See more at http://blog.squix.ch
37+
#ifndef DEBUG_SSD1306
38+
#define DEBUG_SSD1306(...)
39+
#endif
2440

25-
Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
26-
*/
27-
#pragma once
41+
// Use DOUBLE BUFFERING by default
42+
#ifndef SSD1306_REDUCE_MEMORY
43+
#define SSD1306_DOUBLE_BUFFER
44+
#endif
2845

29-
#include <Arduino.h>
30-
#include "SSD1306Fonts.h"
3146

32-
#define BLACK 0
33-
#define WHITE 1
34-
#define INVERSE 2
47+
// Display settings
48+
#define DISPLAY_WIDTH 128
49+
#define DISPLAY_HEIGHT 64
50+
#define DISPLAY_BUFFER_SIZE 1024
51+
52+
// Header Values
53+
#define JUMPTABLE_BYTES 4
54+
#define JUMPTABLE_LSB 1
55+
#define JUMPTABLE_SIZE 2
56+
#define JUMPTABLE_WIDTH 3
57+
58+
#define JUMPTABLE_START 4
3559

3660
#define WIDTH_POS 0
3761
#define HEIGHT_POS 1
3862
#define FIRST_CHAR_POS 2
3963
#define CHAR_NUM_POS 3
40-
#define CHAR_WIDTH_START_POS 4
4164

42-
#define TEXT_ALIGN_LEFT 0
43-
#define TEXT_ALIGN_CENTER 1
44-
#define TEXT_ALIGN_RIGHT 2
4565

66+
// Display commands
4667
#define CHARGEPUMP 0x8D
4768
#define COLUMNADDR 0x21
4869
#define COMSCANDEC 0xC8
@@ -70,104 +91,139 @@ Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
7091
#define SETVCOMDETECT 0xDB
7192
#define SWITCHCAPVCC 0x2
7293

94+
enum SSD1306_COLOR {
95+
BLACK = 0,
96+
WHITE = 1,
97+
INVERSE = 2
98+
};
99+
100+
enum SSD1306_TEXT_ALIGNMENT {
101+
TEXT_ALIGN_LEFT = 0,
102+
TEXT_ALIGN_RIGHT = 1,
103+
TEXT_ALIGN_CENTER = 2,
104+
TEXT_ALIGN_CENTER_BOTH = 3
105+
};
106+
73107
class SSD1306 {
108+
private:
109+
110+
uint8_t i2cAddress;
111+
uint8_t sda;
112+
uint8_t sdc;
113+
114+
uint8_t *buffer;
115+
116+
#ifdef SSD1306_DOUBLE_BUFFER
117+
uint8_t *buffer_back;
118+
#endif
119+
120+
SSD1306_TEXT_ALIGNMENT textAlignment = TEXT_ALIGN_LEFT;
121+
SSD1306_COLOR color = WHITE;
122+
123+
const char *fontData = ArialMT_Plain_10;
124+
125+
// Send a command to the display (low level function)
126+
void sendCommand(unsigned char com);
127+
128+
// Send all the init commands
129+
void sendInitCommands(void);
130+
131+
// converts utf8 characters to extended ascii
132+
byte utf8ascii(byte ascii);
133+
char* utf8ascii(String s);
134+
135+
void drawInternal(int16_t xMove, int16_t yMove, int16_t width, int16_t height, const char *data, uint16_t offset, uint16_t bytesInData);
136+
137+
public:
138+
139+
// Create the display object connected to pin sda and sdc
140+
SSD1306(uint8_t i2cAddress, uint8_t sda, uint8_t sdc);
141+
142+
// Initialize the display
143+
bool init();
74144

75-
private:
76-
int myI2cAddress;
77-
int mySda;
78-
int mySdc;
79-
uint8_t buffer[128 * 64 / 8];
80-
int myTextAlignment = TEXT_ALIGN_LEFT;
81-
int myColor = WHITE;
82-
byte lastChar;
83-
const char *myFontData = ArialMT_Plain_10;
145+
// Free the memory used by the display
146+
void end();
84147

85-
public:
86-
// Create the display object connected to pin sda and sdc
87-
SSD1306(int i2cAddress, int sda, int sdc);
148+
// Cycle through the initialization
149+
void resetDisplay(void);
88150

89-
// Initialize the display
90-
void init();
151+
// Connect again to the display through I2C
152+
void reconnect(void);
91153

92-
// Cycle through the initialization
93-
void resetDisplay(void);
154+
/* Drawing functions */
94155

95-
// Connect again to the display through I2C
96-
void reconnect(void);
156+
// Sets the color of all pixel operations
157+
void setColor(SSD1306_COLOR color);
97158

98-
// Turn the display on
99-
void displayOn(void);
159+
// Draw a pixel at given position
160+
void setPixel(int16_t x, int16_t y);
100161

101-
// Turn the display offs
102-
void displayOff(void);
162+
// Draw the border of a rectangle at the given location
163+
void drawRect(int16_t x, int16_t y, int16_t width, int16_t height);
103164

104-
// Clear the local pixel buffer
105-
void clear(void);
165+
// Fill the rectangle
166+
void fillRect(int16_t x, int16_t y, int16_t width, int16_t height);
106167

107-
// Write the buffer to the display memory
108-
void display(void);
168+
// Draw a line horizontally
169+
void drawHorizontalLine(int16_t x, int16_t y, int16_t length);
109170

110-
// Set display contrast
111-
void setContrast(char contrast);
171+
// Draw a lin vertically
172+
void drawVerticalLine(int16_t x, int16_t y, int16_t length);
112173

113-
// Turn the display upside down
114-
void flipScreenVertically();
174+
// Draw a bitmap in the internal image format
175+
void drawFastImage(int16_t x, int16_t y, int16_t width, int16_t height, const char *image);
115176

116-
// Send a command to the display (low level function)
117-
void sendCommand(unsigned char com);
177+
// Draw a XBM
178+
void drawXbm(int16_t x, int16_t y, int16_t width, int16_t height, const char *xbm);
118179

119-
// Send all the init commands
120-
void sendInitCommands(void);
180+
/* Text functions */
121181

122-
// Draw a pixel at given position
123-
void setPixel(int x, int y);
182+
// Draws a string at the given location
183+
void drawString(int16_t x, int16_t y, String text);
124184

125-
// Draw 8 bits at the given position
126-
void setChar(int x, int y, unsigned char data);
185+
// Draws a String with a maximum width at the given location.
186+
// If the given String is wider than the specified width
187+
// The text will be wrapped to the next line at a space or dash
188+
void drawStringMaxWidth(int16_t x, int16_t y, int16_t maxLineWidth, String text);
127189

128-
// Draw the border of a rectangle at the given location
129-
void drawRect(int x, int y, int width, int height);
190+
// Returns the width of the String with the current
191+
// font settings
192+
uint16_t getStringWidth(const char* text);
130193

131-
// Fill the rectangle
132-
void fillRect(int x, int y, int width, int height);
194+
// Specifies relative to which anchor point
195+
// the text is rendered. Available constants:
196+
// TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER_BOTH
197+
void setTextAlignment(SSD1306_TEXT_ALIGNMENT textAlignment);
133198

134-
// Draw a bitmap with the given dimensions
135-
void drawBitmap(int x, int y, int width, int height, const char *bitmap);
199+
// Sets the current font. Available default fonts
200+
// ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24
201+
void setFont(const char *fontData);
136202

137-
// Draw an XBM image with the given dimensions
138-
void drawXbm(int x, int y, int width, int height, const char *xbm);
203+
/* Display functions */
139204

140-
// Sets the color of all pixel operations
141-
void setColor(int color);
205+
// Turn the display on
206+
void displayOn(void);
142207

143-
// converts utf8 characters to extended ascii
144-
// taken from http://playground.arduino.cc/Main/Utf8ascii
145-
byte utf8ascii(byte ascii);
208+
// Turn the display offs
209+
void displayOff(void);
146210

147-
// converts utf8 string to extended ascii
148-
// taken from http://playground.arduino.cc/Main/Utf8ascii
149-
String utf8ascii(String s);
211+
// Inverted display mode
212+
void invertDisplay(void);
150213

151-
// Draws a string at the given location
152-
void drawString(int x, int y, String text);
214+
// Normal display mode
215+
void normalDisplay(void);
153216

154-
// Draws a String with a maximum width at the given location.
155-
// If the given String is wider than the specified width
156-
// The text will be wrapped to the next line at a space or dash
157-
void drawStringMaxWidth(int x, int y, int maxLineWidth, String text);
217+
// Set display contrast
218+
void setContrast(char contrast);
158219

159-
// Returns the width of the String with the current
160-
// font settings
161-
int getStringWidth(String text);
220+
// Turn the display upside down
221+
void flipScreenVertically();
162222

163-
// Specifies relative to which anchor point
164-
// the text is rendered. Available constants:
165-
// TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT
166-
void setTextAlignment(int textAlignment);
223+
// Write the buffer to the display memory
224+
void display(void);
167225

168-
// Sets the current font. Available default fonts
169-
// defined in SSD1306Fonts.h:
170-
// ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24
171-
void setFont(const char *fontData);
226+
// Clear the local pixel buffer
227+
void clear(void);
172228

173-
};
229+
};

0 commit comments

Comments
 (0)