GxEPD2_BW.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. #ifndef _GxEPD2_BW_H_
  12. #define _GxEPD2_BW_H_
  13. #include <Adafruit_GFX.h>
  14. #include "GxEPD2_EPD.h"
  15. #include "epd/GxEPD2_154.h"
  16. #include "epd/GxEPD2_213.h"
  17. #include "epd/GxEPD2_213_flex.h"
  18. #include "epd/GxEPD2_290.h"
  19. #include "epd/GxEPD2_270.h"
  20. #include "epd/GxEPD2_420.h"
  21. #include "epd/GxEPD2_583.h"
  22. #include "epd/GxEPD2_750.h"
  23. #ifndef ENABLE_GxEPD2_GFX
  24. // default is off
  25. #define ENABLE_GxEPD2_GFX 0
  26. #endif
  27. #if ENABLE_GxEPD2_GFX
  28. #include "GxEPD2_GFX.h"
  29. #endif
  30. template<typename GxEPD2_Type, const uint16_t page_height>
  31. #if ENABLE_GxEPD2_GFX
  32. class GxEPD2_BW : public GxEPD2_GFX
  33. #else
  34. class GxEPD2_BW : public Adafruit_GFX
  35. #endif
  36. {
  37. public:
  38. GxEPD2_Type epd2;
  39. #if ENABLE_GxEPD2_GFX
  40. GxEPD2_BW(GxEPD2_Type epd2_instance) : epd2(epd2_instance), GxEPD2_GFX(epd2, GxEPD2_Type::WIDTH, GxEPD2_Type::HEIGHT)
  41. #else
  42. GxEPD2_BW(GxEPD2_Type epd2_instance) : Adafruit_GFX(GxEPD2_Type::WIDTH, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  43. #endif
  44. {
  45. _page_height = page_height;
  46. _pages = (HEIGHT / _page_height) + ((HEIGHT % _page_height) > 0);
  47. _reverse = (epd2_instance.panel == GxEPD2::GDE0213B1);
  48. _using_partial_mode = false;
  49. _current_page = 0;
  50. setFullWindow();
  51. }
  52. uint16_t pages()
  53. {
  54. return _pages;
  55. }
  56. uint16_t pageHeight()
  57. {
  58. return _page_height;
  59. }
  60. bool mirror(bool m)
  61. {
  62. swap (_mirror, m);
  63. return m;
  64. }
  65. void drawPixel(int16_t x, int16_t y, uint16_t color)
  66. {
  67. if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
  68. if (_mirror) x = width() - x - 1;
  69. // check rotation, move pixel around if necessary
  70. switch (getRotation())
  71. {
  72. case 1:
  73. swap(x, y);
  74. x = WIDTH - x - 1;
  75. break;
  76. case 2:
  77. x = WIDTH - x - 1;
  78. y = HEIGHT - y - 1;
  79. break;
  80. case 3:
  81. swap(x, y);
  82. y = HEIGHT - y - 1;
  83. break;
  84. }
  85. // transpose partial window to 0,0
  86. x -= _pw_x;
  87. y -= _pw_y;
  88. // adjust for current page
  89. y -= _current_page * _page_height;
  90. if (_reverse) y = _page_height - y - 1;
  91. // check if in current page
  92. if ((y < 0) || (y >= _page_height)) return;
  93. uint16_t i = x / 8 + y * (_pw_w / 8);
  94. if (color)
  95. _buffer[i] = (_buffer[i] | (1 << (7 - x % 8)));
  96. else
  97. _buffer[i] = (_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
  98. }
  99. void init(uint32_t serial_diag_bitrate = 0) // = 0 : disabled
  100. {
  101. epd2.init(serial_diag_bitrate);
  102. _using_partial_mode = false;
  103. _current_page = 0;
  104. setFullWindow();
  105. }
  106. void fillScreen(uint16_t color) // 0x0 black, >0x0 white, to buffer
  107. {
  108. uint8_t data = (color == GxEPD_BLACK) ? 0x00 : 0xFF;
  109. for (uint16_t x = 0; x < sizeof(_buffer); x++)
  110. {
  111. _buffer[x] = data;
  112. }
  113. }
  114. // display buffer content to screen, useful for full screen buffer
  115. void display(bool partial_update_mode = false)
  116. {
  117. epd2.writeImage(_buffer, 0, 0, WIDTH, HEIGHT);
  118. epd2.refresh(partial_update_mode);
  119. }
  120. void setFullWindow()
  121. {
  122. _using_partial_mode = false;
  123. _pw_x = 0;
  124. _pw_y = 0;
  125. _pw_w = WIDTH;
  126. _pw_h = HEIGHT;
  127. }
  128. void setPartialWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  129. {
  130. _rotate(x, y, w, h);
  131. _using_partial_mode = true;
  132. _pw_x = gx_uint16_min(x, WIDTH);
  133. _pw_y = gx_uint16_min(y, HEIGHT);
  134. _pw_w = gx_uint16_min(w, WIDTH - _pw_x);
  135. _pw_h = gx_uint16_min(h, HEIGHT - _pw_y);
  136. // make _pw_x, _pw_w multiple of 8
  137. _pw_w += _pw_x % 8;
  138. if (_pw_w % 8 > 0) _pw_w += 8 - _pw_w % 8;
  139. _pw_x -= _pw_x % 8;
  140. }
  141. void firstPage()
  142. {
  143. fillScreen(GxEPD_WHITE);
  144. _current_page = 0;
  145. _second_phase = false;
  146. }
  147. bool nextPage()
  148. {
  149. if (1 == _pages)
  150. {
  151. if (_using_partial_mode)
  152. {
  153. uint32_t offset = _reverse ? (HEIGHT - _pw_h) * _pw_w / 8 : 0;
  154. epd2.writeImage(_buffer + offset, _pw_x, _pw_y, _pw_w, _pw_h);
  155. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  156. if (epd2.hasFastPartialUpdate)
  157. {
  158. epd2.writeImage(_buffer + offset, _pw_x, _pw_y, _pw_w, _pw_h);
  159. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  160. }
  161. }
  162. else
  163. {
  164. epd2.writeImage(_buffer, 0, 0, WIDTH, HEIGHT);
  165. epd2.refresh(false);
  166. if (epd2.hasFastPartialUpdate)
  167. {
  168. epd2.writeImage(_buffer, 0, 0, WIDTH, HEIGHT);
  169. epd2.refresh(true);
  170. }
  171. }
  172. return false;
  173. }
  174. uint16_t page_ys = _current_page * _page_height;
  175. if (_using_partial_mode)
  176. {
  177. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  178. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  179. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  180. uint16_t dest_ys = _pw_y + page_ys; // transposed
  181. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  182. if (dest_ye > dest_ys)
  183. {
  184. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  185. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  186. uint32_t offset = _reverse ? (_page_height - (dest_ye - dest_ys)) * _pw_w / 8 : 0;
  187. epd2.writeImage(_buffer + offset, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  188. }
  189. else
  190. {
  191. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  192. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  193. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  194. }
  195. _current_page++;
  196. if (_current_page == _pages)
  197. {
  198. _current_page = 0;
  199. if (!_second_phase)
  200. {
  201. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  202. if (epd2.hasFastPartialUpdate)
  203. {
  204. _second_phase = true;
  205. fillScreen(GxEPD_WHITE);
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. fillScreen(GxEPD_WHITE);
  212. return true;
  213. }
  214. else
  215. {
  216. epd2.writeImage(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  217. _current_page++;
  218. if (_current_page == _pages)
  219. {
  220. _current_page = 0;
  221. if (epd2.hasFastPartialUpdate)
  222. {
  223. if (!_second_phase)
  224. {
  225. epd2.refresh(false);
  226. _second_phase = true;
  227. fillScreen(GxEPD_WHITE);
  228. return true;
  229. }
  230. else epd2.refresh(true);
  231. } else epd2.refresh(false);
  232. epd2.powerOff();
  233. return false;
  234. }
  235. fillScreen(GxEPD_WHITE);
  236. return true;
  237. }
  238. }
  239. // GxEPD style paged drawing; drawCallback() is called as many times as needed
  240. void drawPaged(void (*drawCallback)(const void*), const void* pv)
  241. {
  242. if (_using_partial_mode)
  243. {
  244. for (uint16_t phase = 1; phase <= 2; phase++)
  245. {
  246. for (_current_page = 0; _current_page < _pages; _current_page++)
  247. {
  248. uint16_t page_ys = _current_page * _page_height;
  249. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  250. uint16_t dest_ys = _pw_y + page_ys; // transposed
  251. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  252. if (dest_ye > dest_ys)
  253. {
  254. fillScreen(GxEPD_WHITE);
  255. drawCallback(pv);
  256. uint32_t offset = _reverse ? (_page_height - (dest_ye - dest_ys)) * _pw_w / 8 : 0;
  257. epd2.writeImage(_buffer + offset, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  258. }
  259. }
  260. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  261. if (!epd2.hasFastPartialUpdate) break;
  262. // else make both controller buffers have equal content
  263. }
  264. }
  265. else
  266. {
  267. for (_current_page = 0; _current_page < _pages; _current_page++)
  268. {
  269. uint16_t page_ys = _current_page * _page_height;
  270. fillScreen(GxEPD_WHITE);
  271. drawCallback(pv);
  272. epd2.writeImage(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  273. }
  274. epd2.refresh(false);
  275. if (epd2.hasFastPartialUpdate)
  276. {
  277. // make both controller buffers have equal content
  278. for (_current_page = 0; _current_page < _pages; _current_page++)
  279. {
  280. uint16_t page_ys = _current_page * _page_height;
  281. fillScreen(GxEPD_WHITE);
  282. drawCallback(pv);
  283. epd2.writeImage(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  284. }
  285. epd2.refresh(true);
  286. }
  287. }
  288. _current_page = 0;
  289. }
  290. void drawInvertedBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
  291. {
  292. // taken from Adafruit_GFX.cpp, modified
  293. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  294. uint8_t byte = 0;
  295. for (int16_t j = 0; j < h; j++)
  296. {
  297. for (int16_t i = 0; i < w; i++ )
  298. {
  299. if (i & 7) byte <<= 1;
  300. else
  301. {
  302. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  303. byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  304. #else
  305. byte = bitmap[j * byteWidth + i / 8];
  306. #endif
  307. }
  308. if (!(byte & 0x80))
  309. {
  310. drawPixel(x + i, y + j, color);
  311. }
  312. }
  313. }
  314. }
  315. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  316. void clearScreen(uint8_t value = 0xFF) // init controller memory and screen (default white)
  317. {
  318. epd2.clearScreen(value);
  319. }
  320. void writeScreenBuffer(uint8_t value = 0xFF) // init controller memory (default white)
  321. {
  322. epd2.writeScreenBuffer(value);
  323. }
  324. // write to controller memory, without screen refresh; x and w should be multiple of 8
  325. 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)
  326. {
  327. epd2.writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  328. }
  329. void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  330. {
  331. epd2.writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  332. }
  333. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  334. void 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)
  335. {
  336. epd2.writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  337. }
  338. // write to controller memory, with screen refresh; x and w should be multiple of 8
  339. 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)
  340. {
  341. epd2.drawImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  342. }
  343. void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  344. {
  345. epd2.drawImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  346. }
  347. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  348. void 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)
  349. {
  350. epd2.drawNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  351. }
  352. void refresh(bool partial_update_mode = false) // screen refresh from controller memory to full screen
  353. {
  354. epd2.refresh(partial_update_mode);
  355. }
  356. void refresh(int16_t x, int16_t y, int16_t w, int16_t h) // screen refresh from controller memory, partial screen
  357. {
  358. epd2.refresh(x, y, w, h);
  359. }
  360. void powerOff()
  361. {
  362. epd2.powerOff();
  363. }
  364. private:
  365. template <typename T> static inline void
  366. swap(T & a, T & b)
  367. {
  368. T t = a;
  369. a = b;
  370. b = t;
  371. };
  372. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  373. {
  374. return (a < b ? a : b);
  375. };
  376. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  377. {
  378. return (a > b ? a : b);
  379. };
  380. void _rotate(uint16_t& x, uint16_t& y, uint16_t& w, uint16_t& h)
  381. {
  382. switch (getRotation())
  383. {
  384. case 1:
  385. swap(x, y);
  386. swap(w, h);
  387. x = WIDTH - x - w;
  388. break;
  389. case 2:
  390. x = WIDTH - x - w;
  391. y = HEIGHT - y - h;
  392. break;
  393. case 3:
  394. swap(x, y);
  395. swap(w, h);
  396. y = HEIGHT - y - h;
  397. break;
  398. }
  399. }
  400. private:
  401. uint8_t _buffer[(GxEPD2_Type::WIDTH / 8) * page_height];
  402. bool _using_partial_mode, _second_phase, _mirror, _reverse;
  403. uint16_t _width_bytes, _pixel_bytes;
  404. int16_t _current_page;
  405. uint16_t _pages, _page_height;
  406. uint16_t _pw_x, _pw_y, _pw_w, _pw_h;
  407. };
  408. #endif