-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepd2in13b_V4.cpp
More file actions
233 lines (202 loc) · 6.04 KB
/
epd2in13b_V4.cpp
File metadata and controls
233 lines (202 loc) · 6.04 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
/**
* @filename : epd2in13b_V4.cpp
* @brief : Implements for Three-color e-paper library
* @author : Yehui from Waveshare
*
* Copyright (C) Waveshare April 25 2022
*
* 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 "epd2in13b_V4.h"
Epd::~Epd() {
};
Epd::Epd() {
reset_pin = RST_PIN;
dc_pin = DC_PIN;
cs_pin = CS_PIN;
busy_pin = BUSY_PIN;
width = EPD_WIDTH;
bufwidth = 128/8;
height = EPD_HEIGHT;
bufheight = 63;
};
int Epd::Init(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(0xf9);
SendData(0x00);
SendData(0x00);
SendCommand(0x11); //data entry mode
SendData(0x03);
SetWindows(0, 0, width-1, height-1);
SetCursor(0, 0);
SendCommand(0x3C); //BorderWavefrom
SendData(0x05);
SendCommand(0x18); //Read built-in temperature sensor
SendData(0x80);
SendCommand(0x21); // Display update control
SendData(0x80);
SendData(0x80);
WaitUntilIdle();
return 0;
}
/**
* @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) { //1: busy, 0: idle
DelayMs(10);
}
DelayMs(10);
}
/**
* @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);
DelayMs(2);
DigitalWrite(reset_pin, HIGH);
DelayMs(20);
this->count = 0;
}
// Setting the display window
void Epd::SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend) {
SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
SendData((Xstart>>3) & 0xFF);
SendData((Xend>>3) & 0xFF);
SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
SendData(Ystart & 0xFF);
SendData((Ystart >> 8) & 0xFF);
SendData(Yend & 0xFF);
SendData((Yend >> 8) & 0xFF);
}
// Set Cursor
void Epd::SetCursor(UWORD Xstart, UWORD Ystart) {
SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
SendData(Xstart & 0xFF);
SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
SendData(Ystart & 0xFF);
SendData((Ystart >> 8) & 0xFF);
}
/**
* @brief: refresh and displays the frame
*/
void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
if (frame_buffer_black != NULL) {
SendCommand(0x24);
for (int i = 0; i < this->bufwidth * this->height; i++) {
SendData(pgm_read_byte(&frame_buffer_black[i]));
}
DelayMs(2);
}
if (frame_buffer_red != NULL) {
SendCommand(0x26);
for (int i = 0; i < this->bufwidth * this->height; i++) {
SendData(pgm_read_byte(&frame_buffer_red[i]));
}
}
SendCommand(0x20);
WaitUntilIdle();
this->count = 0;
}
void Epd::Display(const unsigned char* frame_buffer) {
if(this->count == 0){
SendCommand(0x24);
this->count++;
}else if(this->count > 0 && this->count < 4 ){
this->count++;
}else if(this->count == 4){
SendCommand(0x26);
this->count++;
}else if(this->count > 4 && this->count < 8 ){
this->count++;
}
for(int i = 0; i < this->bufwidth * this->bufheight; i++){
SendData(frame_buffer[i]);
}
if(this->count == 8){
SendCommand(0x20);
WaitUntilIdle();
this->count = 0;
}
}
/**
* @brief: clear the frame data from the SRAM, this won't refresh the display
*/
void Epd::ClearFrame(void) {
SendCommand(0x24);
for(int i = 0; i < bufwidth * height; i++) {
SendData(0xFF);
}
SendCommand(0x26);
for(int i = 0; i < bufwidth * height; i++) {
SendData(0xFF);
}
SendCommand(0x20);
WaitUntilIdle();
this->count = 0;
}
/**
* @brief: This displays the frame data from SRAM
*/
void Epd::DisplayFrame(void) {
this->count = 0;
SendCommand(0x20); // Activate Display Update Sequence
WaitUntilIdle();
}
/**
* @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::Reset() to awaken and use Epd::Init() to initialize.
*/
void Epd::Sleep() {
SendCommand(0x10); //enter deep sleep
SendData(0x01);
DelayMs(100);
this->count = 0;
}
/* END OF FILE */