Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
GxEPD2_154_D67.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, available here: http://www.e-paper-display.com/download_detail/downloadsId=806.html
5// Panel: GDEH0154D67 : http://www.e-paper-display.com/products_detail/productId=455.html
6// Controller : SSD1681 : http://www.e-paper-display.com/download_detail/downloadsId=825.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_154_D67.h"
15
16GxEPD2_154_D67::GxEPD2_154_D67(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
17 GxEPD2_EPD(cs, dc, rst, busy, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
18{
19}
20
21void GxEPD2_154_D67::clearScreen(uint8_t value)
22{
23 writeScreenBuffer(value);
24 refresh(true);
26}
27
29{
30 if (!_using_partial_mode) _Init_Part();
31 if (_initial_write) _writeScreenBuffer(0x26, value); // set previous
32 _writeScreenBuffer(0x24, value); // set current
33 _initial_write = false; // initial full screen buffer clean done
34}
35
37{
38 if (!_using_partial_mode) _Init_Part();
39 _writeScreenBuffer(0x24, value); // set current
40}
41
42void GxEPD2_154_D67::_writeScreenBuffer(uint8_t command, uint8_t value)
43{
44 _writeCommand(command);
45 for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
46 {
47 _writeData(value);
48 }
49}
50
51void GxEPD2_154_D67::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 _writeImage(0x24, bitmap, x, y, w, h, invert, mirror_y, pgm);
54}
55
56void GxEPD2_154_D67::writeImageForFullRefresh(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
57{
58 _writeImage(0x26, bitmap, x, y, w, h, invert, mirror_y, pgm);
59 _writeImage(0x24, bitmap, x, y, w, h, invert, mirror_y, pgm);
60}
61
62void GxEPD2_154_D67::writeImageAgain(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
63{
64 _writeImage(0x24, bitmap, x, y, w, h, invert, mirror_y, pgm);
65}
66
67void GxEPD2_154_D67::_writeImage(uint8_t command, const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
68{
69 if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
70 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
71 int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
72 x -= x % 8; // byte boundary
73 w = wb * 8; // byte boundary
74 int16_t x1 = x < 0 ? 0 : x; // limit
75 int16_t y1 = y < 0 ? 0 : y; // limit
76 int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
77 int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
78 int16_t dx = x1 - x;
79 int16_t dy = y1 - y;
80 w1 -= dx;
81 h1 -= dy;
82 if ((w1 <= 0) || (h1 <= 0)) return;
83 if (!_using_partial_mode) _Init_Part();
84 _setPartialRamArea(x1, y1, w1, h1);
85 _writeCommand(command);
86 for (int16_t i = 0; i < h1; i++)
87 {
88 for (int16_t j = 0; j < w1 / 8; j++)
89 {
90 uint8_t data;
91 // use wb, h of bitmap for index!
92 int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
93 if (pgm)
94 {
95#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
96 data = pgm_read_byte(&bitmap[idx]);
97#else
98 data = bitmap[idx];
99#endif
100 }
101 else
102 {
103 data = bitmap[idx];
104 }
105 if (invert) data = ~data;
106 _writeData(data);
107 }
108 }
109 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
110}
111
112void GxEPD2_154_D67::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
113 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
114{
115 _writeImagePart(0x24, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
116}
117
118void GxEPD2_154_D67::writeImagePartAgain(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
119 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
120{
121 _writeImagePart(0x24, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
122}
123
124void GxEPD2_154_D67::_writeImagePart(uint8_t command, const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
125 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
126{
127 if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
128 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
129 if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
130 if ((x_part < 0) || (x_part >= w_bitmap)) return;
131 if ((y_part < 0) || (y_part >= h_bitmap)) return;
132 int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
133 x_part -= x_part % 8; // byte boundary
134 w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
135 h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
136 x -= x % 8; // byte boundary
137 w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
138 int16_t x1 = x < 0 ? 0 : x; // limit
139 int16_t y1 = y < 0 ? 0 : y; // limit
140 int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
141 int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
142 int16_t dx = x1 - x;
143 int16_t dy = y1 - y;
144 w1 -= dx;
145 h1 -= dy;
146 if ((w1 <= 0) || (h1 <= 0)) return;
147 if (!_using_partial_mode) _Init_Part();
148 _setPartialRamArea(x1, y1, w1, h1);
149 _writeCommand(command);
150 for (int16_t i = 0; i < h1; i++)
151 {
152 for (int16_t j = 0; j < w1 / 8; j++)
153 {
154 uint8_t data;
155 // use wb_bitmap, h_bitmap of bitmap for index!
156 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;
157 if (pgm)
158 {
159#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
160 data = pgm_read_byte(&bitmap[idx]);
161#else
162 data = bitmap[idx];
163#endif
164 }
165 else
166 {
167 data = bitmap[idx];
168 }
169 if (invert) data = ~data;
170 _writeData(data);
171 }
172 }
173 delay(1); // yield() to avoid WDT on ESP8266 and ESP32
174}
175
176void GxEPD2_154_D67::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)
177{
178 if (black)
179 {
180 writeImage(black, x, y, w, h, invert, mirror_y, pgm);
181 }
182}
183
184void GxEPD2_154_D67::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,
185 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
186{
187 if (black)
188 {
189 writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
190 }
191}
192
193void GxEPD2_154_D67::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)
194{
195 if (data1)
196 {
197 writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
198 }
199}
200
201void GxEPD2_154_D67::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
202{
203 writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
204 refresh(x, y, w, h);
205 writeImageAgain(bitmap, x, y, w, h, invert, mirror_y, pgm);
206}
207
208void GxEPD2_154_D67::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
209 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
210{
211 writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
212 refresh(x, y, w, h);
213 writeImagePartAgain(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
214}
215
216void GxEPD2_154_D67::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)
217{
218 if (black)
219 {
220 drawImage(black, x, y, w, h, invert, mirror_y, pgm);
221 }
222}
223
224void GxEPD2_154_D67::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,
225 int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
226{
227 if (black)
228 {
229 drawImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
230 }
231}
232
233void GxEPD2_154_D67::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)
234{
235 if (data1)
236 {
237 drawImage(data1, x, y, w, h, invert, mirror_y, pgm);
238 }
239}
240
241void GxEPD2_154_D67::refresh(bool partial_update_mode)
242{
243 if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
244 else
245 {
246 if (_using_partial_mode) _Init_Full();
247 _Update_Full();
248 _initial_refresh = false; // initial full update done
249 }
250}
251
252void GxEPD2_154_D67::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
253{
254 if (_initial_refresh) return refresh(false); // initial update needs be full update
255 // intersection with screen
256 int16_t w1 = x < 0 ? w + x : w; // reduce
257 int16_t h1 = y < 0 ? h + y : h; // reduce
258 int16_t x1 = x < 0 ? 0 : x; // limit
259 int16_t y1 = y < 0 ? 0 : y; // limit
260 w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
261 h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
262 if ((w1 <= 0) || (h1 <= 0)) return;
263 // make x1, w1 multiple of 8
264 w1 += x1 % 8;
265 if (w1 % 8 > 0) w1 += 8 - w1 % 8;
266 x1 -= x1 % 8;
267 if (!_using_partial_mode) _Init_Part();
268 _setPartialRamArea(x1, y1, w1, h1);
269 _Update_Part();
270}
271
273{
274 _PowerOff();
275}
276
278{
279 _PowerOff();
280 if (_rst >= 0)
281 {
282 _writeCommand(0x10); // deep sleep mode
283 _writeData(0x1); // enter deep sleep
284 _hibernating = true;
285 }
286}
287
288void GxEPD2_154_D67::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
289{
290 _writeCommand(0x11); // set ram entry mode
291 _writeData(0x03); // x increase, y increase : normal mode
292 _writeCommand(0x44);
293 _writeData(x / 8);
294 _writeData((x + w - 1) / 8);
295 _writeCommand(0x45);
296 _writeData(y % 256);
297 _writeData(y / 256);
298 _writeData((y + h - 1) % 256);
299 _writeData((y + h - 1) / 256);
300 _writeCommand(0x4e);
301 _writeData(x / 8);
302 _writeCommand(0x4f);
303 _writeData(y % 256);
304 _writeData(y / 256);
305}
306
307void GxEPD2_154_D67::_PowerOn()
308{
309 if (!_power_is_on)
310 {
311 _writeCommand(0x22);
312 _writeData(0xf8);
313 _writeCommand(0x20);
314 _waitWhileBusy("_PowerOn", power_on_time);
315 }
316 _power_is_on = true;
317}
318
319void GxEPD2_154_D67::_PowerOff()
320{
321 if (_power_is_on)
322 {
323 _writeCommand(0x22);
324 _writeData(0x83);
325 _writeCommand(0x20);
326 _waitWhileBusy("_PowerOff", power_off_time);
327 }
328 _power_is_on = false;
329 _using_partial_mode = false;
330}
331
332void GxEPD2_154_D67::_InitDisplay()
333{
334 if (_hibernating) _reset();
335 delay(10); // 10ms according to specs
336 _writeCommand(0x12); // soft reset
337 delay(10); // 10ms according to specs
338 _writeCommand(0x01); // Driver output control
339 _writeData(0xC7);
340 _writeData(0x00);
341 _writeData(0x00);
342 _writeCommand(0x3C); // BorderWavefrom
343 _writeData(0x05);
344 _writeCommand(0x18); // Read built-in temperature sensor
345 _writeData(0x80);
346 _setPartialRamArea(0, 0, WIDTH, HEIGHT);
347}
348
349void GxEPD2_154_D67::_Init_Full()
350{
351 _InitDisplay();
352 _PowerOn();
353 _using_partial_mode = false;
354}
355
356void GxEPD2_154_D67::_Init_Part()
357{
358 _InitDisplay();
359 _PowerOn();
360 _using_partial_mode = true;
361}
362
363void GxEPD2_154_D67::_Update_Full()
364{
365 _writeCommand(0x22);
366 _writeData(0xf4);
367 _writeCommand(0x20);
368 _waitWhileBusy("_Update_Full", full_refresh_time);
369}
370
371void GxEPD2_154_D67::_Update_Part()
372{
373 _writeCommand(0x22);
374 _writeData(0xfc);
375 _writeCommand(0x20);
376 _waitWhileBusy("_Update_Part", partial_refresh_time);
377}
static const uint16_t WIDTH
static const uint16_t full_refresh_time
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 writeImagePartAgain(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)
void writeScreenBuffer(uint8_t value=0xFF)
static const uint16_t partial_refresh_time
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 writeScreenBufferAgain(uint8_t value=0xFF)
void clearScreen(uint8_t value=0xFF)
void writeImageForFullRefresh(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)
GxEPD2_154_D67(int16_t cs, int16_t dc, int16_t rst, int16_t busy)
void refresh(bool partial_update_mode=false)
static const uint16_t power_off_time
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 HEIGHT
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 power_on_time
void writeImageAgain(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 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)
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 _reset()
bool _initial_write
Definition GxEPD2_EPD.h:117
int16_t _rst
Definition GxEPD2_EPD.h:112
bool _hibernating
Definition GxEPD2_EPD.h:118