-
Notifications
You must be signed in to change notification settings - Fork 1
/
Eliuslab_SSD1307_OLED_Driver.h
245 lines (226 loc) · 9.11 KB
/
Eliuslab_SSD1307_OLED_Driver.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
/*
Eliuslab_SSD1307_OLED_Driver.h - Eliuslab_SSD1307_OLED_Driver library for ATMEGA328P - implementation
Copyright (c) 2020 Elia Lazzari. All right reserved.
*/
// ensure this library description is only included once
#ifndef Test_h
#define Test_h
#ifndef USE_EEPROM
#define USE_EEPROM
#endif
#ifdef USE_EEPROM
#include <EEPROM.h>
#endif
#include <SPI.h>
#include "fonts/fonts.h"
extern const PROGMEM byte Terminal_8pt[];
extern const PROGMEM byte MedProp_11pt[];
extern const PROGMEM byte LargeProp_25pt[];
extern const PROGMEM byte LCDLarge_24pt[];
//=====================================================================================
const PROGMEM unsigned char testBitmap[4][128] = { //Position(coordinate)
0xFF, 0x11, 0x01, 0x01, 0x03, 0x01, 0x41, 0x81,
0x41, 0x8F, 0x41, 0x81, 0x41, 0x81, 0x43, 0x81,
0x41, 0x81, 0x41, 0x8F, 0x41, 0x81, 0x41, 0x81,
0x43, 0x81, 0x41, 0x81, 0x41, 0x8F, 0x41, 0x81,
0x41, 0x81, 0x43, 0x81, 0x41, 0x81, 0x41, 0x8F,
0x41, 0x81, 0x41, 0x01, 0x03, 0x01, 0x81, 0x81,
0x81, 0x0F, 0x81, 0x81, 0x81, 0x01, 0x03, 0x01,
0x41, 0x81, 0x41, 0x8F, 0x41, 0x81, 0x41, 0x81,
0x43, 0x81, 0x41, 0x81, 0x41, 0x8F, 0x41, 0x81,
0x41, 0x81, 0x43, 0x81, 0x41, 0x81, 0x41, 0x8F,
0x41, 0x81, 0x41, 0x81, 0x43, 0x81, 0x41, 0x81,
0x41, 0x8F, 0x41, 0x81, 0x41, 0x01, 0x03, 0x01,
0x81, 0x01, 0x81, 0x8F, 0x81, 0x01, 0x81, 0x81,
0x83, 0x01, 0x01, 0x01, 0x41, 0x8F, 0x41, 0x81,
0x41, 0x81, 0x43, 0x81, 0x41, 0x81, 0x41, 0x8F,
0x41, 0x81, 0x41, 0x81, 0x43, 0x81, 0x41, 0xFF,
0xFF, 0x42, 0x02, 0x02, 0x00, 0x00, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0x80, 0x40, 0x80, 0x4B, 0x8A,
0x4E, 0x80, 0x4F, 0x88, 0x4F, 0x80, 0x40, 0x80,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55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5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA,
0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xAA, 0xD5, 0xFF};
/* Various fonts */
enum fontName
{
_Terminal_8pt = 1,
_MedProp_11pt = 2,
_LargeProp_25pt = 3,
_LCDLarge_24pt = 4
};
// Display Settings
#define SSD1307_DISPLAY_OFF 0xAE // Display Off
#define SSD1307_DISPLAY_CLOCK 0xD5 //SET DISPLAY CLOCK
#define SSD1307_SELECT_MULTIPLEX_RATIO 0xA8 // Select Multiplex Ratio
#define SSD1307_DISPLAY_OFFSET 0xD3 //Setting Display Offset
#define SSD1307_DISPLAY_START_LINE 0x40 //Set Display Start Line
#define SSD1307_SEGMENT_REMAP_DEFAULT 0xA1 //Set Segment Re-Map Default
#define SSD1307_SEGMENT_REMAP_INVERTED 0xA0 //Set Segment Re-Map Default
//0xA0 (0x00) => column Address 0 mapped to 127
//0xA1 (0x01) => Column Address 127 mapped to 0
#define SSD1307_SET_COM_OUTPUT_SCAN_DIRECTION 0xC0 //Set COM Output Scan Direction
#define SSD1307_SET_COM_HW_CONFIGURATION 0xDA //Set COM Hardware Configuration
#define SSD1307_DEFAULT_COM_PIN 0x12 //Alternative COM Pin---See IC Spec page 34
#define SSD1307_SET_CONTRAST_CONTROL 0x81 //Set Contrast Control
#define SSD1307_DFAULT_CONTRAST 0x48
#define SSD1307_SET_PRE_CHARGE_PERIOD 0xD9 //Set Pre-Charge period
#define SSD1307_DEFAULT_PRE_CHARGE_PERIOD 0xA1
#define SSD1307_SET_DESELECT_VCOMH_LEVEL 0xDB //Set Deselect Vcomh level
#define SSD1307_ENTIRE_DISPLAY_ON 0xA4 //Entire Display ON
#define SSD1307_SET_NORMAL_DISPLAY 0xA6 //Set Normal Display
#define SSD1307_DISPLAY_ON 0xAF // Display ON
// Display Scrolling Parameters
#define SSD1307_RIGHT_HORIZONTAL_SCROLL 0x26 ///< Init rt scroll
#define SSD1307_LEFT_HORIZONTAL_SCROLL 0x27 ///< Init left scroll
#define SSD1307_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 ///< Init diag scroll
#define SSD1307_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A ///< Init diag scroll
#define SSD1307_DEACTIVATE_SCROLL 0x2E ///< Stop scroll
#define SSD1307_ACTIVATE_SCROLL 0x2F ///< Start scroll
#define SSD1307_SET_VERTICAL_SCROLL_AREA 0xA3 ///< Set scroll range
#define SSD1307_128_32_RES_X 128
#define SSD1307_128_32_RES_Y 32
#define SSD1307_128_32_GRAM_COL_START 0x00
#define SSD1307_128_32_GRAM_COL_END 0x7F
#define SSD1307_128_32_GRAM_PAGE_START 0
#define SSD1307_128_32_GRAM_PAGE_END 3
#define SPI_SETTINGS SPISettings(8000000, MSBFIRST, SPI_MODE0)
/* Display resolution */
#define BUFFERCOLSIZE 128
#define BUFFERROWSIZE 4
/* Display command register addresses */
#define COLLOWADD 0x00
#define COLHIGHADD 0x10
#define MEMORYADDRESSMODE 0x20
#define SETCOLADDRESS 0x21
#define SETPAGEADDRESS 0x22
#define CHARGEPUMP 0x8D
#define SETMUXRATIO 0xA8
#define DISPLAYONADD 0xAE
#define PAGEADD 0xB0
#define STARTLINEADD 0x40
#define SCANDIRECTIONADD 0xC0
#define SEGMENTMAPADD 0xA0
/* Command register parameters */
#define SCANDIRNORMAL 0x00
#define SCANDIRREVERSE 0x08
#define SEGMENTMAPNORMAL 0
#define SEGMENTMAPREVERSE 1
#define ENABLECHARGEPUMP 0x14
#define HORIZONTALADDRESSMODE 0x00
/* Various draw modes */
enum HCuOLEDDrawModes
{
NORMAL = 1,
INVERT = 2,
CLEAR = 3,
OUTLINE = 4,
SOLID = 5
};
// library interface description
class Eliuslab_OLED
{
// user-accessible "public" interface
public:
Eliuslab_OLED(uint8_t _width, uint8_t _height, uint8_t _ssPin, uint8_t _dcPin, uint8_t _rstPin);
void begin(void);
void startScrollRight(uint8_t start, uint8_t _stop);
void startScrollLeft(uint8_t start, uint8_t _stop);
void startScrollDiagRight(uint8_t start, uint8_t _stop);
void stopScroll(void);
void full_on(void);
void full_off(void);
void reset(void);
void test(void);
#ifdef USE_EEPROM
void PrintBitmapFromEEPROM(uint16_t startAddress);
void BurnTestBitmapToEEPROM(uint16_t startAddress);
#endif
byte BufferStartOffset;
// DRAWING FUNCTIONS
void Refresh(void);
static void ClearBuffer(void);
//void ClearDisplayRam(void);
void Flip_H(void);
void Flip_V(void);
static void Bitmap(uint8_t Cols, uint8_t ByteRows, const uint8_t BitmapData[]);
static void Cursor(uint8_t X, uint8_t Y);
static void Print(char TextString[]);
static void Print(long Value);
static void Print(int long Value, byte DecimalPlaces);
static void Print(float value, byte digits, byte DecimalPlaces);
static void SetFont(uint8_t font_name);
static void Plot(uint8_t X, uint8_t Y);
static void Line(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2);
static void Rect(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2, uint8_t FillMode);
static void Erase(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2);
static void DrawMode(byte DrawMode);
// library-accessible "private" interface
private:
uint8_t width, height;
uint8_t ssPin, dcPin, rstPin;
void Initial_SSD1307ZD(void);
void Write_command(byte command);
void Write_data(byte data);
//unsigned char testBitmap[4][128];
static void _WriteChar(char character);
bool _V_Ori;
bool _H_Ori;
static byte _XPos;
static byte _YPos;
static const byte *_FontType;
static const uint16_t *_FontDescriptor;
static byte _FontHight;
//byte _DisplayRAMColSize;
static byte _DrawMode;
byte _Res_Max_X;
byte _Res_Max_Y;
byte _GRAM_Col_Start;
byte _GRAM_Col_End;
byte _GRAM_Page_Start;
byte _GRAM_Page_End;
byte _RAM_Pages;
};
#endif