-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcd.cpp
352 lines (320 loc) · 13.2 KB
/
lcd.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
#include "lcd.h"
#include <Arduino.h>
#include <Wire.h>
#include "font.c"
#include "picture.c"
/******************************************
*******************************************/
lcd::lcd(){
}
/******************************************
* Write a command to LCD.
* Define instructions on page 23 of the ST7567A data sheet.
*******************************************/
void lcd::WriteByte_command(int dat){
Wire.beginTransmission(addr); // transmit to device 0x3f
Wire.write(0x00); // Co=0,A0=0. data= Co-A0-0-0-0-0-0-0.
Wire.write(dat); // sends restart command.
Wire.endTransmission(); // stop transmitting
}
/******************************************
* Init the LCD.
* This initialization function is called when using LCD.
*******************************************/
void lcd::Init(){
Wire.begin();
delay(10);
WriteByte_command(restart); //resets Start Line (S[5:0]), Column Address (X[7:0]),
delay(10); //Page Address (Y[3:0]) and COM Direction (MY) to their default setting.
WriteByte_command(BS); //select bias
WriteByte_command(seg); //seg output scan direction setting
WriteByte_command(com); //com output scan direction setting
/********Adjust display brightness********/
WriteByte_command(regRatio); //Controls the regulation ratio of the built-in regulator.
WriteByte_command(EV1);
WriteByte_command(EV2);
WriteByte_command(powerCon1); //Controls the built-in power circuits.
WriteByte_command(powerCon2);
WriteByte_command(powerCon3);
Clear(); //initalize DDRAM
/**********display setting mode**********/
WriteByte_command(enter_EC); //enter extension command set.
WriteByte_command(DSM_ON); //display settin mode on.
WriteByte_command(exit_EC); //exit extension command set.
WriteByte_command(DT); //set the display duty, DT=33.
WriteByte_command(BA); //This command has priority over the Bias Select (BS).
WriteByte_command(FR); //specifies the frame rate for duty. duty=250.
//*/
WriteByte_command(displayON); //display on.
WriteByte_command(startLine); //Set display start line.
}
/******************************************
* Used to test screen pixels.
*******************************************/
void lcd::testPixel(int t){
for(int x=0; x<4; x++){
WriteByte_command(0xb0 + x); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
/*******automatically increased by one******/
WriteByte_command(0x10); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(0x00); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
for(int i=0; i<128; i++){
WriteByte_dat(0xff); //row=bit0--bit7, display on
delay(t);
}
}
for(int x=0; x<4; x++){
WriteByte_command(0xb0 + x);
WriteByte_command(0x00);
WriteByte_command(0x10);
for(int i=0; i<128; i++){
WriteByte_dat(0x00); //row=bit0--bit7, display off
delay(t);
}
}
}
/******************************************
* Write a data to LCD.
* Define instructions on page 23 of the ST7567A data sheet.
*******************************************/
void lcd::WriteByte_dat(int dat){
Wire.beginTransmission(addr); // transmit to device 0x7e
Wire.write(0x40); // Co=0,A0=1. data= Co-A0-0-0-0-0-0-0.
Wire.write(dat); // sends data.
Wire.endTransmission(); // stop transmitting
}
/******************************************
* Continuously write data to LCD.
* Define instructions on page 23 of the ST7567A data sheet.
*******************************************/
void lcd::WriteCont_dat(int str[]){
int len = 0;
len = sizeof(str);
Wire.beginTransmission(addr); // transmit to device 0x7e
for(int i=0; i<len-1; i++){
Wire.write(0xc0); // Co=1,A0=1. data= Co-A0-0-0-0-0-0-0.
Wire.write(str[i]); // sends command.
}
Wire.write(0x40); // Co=0,A0=1. data= Co-A0-0-0-0-0-0-0.
Wire.write(str[len-1]); // sends command.
Wire.endTransmission(); // stop transmitting
}
/******************************************
* read one byte RAM data to MCU.
* Define instructions on page 23 of the ST7567A data sheet.
*******************************************/
int lcd::ReadByte_dat(int col, int page){
int dat;
WriteByte_command(0xb0 + page); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
WriteByte_command(0x10 + col/16); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(col%16); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
for(int i=0; i<2; i++){
Wire.requestFrom(addr, 2); //request 2 bytes from slave device
while(Wire.available()){ //slave may send less than requested
dat = Wire.read(); //receive a byte as character
}
}
return dat;
}
/******************************************
* display one pixel. Read-modify-Write command on page 42 of ST7567A datasheet.
* x=0-128, y=0-31
*******************************************/
void lcd::DisplayPixel(int x,int y){
int dat;
WriteByte_command(0xb0 + y/8); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
WriteByte_command(0x10 + x/16); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(x%16); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
WriteByte_command(RMW); //start Read-modify-Write
for(int i=0; i<2; i++){
Wire.requestFrom(addr, 2); //request 2 bytes from slave device
while(Wire.available()){ //slave may send less than requested
dat = Wire.read(); //receive a byte as character
}
}
dat = (dat | (1<<(y%8)));
WriteByte_dat(dat); //row=bit0--bit7
WriteByte_command(END); //end Read-modify-Write
}
/******************************************
* Does not display a pixel. Read-modify-Write command on page 42 of ST7567A datasheet.
* x=0-128, y=0-31
*******************************************/
void lcd::ClearPixel(int x,int y){
int dat;
WriteByte_command(0xb0 + y/8); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
WriteByte_command(0x10 + x/16); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(x%16); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
WriteByte_command(RMW); //start Read-modify-Write
for(int i=0; i<2; i++){
Wire.requestFrom(addr, 2); //request 2 bytes from slave device
while(Wire.available()){ //slave may send less than requested
dat = Wire.read(); //receive a byte as character
}
}
//Serial.println(dat); // print the data
dat = (dat & (0xfe<<(y%8)));
WriteByte_dat(dat); //row=bit0--bit7
WriteByte_command(END); //end Read-modify-Write
}
/******************************************
* clear screen, all pixel off.
* screen size: 128*32 dot
*******************************************/
void lcd::Clear(){
for(int x=0; x<4; x++){
WriteByte_command(0xb0 + x); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
/*******automatically increased by one******/
WriteByte_command(0x10); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(0x00); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
for(int i=0; i<128; i++){
WriteByte_dat(0x00); //row=bit0--bit7
}
}
}
///////////////////reserve///////////////////
void lcd::FontSize(int num){
}
/******************************************
* Character display position. x=0-3, y=0-17
*******************************************/
void lcd::Cursor(int y,int x){
if(x>17){x=17;}
if(y>3){x=3;}
cursor[0]=y;
cursor[1]=x;
}
/******************************************
* display picture.
*******************************************/
void lcd::DisplayPicture(){
for(int x=0; x<4; x++){
WriteByte_command(0xb0 + x); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
/*******automatically increased by one******/
WriteByte_command(0x10); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(0x00); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
for(int i=0; i<128; i++){
WriteByte_dat(pgm_read_word_near(picture+i+x*128));
}
}
}
/******************************************
* Writes the data from the font.c file to RAM.
*******************************************/
void lcd::WriteFont(int num){
for(int i=0; i<7; i++){
//reference: https://www.arduino.cc/reference/en/language/variables/utilities/progmem/
WriteByte_dat(pgm_read_word_near(font_7x8[num]+i));
//Serial.println(pgm_read_word_near(font_7x8[num]+i),HEX);
}
}
/******************************************
* display font.
*******************************************/
void lcd::Display(char *str){
int len = 0;
len = strlen(str);
WriteByte_command(0xb0 + cursor[0]); //y, page address y=1-0-1-1-y3-y2-y1-y0, 1-page with 8-rows
/*******automatically increased by one******/
WriteByte_command(0x10 + cursor[1]*7/16); //x, column address x=0-0-0-0-1-x7-x6-x5-x4
WriteByte_command(0x00 + cursor[1]*7%16); //x, column address x=0-0-0-0-0-x3-x2-x1-x0
for(int num=0; num<len; num++){
switch(str[num]){
case '0': WriteFont(0); break;
case '1': WriteFont(1); break;
case '2': WriteFont(2); break;
case '3': WriteFont(3); break;
case '4': WriteFont(4); break;
case '5': WriteFont(5); break;
case '6': WriteFont(6); break;
case '7': WriteFont(7); break;
case '8': WriteFont(8); break;
case '9': WriteFont(9); break;
case 'a': WriteFont(10); break;
case 'b': WriteFont(11); break;
case 'c': WriteFont(12); break;
case 'd': WriteFont(13); break;
case 'e': WriteFont(14); break;
case 'f': WriteFont(15); break;
case 'g': WriteFont(16); break;
case 'h': WriteFont(17); break;
case 'i': WriteFont(18); break;
case 'j': WriteFont(19); break;
case 'k': WriteFont(20); break;
case 'l': WriteFont(21); break;
case 'm': WriteFont(22); break;
case 'n': WriteFont(23); break;
case 'o': WriteFont(24); break;
case 'p': WriteFont(25); break;
case 'q': WriteFont(26); break;
case 'r': WriteFont(27); break;
case 's': WriteFont(28); break;
case 't': WriteFont(29); break;
case 'u': WriteFont(30); break;
case 'v': WriteFont(31); break;
case 'w': WriteFont(32); break;
case 'x': WriteFont(33); break;
case 'y': WriteFont(34); break;
case 'z': WriteFont(35); break;
case 'A': WriteFont(36); break;
case 'B': WriteFont(37); break;
case 'C': WriteFont(38); break;
case 'D': WriteFont(39); break;
case 'E': WriteFont(40); break;
case 'F': WriteFont(41); break;
case 'G': WriteFont(42); break;
case 'H': WriteFont(43); break;
case 'I': WriteFont(44); break;
case 'J': WriteFont(45); break;
case 'K': WriteFont(46); break;
case 'L': WriteFont(47); break;
case 'M': WriteFont(48); break;
case 'N': WriteFont(49); break;
case 'O': WriteFont(50); break;
case 'P': WriteFont(51); break;
case 'Q': WriteFont(52); break;
case 'R': WriteFont(53); break;
case 'S': WriteFont(54); break;
case 'T': WriteFont(55); break;
case 'U': WriteFont(56); break;
case 'V': WriteFont(57); break;
case 'W': WriteFont(58); break;
case 'X': WriteFont(59); break;
case 'Y': WriteFont(60); break;
case 'Z': WriteFont(61); break;
case '!': WriteFont(62); break;
case '"': WriteFont(63); break;
case '#': WriteFont(64); break;
case '$': WriteFont(65); break;
case '%': WriteFont(66); break;
case '&': WriteFont(67); break;
case '\'': WriteFont(68); break;
case '(': WriteFont(69); break;
case ')': WriteFont(70); break;
case '*': WriteFont(71); break;
case '+': WriteFont(72); break;
case ',': WriteFont(73); break;
case '-': WriteFont(74); break;
case '/': WriteFont(75); break;
case ':': WriteFont(76); break;
case ';': WriteFont(77); break;
case '<': WriteFont(78); break;
case '=': WriteFont(79); break;
case '>': WriteFont(80); break;
case '?': WriteFont(81); break;
case '@': WriteFont(82); break;
case '{': WriteFont(83); break;
case '|': WriteFont(84); break;
case '}': WriteFont(85); break;
case '~': WriteFont(86); break;
case ' ': WriteFont(87); break;
case '.': WriteFont(88); break;
case '^': WriteFont(89); break;
case '_': WriteFont(90); break;
case '`': WriteFont(91); break;
case '[': WriteFont(92); break;
case '\\': WriteFont(93); break;
case ']': WriteFont(94); break;
default: break;
}
}
}