-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtiny_oled.cpp
More file actions
334 lines (304 loc) · 9.67 KB
/
Copy pathtiny_oled.cpp
File metadata and controls
334 lines (304 loc) · 9.67 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <tiny_oled.h>
static void oled_command(struct oled *ol, const unsigned char c)
{
WIRE.beginTransmission(ol->i2c);
WIRE.WRITE(0x00);
WIRE.WRITE(c);
WIRE.endTransmission();
}
static void oled_commands(struct oled *ol, const unsigned char *c, byte n)
{
while (n--) oled_command(ol, pgm_read_byte_near(c++));
}
static const byte PROGMEM oled_init_cs[] = {
SSD1306_DISPLAYOFF,
SSD1306_SETDISPLAYCLOCKDIV,
0x80,
SSD1306_SETMULTIPLEX,
SSD1306_LCDHEIGHT - 1,
SSD1306_SETDISPLAYOFFSET,
0x0,
SSD1306_SETSTARTLINE | 0x0,
SSD1306_CHARGEPUMP,
};
static const byte PROGMEM oled_init_2[] = {
SSD1306_MEMORYMODE,
0x00,
SSD1306_SEGREMAP | 0x1,
SSD1306_COMSCANDEC,
#if defined SSD1306_128_32
SSD1306_SETCOMPINS,
0x02,
SSD1306_SETCONTRAST,
0x8F,
#elif defined SSD1306_128_64
SSD1306_SETCOMPINS,
0x12,
SSD1306_SETCONTRAST,
#elif defined SSD1306_96_16
SSD1306_SETCOMPINS,
0x2,
SSD1306_SETCONTRAST,
#endif
};
static const byte PROGMEM oled_init_3[] = {
SSD1306_SETVCOMDETECT,
0x40,
SSD1306_DISPLAYALLON_RESUME,
SSD1306_NORMALDISPLAY,
SSD1306_DEACTIVATE_SCROLL,
SSD1306_DISPLAYON,
};
void oled_init(struct oled *ol, byte i2c, int reset, int vccstate)
{
// I2C Init
ol->i2c = i2c;
oled_clear(ol);
WIRE.begin();
if (reset) {
// Setup reset pin direction (used by both SPI and I2C)
pinMode(reset, OUTPUT);
digitalWrite(reset, HIGH);
// VDD (3.3V) goes high at start, lets just chill for a ms
delay(1);
// bring reset low
digitalWrite(reset, LOW);
// wait 10ms
delay(10);
// bring out of reset
digitalWrite(reset, HIGH);
// turn on VCC (9V?)
}
// Init sequence
oled_commands(ol, oled_init_cs, sizeof(oled_init_cs));
if (vccstate == SSD1306_EXTERNALVCC) {
oled_command(ol, 0x10);
} else {
oled_command(ol, 0x14);
}
oled_commands(ol, oled_init_2, sizeof(oled_init_2));
#if defined SSD1306_128_64
if (vccstate == SSD1306_EXTERNALVCC) {
oled_command(ol, 0x9F);
} else {
oled_command(ol, 0xCF);
}
#elif defined SSD1306_96_16
if (vccstate == SSD1306_EXTERNALVCC) {
oled_command(ol, 0x10);
} else {
oled_command(ol, 0xAF);
}
#endif
oled_command(ol, SSD1306_SETPRECHARGE); // 0xd9
if (vccstate == SSD1306_EXTERNALVCC) {
oled_command(ol, 0x22);
} else {
oled_command(ol, 0xF1);
}
oled_commands(ol, oled_init_3, sizeof(oled_init_3));
}
// 8x8 font for chars 32..127 in column-major order per char (each char on its own line)
const unsigned char PROGMEM font[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x14, 0x3e, 0x14, 0x3e, 0x14, 0x00, 0x00,
0x00, 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12, 0x00,
0x02, 0x05, 0x15, 0x0a, 0x14, 0x2a, 0x28, 0x10,
0x00, 0x00, 0x16, 0x29, 0x29, 0x11, 0x10, 0x28,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0c, 0x12, 0x21, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x21, 0x12, 0x0c, 0x00, 0x00,
0x00, 0x00, 0x15, 0x0e, 0x1f, 0x0e, 0x15, 0x00,
0x00, 0x00, 0x08, 0x08, 0x3e, 0x08, 0x08, 0x00,
0x00, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00,
0x00, 0x0c, 0x12, 0x21, 0x21, 0x12, 0x0c, 0x00,
0x00, 0x00, 0x00, 0x22, 0x3f, 0x20, 0x00, 0x00,
0x00, 0x22, 0x31, 0x29, 0x29, 0x25, 0x22, 0x00,
0x00, 0x12, 0x21, 0x29, 0x29, 0x25, 0x12, 0x00,
0x00, 0x18, 0x14, 0x12, 0x3f, 0x10, 0x00, 0x00,
0x00, 0x17, 0x25, 0x25, 0x25, 0x25, 0x19, 0x00,
0x00, 0x1e, 0x25, 0x25, 0x25, 0x25, 0x18, 0x00,
0x00, 0x21, 0x11, 0x09, 0x05, 0x03, 0x01, 0x00,
0x00, 0x1a, 0x25, 0x25, 0x25, 0x25, 0x1a, 0x00,
0x00, 0x06, 0x29, 0x29, 0x29, 0x29, 0x1e, 0x00,
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x40, 0x24, 0x00, 0x00, 0x00,
0x00, 0x08, 0x08, 0x14, 0x14, 0x22, 0x22, 0x00,
0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00,
0x00, 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x00,
0x00, 0x02, 0x01, 0x29, 0x09, 0x06, 0x00, 0x00,
0x18, 0x24, 0x42, 0xba, 0xaa, 0xbe, 0x00, 0x00,
0x00, 0x30, 0x0c, 0x0b, 0x0b, 0x0c, 0x30, 0x00,
0x00, 0x3f, 0x25, 0x25, 0x25, 0x1a, 0x00, 0x00,
0x0c, 0x12, 0x21, 0x21, 0x21, 0x12, 0x00, 0x00,
0x00, 0x3f, 0x21, 0x21, 0x21, 0x1e, 0x00, 0x00,
0x00, 0x3f, 0x25, 0x25, 0x25, 0x21, 0x00, 0x00,
0x00, 0x3f, 0x05, 0x05, 0x05, 0x01, 0x00, 0x00,
0x0c, 0x12, 0x21, 0x29, 0x29, 0x1a, 0x00, 0x00,
0x00, 0x3f, 0x04, 0x04, 0x04, 0x04, 0x3f, 0x00,
0x00, 0x00, 0x21, 0x21, 0x3f, 0x21, 0x21, 0x00,
0x00, 0x10, 0x20, 0x21, 0x21, 0x1f, 0x00, 0x00,
0x00, 0x3f, 0x08, 0x0c, 0x12, 0x21, 0x00, 0x00,
0x00, 0x3f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00,
0x00, 0x3f, 0x02, 0x04, 0x08, 0x04, 0x02, 0x3f,
0x00, 0x3f, 0x02, 0x04, 0x08, 0x10, 0x3f, 0x00,
0x00, 0x00, 0x1e, 0x21, 0x21, 0x21, 0x1e, 0x00,
0x00, 0x3f, 0x05, 0x05, 0x05, 0x02, 0x00, 0x00,
0x00, 0x00, 0x1e, 0x21, 0x21, 0x21, 0x5e, 0x00,
0x00, 0x3f, 0x05, 0x0d, 0x15, 0x22, 0x00, 0x00,
0x00, 0x00, 0x12, 0x25, 0x29, 0x29, 0x12, 0x00,
0x00, 0x01, 0x01, 0x01, 0x3f, 0x01, 0x01, 0x01,
0x00, 0x1f, 0x20, 0x20, 0x20, 0x20, 0x1f, 0x00,
0x01, 0x06, 0x18, 0x20, 0x20, 0x18, 0x06, 0x01,
0x00, 0x3f, 0x10, 0x08, 0x04, 0x08, 0x10, 0x3f,
0x00, 0x21, 0x12, 0x0c, 0x0c, 0x12, 0x21, 0x00,
0x00, 0x01, 0x02, 0x04, 0x38, 0x04, 0x02, 0x01,
0x00, 0x21, 0x31, 0x29, 0x25, 0x23, 0x21, 0x00,
0x00, 0x00, 0x3f, 0x21, 0x21, 0x00, 0x00, 0x00,
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00,
0x00, 0x00, 0x21, 0x21, 0x3f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0x00,
0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00,
0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00,
0x00, 0x10, 0x2a, 0x2a, 0x2a, 0x1a, 0x3c, 0x00,
0x00, 0x3f, 0x14, 0x24, 0x24, 0x24, 0x18, 0x00,
0x00, 0x00, 0x18, 0x24, 0x24, 0x24, 0x00, 0x00,
0x00, 0x18, 0x24, 0x24, 0x24, 0x14, 0x3f, 0x00,
0x00, 0x1c, 0x2a, 0x2a, 0x2a, 0x2a, 0x0c, 0x00,
0x00, 0x00, 0x08, 0x3e, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x18, 0xa4, 0xa4, 0x88, 0x7c, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x04, 0x04, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x00,
0x00, 0x80, 0x80, 0x84, 0x7d, 0x00, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x10, 0x28, 0x24, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0x20, 0x00, 0x00, 0x00,
0x00, 0x3c, 0x04, 0x08, 0x08, 0x04, 0x3c, 0x00,
0x00, 0x00, 0x3c, 0x08, 0x04, 0x04, 0x3c, 0x00,
0x00, 0x18, 0x24, 0x24, 0x24, 0x24, 0x18, 0x00,
0x00, 0xfc, 0x28, 0x24, 0x24, 0x24, 0x18, 0x00,
0x00, 0x18, 0x24, 0x24, 0x24, 0x28, 0xfc, 0x00,
0x00, 0x00, 0x3c, 0x08, 0x04, 0x04, 0x08, 0x00,
0x00, 0x00, 0x24, 0x2a, 0x2a, 0x12, 0x00, 0x00,
0x00, 0x00, 0x04, 0x3e, 0x24, 0x04, 0x00, 0x00,
0x00, 0x00, 0x1c, 0x20, 0x20, 0x10, 0x3c, 0x00,
0x00, 0x0c, 0x10, 0x20, 0x20, 0x10, 0x0c, 0x00,
0x0c, 0x30, 0x20, 0x10, 0x10, 0x20, 0x30, 0x0c,
0x00, 0x24, 0x28, 0x10, 0x10, 0x28, 0x24, 0x00,
0x00, 0x84, 0x88, 0x50, 0x20, 0x10, 0x0c, 0x00,
0x00, 0x00, 0x24, 0x34, 0x2c, 0x24, 0x00, 0x00,
0x00, 0x00, 0x0c, 0x3f, 0x21, 0x21, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x21, 0x21, 0x3f, 0x0c, 0x00, 0x00,
0x00, 0x10, 0x08, 0x08, 0x10, 0x10, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const byte PROGMEM oled_display_sequence[] = {
SSD1306_COLUMNADDR,
0,
SSD1306_LCDWIDTH - 1,
SSD1306_PAGEADDR,
0,
#if SSD1306_LCDHEIGHT == 64
7,
#endif
#if SSD1306_LCDHEIGHT == 32
3,
#endif
#if SSD1306_LCDHEIGHT == 16
1,
#endif
};
void oled_display(struct oled *ol)
{
unsigned char tx, ty;
unsigned short ch = 0;
oled_commands(ol, oled_display_sequence, sizeof(oled_display_sequence));
tx = ty = 0;
for (uint16_t i = 0; i < (SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT); i += SSD1306_LCDWIDTH) {
// send a bunch of data in one xmission
WIRE.beginTransmission(ol->i2c);
WIRE.WRITE(0x40);
for (uint8_t x = 0; x < 16; ) {
if (!(x & 7)) {
ch = ol->textbuf[(ty * TXTCOLS) + tx];
if (ch < 32) ch = 32;
ch -= 32;
ch <<= 3;
}
WIRE.WRITE(pgm_read_byte_near(font + (ch + (x & 7))));
++x;
if (!(x & 7)) {
++tx;
if (!(tx & 15)) {
tx = 0;
++ty;
}
}
}
WIRE.endTransmission();
}
}
void oled_putchar(struct oled *ol, unsigned char ch)
{
switch (ch) {
case '\r': ol->tx = 0; break;
case '\n': ol->ty++; ol->tx = 0; break;
case 8: if (ol->tx) --(ol->tx); break;
default:
ol->textbuf[(ol->ty * TXTCOLS) + ol->tx++] = ch;
if (ol->tx == 16) {
ol->tx = 0;
++(ol->ty);
}
break;
}
if (ol->ty == TXTROWS) {
// scroll buffer
for (unsigned char i = TXTCOLS; i < sizeof(ol->textbuf); i++) ol->textbuf[i-16] = ol->textbuf[i];
for (unsigned char i = sizeof(ol->textbuf)-TXTCOLS; i < sizeof(ol->textbuf); i++) ol->textbuf[i] = 32;
ol->ty = TXTROWS-1;
}
}
void oled_puts(struct oled *ol, const char *msg)
{
while (*msg) {
oled_putchar(ol, (unsigned char)*msg++);
}
}
// can output strings in program memory
void oled_puts_pgm(struct oled *ol, const char *msg)
{
unsigned x = 0;
while (pgm_read_byte_near(msg + x)) {
oled_putchar(ol, pgm_read_byte_near(msg + x++));
}
}
void oled_putn(struct oled *ol, int n)
{
unsigned char buf[8];
unsigned char i;
if (n < 0) { oled_putchar(ol, '-'); n = -n; }
i = 0;
do {
buf[i++] = '0' + (n % 10);
n /= 10;
} while (n);
while (--i != 255) {
oled_putchar(ol, buf[i]);
}
}
void oled_clear(struct oled *ol)
{
ol->tx = ol->ty = 0;
for (unsigned char i = 0; i < sizeof(ol->textbuf); i++) ol->textbuf[i] = 32;
}
void oled_gotoxy(struct oled *ol, unsigned char x, unsigned char y)
{
ol->tx = x;
ol->ty = y;
}