GxEPD2_3C.h 13 KB

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