Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
GxEPD2_213c.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: these e-papers require 3.3V supply AND data lines!
3//
4// based on Demo Example from Good Display: http://www.e-paper-display.com/download_list/downloadcategoryid=34&isMode=false.html
5// Controller: IL0373 : http://www.e-paper-display.com/download_detail/downloadsId=535.html
6//
7// Author: Jean-Marc Zingg
8//
9// Version: see library.properties
10//
11// Library: https://github.com/ZinggJM/GxEPD2
12
13#include "GxEPD2_213c.h"
14
15GxEPD2_213c::GxEPD2_213c(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
16 GxEPD2_EPD(cs, dc, rst, busy, LOW, 20000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
17{
18}
19
20void GxEPD2_213c::clearScreen(uint8_t value)
21{
22 clearScreen(value, 0xFF);
23}
24
25void GxEPD2_213c::clearScreen(uint8_t black_value, uint8_t color_value)
26{
27 _initial_write = false; // initial full screen buffer clean done
28 _Init_Part();
29 _writeCommand(0x91); // partial in
30 _setPartialRamArea(0, 0, WIDTH, HEIGHT);
31 _writeCommand(0x10);
32 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
33 {
34 _writeData(black_value);
35 }
36 _writeCommand(0x13);
37 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
38 {
39 _writeData(color_value);
40 }
41 _Update_Part();
42 _writeCommand(0x92); // partial out
43}
44
46{
47 writeScreenBuffer(value, 0xFF);
48}
49
50void GxEPD2_213c::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
51{
52 _initial_write = false; // initial full screen buffer clean done
53 _Init_Part();
54 _writeCommand(0x91); // partial in
55 _setPartialRamArea(0, 0, WIDTH, HEIGHT);
56 _writeCommand(0x10);
57 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
58 {
59 _writeData(black_value);
60 }
61 _writeCommand(0x13);
62 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
63 {
64 _writeData(color_value);
65 }
66 _writeCommand(0x92); // partial out
67}
68
69void GxEPD2_213c::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
70{
71 writeImage(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
72}
73
74void GxEPD2_213c::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)
75{
76 if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
77 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
78 int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
79 x -= x % 8; // byte boundary
80 w = wb * 8; // byte boundary
81 int16_t x1 = x < 0 ? 0 : x; // limit
82 int16_t y1 = y < 0 ? 0 : y; // limit
83 int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
84 int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
85 int16_t dx = x1 - x;
86 int16_t dy = y1 - y;
87 w1 -= dx;
88 h1 -= dy;
89 if ((w1 <= 0) || (h1 <= 0)) return;
90 _Init_Part();
91 _writeCommand(0x91); // partial in
92 _setPartialRamArea(x1, y1, w1, h1);
93 _writeCommand(0x10);
94 for (int16_t i = 0; i < h1; i++)
95 {
96 for (int16_t j = 0; j < w1 / 8; j++)
97 {
98 uint8_t data = 0xFF;
99 if (black)
100 {
101 // use wb, h of bitmap for index!
102 int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
103 if (pgm)
104 {
105#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
106 data = pgm_read_byte(&black[idx]);
107#else
108 data = black[idx];
109#endif
110 }
111 else
112 {
113 data = black[idx];
114 }
115 if (invert) data = ~data;
116 }
117 _writeData(data);
118 }
119 }
120 _writeCommand(0x13);
121 for (int16_t i = 0; i < h1; i++)
122 {
123 for (int16_t j = 0; j < w1 / 8; j++)
124 {
125 uint8_t data = 0xFF;
126 if (color)
127 {
128 // use wb, h of bitmap for index!
129 int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
130 if (pgm)
131 {
132#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
133 data = pgm_read_byte(&color[idx]);
134#else
135 data = color[idx];
136#endif
137 }
138 else
139 {
140 data = color[idx];
141 }
142 if (invert) data = ~data;
143 }
144 _writeData(data);
145 }
146 }
147 _writeCommand(0x92); // partial out
148 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
149}
150
151void GxEPD2_213c::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
152 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
153{
154 writeImagePart(bitmap, NULL, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
155}
156
157void GxEPD2_213c::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,
158 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
159{
160 if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
161 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
162 if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
163 if ((x_part < 0) || (x_part >= w_bitmap)) return;
164 if ((y_part < 0) || (y_part >= h_bitmap)) return;
165 int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
166 x_part -= x_part % 8; // byte boundary
167 w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
168 h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
169 x -= x % 8; // byte boundary
170 w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
171 int16_t x1 = x < 0 ? 0 : x; // limit
172 int16_t y1 = y < 0 ? 0 : y; // limit
173 int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
174 int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
175 int16_t dx = x1 - x;
176 int16_t dy = y1 - y;
177 w1 -= dx;
178 h1 -= dy;
179 if ((w1 <= 0) || (h1 <= 0)) return;
180 if (!_using_partial_mode) _Init_Part();
181 _writeCommand(0x91); // partial in
182 _setPartialRamArea(x1, y1, w1, h1);
183 _writeCommand(0x10);
184 for (int16_t i = 0; i < h1; i++)
185 {
186 for (int16_t j = 0; j < w1 / 8; j++)
187 {
188 uint8_t data;
189 // use wb_bitmap, h_bitmap of bitmap for index!
190 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;
191 if (pgm)
192 {
193#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
194 data = pgm_read_byte(&black[idx]);
195#else
196 data = black[idx];
197#endif
198 }
199 else
200 {
201 data = black[idx];
202 }
203 if (invert) data = ~data;
204 _writeData(data);
205 }
206 }
207 _writeCommand(0x13);
208 for (int16_t i = 0; i < h1; i++)
209 {
210 for (int16_t j = 0; j < w1 / 8; j++)
211 {
212 uint8_t data = 0xFF;
213 if (color)
214 {
215 // use wb_bitmap, h_bitmap of bitmap for index!
216 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;
217 if (pgm)
218 {
219#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
220 data = pgm_read_byte(&color[idx]);
221#else
222 data = color[idx];
223#endif
224 }
225 else
226 {
227 data = color[idx];
228 }
229 if (invert) data = ~data;
230 }
231 _writeData(data);
232 }
233 }
234 _writeCommand(0x92); // partial out
235 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
236}
237
238void GxEPD2_213c::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)
239{
240 if (data1)
241 {
242 writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
243 }
244}
245
246void GxEPD2_213c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
247{
248 writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
249 refresh(x, y, w, h);
250}
251
252void GxEPD2_213c::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
253 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
254{
255 writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
256 refresh(x, y, w, h);
257}
258
259void GxEPD2_213c::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)
260{
261 writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
262 refresh(x, y, w, h);
263}
264
265void GxEPD2_213c::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,
266 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
267{
268 writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
269 refresh(x, y, w, h);
270}
271
272void GxEPD2_213c::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)
273{
274 writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
275 refresh(x, y, w, h);
276}
277
278void GxEPD2_213c::refresh(bool partial_update_mode)
279{
280 if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
281 else _Update_Full();
282}
283
284void GxEPD2_213c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
285{
286 // intersection with screen
287 int16_t w1 = x < 0 ? w + x : w; // reduce
288 int16_t h1 = y < 0 ? h + y : h; // reduce
289 int16_t x1 = x < 0 ? 0 : x; // limit
290 int16_t y1 = y < 0 ? 0 : y; // limit
291 w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
292 h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
293 if ((w1 <= 0) || (h1 <= 0)) return;
294 // make x1, w1 multiple of 8
295 w1 += x1 % 8;
296 if (w1 % 8 > 0) w1 += 8 - w1 % 8;
297 x1 -= x1 % 8;
298 _Init_Part();
299 if (usePartialUpdateWindow) _writeCommand(0x91); // partial in
300 _setPartialRamArea(x1, y1, w1, h1);
301 _Update_Part();
302 if (usePartialUpdateWindow) _writeCommand(0x92); // partial out
303}
304
306{
307 _PowerOff();
308}
309
311{
312 _PowerOff();
313 if (_rst >= 0)
314 {
315 _writeCommand(0x07); // deep sleep
316 _writeData(0xA5); // check code
317 _hibernating = true;
318 }
319}
320
321void GxEPD2_213c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
322{
323 uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
324 uint16_t ye = y + h - 1;
325 x &= 0xFFF8; // byte boundary
326 _writeCommand(0x90); // partial window
327 //_writeData(x / 256);
328 _writeData(x % 256);
329 //_writeData(xe / 256);
330 _writeData(xe % 256);
331 _writeData(y / 256);
332 _writeData(y % 256);
333 _writeData(ye / 256);
334 _writeData(ye % 256);
335 _writeData(0x01); // don't see any difference
336 //_writeData(0x00); // don't see any difference
337}
338
339void GxEPD2_213c::_PowerOn()
340{
341 if (!_power_is_on)
342 {
343 _writeCommand(0x04);
344 _waitWhileBusy("_PowerOn", power_on_time);
345 }
346 _power_is_on = true;
347}
348
349void GxEPD2_213c::_PowerOff()
350{
351 _writeCommand(0x02); // power off
352 _waitWhileBusy("_PowerOff", power_off_time);
353 _power_is_on = false;
354}
355
356void GxEPD2_213c::_InitDisplay()
357{
358 if (_hibernating) _reset();
359 _writeCommand(0x06);
360 _writeData (0x17);
361 _writeData (0x17);
362 _writeData (0x17);
363 //_writeCommand(0x04);
364 //_waitWhileBusy("_wakeUp Power On");
365 _writeCommand(0X00);
366 _writeData(0x8f);
367 _writeCommand(0x50); //VCOM AND DATA INTERVAL SETTING
368 _writeData(0x77); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
369 _writeCommand(0x61); //resolution setting
370 _writeData (0x68); //source 104
371 _writeData (0x00);
372 _writeData (0xd4); //gate 212
373}
374
375void GxEPD2_213c::_Init_Full()
376{
377 _InitDisplay();
378 _PowerOn();
379}
380
381void GxEPD2_213c::_Init_Part()
382{
383 _InitDisplay();
384 _writeCommand(0x50); //VCOM AND DATA INTERVAL SETTING
385 _writeData(0x77); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
386 //_writeData(0xF7); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
387 _PowerOn();
388}
389
390void GxEPD2_213c::_Update_Full()
391{
392 _writeCommand(0x12); //display refresh
393 _waitWhileBusy("_Update_Full", full_refresh_time);
394}
395
396void GxEPD2_213c::_Update_Part()
397{
398 _writeCommand(0x12); //display refresh
399 _waitWhileBusy("_Update_Part", partial_refresh_time);
400}
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)
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)
static const uint16_t WIDTH
Definition GxEPD2_213c.h:22
static const uint16_t full_refresh_time
Definition GxEPD2_213c.h:32
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)
void refresh(bool partial_update_mode=false)
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 uint16_t HEIGHT
Definition GxEPD2_213c.h:24
void writeScreenBuffer(uint8_t value=0xFF)
GxEPD2_213c(int16_t cs, int16_t dc, int16_t rst, int16_t busy)
void clearScreen(uint8_t value=0xFF)
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 uint16_t power_on_time
Definition GxEPD2_213c.h:30
static const bool usePartialUpdateWindow
Definition GxEPD2_213c.h:28
static const uint16_t partial_refresh_time
Definition GxEPD2_213c.h:33
static const uint16_t power_off_time
Definition GxEPD2_213c.h:31
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 _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
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