-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepd1in54_V2.cpp
576 lines (501 loc) · 12.9 KB
/
epd1in54_V2.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
569
570
571
572
573
574
575
576
/*****************************************************************************
* | File : epd1in54_V2.cpp
* | Author : Waveshare team
* | Function : 1.54inch e-paper V2
* | Info :
*----------------
* | This version: V1.0
* | Date : 2019-06-24
* | Info :
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include <stdlib.h>
#include "epd1in54_V2.h"
// waveform full refresh
unsigned char WF_Full_1IN54[159] =
{
0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x80, 0x48, 0x40, 0x0, 0x0, 0x0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0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
0x02,0x17,0x41,0xB0,0x32,0x28,
};
Epd::~Epd()
{
};
Epd::Epd()
{
reset_pin = RST_PIN;
dc_pin = DC_PIN;
cs_pin = CS_PIN;
busy_pin = BUSY_PIN;
width = EPD_WIDTH;
height = EPD_HEIGHT;
};
/**
* @brief: basic function for sending commands
*/
void Epd::SendCommand(unsigned char command)
{
DigitalWrite(dc_pin, LOW);
SpiTransfer(command);
}
/**
* @brief: basic function for sending data
*/
void Epd::SendData(unsigned char data)
{
DigitalWrite(dc_pin, HIGH);
SpiTransfer(data);
}
/**
* @brief: Wait until the busy_pin goes HIGH
*/
void Epd::WaitUntilIdle(void)
{
while(DigitalRead(busy_pin) == 1) { //LOW: idle, HIGH: busy
DelayMs(100);
}
DelayMs(200);
}
void Epd::Lut(unsigned char* lut)
{
SendCommand(0x32);
for(unsigned char i=0; i<153; i++)
SendData(lut[i]);
WaitUntilIdle();
}
void Epd::SetLut(unsigned char* lut)
{
Lut(lut);
SendCommand(0x3f);
SendData(lut[153]);
SendCommand(0x03);
SendData(lut[154]);
SendCommand(0x04);
SendData(lut[155]);
SendData(lut[156]);
SendData(lut[157]);
SendCommand(0x2c);
SendData(lut[158]);
}
// High Direction
int Epd::HDirInit(void)
{
/* this calls the peripheral hardware interface, see epdif */
if (IfInit() != 0) {
return -1;
}
/* EPD hardware init start */
Reset();
WaitUntilIdle();
SendCommand(0x12); //SWRESET
WaitUntilIdle();
SendCommand(0x01); //Driver output control
SendData(0xC7);
SendData(0x00);
SendData(0x01);
SendCommand(0x11); //data entry mode
SendData(0x01);
SendCommand(0x44); //set Ram-X address start/end position
SendData(0x00);
SendData(0x18); //0x0C-->(18+1)*8=200
SendCommand(0x45); //set Ram-Y address start/end position
SendData(0xC7); //0xC7-->(199+1)=200
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendCommand(0x3C); //BorderWavefrom
SendData(0x01);
SendCommand(0x18);
SendData(0x80);
SendCommand(0x22); // //Load Temperature and waveform setting.
SendData(0XB1);
SendCommand(0x20);
SendCommand(0x4E); // set RAM x address count to 0;
SendData(0x00);
SendCommand(0x4F); // set RAM y address count to 0X199;
SendData(0xC7);
SendData(0x00);
WaitUntilIdle();
SetLut(WF_Full_1IN54);
/* EPD hardware init end */
return 0;
}
// Low Direction
int Epd::LDirInit(void)
{
/* this calls the peripheral hardware interface, see epdif */
if (IfInit() != 0) {
return -1;
}
/* EPD hardware init start */
Reset();
WaitUntilIdle();
SendCommand(0x12); //SWRESET
WaitUntilIdle();
SendCommand(0x01); //Driver output control
SendData(0xC7);
SendData(0x00);
SendData(0x00);
SendCommand(0x11); //data entry mode
SendData(0x03);
SendCommand(0x44);
/* x point must be the multiple of 8 or the last 3 bits will be ignored */
SendData((0 >> 3) & 0xFF);
SendData((199 >> 3) & 0xFF);
SendCommand(0x45);
SendData(0 & 0xFF);
SendData((0 >> 8) & 0xFF);
SendData(199 & 0xFF);
SendData((199 >> 8) & 0xFF);
SendCommand(0x3C); //BorderWavefrom
SendData(0x01);
SendCommand(0x18);
SendData(0x80);
SendCommand(0x22); // //Load Temperature and waveform setting.
SendData(0XB1);
SendCommand(0x20);
SendCommand(0x4E); // set RAM x address count to 0;
SendData(0x00);
SendCommand(0x4F); // set RAM y address count to 0X199;
SendData(0xC7);
SendData(0x00);
WaitUntilIdle();
SetLut(WF_Full_1IN54);
/* EPD hardware init end */
return 0;
}
/**
* @brief: module reset.
* often used to awaken the module in deep sleep,
* see Epd::Sleep();
*/
void Epd::Reset(void)
{
DigitalWrite(reset_pin, HIGH);
DelayMs(20);
DigitalWrite(reset_pin, LOW); //module reset
DelayMs(5);
DigitalWrite(reset_pin, HIGH);
DelayMs(20);
}
void Epd::Clear(void)
{
int w, h;
w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
h = EPD_HEIGHT;
SendCommand(0x24);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(0xff);
}
}
SendCommand(0x26);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(0xff);
}
}
//DISPLAY REFRESH
DisplayFrame();
}
void Epd::Display(const unsigned char* frame_buffer)
{
int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
int h = EPD_HEIGHT;
if (frame_buffer != NULL) {
SendCommand(0x24);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(pgm_read_byte(&frame_buffer[i + j * w]));
}
}
}
//DISPLAY REFRESH
DisplayFrame();
}
void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
{
int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
int h = EPD_HEIGHT;
if (frame_buffer != NULL) {
SendCommand(0x24);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(pgm_read_byte(&frame_buffer[i + j * w]));
}
}
SendCommand(0x26);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(pgm_read_byte(&frame_buffer[i + j * w]));
}
}
}
//DISPLAY REFRESH
DisplayFrame();
}
void Epd::DisplayPartBaseWhiteImage(void)
{
int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
int h = EPD_HEIGHT;
SendCommand(0x24);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(0xff);
}
}
SendCommand(0x26);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(0xff);
}
}
//DISPLAY REFRESH
DisplayFrame();
}
void Epd::DisplayPart(const unsigned char* frame_buffer)
{
int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
int h = EPD_HEIGHT;
if (frame_buffer != NULL) {
SendCommand(0x24);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
SendData(pgm_read_byte(&frame_buffer[i + j * w]));
}
}
}
//DISPLAY REFRESH
DisplayPartFrame();
}
/**
* @brief: private function to specify the memory area for data R/W
*/
void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end)
{
SendCommand(0x44);
/* x point must be the multiple of 8 or the last 3 bits will be ignored */
SendData((x_start >> 3) & 0xFF);
SendData((x_end >> 3) & 0xFF);
SendCommand(0x45);
SendData(y_start & 0xFF);
SendData((y_start >> 8) & 0xFF);
SendData(y_end & 0xFF);
SendData((y_end >> 8) & 0xFF);
}
/**
* @brief: private function to specify the start point for data R/W
*/
void Epd::SetMemoryPointer(int x, int y)
{
SendCommand(0x4e);
/* x point must be the multiple of 8 or the last 3 bits will be ignored */
SendData((x >> 3) & 0xFF);
SendCommand(0x4F);
SendData(y & 0xFF);
SendData((y >> 8) & 0xFF);
WaitUntilIdle();
}
/**
* @brief: update the display
* there are 2 memory areas embedded in the e-paper display
* but once this function is called,
* the the next action of SetFrameMemory or ClearFrame will
* set the other memory area.
*/
void Epd::DisplayFrame(void)
{
//DISPLAY REFRESH
SendCommand(0x22);
SendData(0xc7);
SendCommand(0x20);
WaitUntilIdle();
}
void Epd::DisplayPartFrame(void)
{
SendCommand(0x22);
SendData(0xcF);
SendCommand(0x20);
WaitUntilIdle();
}
void Epd::SetFrameMemory(
const unsigned char* image_buffer,
int x,
int y,
int image_width,
int image_height
)
{
int x_end;
int y_end;
DigitalWrite(reset_pin, LOW); //module reset
DelayMs(2);
DigitalWrite(reset_pin, HIGH);
DelayMs(2);
SendCommand(0x3c);
SendData(0x80);
if (
image_buffer == NULL ||
x < 0 || image_width < 0 ||
y < 0 || image_height < 0
) {
return;
}
/* x point must be the multiple of 8 or the last 3 bits will be ignored */
x &= 0xF8;
image_width &= 0xF8;
if (x + image_width >= this->width) {
x_end = this->width - 1;
} else {
x_end = x + image_width - 1;
}
if (y + image_height >= this->height) {
y_end = this->height - 1;
} else {
y_end = y + image_height - 1;
}
SetMemoryArea(x, y, x_end, y_end);
SetMemoryPointer(x, y);
SendCommand(0x24);
/* send the image data */
for (int j = 0; j < y_end - y + 1; j++) {
for (int i = 0; i < (x_end - x + 1) / 8; i++) {
SendData(image_buffer[i + j * (image_width / 8)]);
}
}
}
void Epd::SetFrameMemoryPartial(
const unsigned char* image_buffer,
int x,
int y,
int image_width,
int image_height
)
{
int x_end;
int y_end;
DigitalWrite(reset_pin, LOW); //module reset
DelayMs(2);
DigitalWrite(reset_pin, HIGH);
DelayMs(2);
SetLut(WF_PARTIAL_1IN54_0);
SendCommand(0x37);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendData(0x40);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendCommand(0x3c);
SendData(0x80);
SendCommand(0x22);
SendData(0xc0);
SendCommand(0x20);
WaitUntilIdle();
if (
image_buffer == NULL ||
x < 0 || image_width < 0 ||
y < 0 || image_height < 0
) {
return;
}
/* x point must be the multiple of 8 or the last 3 bits will be ignored */
x &= 0xF8;
image_width &= 0xF8;
if (x + image_width >= this->width) {
x_end = this->width - 1;
} else {
x_end = x + image_width - 1;
}
if (y + image_height >= this->height) {
y_end = this->height - 1;
} else {
y_end = y + image_height - 1;
}
SetMemoryArea(x, y, x_end, y_end);
SetMemoryPointer(x, y);
SendCommand(0x24);
/* send the image data */
for (int j = 0; j < y_end - y + 1; j++) {
for (int i = 0; i < (x_end - x + 1) / 8; i++) {
SendData(image_buffer[i + j * (image_width / 8)]);
}
}
}
/**
* @brief: After this command is transmitted, the chip would enter the
* deep-sleep mode to save power.
* The deep sleep mode would return to standby by hardware reset.
* The only one parameter is a check code, the command would be
* executed if check code = 0xA5.
* You can use Epd::Init() to awaken
*/
void Epd::Sleep()
{
SendCommand(0x10); //enter deep sleep
SendData(0x01);
DelayMs(200);
DigitalWrite(reset_pin, LOW);
}
/* END OF FILE */