-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSD1306.h
312 lines (302 loc) · 10.3 KB
/
SSD1306.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#ifndef SSD1306
#define SSD1306
#define ADDR 0x3C
/**
* @author Saurav Sajeev
*/
#include <Arduino.h>
#include <Wire.h>
#ifdef __cplusplus
enum dimensions
{
WIDTH_128 = 0x80,
WIDTH_64 = 0x40,
HEIGHT_64 = 0x40,
HEIGHT_32 = 0x20
};
enum registryAddress
{
OLED_OFF = 0xAE,
OLED_ON = 0xAF,
INVERT = 0xA7,
REVERT = 0xA6,
ENTIRE_DISP_ON = 0xA5,
RESUME_FROM_VRAM = 0xA4,
DISP_CLOCK_DIV_RATIO = 0xD5,
MULTIPLEX = 0xA8,
CHRG_PUMP = 0x8D,
DISP_OFFSET = 0xD3,
MEM_ADDRESS_MODE = 0x20,
COM_CONFIG = 0xDA,
CONTRAST = 0x81,
PRE_CHRG = 0xD9,
VCOMH_DESEL = 0xDB,
TURN_ON_ALL_PIXELS = 0xA4,
SET_DISP_NORMAL = 0xA6,
SET_COL_ADDR = 0x21,
SET_PG_ADDR = 0x22
};
enum registryCommands
{
CLK_RATIO_RST = 0x80,
CLK_RATIO_HIGH = 0x80,
CLK_RATIO_MED = 0xB0,
CLK_RATIO_LOW = 0xF0,
MULTIPLEX_RATIO_RST = 0x3F,
OFFSET_NO = 0x00,
HSCROLL_RIGHT = 0x26,
HSCROLL_LEFT = 0x27,
VHSCROLL_RIGHT = 0x29,
VHSCROLL_LEFT = 0x2A,
STOP_SCROLL = 0x2E,
START_SCROLL = 0x2F,
START_LINE = 0x40,
CHRG_PUMP_75 = 0x24,
CHRG_PUMP_85 = 0x27,
CHRG_PUMP_90 = 0x4F,
CHRG_PUMP_OFF = 0x10,
HORIZONTAL = 0x00,
VERTICAL = 0x01,
PAGE = 0x02,
SEG_REMAP_0 = 0xA0,
SEG_REMAP_127 = 0xA1,
COM_OUT_SCAN_NORMAL = 0xC0,
COM_OUT_SCAN_REMAP = 0xC8,
COM_ALT = 0x12,
CONTRAST_RST = 0x7F,
CONTRAST_MAX = 0xFF,
PRE_CHRG_RST = 0x22,
PRE_CHRG_MAX = 0xAA,
VCOMH_65 = 0x00,
VCOMH_71 = 0x10,
VCOMH_77 = 0x20,
VCOMH_83 = 0x30,
ZERO = 0x00
};
enum powerModes
{
LOW_POWER_MODE = 0x01, // Low power consumption, slow display, uses I2C STANDARD MODE (100kHz)
BALANCED_MODE = 0x02, // Balanced power consumption, balanced performance, uses I2C STANDARD FAST MODE (200kHz)
PERFORMANCE_MODE = 0x03, // More power consumption, faster display, enables I2C FAST MODE (400kHz)
TURBO_MODE = 0x04 // I2C FAST MODE PLUS (1MHz). If your device doesn't support this mode, it will automatically switch to last selected mode.
};
class OLED
{
public:
/**
* @brief Constructor which initialize OLED display object. By default, address is 0x3C. If you want to use a different address, please specify it as the third parameter.
* @param width The width of the OLED display.
* @param height The height of the OLED display.
*/
OLED(uint8_t width, uint8_t height);
/**
* @brief Constructor which initialize OLED display object, with a custom I2C address.
* @param width The width of the OLED display.
* @param height The height of the OLED display.
* @param address The SSD1306 display's I2C address.
*/
OLED(uint8_t width, uint8_t height, uint8_t address);
/**
* @brief Set a custom font set for the characters. You could also set a custom language character.
* @param fontArray The array of fonts.
*/
void setFont(const uint8_t (*fontArray)[5]);
/**
* @brief Setup the display. You may call this in the setup function.
*/
void begin();
/**
* @brief Set the font as the default font.
*/
void clearCustomFont();
/**
* @brief Prints the string passed on the display at the specified X, Y coordinates.
* @param string The string to be printed.
* @param x The X coordinate.
* @param y The y coordinate.
*/
void print(String string, uint8_t x, uint8_t y);
/**
* @brief Prints the string passed on the display at the specified X, Y coordinates as highlighted.
* @param string The string to be printed.
* @param x The X coordinate.
* @param y The y coordinate.
*/
void printHighlighted(String string, uint8_t x, uint8_t y);
/**
* @brief Prints the string passed on the display at the specified X, Y coordinates with a typewriter animation.
* @param string The string to be printed.
* @param x The X coordinate.
* @param y The y coordinate.
* @param delay The delay between each character.
*/
void printAnimated(String string, uint8_t x, uint8_t y, int delay, bool highlight);
/**
* @brief Clears the screen and prints the string passed on the display at the specified X, Y coordinates.
* @param string The string to be printed.
* @param x The X coordinate.
* @param y The y coordinate.
*/
void print_c(String string, uint8_t x, uint8_t y);
/**
* @brief Accepts a dataSet and setup the display with custom preferences.
* @param dataSet Array of custom preferences.
*/
void manualSetup(uint8_t *dataSet);
/**
* @brief Clears the OLED display.
*/
void clearScr();
/**
* @brief Converts a string to dynamic char* array.
* @param string Input string.
* @returns Dynamic char* array of input string.
*/
char *convertString(String string);
/**
* @brief Displays a progress bar.
* @param progress Progress to be set.
* @param x The X coordinate.
* @param y The Y coordinate.
* @param style Progress bar variant [USE BETWEEN 1-15 VARIANTS].
* @note Styles 1-10 are progress bars and 11-15 are loaders.
*/
void progressBar(uint8_t progress, uint8_t x, uint8_t y, int style);
/**
* @brief Sets the display mode to off when clear screen is called.
* @param mode Set true to turn off the display.
*/
void turnOffOnClr(bool mode);
/**
* @brief Sets the brightness of the pixels in percentage.
* @param brightness Percentage of brightness.
*/
void setBrightness(uint8_t brightness);
/**
* @brief Draws a bitmap image on the display, respecting the specified coordinates.
* @param dataSet Bitmap array.
* @param x The X coordinate.
* @param y The Y coordinate.
* @param width Width of the bitmap
* @param height Height of the bitmap.
*/
void draw(const uint8_t *dataSet, uint8_t x, uint8_t y, uint8_t width, uint8_t height);
/**
* @brief Sets the power mode. There are four available power modes.
* @param mode The power mode. Use enum values for power mode - (LOW_POWER_MODE or BALANCED_MODE or PERFORMANCE_MODE or TURBO_MODE).
*/
void setPowerMode(uint8_t mode);
/**
* @brief Turns super brightness ON or OFF. Super brightness may be unstable.
* @param mode True or false to turn it ON or OFF.
*/
void superBrightness(bool mode);
/**
* @brief Inverts the display pixels.
*/
void invertDisplay();
/**
* @brief Turns all the pixels ON.
*/
void entireDisplayON();
/**
* @brief Reverts back to the content.
*/
void entireDisplayOFF();
/**
* @brief Draws a rectangle with the specified dimensions at the specified coordinates.
* @param startX Top-left X coordinate.
* @param startY Top-left Y coordinate.
* @param width Width of the rectangle in pixels
* @param height Height of the rectangle in pixels.
* @param cornerRadius Radius of each corner. Use 0 for flat.
* @param thickness Thickness of the rectangle.
* @param fill Fill the rectangle with pixels.
*/
void rectangle(uint8_t startX, uint8_t startY, uint8_t width, uint8_t height, uint8_t cornerRadius, uint8_t thickness, bool fill);
/**
* @brief Draws a circle at the specified coordinates.
* @param centerX X coordinate of center.
* @param centerY Y coordinate of center.
* @param radius Radius of the circle.
* @param thickness Thickness of the circle.
*/
void circle(uint8_t centerX, uint8_t centerY, uint8_t radius, uint8_t thickness);
/**
* @brief Draws a line between the specified coordinates.
* @param startX Starting X coordinate.
* @param startY Starting Y coordinate.
* @param endX Ending X coordinate.
* @param endY Ending Y coordinate.
* @param thickness Thickness of the line.
*/
void line(uint8_t startX, uint8_t startY, uint8_t endX, uint8_t endY, uint8_t thickness);
/**
* @name Text chaining operator.
*/
OLED &operator<<(String string);
/**
* @name Bitmap chaining operator.
*/
OLED &operator[](const uint8_t *bitmap);
/**
* @name Coordinates chaining operator.
*/
OLED &operator<<(int coordinate);
/**
* @brief Clears the area between the two specified coordinates.
* @param startX Starting point for clearing the screen.
* @param endX The point to which the display shall be cleared.
* @param page Page number.
*/
void clearArea(uint8_t startX, uint8_t endX, uint8_t page);
/**
* @brief Inflates the drawing canvas. Draws all the items on the display.
*/
void inflate();
/**
* @brief Inflates the drawing canvas and clears it. Similar to clearing screen before inflating next time.
*/
void inflateAndClear();
/**
* @brief Draws a pulse plot on the display.
* @param x The X coordinate.
* @param y The Y coordinate.
* @param width Width of the plot.
* @param height Height of the plot.
* @param data Data array.
* @param size Size of the data array.
* @param maxVal Maximum value of the data array.
* @note EXPERIMENTAL FEATURE
*/
void pulsePlot(uint8_t x, uint8_t y, uint8_t width, uint8_t height, int *data, uint8_t size, int maxVal);
private:
uint8_t HEIGHT = 0, WIDTH = 0, address = 0, charWidth = 0, step = 0, fontWidth = 5, currentPowerMode = BALANCED_MODE, invert = 0, pulsePlotPos = 0;
String stringToPrint;
uint8_t bitmapCoords[3];
uint8_t buffer[128 * 64 / 8];
const uint8_t *imgData;
char *tempString;
uint8_t count = 0, outMode = 0;
const uint8_t (*fontSet)[5];
bool IS_SETUP = false, clear = false;
void autoSetup();
void execute(uint8_t instruction);
void getFont(char c, bool highlight, uint8_t y);
void convert(String string);
void sendData(uint8_t data);
void setPosition(uint8_t x, uint8_t y);
void lowPowerMode(void);
void balancedPowerMode(void);
void performancePowerMode(void);
void drawPixel(uint8_t x, uint8_t y);
void displayBuffer(void);
void drawCircleQuarter(uint8_t centerX, uint8_t centerY, uint8_t radius, uint8_t corner);
uint8_t checkXBounds(uint8_t x);
uint8_t checkYBounds(uint8_t y);
void drawCircleHelper(int x0, int y0, int r, uint8_t cornerName);
void clearBuffer(void);
void drawFontPixel(uint8_t data, uint8_t x, uint8_t y);
};
#endif
#endif