Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
GxEPD2_371.cpp
Go to the documentation of this file.
1// Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
2// Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
3//
4// based on Demo Example from Good Display: http://www.e-paper-display.com/download_detail/downloadsId=639.html
5// Panel: GDEW0371W7 : http://www.e-paper-display.com/products_detail/productId=401.html
6// Controller: IL0324 : http://www.e-paper-display.com/download_detail/downloadsId=763.html
7//
8// Author: Jean-Marc Zingg
9//
10// Version: see library.properties
11//
12// Library: https://github.com/ZinggJM/GxEPD2
13
14#include "GxEPD2_371.h"
15
16GxEPD2_371::GxEPD2_371(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
17 GxEPD2_EPD(cs, dc, rst, busy, LOW, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
18{
19}
20
21void GxEPD2_371::clearScreen(uint8_t value)
22{
23 writeScreenBuffer(value);
24 refresh(true);
25 writeScreenBuffer(value);
26}
27
29{
30 _initial_write = false; // initial full screen buffer clean done
31 if (!_using_partial_mode) _Init_Part();
32 _writeCommand(0x13); // set current
34 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
35 {
36 _transfer(value);
37 }
40 {
41 _writeCommand(0x10); // preset previous
43 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
44 {
45 _transfer(0xFF); // 0xFF is white
46 }
48 }
49}
50
51void GxEPD2_371::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
52{
53 if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
54 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
55 int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
56 x -= x % 8; // byte boundary
57 w = wb * 8; // byte boundary
58 int16_t x1 = x < 0 ? 0 : x; // limit
59 int16_t y1 = y < 0 ? 0 : y; // limit
60 int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
61 int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
62 int16_t dx = x1 - x;
63 int16_t dy = y1 - y;
64 w1 -= dx;
65 h1 -= dy;
66 if ((w1 <= 0) || (h1 <= 0)) return;
67 if (!_using_partial_mode) _Init_Part();
68 _writeCommand(0x91); // partial in
69 _setPartialRamArea(x1, y1, w1, h1);
70 _writeCommand(0x13);
72 for (int16_t i = 0; i < h1; i++)
73 {
74 for (int16_t j = 0; j < w1 / 8; j++)
75 {
76 uint8_t data;
77 // use wb, h of bitmap for index!
78 int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
79 if (pgm)
80 {
81#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
82 data = pgm_read_byte(&bitmap[idx]);
83#else
84 data = bitmap[idx];
85#endif
86 }
87 else
88 {
89 data = bitmap[idx];
90 }
91 if (invert) data = ~data;
92 _transfer(data);
93 }
94 }
96 _writeCommand(0x92); // partial out
97 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
98}
99
100void GxEPD2_371::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
101 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
102{
103 if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
104 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
105 if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
106 if ((x_part < 0) || (x_part >= w_bitmap)) return;
107 if ((y_part < 0) || (y_part >= h_bitmap)) return;
108 int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
109 x_part -= x_part % 8; // byte boundary
110 w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
111 h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
112 x -= x % 8; // byte boundary
113 w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
114 int16_t x1 = x < 0 ? 0 : x; // limit
115 int16_t y1 = y < 0 ? 0 : y; // limit
116 int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
117 int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
118 int16_t dx = x1 - x;
119 int16_t dy = y1 - y;
120 w1 -= dx;
121 h1 -= dy;
122 if ((w1 <= 0) || (h1 <= 0)) return;
123 if (!_using_partial_mode) _Init_Part();
124 _writeCommand(0x91); // partial in
125 _setPartialRamArea(x1, y1, w1, h1);
126 _writeCommand(0x13);
128 for (int16_t i = 0; i < h1; i++)
129 {
130 for (int16_t j = 0; j < w1 / 8; j++)
131 {
132 uint8_t data;
133 // use wb_bitmap, h_bitmap of bitmap for index!
134 int16_t idx = mirror_y ? x_part / 8 + j + dx / 8 + ((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + (y_part + i + dy) * wb_bitmap;
135 if (pgm)
136 {
137#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
138 data = pgm_read_byte(&bitmap[idx]);
139#else
140 data = bitmap[idx];
141#endif
142 }
143 else
144 {
145 data = bitmap[idx];
146 }
147 if (invert) data = ~data;
148 _transfer(data);
149 }
150 }
151 _endTransfer();
152 _writeCommand(0x92); // partial out
153 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
154}
155
156void GxEPD2_371::writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
157{
158 if (black)
159 {
160 writeImage(black, x, y, w, h, invert, mirror_y, pgm);
161 }
162}
163
164void GxEPD2_371::writeImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
165 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
166{
167 if (black)
168 {
169 writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
170 }
171}
172
173void GxEPD2_371::writeNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
174{
175 if (data1)
176 {
177 writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
178 }
179}
180
181void GxEPD2_371::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
182{
183 writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
184 refresh(x, y, w, h);
185 writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
186}
187
188void GxEPD2_371::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
189 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
190{
191 writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
192 refresh(x, y, w, h);
193 writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
194}
195
196void GxEPD2_371::drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
197{
198 writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
199 refresh(x, y, w, h);
200 writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
201}
202
203void GxEPD2_371::drawImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
204 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
205{
206 writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
207 refresh(x, y, w, h);
208 writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
209}
210
211void GxEPD2_371::drawNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
212{
213 writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
214 refresh(x, y, w, h);
215 writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
216}
217
218void GxEPD2_371::refresh(bool partial_update_mode)
219{
220 if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
221 else
222 {
223 if (_using_partial_mode) _Init_Full();
224 _Update_Full();
225 _initial_refresh = false; // initial full update done
226 }
227}
228
229void GxEPD2_371::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
230{
231 if (_initial_refresh) return refresh(false); // initial update needs be full update
232 // intersection with screen
233 int16_t w1 = x < 0 ? w + x : w; // reduce
234 int16_t h1 = y < 0 ? h + y : h; // reduce
235 int16_t x1 = x < 0 ? 0 : x; // limit
236 int16_t y1 = y < 0 ? 0 : y; // limit
237 w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
238 h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
239 if ((w1 <= 0) || (h1 <= 0)) return;
240 // make x1, w1 multiple of 8
241 w1 += x1 % 8;
242 if (w1 % 8 > 0) w1 += 8 - w1 % 8;
243 x1 -= x1 % 8;
244 if (!_using_partial_mode) _Init_Part();
245 if (usePartialUpdateWindow) _writeCommand(0x91); // partial in
246 _setPartialRamArea(x1, y1, w1, h1);
247 _Update_Part();
248 if (usePartialUpdateWindow) _writeCommand(0x92); // partial out
249}
250
252{
253 _PowerOff();
254}
255
257{
258 _PowerOff();
259 if (_rst >= 0)
260 {
261 _writeCommand(0x07); // deep sleep
262 _writeData(0xA5); // check code
263 _hibernating = true;
264 }
265}
266
267void GxEPD2_371::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
268{
269 uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
270 uint16_t ye = y + h - 1;
271 x &= 0xFFF8; // byte boundary
272 _writeCommand(0x90); // partial window
273 _writeData(x % 256);
274 _writeData(xe % 256);
275 _writeData(y / 256);
276 _writeData(y % 256);
277 _writeData(ye / 256);
278 _writeData(ye % 256);
279 _writeData(0x01);
280}
281
282void GxEPD2_371::_PowerOn()
283{
284 if (!_power_is_on)
285 {
286 _writeCommand(0x04);
287 _waitWhileBusy("_PowerOn", power_on_time);
288 }
289 _power_is_on = true;
290}
291
292void GxEPD2_371::_PowerOff()
293{
294 if (_power_is_on)
295 {
296 _writeCommand(0x03); // power off sequence
297 _writeData(0x30);
298 _writeCommand(0x02); // power off
299 _waitWhileBusy("_PowerOff", power_off_time);
300 }
301 _power_is_on = false;
302 _using_partial_mode = false;
303}
304
305void GxEPD2_371::_InitDisplay()
306{
307 if (_hibernating) _reset();
308 _writeCommand(0x01); // power setting
309 _writeData (0x07);
310 _writeData (0x07); // VGH=20V,VGL=-20V
311 _writeData (0x3f); // VDH=15V
312 _writeData (0x3f); // VDL=-15V
313 _writeCommand(0x06); // boost soft start
314 _writeData (0x17); // A
315 _writeData (0x17); // B
316 _writeData (0x1d); // C
317 _PowerOn();
318 _writeCommand(0x00); // panel setting
319 _writeData(0x1f); // LUT from OTP
320 _writeCommand(0x61); // resolution setting
322 _writeData (HEIGHT / 256);
323 _writeData (HEIGHT % 256);
324 _writeCommand(0x82); // vcom_DC setting
325 _writeData (0x1C);
326 _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
327 _writeData(0x29); // LUTKW, N2OCP: copy new to old
328 _writeData(0x07);
329}
330
331// experimental partial screen update LUTs with balanced charge
332// LUTs are filled with zeroes
333
334#define T1 35 // charge balance pre-phase
335#define T2 0 // optional extension
336#define T3 35 // color change phase (b/w)
337#define T4 0 // optional extension for one color
338
339const unsigned char GxEPD2_371::lut_20_LUTC_partial[] PROGMEM =
340{
341 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
342};
343
344const unsigned char GxEPD2_371::lut_21_LUTWW_partial[] PROGMEM =
345{ // 10 w
346 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
347};
348
349const unsigned char GxEPD2_371::lut_22_LUTKW_partial[] PROGMEM =
350{ // 10 w
351 0x48, T1, T2, T3, T4, 1, // 01 00 10 00
352 //0x5A, T1, T2, T3, T4, 1, // 01 01 10 10 more white
353};
354
355const unsigned char GxEPD2_371::lut_23_LUTWK_partial[] PROGMEM =
356{ // 01 b
357 0x84, T1, T2, T3, T4, 1, // 10 00 01 00
358 //0xA5, T1, T2, T3, T4, 1, // 10 10 01 01 more black
359};
360
361const unsigned char GxEPD2_371::lut_24_LUTKK_partial[] PROGMEM =
362{ // 01 b
363 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
364};
365
366const unsigned char GxEPD2_371::lut_25_LUTBD_partial[] PROGMEM =
367{
368 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
369};
370
371void GxEPD2_371::_Init_Full()
372{
373 _InitDisplay();
374 _writeCommand(0x00); // panel setting
375 _writeData(0x1f); // full update LUT from OTP
376 _PowerOn();
377 _using_partial_mode = false;
378}
379
380void GxEPD2_371::_Init_Part()
381{
382 _InitDisplay();
383 _writeCommand(0x00); //panel setting
384 _writeData(hasFastPartialUpdate ? 0x3f : 0x1f); // partial update LUT from registers
385 _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
386 _writeData(0x39); // LUTBD, N2OCP: copy new to old
387 _writeData(0x07);
388 _writeCommand(0x20);
389 _writeDataPGM(lut_20_LUTC_partial, sizeof(lut_20_LUTC_partial), 42 - sizeof(lut_20_LUTC_partial));
390 _writeCommand(0x21);
391 _writeDataPGM(lut_21_LUTWW_partial, sizeof(lut_21_LUTWW_partial), 42 - sizeof(lut_21_LUTWW_partial));
392 _writeCommand(0x22);
393 _writeDataPGM(lut_22_LUTKW_partial, sizeof(lut_22_LUTKW_partial), 42 - sizeof(lut_22_LUTKW_partial));
394 _writeCommand(0x23);
395 _writeDataPGM(lut_23_LUTWK_partial, sizeof(lut_23_LUTWK_partial), 42 - sizeof(lut_23_LUTWK_partial));
396 _writeCommand(0x24);
397 _writeDataPGM(lut_24_LUTKK_partial, sizeof(lut_24_LUTKK_partial), 42 - sizeof(lut_24_LUTKK_partial));
398 _writeCommand(0x25);
399 _writeDataPGM(lut_25_LUTBD_partial, sizeof(lut_25_LUTBD_partial), 42 - sizeof(lut_25_LUTBD_partial));
400 _PowerOn();
401 _using_partial_mode = true;
402}
403
404void GxEPD2_371::_Update_Full()
405{
406 _writeCommand(0x12); //display refresh
407 _waitWhileBusy("_Update_Full", full_refresh_time);
408}
409
410void GxEPD2_371::_Update_Part()
411{
412 _writeCommand(0x12); //display refresh
413 _waitWhileBusy("_Update_Part", partial_refresh_time);
414}
#define T2
#define T3
#define T4
#define T1
const unsigned char GxEPD2_371::lut_20_LUTC_partial[] PROGMEM
void refresh(bool partial_update_mode=false)
void clearScreen(uint8_t value=0xFF)
GxEPD2_371(int16_t cs, int16_t dc, int16_t rst, int16_t busy)
static const uint16_t full_refresh_time
Definition GxEPD2_371.h:32
void writeNative(const uint8_t *data1, const uint8_t *data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert=false, bool mirror_y=false, bool pgm=false)
void drawNative(const uint8_t *data1, const uint8_t *data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert=false, bool mirror_y=false, bool pgm=false)
void hibernate()
void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap, int16_t x, int16_t y, int16_t w, int16_t h, bool invert=false, bool mirror_y=false, bool pgm=false)
static const bool hasFastPartialUpdate
Definition GxEPD2_371.h:29
void drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert=false, bool mirror_y=false, bool pgm=false)
static const uint16_t power_off_time
Definition GxEPD2_371.h:31
static const uint16_t WIDTH
Definition GxEPD2_371.h:23
static const uint16_t power_on_time
Definition GxEPD2_371.h:30
void writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert=false, bool mirror_y=false, bool pgm=false)
static const uint16_t HEIGHT
Definition GxEPD2_371.h:24
void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap, int16_t x, int16_t y, int16_t w, int16_t h, bool invert=false, bool mirror_y=false, bool pgm=false)
static const bool usePartialUpdateWindow
Definition GxEPD2_371.h:28
void powerOff()
static const uint16_t partial_refresh_time
Definition GxEPD2_371.h:33
void writeScreenBuffer(uint8_t value=0xFF)
void _endTransfer()
void _writeCommand(uint8_t c)
void _writeData(uint8_t d)
bool _using_partial_mode
Definition GxEPD2_EPD.h:118
void _waitWhileBusy(const char *comment=0, uint16_t busy_time=5000)
bool _power_is_on
Definition GxEPD2_EPD.h:118
bool _initial_refresh
Definition GxEPD2_EPD.h:117
void _startTransfer()
void _transfer(uint8_t value)
void _reset()
bool _initial_write
Definition GxEPD2_EPD.h:117
int16_t _rst
Definition GxEPD2_EPD.h:112
bool _hibernating
Definition GxEPD2_EPD.h:118
void _writeDataPGM(const uint8_t *data, uint16_t n, int16_t fill_with_zeroes=0)