-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathST7565.cpp
568 lines (461 loc) · 12.5 KB
/
ST7565.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
$Id:$
ST7565 LCD library!
Copyright (C) 2010 Limor Fried, Adafruit Industries
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// some of this code was written by <[email protected]> originally; it is in the public domain.
*/
//#include <Wire.h>
#ifdef __AVR__
#include <avr/pgmspace.h>
#include <util/delay.h>
#endif
#ifndef _delay_ms
#define _delay_ms(t) delay(t)
#endif
#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif
#include <stdlib.h>
#include "ST7565.h"
#define ST7565_STARTBYTES 0
uint8_t is_reversed = 0;
// a 5x7 font table
const extern uint8_t PROGMEM font[];
// the memory buffer for the LCD
uint8_t st7565_buffer[512];
// reduces how much is refreshed, which speeds it up!
// originally derived from Steve Evans/JCW's mod but cleaned up and
// optimized
#define enablePartialUpdate
#ifdef enablePartialUpdate
static uint8_t xUpdateMin, xUpdateMax, yUpdateMin, yUpdateMax;
#endif
static void updateBoundingBox(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax) {
#ifdef enablePartialUpdate
if (xmin < xUpdateMin) xUpdateMin = xmin;
if (xmax > xUpdateMax) xUpdateMax = xmax;
if (ymin < yUpdateMin) yUpdateMin = ymin;
if (ymax > yUpdateMax) yUpdateMax = ymax;
#endif
}
void ST7565::drawbitmap(uint8_t x, uint8_t y,
const uint8_t *bitmap, uint8_t w, uint8_t h,
uint8_t color) {
for (uint8_t j=0; j<h; j++) {
for (uint8_t i=0; i<w; i++ ) {
if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
my_setpixel(x+i, y+j, color);
}
}
}
updateBoundingBox(x, y, x+w, y+h);
}
void ST7565::drawstring(uint8_t x, uint8_t line, char *c) {
while (c[0] != 0) {
drawchar(x, line, c[0]);
c++;
x += 6; // 6 pixels wide
if (x + 6 >= LCDWIDTH) {
x = 0; // ran out of this line
line++;
}
if (line >= (LCDHEIGHT/8))
return; // ran out of space :(
}
}
void ST7565::drawstring_P(uint8_t x, uint8_t line, const char *str) {
while (1) {
char c = pgm_read_byte(str++);
if (! c)
return;
drawchar(x, line, c);
x += 6; // 6 pixels wide
if (x + 6 >= LCDWIDTH) {
x = 0; // ran out of this line
line++;
}
if (line >= (LCDHEIGHT/8))
return; // ran out of space :(
}
}
void ST7565::drawchar(uint8_t x, uint8_t line, char c) {
updateBoundingBox(x, line*8, x+5, line*8 + 8);
for (uint8_t i =0; i<5; i++ ) {
st7565_buffer[x + (line*128) ] = pgm_read_byte(font+(c*5)+i);
x++;
}
}
// bresenham's algorithm - thx wikpedia
void ST7565::drawline(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
uint8_t color) {
uint8_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
// much faster to put the test here, since we've already sorted the points
updateBoundingBox(x0, y0, x1, y1);
uint8_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int8_t err = dx / 2;
int8_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;}
for (; x0<=x1; x0++) {
if (steep) {
my_setpixel(y0, x0, color);
} else {
my_setpixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
// filled rectangle
void ST7565::fillrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint8_t color) {
// stupidest version - just pixels - but fast with internal buffer!
for (uint8_t i=x; i<x+w; i++) {
for (uint8_t j=y; j<y+h; j++) {
my_setpixel(i, j, color);
}
}
updateBoundingBox(x, y, x+w, y+h);
}
// draw a rectangle
void ST7565::drawrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint8_t color) {
// stupidest version - just pixels - but fast with internal buffer!
for (uint8_t i=x; i<x+w; i++) {
my_setpixel(i, y, color);
my_setpixel(i, y+h-1, color);
}
for (uint8_t i=y; i<y+h; i++) {
my_setpixel(x, i, color);
my_setpixel(x+w-1, i, color);
}
updateBoundingBox(x, y, x+w, y+h);
}
// draw a circle outline
void ST7565::drawcircle(uint8_t x0, uint8_t y0, uint8_t r,
uint8_t color) {
updateBoundingBox(x0-r, y0-r, x0+r, y0+r);
int8_t f = 1 - r;
int8_t ddF_x = 1;
int8_t ddF_y = -2 * r;
int8_t x = 0;
int8_t y = r;
my_setpixel(x0, y0+r, color);
my_setpixel(x0, y0-r, color);
my_setpixel(x0+r, y0, color);
my_setpixel(x0-r, y0, color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
my_setpixel(x0 + x, y0 + y, color);
my_setpixel(x0 - x, y0 + y, color);
my_setpixel(x0 + x, y0 - y, color);
my_setpixel(x0 - x, y0 - y, color);
my_setpixel(x0 + y, y0 + x, color);
my_setpixel(x0 - y, y0 + x, color);
my_setpixel(x0 + y, y0 - x, color);
my_setpixel(x0 - y, y0 - x, color);
}
}
void ST7565::fillcircle(uint8_t x0, uint8_t y0, uint8_t r,
uint8_t color) {
updateBoundingBox(x0-r, y0-r, x0+r, y0+r);
int8_t f = 1 - r;
int8_t ddF_x = 1;
int8_t ddF_y = -2 * r;
int8_t x = 0;
int8_t y = r;
for (uint8_t i=y0-r; i<=y0+r; i++) {
my_setpixel(x0, i, color);
}
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
for (uint8_t i=y0-y; i<=y0+y; i++) {
my_setpixel(x0+x, i, color);
my_setpixel(x0-x, i, color);
}
for (uint8_t i=y0-x; i<=y0+x; i++) {
my_setpixel(x0+y, i, color);
my_setpixel(x0-y, i, color);
}
}
}
void ST7565::my_setpixel(uint8_t x, uint8_t y, uint8_t color) {
if ((x >= LCDWIDTH) || (y >= LCDHEIGHT))
return;
// x is which column
if (color)
st7565_buffer[x+ (y/8)*128] |= _BV(7-(y%8));
else
st7565_buffer[x+ (y/8)*128] &= ~_BV(7-(y%8));
}
// the most basic function, set a single pixel
void ST7565::setpixel(uint8_t x, uint8_t y, uint8_t color) {
if ((x >= LCDWIDTH) || (y >= LCDHEIGHT))
return;
// x is which column
if (color)
st7565_buffer[x+ (y/8)*128] |= _BV(7-(y%8));
else
st7565_buffer[x+ (y/8)*128] &= ~_BV(7-(y%8));
updateBoundingBox(x,y,x,y);
}
// the most basic function, get a single pixel
uint8_t ST7565::getpixel(uint8_t x, uint8_t y) {
if ((x >= LCDWIDTH) || (y >= LCDHEIGHT))
return 0;
return (st7565_buffer[x+ (y/8)*128] >> (7-(y%8))) & 0x1;
}
void ST7565::begin(uint8_t contrast) {
st7565_init();
st7565_command(CMD_DISPLAY_ON);
st7565_command(CMD_SET_ALLPTS_NORMAL);
st7565_set_brightness(contrast);
}
void ST7565::st7565_init(void) {
// set pin directions
pinMode(sid, OUTPUT);
pinMode(sclk, OUTPUT);
pinMode(a0, OUTPUT);
pinMode(rst, OUTPUT);
pinMode(cs, OUTPUT);
// toggle RST low to reset; CS low so it'll listen to us
if (cs > 0)
digitalWrite(cs, LOW);
digitalWrite(rst, LOW);
_delay_ms(500);
digitalWrite(rst, HIGH);
// LCD bias select
st7565_command(CMD_SET_BIAS_7);
// ADC select
st7565_command(CMD_SET_ADC_NORMAL);
// SHL select
st7565_command(CMD_SET_COM_NORMAL);
// Initial display line
st7565_command(CMD_SET_DISP_START_LINE);
// turn on voltage converter (VC=1, VR=0, VF=0)
st7565_command(CMD_SET_POWER_CONTROL | 0x4);
// wait for 50% rising
_delay_ms(50);
// turn on voltage regulator (VC=1, VR=1, VF=0)
st7565_command(CMD_SET_POWER_CONTROL | 0x6);
// wait >=50ms
_delay_ms(50);
// turn on voltage follower (VC=1, VR=1, VF=1)
st7565_command(CMD_SET_POWER_CONTROL | 0x7);
// wait
_delay_ms(10);
// set lcd operating voltage (regulator resistor, ref voltage resistor)
st7565_command(CMD_SET_RESISTOR_RATIO | 0x2);
// initial display line
// set page address
// set column address
// write display data
// set up a bounding box for screen updates
updateBoundingBox(0, 0, LCDWIDTH-1, LCDHEIGHT-1);
}
inline void ST7565::spiwrite(uint8_t c) {
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
shiftOut(sid, sclk, MSBFIRST, c);
#else
int8_t i;
for (i=7; i>=0; i--) {
digitalWrite(sclk, LOW);
delayMicroseconds(5); //need to slow down the data rate for Due and Zero
if (c & _BV(i))
digitalWrite(sid, HIGH);
else
digitalWrite(sid, LOW);
// delayMicroseconds(5); //need to slow down the data rate for Due and Zero
digitalWrite(sclk, HIGH);
}
#endif
/*
int8_t i;
for (i=7; i>=0; i--) {
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(i))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
}
*/
/*
// loop unwrapped! too fast doesnt work :(
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(7))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(6))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(5))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(4))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(3))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(2))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(1))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(0))
SID_PORT |= _BV(SID);
else
SID_PORT &= ~_BV(SID);
SCLK_PORT |= _BV(SCLK);
*/
}
void ST7565::st7565_command(uint8_t c) {
digitalWrite(a0, LOW);
digitalWrite(cs, LOW);
spiwrite(c);
digitalWrite(cs, HIGH);
}
void ST7565::st7565_data(uint8_t c) {
digitalWrite(a0, HIGH);
digitalWrite(cs, LOW);
spiwrite(c);
digitalWrite(cs, HIGH);
}
void ST7565::st7565_set_brightness(uint8_t val) {
st7565_command(CMD_SET_VOLUME_FIRST);
st7565_command(CMD_SET_VOLUME_SECOND | (val & 0x3f));
}
void ST7565::display(void) {
uint8_t col, maxcol, p;
/*
Serial.print("Refresh ("); Serial.print(xUpdateMin, DEC);
Serial.print(", "); Serial.print(xUpdateMax, DEC);
Serial.print(","); Serial.print(yUpdateMin, DEC);
Serial.print(", "); Serial.print(yUpdateMax, DEC); Serial.println(")");
*/
for(p = 0; p < 4; p++) {
/*
putstring("new page! ");
uart_putw_dec(p);
putstring_nl("");
*/
#ifdef enablePartialUpdate
// check if this page is part of update
if ( yUpdateMin >= ((p+1)*8) ) {
continue; // nope, skip it!
}
if (yUpdateMax < p*8) {
break;
}
#endif
st7565_command(CMD_SET_PAGE | (3 - p));
#ifdef enablePartialUpdate
col = xUpdateMin;
maxcol = xUpdateMax;
#else
// start at the beginning of the row
col = 0;
maxcol = LCDWIDTH-1;
#endif
st7565_command(CMD_SET_COLUMN_LOWER | ((col+ST7565_STARTBYTES) & 0xf));
st7565_command(CMD_SET_COLUMN_UPPER | (((col+ST7565_STARTBYTES) >> 4) & 0x0F));
for(; col <= maxcol; col++) {
//uart_putw_dec(col);
//uart_putchar(' ');
st7565_data(st7565_buffer[(128*p)+col]);
}
}
#ifdef enablePartialUpdate
xUpdateMin = LCDWIDTH - 1;
xUpdateMax = 0;
yUpdateMin = LCDHEIGHT-1;
yUpdateMax = 0;
#endif
}
// clear everything
void ST7565::clear(void) {
memset(st7565_buffer, 0, sizeof(st7565_buffer));
updateBoundingBox(0, 0, LCDWIDTH-1, LCDHEIGHT-1);
}
// this doesnt touch the buffer, just clears the display RAM - might be handy
void ST7565::clear_display(void) {
uint8_t p, c;
for(p = 0; p < 8; p++) {
/*
putstring("new page! ");
uart_putw_dec(p);
putstring_nl("");
*/
st7565_command(CMD_SET_PAGE | p);
for(c = 0; c < 129; c++) {
//uart_putw_dec(c);
//uart_putchar(' ');
st7565_command(CMD_SET_COLUMN_LOWER | (c & 0xf));
st7565_command(CMD_SET_COLUMN_UPPER | ((c >> 4) & 0xf));
st7565_data(0x0);
}
}
}