forked from ShiningMan123/EPD-ESP32-Arduino-IDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPD.cpp
517 lines (408 loc) · 12.7 KB
/
EPD.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
#include <Arduino.h>
#include "EPD.h"
//////需要哪款取消哪款的注释
//#include "GDEY0213B74.h"
//#include "GDEY0266T90.h"
//#include "GDEY027T91.h"
//#include "GDEY029T94.h"
#include "GDEW0371W7.h"
////////////
//GPIO设置,自由定义io口
int BUSY_Pin = 14;
int RES_Pin = 15;
int DC_Pin = 16;
int CS_Pin = 13;
int SCK_Pin = 12;
int SDI_Pin = 11;
#define EPD_W21_MOSI_0 digitalWrite(SDI_Pin,LOW)
#define EPD_W21_MOSI_1 digitalWrite(SDI_Pin,HIGH)
#define EPD_W21_CLK_0 digitalWrite(SCK_Pin,LOW)
#define EPD_W21_CLK_1 digitalWrite(SCK_Pin,HIGH)
#define EPD_W21_CS_0 digitalWrite(CS_Pin,LOW)
#define EPD_W21_CS_1 digitalWrite(CS_Pin,HIGH)
#define EPD_W21_DC_0 digitalWrite(DC_Pin,LOW)
#define EPD_W21_DC_1 digitalWrite(DC_Pin,HIGH)
#define EPD_W21_RST_0 digitalWrite(RES_Pin,LOW)
#define EPD_W21_RST_1 digitalWrite(RES_Pin,HIGH)
#define isEPD_W21_BUSY digitalRead(BUSY_Pin)
//定义RAM空间
#define MONOMSB_MODE 1
#define MONOLSB_MODE 2
#define RED_MODE 3
//GPIO初始化
void EPD::GPIO_set(void)
{
pinMode(BUSY_Pin, INPUT);
pinMode(RES_Pin, OUTPUT);
pinMode(DC_Pin, OUTPUT);
pinMode(CS_Pin, OUTPUT);
pinMode(SCK_Pin, OUTPUT);
pinMode(SDI_Pin, OUTPUT);
}
//模拟spi波形
void EPD::SPI_Write(unsigned char value)
{
unsigned char i;
SPI_Delay(1);
for(i=0; i<8; i++)
{
EPD_W21_CLK_0;
SPI_Delay(1);
if(value & 0x80)
EPD_W21_MOSI_1;
else
EPD_W21_MOSI_0;
value = (value << 1);
SPI_Delay(1);
driver_delay_us(1);
EPD_W21_CLK_1;
SPI_Delay(1);
}
}
//写入电子纸芯片命令
void EPD::Epaper_Write_Command(unsigned char cmd)
{
SPI_Delay(1);
EPD_W21_CS_0;
EPD_W21_DC_0; // command write
SPI_Write(cmd);
EPD_W21_CS_1;
}
//写入电子纸芯片数据
void EPD::Epaper_Write_Data(unsigned char datas)
{
SPI_Delay(1);
EPD_W21_CS_0;
EPD_W21_DC_1; // data write
SPI_Write(datas);
EPD_W21_CS_1;
}
//读取芯片状态
void EPD::Epaper_READBUSY(void)
{
while(1)
{ //=1 BUSY
if(isEPD_W21_BUSY==0) break;
}
}
//延时函数
void EPD::driver_delay_us(unsigned int xus) //1us
{
for(;xus>1;xus--);
}
void EPD::driver_delay(unsigned long xms) //1ms
{
unsigned long i = 0 , j=0;
for(j=0;j<xms;j++)
{
for(i=0; i<256; i++);
}
}
void EPD::DELAY_S(unsigned int delaytime)
{
int i,j,k;
for(i=0;i<delaytime;i++)
{
for(j=0;j<4000;j++)
{
for(k=0;k<222;k++);
}
}
}
void EPD::SPI_Delay(unsigned char xrate)
{
unsigned char i;
while(xrate)
{
for(i=0;i<2;i++);
xrate--;
}
}
//display ///////////
//刷新初始化SSD1680
void EPD:: EPD_HW1_Init(void)
{
EPD_W21_RST_0; // Module reset
delay(10);//At least 10ms delay
EPD_W21_RST_1;
delay(10); //At least 10ms delay
Epaper_READBUSY();
Epaper_Write_Command(0x12); //SWRESET
Epaper_READBUSY();
}
//高刷初始化SSD1680
void EPD::EPD_HW_Init_Fast(void)
{
EPD_W21_RST_0; // Module reset
delay(10);//At least 10ms delay
EPD_W21_RST_1;
delay(10); //At least 10ms delay
Epaper_Write_Command(0x12); //SWRESET
Epaper_READBUSY();
Epaper_Write_Command(0x18); //Read built-in temperature sensor
Epaper_Write_Data(0x80);
Epaper_Write_Command(0x22); // Load temperature value
Epaper_Write_Data(0xB1);
Epaper_Write_Command(0x20);
Epaper_READBUSY();
Epaper_Write_Command(0x1A); // Write to temperature register
Epaper_Write_Data(0x64);
Epaper_Write_Data(0x00);
Epaper_Write_Command(0x22); // Load temperature value
Epaper_Write_Data(0x91);
Epaper_Write_Command(0x20);
Epaper_READBUSY();
}
//////////UC系列初始化//////
//////////////////////////////All screen update////////////////////////////////////////////
void EPD::EPD_WhiteScreen_ALL(const unsigned char * datas)
{
unsigned int i;
Epaper_Write_Command(0x24); //write RAM for black(0)/white (1)
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(pgm_read_byte(&datas[i]));
}
EPD_Update();
}
//////////高刷全图/////////
void EPD::EPD_WhiteScreen_ALL_fast(const unsigned char * datas)
{
unsigned int i;
Epaper_Write_Command(0x24); //write RAM for black(0)/white (1)
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(pgm_read_byte(&datas[i]));
}
EPD_Update_Fast();
}
////////////////////////3种刷新方式SSD1680///////////////////////////////////////
void EPD::EPD_Update(void)
{
Epaper_Write_Command(0x22); //Display Update Control
Epaper_Write_Data(0xF7);
Epaper_Write_Command(0x20); //Activate Display Update Sequence
Epaper_READBUSY();
}
void EPD::EPD_Part_Update(void)
{
Epaper_Write_Command(0x22); //Display Update Control
Epaper_Write_Data(0xFF);
Epaper_Write_Command(0x20); //Activate Display Update Sequence
Epaper_READBUSY();
}
void EPD::EPD_Update_Fast(void)
{
Epaper_Write_Command(0x22); //Display Update Control
Epaper_Write_Data(0xC7);
Epaper_Write_Command(0x20); //Activate Display Update Sequence
Epaper_READBUSY();
}
//////UC8253刷新方式//////////
/////////睡眠模式SSD1680/////////
void EPD::EPD_DeepSleep(void)
{
Epaper_Write_Command(0x10); //enter deep sleep
Epaper_Write_Data(0x01);
delay(100);
}
/////////睡眠模式UC8253/////////
///////////////////////////Part update//////////////////////////////////////////////
void EPD::EPD_SetRAMValue_BaseMap( const unsigned char * datas)
{
unsigned int i;
const unsigned char *datas_flag;
datas_flag=datas;
Epaper_Write_Command(0x24); //Write Black and White image to RAM
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(pgm_read_byte(&datas[i]));
}
datas=datas_flag;
Epaper_Write_Command(0x26); //Write Black and White image to RAM
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(pgm_read_byte(&datas[i]));
}
EPD_Update();
}
void EPD::EPD_Dis_Part(unsigned int x_start,unsigned int y_start,const unsigned char * datas,unsigned int PART_COLUMN,unsigned int PART_LINE)
{
unsigned int i;
unsigned int x_end,y_start1,y_start2,y_end1,y_end2;
x_start=x_start/8;//
x_end=x_start+PART_LINE/8-1;
y_start1=0;
y_start2=y_start;
if(y_start>=256)
{
y_start1=y_start2/256;
y_start2=y_start2%256;
}
y_end1=0;
y_end2=y_start+PART_COLUMN-1;
if(y_end2>=256)
{
y_end1=y_end2/256;
y_end2=y_end2%256;
}
//Reset
EPD_W21_RST_0; // Module reset
delay(10);//At least 10ms delay
EPD_W21_RST_1;
delay(10); //At least 10ms delay
Epaper_Write_Command(0x3C); //BorderWavefrom
Epaper_Write_Data(0x80);
//
Epaper_Write_Command(0x44); // set RAM x address start/end, in page 35
Epaper_Write_Data(x_start); // RAM x address start at 00h;
Epaper_Write_Data(x_end); // RAM x address end at 0fh(15+1)*8->128
Epaper_Write_Command(0x45); // set RAM y address start/end, in page 35
Epaper_Write_Data(y_start2); // RAM y address start at 0127h;
Epaper_Write_Data(y_start1); // RAM y address start at 0127h;
Epaper_Write_Data(y_end2); // RAM y address end at 00h;
Epaper_Write_Data(y_end1); // ????=0
Epaper_Write_Command(0x4E); // set RAM x address count to 0;
Epaper_Write_Data(x_start);
Epaper_Write_Command(0x4F); // set RAM y address count to 0X127;
Epaper_Write_Data(y_start2);
Epaper_Write_Data(y_start1);
Epaper_Write_Command(0x24); //Write Black and White image to RAM
for(i=0;i<PART_COLUMN*PART_LINE/8;i++)
{
Epaper_Write_Data(pgm_read_byte(&datas[i]));
}
EPD_Part_Update();
}
/////////////////////////////////Single display////////////////////////////////////////////////
void EPD::EPD_WhiteScreen_Black(void)
{
unsigned int i,k;
Epaper_Write_Command(0x24); //write RAM for black(0)/white (1)
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(0x00);
}
EPD_Update();
}
///////清屏SSD1680////////
void EPD::EPD_WhiteScreen_White(void)
{
unsigned int i;
Epaper_Write_Command(0x24); //write RAM for black(0)/white (1)
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(0xff);
}
EPD_Update();
}
///////清屏UC8253////////
void EPD::EPD_SSD1680_DISPLAY_TEXT(void)
{
unsigned char fen_L,fen_H,miao_L,miao_H;
EPD_HW1_Init(); //Electronic paper initialization
EPD_WhiteScreen_ALL(gImage_1); //Refresh the picture in full screen
delay(2000);
EPD_HW_Init_Fast();
EPD_WhiteScreen_ALL_fast(gImage_2);
delay(2000);
//////////////////////Partial refresh digital presentation//////////////////////////////////////
EPD_HW1_Init(); //Electronic paper initialization
EPD_SetRAMValue_BaseMap(gImage_basemap); //Partial refresh background color,Brush map is a must, please do not delete
delay(100);
EPD_Dis_Part(0,32,gImage_num1,32,32); //x,y,DATA,resolution 32*32
EPD_Dis_Part(0,32,gImage_num2,32,32); //x,y,DATA,resolution 32*32
EPD_Dis_Part(0,32,gImage_num3,32,32); //x,y,DATA,resolution 32*32
EPD_Dis_Part(0,32,gImage_num4,32,32); //x,y,DATA,resolution 32*32
EPD_Dis_Part(0,32,gImage_num5,32,32); //x,y,DATA,resolution 32*32
delay(1000);
////////////////////////////////////////////////////////////////////////
//Clean
EPD_HW1_Init();//Electronic paper initialization
EPD_WhiteScreen_White(); //Show all white
EPD_DeepSleep(); //Enter deep sleep,Sleep instruction is necessary, please do not delete!!!
while(1);
}
////////UC8253/////////
void EPD::EPD_UC8253_init(void)
{
HRES=0xf0; //240
VRES_byte1=0x01; //416
VRES_byte2=0xa0;
EPD_W21_RST_0; // Module reset
delay(10);//At least 10ms delay
EPD_W21_RST_1;
delay(10); //At least 10ms delay
// delay(100);
Epaper_Write_Command(0x06); //boost soft start
Epaper_Write_Data (0x17); //A
Epaper_Write_Data (0x17); //B
Epaper_Write_Data (0x1d); //C
Epaper_Write_Command(0x04); //Power on
Epaper_READBUSY(); //waiting for the electronic paper IC to release the idle signal
Epaper_Write_Command(0x00); //panel setting
Epaper_Write_Data(0x1f); //LUT from OTP--240x416
Epaper_Write_Command(0x61); //resolution setting
Epaper_Write_Data (HRES);
Epaper_Write_Data (VRES_byte1);
Epaper_Write_Data (VRES_byte2);
Epaper_Write_Command(0x50);
Epaper_Write_Data (0x23);
Epaper_Write_Data (0x07);
}
void EPD::EPD_UC8253_WhiteScreen_ALL(const unsigned char * datas)
{
unsigned int i;
Epaper_Write_Command(0x10);
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
Epaper_Write_Data(0xff);
Epaper_Write_Command(0x13); //write RAM for black(0)/white (1)
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
Epaper_Write_Data(pgm_read_byte(&datas[i]));
// EPD_UC8253_refresh();
}
void EPD::EPD_UC8253_refresh(void)
{
Epaper_Write_Command(0x12); //DISPLAY REFRESH
delay(100); //!!!The delay here is necessary, 200uS at least!!!
Epaper_READBUSY();
}
void EPD::EPD_UC8253_sleep(void)
{
Epaper_Write_Command(0X50); //VCOM AND DATA INTERVAL SETTING
Epaper_Write_Data(0xf7);
Epaper_Write_Command(0X02); //power off
Epaper_READBUSY();
Epaper_Write_Command(0X07); //deep sleep
Epaper_Write_Data(0xA5);
}
void EPD::EPD_UC8253_WhiteScreen_White(void)
{
unsigned int i;
Epaper_Write_Command(0x10); //write RAM for black(0)/white (1)
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
Epaper_Write_Data(0xff);
Epaper_Write_Command(0x13);
for(i=0;i<ALLSCREEN_GRAGHBYTES;i++)
{
Epaper_Write_Data(0xff);
}
EPD_UC8253_refresh();
}
void EPD::EPD_UC8253_DISPLAY_TEXT(void)
{
EPD_UC8253_init();
EPD_UC8253_WhiteScreen_ALL(gImage_1);
EPD_UC8253_refresh();
EPD_UC8253_sleep();
delay(5000);
EPD_UC8253_init();
EPD_UC8253_WhiteScreen_ALL(gImage_2);
EPD_UC8253_refresh();
EPD_UC8253_sleep();
delay(5000);
EPD_UC8253_init();
EPD_UC8253_WhiteScreen_White();
EPD_UC8253_sleep();
while(1);
}