-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGyverOLED.h
285 lines (221 loc) · 6.33 KB
/
GyverOLED.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
// AlexGyver, alex@alexgyver.ru
// https://alexgyver.ru/
// MIT License
#ifndef GyverOLED_h
#define GyverOLED_h
#include <Arduino.h>
#include <SPI.h>
#include "charMap.h"
#include <Print.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT_64 0x12
#define OLED_64 0x3F
#define OLED_DISPLAY_OFF 0xAE
#define OLED_DISPLAY_ON 0xAF
#define OLED_ADDRESSING_MODE 0x20
#define OLED_VERTICAL 0x01
#define OLED_NORMAL_V 0xC8
#define OLED_NORMAL_H 0xA1
#define OLED_CONTRAST 0x81
#define OLED_SETCOMPINS 0xDA
#define OLED_SETVCOMDETECT 0xDB
#define OLED_CLOCKDIV 0xD5
#define OLED_SETMULTIPLEX 0xA8
#define OLED_COLUMNADDR 0x21
#define OLED_PAGEADDR 0x22
#define OLED_CHARGEPUMP 0x8D
#define OLED_NORMALDISPLAY 0xA6
#ifndef OLED_SPI_SPEED
#define OLED_SPI_SPEED 1000000ul
#endif
static SPISettings OLED_SPI_SETT(OLED_SPI_SPEED, MSBFIRST, SPI_MODE0);
static const uint8_t _oled_init[] PROGMEM = {
OLED_DISPLAY_OFF,
OLED_CLOCKDIV,
0x80, // value
OLED_CHARGEPUMP,
0x14, // value
OLED_ADDRESSING_MODE,
OLED_VERTICAL,
OLED_NORMAL_H,
OLED_NORMAL_V,
OLED_CONTRAST,
0x7F, // value
OLED_SETVCOMDETECT,
0x40, // value
OLED_NORMALDISPLAY,
OLED_DISPLAY_ON,
};
template<uint8_t _CS, uint8_t _DC, uint8_t _RST>
class GyverOLED : public Print {
public:
void init() {
SPI.begin();
pinMode(_CS, OUTPUT);
fastWrite(_CS, 1);
pinMode(_DC, OUTPUT);
pinMode(_RST, OUTPUT);
fastWrite(_RST, 1);
delay(1);
fastWrite(_RST, 0);
delay(20);
fastWrite(_RST, 1);
beginCommand();
for (uint8_t i = 0; i < 15; i++) SPI.transfer(pgm_read_byte(&_oled_init[i]));
endTransm();
beginCommand();
SPI.transfer(OLED_SETCOMPINS);
SPI.transfer(OLED_HEIGHT_64);
SPI.transfer(OLED_SETMULTIPLEX);
SPI.transfer(OLED_64);
endTransm();
setCursorXY(0, 0);
}
void clear() {
fill(0);
}
void clear(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
x1++;
y1++;
y0 >>= 3;
y1 = (y1 - 1) >> 3;
setWindow(x0, y0, x1, y1);
beginData();
for (uint8_t x = x0; x < x1; x++)
for (uint8_t y = y0; y < y1 + 1; y++)
SPI.transfer(0);
endTransm();
setCursorXY(_x, _y);
}
void setContrast(uint8_t value) {
sendCommand(OLED_CONTRAST, value);
}
virtual size_t write(uint8_t data) {
bool newPos = false;
if (data == '\r') {
_x = 0;
newPos = true;
data = 0;
} else if (data == '\n') {
_y += _scaleY;
newPos = true;
data = 0;
_getn = 1;
}
if (newPos) setCursorXY(_x, _y); // set cursor position
if (data == 0) return 1; // if no character to print
beginData();
for (uint8_t col = 0; col < 6; col++) { // 6 columns per character
uint8_t bits = getFont(data, col); // get byte
if (_invState) bits = ~bits; // inversion
if (_scaleX == 1) { // if scale is 1
SPI.transfer(bits); // output
} else { // scale 2, 3 or 4 - stretch font
uint32_t newData = 0; // buffer
for (uint8_t i = 0, count = 0; i < 8; i++)
for (uint8_t j = 0; j < _scaleX; j++, count++)
bitWrite(newData, count, bitRead(bits, i)); // pack stretched font
for (uint8_t i = 0; i < _scaleX; i++) { // output horizontally
uint8_t prevData = 0;
if (_x + i >= 0 && _x + i <= _maxX) // within display
for (uint8_t j = 0; j < _scaleX; j++) { // output vertically
uint8_t data = newData >> (j * 8); // get buffer piece
if (_shift == 0) { // without line shift
SPI.transfer(data); // output
} else { // with shift
SPI.transfer((prevData >> (8 - _shift)) | (data << _shift)); // merge and output
prevData = data; // remember previous
}
}
if (_shift != 0) SPI.transfer(prevData >> (8 - _shift)); // output lower piece with shift
}
}
_x += _scaleX; // move by pixel width (1-4)
}
endTransm();
return 1;
}
void home() {
setCursorXY(0, 0);
}
void setCursorXY(uint8_t x, uint8_t y) {
_x = x;
_y = y;
setWindowShift(x, y);
}
void setScale(uint8_t scale) {
_scaleX = scale;
_scaleY = scale * 8;
setCursorXY(_x, _y);
}
void invertText(bool inv) {
_invState = inv;
}
void fill(uint8_t data) {
setWindow(0, 0, _maxX, _maxRow);
beginData();
for (int i = 0; i < 1024; i++) SPI.transfer(data);
endTransm();
setCursorXY(_x, _y);
}
void setWindowShift(uint8_t x0, uint8_t y0) {
_shift = y0 & 0b111;
setWindow(x0, (y0 >> 3), x0 + _maxX, (y0 + _scaleY - 1) >> 3);
}
void sendCommand(uint8_t cmd1) {
beginCommand();
SPI.transfer(cmd1);
endTransm();
}
void sendCommand(uint8_t cmd1, uint8_t cmd2) {
beginCommand();
SPI.transfer(cmd1);
SPI.transfer(cmd2);
endTransm();
}
void setWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
beginCommand();
SPI.transfer(OLED_COLUMNADDR);
SPI.transfer(constrain(x0, 0, _maxX));
SPI.transfer(constrain(x1, 0, _maxX));
SPI.transfer(OLED_PAGEADDR);
SPI.transfer(constrain(y0, 0, _maxRow));
SPI.transfer(constrain(y1, 0, _maxRow));
endTransm();
}
void beginData() {
startTransm();
fastWrite(_DC, 1);
}
void beginCommand() {
startTransm();
fastWrite(_DC, 0);
}
void endTransm() {
fastWrite(_CS, 1);
SPI.endTransaction();
}
void startTransm() {
SPI.beginTransaction(OLED_SPI_SETT);
fastWrite(_CS, 0);
}
uint8_t getFont(uint8_t font, uint8_t row) {
if (row > 4) return 0;
font = font - '0' + 16;
return pgm_read_byte(&(_charMap[font][row]));
}
const uint8_t _maxRow = 8 - 1;
const uint8_t _maxY = 64 - 1;
const uint8_t _maxX = OLED_WIDTH - 1;
private:
void fastWrite(const uint8_t pin, bool val) {
digitalWrite(pin, val);
}
bool _invState = 0;
bool _println = false;
bool _getn = false;
uint8_t _scaleX = 1, _scaleY = 8;
uint8_t _x = 0, _y = 0;
uint8_t _shift = 0;
};
#endif