-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
executable file
·346 lines (282 loc) · 11.1 KB
/
display.c
File metadata and controls
executable file
·346 lines (282 loc) · 11.1 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
335
336
337
338
339
340
341
342
343
344
345
346
#include "cambadge.h"
#include "globals.h"
#include "font6x8.inc"
#include "monorgb.h" // mono8->rgb565 lookup
// oled & display formatting stuff
void oledcmd(unsigned int d) { // send byte to display, bit 8 set for command, clear for data
while (SPI1STATbits.SPIBUSY); // in case previous buffered data still being sent
if (d & 0x100) oled_cd_lo;
else oled_cd_hi;
oled_cs_lo;
SPI1BUF = d;
while (SPI1STATbits.SPIBUSY);
oled_cs_hi;
}
void oled_init(void) { //initialise display after powerup. see SSD1351 chip and OLED module datasheet for details
unsigned int i;
const unsigned short oledinitdata[] = {
0x1fd, 0x12,
0x1fd, 0xb1,
0x1ae,
0x1b3, 0xf1,
0x1ca, 0x7f,
0x1a2, 0x00,
0x1a1, 0x00,
#if oled_upscan==1
0x1a0, 0x35, // colour depth b0 rotate, b1 mirror b2 color orded, b4 v flip, b7,7 colour depth
#else
0x1a0, 0x37, // colour depth b0 rotate, b1 mirror b2 color orded, b4 v flip, b7,7 colour depth
#endif
0x1b5, 0x0C, // GPIO gpio1 backlight power, gpio0 camera powerdown
0x1ab, 0x01, //function sel
0x1b4, 0xa0, 0xb5, 0x55, // seg low voltage
0x1c1, 0xc8, 0x80, 0xc8, // contrast current
0x1c7, 0x0f, // master current
0x1b8, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
0x21, 0x23, 0x25, 0x27, 0x2a, 0x2d, 0x30, 0x33, 0x36, 0x39, 0x3c, 0x3f, 0x42, 0x45, 0x48, 0x4c, 0x50, 0x54, 0x58, 0x5c, 0x60, 0x64, 0x68, 0x6c,
0x70, 0x74, 0x78, 0x7d, 0x82, 0x87, 0x8c, 0x91, 0x96, 0x9b, 0xa0, 0xa5, 0xaa, 0xaf, 0xb4,
0xb1, 0x32, 0xb2, 0xa4, 0x00, 0x00, 0xbb, 0x17, 0xb6, 0x01, 0xbe, 0x05,
0x1a6, // display mode
0x115, 0x00, 0x7f, // col
0x175, 0x00, 0x7f, // row
0x1af
};
oled_rst_hi;
delayus(oled_reset_time);
oled_rst_lo;
delayus(oled_reset_time);
oled_rst_hi;
delayus(oled_reset_time);
for (i = 0; i != sizeof (oledinitdata) / 2; oledcmd(oledinitdata[i++]));
while (SPI1STATbits.SPIBUSY); oled_cs_hi;
plotblock(0, 0, dispwidth, dispheight, 0); // clear display memory
dispx = dispy = 0;
fgcol = 0xffff;
bgcol = 0;
}
void monopalette(unsigned int min, unsigned int max) {
unsigned int i, d;
for (i = 0; i != 256; i++) {
d = (i - min)*255 / (max - min);
if (i <= min) d = 0;
if (i >= max) d = 255;
palette[i] = rgbto16(d, d, d);
}
}
void plotblock(unsigned int xstart, unsigned int ystart, unsigned int xsize, unsigned int ysize, unsigned int col)
{
unsigned int i;
if ((xstart + xsize > dispwidth) || (ystart + ysize > dispheight) || (xsize == 0) || (ysize == 0)) return;
oled_cs_lo;
oled_cd_lo;
SPI1BUF=0x75; while (SPI1STATbits.SPIBUSY); oled_cd_hi;
SPI1BUF=xstart;
SPI1BUF=xstart + xsize - 1;
while (SPI1STATbits.SPIBUSY);
#if oled_upscan==1
oled_cd_lo;
SPI1BUF=0x15;
i=((dispheight - ystart - 1) - ysize + 1);
while (SPI1STATbits.SPIBUSY);
oled_cd_hi;
SPI1BUF=i;
SPI1BUF=dispheight - ystart - 1;
while (SPI1STATbits.SPIBUSY);
#else
oled_cd_lo;
SPI1BUF=0x15;
while (SPI1STATbits.SPIBUSY);
oled_cd_hi;
SPI1BUF=ystart;
SPI1BUF=ystart + ysize - 1;
while (SPI1STATbits.SPIBUSY);
#endif
oled_cd_lo;
SPI1BUF=0x15c; //send data
i = xsize*ysize;
while (SPI1STATbits.SPIBUSY);
oled_cd_hi;
SPI1CONbits.MODE16 = 1; // 16 bit for ease of accesss - no speed improvement
do {
while (SPI1STATbits.SPITBF);
SPI1BUF = col;
} while(--i);
while (SPI1STATbits.SPIBUSY); // wait until last byte sent before releasing CS. if we were DMAing, this would be done in DMA complete int
SPI1CONbits.MODE16 = 0; // back to 8 bit mode
oled_cs_hi;
}
void mplotblock(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int colour, unsigned char* imgaddr) {
// plot block in memory buffer for subsequent display using dispimage, 8bpp only.
// speeds up displays of lots of pixels.
unsigned int xx, yy, gap;
gap = dispwidth - width;
imgaddr += (y * dispwidth + x);
if(((x+width)>dispwidth) || ((y+height)>dispheight)) return;
for (yy = 0; yy != height; yy++) {
for (xx = 0; xx != width; xx++) *imgaddr++ = (unsigned char) colour;
imgaddr += gap;
}
}
void dispimage(unsigned int xstart, unsigned int ystart, unsigned int xsize, unsigned int ysize, unsigned int format, unsigned char* imgaddr) { // display image or solid colour in various formats. Note assumes format = bytes per pixel
// for solid, image pointer is solid colour value, called via plotblock macro
unsigned int i, d, e, y, x, r, g, b, bpp, skip,vdup,vcount;
unsigned char *imgaddr2;
bpp = format & 3;
skip = (format & 0xf0) >> 4;
vdup=(format & img_vdouble)?2:1;
#if oled_upscan==1
format ^= img_revscan;
#endif
if ((xstart + xsize > dispwidth) || (ystart + ysize > dispheight) || (xsize == 0) || (ysize == 0)) return;
oledcmd(0x175);
oledcmd(xstart);
oledcmd(xstart + xsize - 1); // column address
#if oled_upscan==1
oledcmd(0x115);
oledcmd((dispheight - ystart - 1) - ysize*vdup + 1);
oledcmd(dispheight - ystart - 1); // row address
#else
oledcmd(0x115);
oledcmd(ystart);
oledcmd(ystart + ysize*vdup - 1); // row address
#endif
oledcmd(0x15c); //send data
i = xsize*ysize;
if (((unsigned int) imgaddr + i * bpp * (skip + 1)) >= ((unsigned int) &cambuffer + cambufsize)) return;
oled_cd_hi;
oled_cs_lo;
SPI1CONbits.MODE16 = 1; // 16 bit for ease of accesss - no speed improvement
//contrary to what datasheet implies, it doesn't seem necessary to de-assert CS between bytes,
// so we can use buffered mode to avoid gaps between bytes for higher throughput.
// Code doesn't need to be super-efficient as speed is limited by OLED max SPI clock rate.
// as long as we can produce 1 pixel every 1.3uS avaraged over the 8 pixel FIFO size we're maxing out the SPI bus
for (y = 0; y != ysize; y++) {
for(vcount=0;vcount!=vdup;vcount++) {
if (format & img_revscan) imgaddr2 = imgaddr + (ysize - y - 1) * xsize * bpp *(skip + 1);
else imgaddr2 = imgaddr + y * xsize * bpp * (skip + 1);
for (x = 0; x != xsize; x++) {
switch (bpp) {
case 0: d = (unsigned int) imgaddr;
break;
case 1: d = palette[*imgaddr2++];
imgaddr2 += skip;
break;
case 2:
d = *imgaddr2++;
d |= (*imgaddr2++) << 8;
imgaddr2 += skip * 2;
break;
case 3:
b = *imgaddr2++;
g = *imgaddr2++;
r = *imgaddr2++;
d = (r << 8 & 0xf800) | (g << 3 & 0x7C0) | (b >> 3);
imgaddr2 += skip * 3;
} // switch bpp
while (SPI1STATbits.SPITBF);
SPI1BUF = d;
} // for x
} // for vcount
}// for y
while (SPI1STATbits.SPIBUSY); // wait until last byte sent before releasing CS. if we were DMAing, this would be done in DMA complete int
SPI1CONbits.MODE16 = 0; // back to 8 bit mode
oled_cs_hi;
}
void _mon_putc(char c) // STDIO for printf
{
dispchar(c);
}
void dispchar(unsigned char c) {// display 1 character, do control characters
unsigned int x, y, b, m;
if (dispuart) {
if (dispuart & dispuart_u1) u1txbyte(c);
else u2txbyte(c); //UART mode
if(!(dispuart & dispuart_screen) ) return;
}
switch (c) { // control characters
case 2: //0.5s delay
delayus(500000);
break;
case 3: // half space
dispx += charwidth / 2;
break;
case 4: // short backspace
dispx -=3;
break;
case 7:
x = fgcol;
fgcol = bgcol;
bgcol = x;
break; // invert
case 8:// BS
if (dispx >= charwidth) dispx -= charwidth;
break;
case 10:// crlf
dispx = 0;
dispy += vspace;
if (dispy >= dispheight) dispy = 0;
break;
case 12: // CLS
plotblock(0, 0, dispwidth, dispheight, bgcol);
dispx = dispy = 0;
break;
case 13:
dispx = 0;
break; // CR
case 14 : // grey
fgcol=rgbto16(128,128,128);
break;
case 0x80 ... 0x93: // tab x
dispx = (c & 0x1f) * charwidth;
break;
case 0xa0 ... 0xaf: // tab y
dispy = (c & 0x0f) * vspace;
break;
case 0xc0 ... 0xff: // set text primary colour 0b11rgbRGB bg FG
fgcol = rgbto16((c & 1) ? 255 : 0, (c & 2) ? 255 : 0, (c & 4) ? 255 : 0);
bgcol = rgbto16((c & 8) ? 255 : 0, (c & 16) ? 255 : 0, (c & 32) ? 255 : 0);
break;
case startchar ... (nchars_6x8 + startchar - 1): // displayed characters
oled_cs_lo;
oledcmd(0x175);
oledcmd(dispx);
oledcmd(dispx + charwidth - 1); // column address
#if oled_upscan==1
oledcmd(0x115);
oledcmd((dispy + charheight - 1)^127);
oledcmd(dispy^127); // row address
#else
oledcmd(0x115);
oledcmd(dispy);
oledcmd(dispy + charheight - 1); // row address
#endif
oledcmd(0x15c); //send data
SPI1CONbits.MODE16 = 1; // 16 bit SPI so 1 transfer per pixel
oled_cd_hi;
oled_cs_lo;
c -= startchar;
for (y = 0; y != charheight; y++) {
#if oled_upscan==1
b = FONT6x8[c][7 - y]; //lookup outside loop for speed
#else
b = FONT6x8[c][y]; //lookup outside loop for speed
#endif
for (x = 0; x != charwidth; x++) {
while (SPI1STATbits.SPITBF);
SPI1BUF = (b & 0x80) ? fgcol : bgcol;
b <<= 1;
}
} //y
while (SPI1STATbits.SPIBUSY); // wait until last byte sent before releasing CS
SPI1CONbits.MODE16 = 0;
oled_cs_hi;
dispx += charwidth;
if (dispx >= dispwidth) {
dispx = 0;
dispy += vspace;
if (dispy >= dispheight) dispy = 0;
}
break;
}//switch
oled_cs_hi;
}