GxEPD2_290c_GDEY029F51H.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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: https://www.good-display.com/comp/xcompanyFile/downloadNew.do?appId=24&fid=1785&id=1151
  5. // Panel: GDEY029F51H : https://www.good-display.com/product/464.html
  6. // Controller: JD79667, no specs, see GDEY0266F51H panel specs for command list subset
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_290c_GDEY029F51H.h"
  14. GxEPD2_290c_GDEY029F51H::GxEPD2_290c_GDEY029F51H(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
  15. GxEPD2_EPD(cs, dc, rst, busy, LOW, 50000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  16. {
  17. _paged = false;
  18. }
  19. void GxEPD2_290c_GDEY029F51H::clearScreen(uint8_t value)
  20. {
  21. clearScreen(value, 0xFF);
  22. }
  23. void GxEPD2_290c_GDEY029F51H::clearScreen(uint8_t black_value, uint8_t color_value)
  24. {
  25. writeScreenBuffer(black_value, color_value);
  26. refresh();
  27. }
  28. void GxEPD2_290c_GDEY029F51H::writeScreenBuffer(uint8_t value)
  29. {
  30. writeScreenBuffer(value, 0xFF);
  31. }
  32. void GxEPD2_290c_GDEY029F51H::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  33. {
  34. //Serial.println("writeScreenBuffer");
  35. if (!_init_display_done) _InitDisplay();
  36. _writeCommand(0x10);
  37. _startTransfer();
  38. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 4; i++)
  39. {
  40. _transfer(0xFF == black_value ? 0x55 : 0x00);
  41. }
  42. _endTransfer();
  43. _initial_write = false; // initial full screen buffer clean done
  44. }
  45. void GxEPD2_290c_GDEY029F51H::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  46. {
  47. //Serial.print("writeImage("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  48. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  49. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  50. if (!_init_display_done) _InitDisplay();
  51. if (_initial_write) writeScreenBuffer();
  52. if (hasPartialUpdate)
  53. {
  54. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  55. x -= x % 8; // byte boundary
  56. w = wb * 8; // byte boundary
  57. int16_t x1 = x < 0 ? 0 : x; // limit
  58. int16_t y1 = y < 0 ? 0 : y; // limit
  59. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  60. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  61. int16_t dx = x1 - x;
  62. int16_t dy = y1 - y;
  63. w1 -= dx;
  64. h1 -= dy;
  65. if ((w1 <= 0) || (h1 <= 0)) return;
  66. _setPartialRamArea(x1, y1, w1, h1);
  67. _writeCommand(0x10);
  68. _startTransfer();
  69. for (int16_t i = 0; i < h1; i++)
  70. {
  71. for (int16_t j = 0; j < w1 / 8; j++)
  72. {
  73. uint8_t data = 0xFF;
  74. // use wb, h of bitmap for index!
  75. uint32_t idx = mirror_y ? j + dx / 8 + uint32_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint32_t(i + dy) * wb;
  76. if (pgm)
  77. {
  78. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  79. data = pgm_read_byte(&bitmap[idx]);
  80. #else
  81. data = bitmap[idx];
  82. #endif
  83. }
  84. else
  85. {
  86. data = bitmap[idx];
  87. }
  88. if (invert) data = ~data;
  89. for (int16_t k = 0; k < 2; k++)
  90. {
  91. uint8_t data2 = (data & 0x80 ? 0x40 : 0x00) | (data & 0x40 ? 0x10 : 0x00) |
  92. (data & 0x20 ? 0x04 : 0x00) | (data & 0x10 ? 0x01 : 0x00);
  93. data <<= 4;
  94. _transfer(data2);
  95. }
  96. }
  97. }
  98. _endTransfer();
  99. }
  100. else
  101. {
  102. if (_paged && (x == 0) && (w == int16_t(WIDTH)) && (h < int16_t(HEIGHT)))
  103. {
  104. //Serial.println("paged");
  105. _startTransfer();
  106. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(h) / 8; i++)
  107. {
  108. uint8_t data = bitmap[i];
  109. for (int16_t k = 0; k < 2; k++)
  110. {
  111. uint8_t data2 = (data & 0x80 ? 0x40 : 0x00) | (data & 0x40 ? 0x10 : 0x00) |
  112. (data & 0x20 ? 0x04 : 0x00) | (data & 0x10 ? 0x01 : 0x00);
  113. data <<= 4;
  114. _transfer(data2);
  115. }
  116. }
  117. _endTransfer();
  118. if (y + h == HEIGHT) // last page
  119. {
  120. //Serial.println("paged ended");
  121. _paged = false;
  122. }
  123. }
  124. else
  125. {
  126. _paged = false;
  127. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  128. x -= x % 8; // byte boundary
  129. w = wb * 8; // byte boundary
  130. if ((w <= 0) || (h <= 0)) return;
  131. _writeCommand(0x10);
  132. _startTransfer();
  133. for (int16_t i = 0; i < int16_t(HEIGHT); i++)
  134. {
  135. for (int16_t j = 0; j < int16_t(WIDTH); j += 8)
  136. {
  137. uint8_t data = 0xFF;
  138. if ((j >= x) && (j <= x + w) && (i >= y) && (i < y + h))
  139. {
  140. uint32_t idx = mirror_y ? (j - x) / 8 + uint32_t((h - 1 - (i - y))) * wb : (j - x) / 8 + uint32_t(i - y) * wb;
  141. if (pgm)
  142. {
  143. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  144. data = pgm_read_byte(&bitmap[idx]);
  145. #else
  146. data = bitmap[idx];
  147. #endif
  148. }
  149. else
  150. {
  151. data = bitmap[idx];
  152. }
  153. if (invert) data = ~data;
  154. }
  155. for (int16_t k = 0; k < 2; k++)
  156. {
  157. uint8_t data2 = (data & 0x80 ? 0x40 : 0x00) | (data & 0x40 ? 0x10 : 0x00) |
  158. (data & 0x20 ? 0x04 : 0x00) | (data & 0x10 ? 0x01 : 0x00);
  159. data <<= 4;
  160. _transfer(data2);
  161. }
  162. }
  163. }
  164. _endTransfer();
  165. }
  166. }
  167. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  168. }
  169. void GxEPD2_290c_GDEY029F51H::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)
  170. {
  171. if (!black && !color) return;
  172. if (!color) return writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  173. //Serial.print("writeImage("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  174. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  175. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  176. if (!_init_display_done) _InitDisplay();
  177. if (_initial_write) writeScreenBuffer();
  178. if (hasPartialUpdate)
  179. {
  180. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  181. x -= x % 8; // byte boundary
  182. w = wb * 8; // byte boundary
  183. int16_t x1 = x < 0 ? 0 : x; // limit
  184. int16_t y1 = y < 0 ? 0 : y; // limit
  185. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  186. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  187. int16_t dx = x1 - x;
  188. int16_t dy = y1 - y;
  189. w1 -= dx;
  190. h1 -= dy;
  191. if ((w1 <= 0) || (h1 <= 0)) return;
  192. _setPartialRamArea(x1, y1, w1, h1);
  193. _writeCommand(0x10);
  194. _startTransfer();
  195. for (int16_t i = 0; i < h1; i++)
  196. {
  197. for (int16_t j = 0; j < w1 / 8; j++)
  198. {
  199. uint8_t black_data = 0xFF, color_data = 0xFF;
  200. // use wb, h of bitmap for index!
  201. uint32_t idx = mirror_y ? j + dx / 8 + uint32_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint32_t(i + dy) * wb;
  202. if (pgm)
  203. {
  204. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  205. black_data = pgm_read_byte(&black[idx]);
  206. color_data = pgm_read_byte(&color[idx]);
  207. #else
  208. black_data = black[idx];
  209. color_data = color[idx];
  210. #endif
  211. }
  212. else
  213. {
  214. black_data = black[idx];
  215. color_data = color[idx];
  216. }
  217. if (invert)
  218. {
  219. black_data = ~black_data;
  220. color_data = ~color_data;
  221. }
  222. for (int16_t k = 0; k < 2; k++)
  223. {
  224. uint8_t out_data = 0x00;
  225. for (int16_t l = 0; l < 4; l++)
  226. {
  227. out_data <<= 2;
  228. if (!(color_data & 0x80)) out_data |= 0x03; // red
  229. else out_data |= black_data & 0x80 ? 0x01 : 0x00; // white or black
  230. black_data <<= 1;
  231. color_data <<= 1;
  232. }
  233. _transfer(out_data);
  234. }
  235. }
  236. }
  237. _endTransfer();
  238. }
  239. else
  240. {
  241. if (_paged && (x == 0) && (w == int16_t(WIDTH)) && (h < int16_t(HEIGHT)))
  242. {
  243. //Serial.println("paged");
  244. _startTransfer();
  245. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(h) / 8; i++)
  246. {
  247. uint8_t black_data = black[i];
  248. uint8_t color_data = color[i];
  249. for (int16_t k = 0; k < 2; k++)
  250. {
  251. uint8_t out_data = 0x00;
  252. for (int16_t l = 0; l < 4; l++)
  253. {
  254. out_data <<= 2;
  255. if (!(color_data & 0x80)) out_data |= 0x03; // red
  256. else out_data |= black_data & 0x80 ? 0x01 : 0x00; // white or black
  257. black_data <<= 1;
  258. color_data <<= 1;
  259. }
  260. _transfer(out_data);
  261. }
  262. }
  263. _endTransfer();
  264. if (y + h == HEIGHT) // last page
  265. {
  266. //Serial.println("paged ended");
  267. _paged = false;
  268. }
  269. }
  270. else
  271. {
  272. _paged = false;
  273. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  274. x -= x % 8; // byte boundary
  275. w = wb * 8; // byte boundary
  276. if ((w <= 0) || (h <= 0)) return;
  277. _writeCommand(0x10);
  278. _startTransfer();
  279. for (int16_t i = 0; i < int16_t(HEIGHT); i++)
  280. {
  281. for (int16_t j = 0; j < int16_t(WIDTH); j += 8)
  282. {
  283. uint8_t black_data = 0xFF, color_data = 0xFF;
  284. if ((j >= x) && (j < x + w) && (i >= y) && (i < y + h))
  285. {
  286. uint32_t idx = mirror_y ? (j - x) / 8 + uint32_t((h - 1 - (i - y))) * wb : (j - x) / 8 + uint32_t(i - y) * wb;
  287. if (pgm)
  288. {
  289. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  290. black_data = pgm_read_byte(&black[idx]);
  291. color_data = pgm_read_byte(&color[idx]);
  292. #else
  293. black_data = black[idx];
  294. color_data = color[idx];
  295. #endif
  296. }
  297. else
  298. {
  299. black_data = black[idx];
  300. color_data = color[idx];
  301. }
  302. if (invert)
  303. {
  304. black_data = ~black_data;
  305. color_data = ~color_data;
  306. }
  307. }
  308. for (int16_t k = 0; k < 2; k++)
  309. {
  310. uint8_t out_data = 0x00;
  311. for (int16_t l = 0; l < 4; l++)
  312. {
  313. out_data <<= 2;
  314. if (!(color_data & 0x80)) out_data |= 0x03; // red
  315. else out_data |= black_data & 0x80 ? 0x01 : 0x00; // white or black
  316. black_data <<= 1;
  317. color_data <<= 1;
  318. }
  319. _transfer(out_data);
  320. }
  321. }
  322. }
  323. _endTransfer();
  324. }
  325. }
  326. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  327. }
  328. void GxEPD2_290c_GDEY029F51H::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  329. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  330. {
  331. //Serial.print("writeImagePart("); Serial.print(x_part); Serial.print(", "); Serial.print(y_part); Serial.print(", ");
  332. //Serial.print(w_bitmap); Serial.print(", "); Serial.print(h_bitmap); Serial.print(", ");
  333. //Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  334. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  335. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  336. if (!_init_display_done) _InitDisplay();
  337. if (_initial_write) writeScreenBuffer();
  338. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  339. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  340. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  341. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  342. x_part -= x_part % 8; // byte boundary
  343. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  344. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  345. // make x, w multiple of 8
  346. w += x % 8; // adjust for byte boundary of x
  347. x -= x % 8; // byte boundary ox
  348. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  349. int16_t x1 = x < 0 ? 0 : x; // limit
  350. int16_t y1 = y < 0 ? 0 : y; // limit
  351. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  352. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  353. int16_t dx = x1 - x;
  354. int16_t dy = y1 - y;
  355. w1 -= dx;
  356. h1 -= dy;
  357. if ((w1 <= 0) || (h1 <= 0)) return;
  358. if (hasPartialUpdate)
  359. {
  360. _setPartialRamArea(x1, y1, w1, h1);
  361. _writeCommand(0x10);
  362. _startTransfer();
  363. for (int16_t i = 0; i < h1; i++)
  364. {
  365. for (int16_t j = 0; j < w1 / 8; j++)
  366. {
  367. uint8_t data;
  368. // use wb_bitmap, h_bitmap of bitmap for index!
  369. uint32_t idx = mirror_y ? x_part / 8 + j + dx / 8 + uint32_t((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + uint32_t(y_part + i + dy) * wb_bitmap;
  370. if (pgm)
  371. {
  372. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  373. data = pgm_read_byte(&bitmap[idx]);
  374. #else
  375. data = bitmap[idx];
  376. #endif
  377. }
  378. else
  379. {
  380. data = bitmap[idx];
  381. }
  382. if (invert) data = ~data;
  383. for (int16_t k = 0; k < 2; k++)
  384. {
  385. uint8_t data2 = (data & 0x80 ? 0x40 : 0x00) | (data & 0x40 ? 0x10 : 0x00) |
  386. (data & 0x20 ? 0x04 : 0x00) | (data & 0x10 ? 0x01 : 0x00);
  387. data <<= 4;
  388. _transfer(data2);
  389. }
  390. }
  391. }
  392. _endTransfer();
  393. }
  394. else
  395. {
  396. _writeCommand(0x10);
  397. _startTransfer();
  398. for (int16_t i = 0; i < int16_t(HEIGHT); i++)
  399. {
  400. for (int16_t j = 0; j < int16_t(WIDTH); j += 8)
  401. {
  402. uint8_t data = 0xFF;
  403. if ((j >= x1) && (j < x1 + w) && (i >= y1) && (i < y1 + h))
  404. {
  405. // use wb_bitmap, h_bitmap of bitmap for index!
  406. uint32_t idx = mirror_y ? (x_part + j - x1) / 8 + uint32_t((h_bitmap - 1 - (y_part + i - y1))) * wb_bitmap : (x_part + j - x1) / 8 + uint32_t(y_part + i - y1) * wb_bitmap;
  407. if (pgm)
  408. {
  409. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  410. data = pgm_read_byte(&bitmap[idx]);
  411. #else
  412. data = bitmap[idx];
  413. #endif
  414. }
  415. else
  416. {
  417. data = bitmap[idx];
  418. }
  419. if (invert) data = ~data;
  420. }
  421. for (int16_t k = 0; k < 2; k++)
  422. {
  423. uint8_t data2 = (data & 0x80 ? 0x40 : 0x00) | (data & 0x40 ? 0x10 : 0x00) |
  424. (data & 0x20 ? 0x04 : 0x00) | (data & 0x10 ? 0x01 : 0x00);
  425. data <<= 4;
  426. _transfer(data2);
  427. }
  428. }
  429. }
  430. _endTransfer();
  431. }
  432. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  433. }
  434. void GxEPD2_290c_GDEY029F51H::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,
  435. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  436. {
  437. //Serial.print("writeImagePart("); Serial.print(x_part); Serial.print(", "); Serial.print(y_part); Serial.print(", ");
  438. //Serial.print(w_bitmap); Serial.print(", "); Serial.print(h_bitmap); Serial.print(", ");
  439. //Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  440. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  441. if (!black && !color) return;
  442. if (!color) return writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  443. if (!_init_display_done) _InitDisplay();
  444. if (_initial_write) writeScreenBuffer();
  445. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  446. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  447. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  448. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  449. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  450. x_part -= x_part % 8; // byte boundary
  451. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  452. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  453. // make x, w multiple of 8
  454. w += x % 8; // adjust for byte boundary of x
  455. x -= x % 8; // byte boundary ox
  456. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  457. int16_t x1 = x < 0 ? 0 : x; // limit
  458. int16_t y1 = y < 0 ? 0 : y; // limit
  459. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  460. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  461. int16_t dx = x1 - x;
  462. int16_t dy = y1 - y;
  463. w1 -= dx;
  464. h1 -= dy;
  465. if ((w1 <= 0) || (h1 <= 0)) return;
  466. if (hasPartialUpdate)
  467. {
  468. _setPartialRamArea(x1, y1, w1, h1);
  469. _writeCommand(0x10);
  470. _startTransfer();
  471. for (int16_t i = 0; i < h1; i++)
  472. {
  473. for (int16_t j = 0; j < w1 / 8; j++)
  474. {
  475. uint8_t black_data = 0xFF, color_data = 0xFF;
  476. // use wb_bitmap, h_bitmap of bitmap for index!
  477. uint32_t idx = mirror_y ? x_part / 8 + j + dx / 8 + uint32_t((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + uint32_t(y_part + i + dy) * wb_bitmap;
  478. if (pgm)
  479. {
  480. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  481. black_data = pgm_read_byte(&black[idx]);
  482. color_data = pgm_read_byte(&color[idx]);
  483. #else
  484. black_data = black[idx];
  485. color_data = color[idx];
  486. #endif
  487. }
  488. else
  489. {
  490. black_data = black[idx];
  491. color_data = color[idx];
  492. }
  493. for (int16_t k = 0; k < 2; k++)
  494. {
  495. uint8_t out_data = 0x00;
  496. for (int16_t l = 0; l < 4; l++)
  497. {
  498. out_data <<= 2;
  499. if (!(color_data & 0x80)) out_data |= 0x03; // red
  500. else out_data |= black_data & 0x80 ? 0x01 : 0x00; // white or black
  501. black_data <<= 1;
  502. color_data <<= 1;
  503. }
  504. _transfer(out_data);
  505. }
  506. }
  507. }
  508. _endTransfer();
  509. }
  510. else
  511. {
  512. _writeCommand(0x10);
  513. _startTransfer();
  514. for (int16_t i = 0; i < int16_t(HEIGHT); i++)
  515. {
  516. for (int16_t j = 0; j < int16_t(WIDTH); j += 8)
  517. {
  518. uint8_t black_data = 0xFF, color_data = 0xFF;
  519. if ((j >= x1) && (j < x1 + w) && (i >= y1) && (i < y1 + h))
  520. {
  521. // use wb_bitmap, h_bitmap of bitmap for index!
  522. uint32_t idx = mirror_y ? (x_part + j - x1) / 8 + uint32_t((h_bitmap - 1 - (y_part + i - y1))) * wb_bitmap : (x_part + j - x1) / 8 + uint32_t(y_part + i - y1) * wb_bitmap;
  523. if (pgm)
  524. {
  525. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  526. black_data = pgm_read_byte(&black[idx]);
  527. color_data = pgm_read_byte(&color[idx]);
  528. #else
  529. black_data = black[idx];
  530. color_data = color[idx];
  531. #endif
  532. }
  533. else
  534. {
  535. black_data = black[idx];
  536. color_data = color[idx];
  537. }
  538. if (invert)
  539. {
  540. black_data = ~black_data;
  541. color_data = ~color_data;
  542. }
  543. }
  544. for (int16_t k = 0; k < 2; k++)
  545. {
  546. uint8_t out_data = 0x00;
  547. for (int16_t l = 0; l < 4; l++)
  548. {
  549. out_data <<= 2;
  550. if (!(color_data & 0x80)) out_data |= 0x03; // red
  551. else out_data |= black_data & 0x80 ? 0x01 : 0x00; // white or black
  552. black_data <<= 1;
  553. color_data <<= 1;
  554. }
  555. _transfer(out_data);
  556. }
  557. }
  558. }
  559. _endTransfer();
  560. }
  561. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  562. }
  563. void GxEPD2_290c_GDEY029F51H::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)
  564. {
  565. if (!data1) return;
  566. //Serial.print("writeNative("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  567. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  568. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  569. if (!_init_display_done) _InitDisplay();
  570. if (_initial_write) writeScreenBuffer();
  571. if (hasPartialUpdate)
  572. {
  573. int16_t wb = (w + 3) / 4; // width bytes, bitmaps are padded
  574. x -= x % 4; // byte boundary
  575. w = wb * 4; // byte boundary
  576. int16_t x1 = x < 0 ? 0 : x; // limit
  577. int16_t y1 = y < 0 ? 0 : y; // limit
  578. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  579. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  580. int16_t dx = x1 - x;
  581. int16_t dy = y1 - y;
  582. w1 -= dx;
  583. h1 -= dy;
  584. if ((w1 <= 0) || (h1 <= 0)) return;
  585. _setPartialRamArea(x1, y1, w1, h1);
  586. _writeCommand(0x10);
  587. _startTransfer();
  588. for (int16_t i = 0; i < h1; i++)
  589. {
  590. for (int16_t j = 0; j < w1 / 4; j++)
  591. {
  592. uint8_t data;
  593. // use wb, h of bitmap for index!
  594. uint32_t idx = mirror_y ? j + dx / 4 + uint32_t((h - 1 - (i + dy))) * wb : j + dx / 4 + uint32_t(i + dy) * wb;
  595. if (pgm)
  596. {
  597. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  598. data = pgm_read_byte(&data1[idx]);
  599. #else
  600. data = data1[idx];
  601. #endif
  602. }
  603. else
  604. {
  605. data = data1[idx];
  606. }
  607. if (invert) data = ~data;
  608. _transfer(data);
  609. }
  610. }
  611. _endTransfer();
  612. }
  613. else
  614. {
  615. if (_paged && (x == 0) && (w == int16_t(WIDTH)) && (h < int16_t(HEIGHT)))
  616. {
  617. //Serial.println("paged");
  618. _startTransfer();
  619. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(h) / 4; i++)
  620. {
  621. uint8_t data = data1[i];
  622. _transfer(data);
  623. }
  624. _endTransfer();
  625. if (y + h == HEIGHT) // last page
  626. {
  627. //Serial.println("paged ended");
  628. _paged = false;
  629. }
  630. }
  631. else
  632. {
  633. //Serial.println("not paged");
  634. _paged = false;
  635. int16_t wb = (w + 3) / 4; // width bytes, bitmaps are padded
  636. x -= x % 4; // byte boundary
  637. w = wb * 4; // byte boundary
  638. if ((w <= 0) || (h <= 0)) return;
  639. _writeCommand(0x10);
  640. _startTransfer();
  641. for (int16_t i = 0; i < int16_t(HEIGHT); i++)
  642. {
  643. for (int16_t j = 0; j < int16_t(WIDTH); j += 4)
  644. {
  645. uint8_t data = 0x55;
  646. if (data1)
  647. {
  648. if ((j >= x) && (j < x + w) && (i >= y) && (i < y + h))
  649. {
  650. uint32_t idx = mirror_y ? (j - x) / 4 + uint32_t((h - 1 - (i - y))) * wb : (j - x) / 4 + uint32_t(i - y) * wb;
  651. if (pgm)
  652. {
  653. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  654. data = pgm_read_byte(&data1[idx]);
  655. #else
  656. data = data1[idx];
  657. #endif
  658. }
  659. else
  660. {
  661. data = data1[idx];
  662. }
  663. if (invert) data = ~data;
  664. }
  665. }
  666. _transfer(data);
  667. }
  668. }
  669. _endTransfer();
  670. }
  671. }
  672. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  673. }
  674. void GxEPD2_290c_GDEY029F51H::writeNativePart(const uint8_t* data1, const uint8_t* data2, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  675. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  676. {
  677. if (!data1) return;
  678. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  679. if (!_init_display_done) _InitDisplay();
  680. if (_initial_write) writeScreenBuffer();
  681. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  682. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  683. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  684. int16_t wb_bitmap = (w_bitmap + 3) / 4; // width bytes, bitmaps are padded
  685. x_part -= x_part % 4; // byte boundary
  686. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  687. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  688. w += x % 4; // adjust for byte boundary of x
  689. x -= x % 4; // byte boundary
  690. w = 4 * ((w + 3) / 4); // byte boundary, bitmaps are padded
  691. int16_t x1 = x < 0 ? 0 : x; // limit
  692. int16_t y1 = y < 0 ? 0 : y; // limit
  693. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  694. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  695. int16_t dx = x1 - x;
  696. int16_t dy = y1 - y;
  697. w1 -= dx;
  698. h1 -= dy;
  699. if ((w1 <= 0) || (h1 <= 0)) return;
  700. if (hasPartialUpdate)
  701. {
  702. _setPartialRamArea(x1, y1, w1, h1);
  703. _writeCommand(0x10);
  704. _startTransfer();
  705. for (int16_t i = 0; i < h1; i++)
  706. {
  707. for (int16_t j = 0; j < w1 / 4; j++)
  708. {
  709. uint8_t data;
  710. // use wb_bitmap, h_bitmap of bitmap for index!
  711. uint32_t idx = mirror_y ? x_part / 4 + j + dx / 4 + uint32_t((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 4 + j + dx / 4 + uint32_t(y_part + i + dy) * wb_bitmap;
  712. if (pgm)
  713. {
  714. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  715. data = pgm_read_byte(&data1[idx]);
  716. #else
  717. data = data1[idx];
  718. #endif
  719. }
  720. else
  721. {
  722. data = data1[idx];
  723. }
  724. if (invert) data = ~data;
  725. _transfer(data);
  726. }
  727. }
  728. _endTransfer();
  729. }
  730. else
  731. {
  732. _writeCommand(0x10);
  733. _startTransfer();
  734. for (int16_t i = 0; i < int16_t(HEIGHT); i++)
  735. {
  736. for (int16_t j = 0; j < int16_t(WIDTH); j += 4)
  737. {
  738. uint8_t data = 0x55;
  739. if ((j >= x1) && (j < x1 + w) && (i >= y1) && (i < y1 + h))
  740. {
  741. // use wb_bitmap, h_bitmap of bitmap for index!
  742. uint32_t idx = mirror_y ? (x_part + j - x1) / 4 + uint32_t((h_bitmap - 1 - (y_part + i - y1))) * wb_bitmap : (x_part + j - x1) / 4 + uint32_t(y_part + i - y1) * wb_bitmap;
  743. if (pgm)
  744. {
  745. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  746. data = pgm_read_byte(&data1[idx]);
  747. #else
  748. data = data1[idx];
  749. #endif
  750. }
  751. else
  752. {
  753. data = data1[idx];
  754. }
  755. if (invert) data = ~data;
  756. }
  757. _transfer(data);
  758. }
  759. }
  760. _endTransfer();
  761. }
  762. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  763. }
  764. void GxEPD2_290c_GDEY029F51H::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  765. {
  766. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  767. refresh(x, y, w, h);
  768. }
  769. void GxEPD2_290c_GDEY029F51H::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  770. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  771. {
  772. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  773. refresh(x, y, w, h);
  774. }
  775. void GxEPD2_290c_GDEY029F51H::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)
  776. {
  777. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  778. refresh(x, y, w, h);
  779. }
  780. void GxEPD2_290c_GDEY029F51H::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,
  781. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  782. {
  783. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  784. refresh(x, y, w, h);
  785. }
  786. void GxEPD2_290c_GDEY029F51H::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)
  787. {
  788. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  789. refresh(x, y, w, h);
  790. }
  791. void GxEPD2_290c_GDEY029F51H::refresh(bool partial_update_mode)
  792. {
  793. if (hasPartialUpdate) _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  794. _refresh(partial_update_mode);
  795. }
  796. void GxEPD2_290c_GDEY029F51H::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  797. {
  798. // intersection with screen
  799. int16_t w1 = x < 0 ? w + x : w; // reduce
  800. int16_t h1 = y < 0 ? h + y : h; // reduce
  801. int16_t x1 = x < 0 ? 0 : x; // limit
  802. int16_t y1 = y < 0 ? 0 : y; // limit
  803. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  804. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  805. if ((w1 <= 0) || (h1 <= 0)) return;
  806. // make x1, w1 multiple of 4
  807. w1 += x1 % 4;
  808. if (w1 % 4 > 0) w1 += 4 - w1 % 4;
  809. x1 -= x1 % 4;
  810. bool fullscreen = ((x1 == 0) && (y1 == 0) && (w1 == WIDTH) && (h1 == HEIGHT));
  811. if (hasPartialUpdate) _setPartialRamArea(x1, y1, w1, h1, !fullscreen);
  812. _refresh(!fullscreen);
  813. }
  814. void GxEPD2_290c_GDEY029F51H::powerOff()
  815. {
  816. _PowerOff();
  817. }
  818. void GxEPD2_290c_GDEY029F51H::hibernate()
  819. {
  820. _PowerOff();
  821. if (_rst >= 0)
  822. {
  823. _writeCommand(0x07); // deep sleep
  824. _writeData(0xA5); // control code
  825. _hibernating = true;
  826. _init_display_done = false;
  827. }
  828. }
  829. void GxEPD2_290c_GDEY029F51H::setPaged()
  830. {
  831. if (!hasPartialUpdate)
  832. {
  833. _paged = true;
  834. _InitDisplay();
  835. _writeCommand(0x10);
  836. }
  837. }
  838. void GxEPD2_290c_GDEY029F51H::_refresh(bool partial_update_mode)
  839. {
  840. _writeCommand(0x50); // VCOM and Data Interval Setting
  841. _writeData(partial_update_mode ? 0x97 : 0x37); // floating or white
  842. _writeCommand(0x12); // Display Refresh
  843. _writeData(0x00);
  844. delay(1);
  845. _waitWhileBusy("_refresh", full_refresh_time);
  846. //_power_is_on = false; // not needed
  847. _init_display_done = false; // needed
  848. }
  849. void GxEPD2_290c_GDEY029F51H::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h, bool partial_mode)
  850. {
  851. //Serial.print("_setPartialRamArea("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  852. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  853. _writeCommand(0x83);
  854. _writeData(x / 256);
  855. _writeData(x % 256);
  856. _writeData((x + w - 1) / 256);
  857. _writeData((x + w - 1) % 256);
  858. _writeData(y / 256);
  859. _writeData(y % 256);
  860. _writeData((y + h - 1) / 256);
  861. _writeData((y + h - 1) % 256);
  862. _writeData(partial_mode ? 0x01 : 0x00);
  863. }
  864. void GxEPD2_290c_GDEY029F51H::_PowerOn()
  865. {
  866. if (!_power_is_on)
  867. {
  868. _writeCommand(0x04);
  869. _waitWhileBusy("_PowerOn", power_on_time);
  870. }
  871. _power_is_on = true;
  872. }
  873. void GxEPD2_290c_GDEY029F51H::_PowerOff()
  874. {
  875. if (_power_is_on)
  876. {
  877. _writeCommand(0x02);
  878. _writeData(0x00);
  879. _waitWhileBusy("_PowerOff", power_off_time);
  880. }
  881. _power_is_on = false;
  882. }
  883. void GxEPD2_290c_GDEY029F51H::_InitDisplay()
  884. {
  885. //Serial.println("_InitDisplay");
  886. // initcode from Good Dispay demo for GDEY029F51H
  887. if ((_rst >= 0) && (_hibernating || _initial_write))
  888. {
  889. digitalWrite(_rst, HIGH);
  890. delay(20); // At least 20ms delay
  891. digitalWrite(_rst, LOW); // Module reset
  892. delay(2); // At least 40ms delay, 2ms for WS "clever" reset
  893. digitalWrite(_rst, HIGH);
  894. delay(2); // At least 50ms delay (32ms measured)
  895. _waitWhileBusy("_InitDisplay reset", power_on_time);
  896. _hibernating = false;
  897. _power_is_on = false;
  898. }
  899. _writeCommand(0x4D); // ??
  900. _writeData(0x78);
  901. _writeCommand(0x00); // PSR
  902. _writeData(0x0F); // default res (176x296)), scan up, shift left, booster on, no effect (no RST)
  903. _writeData(0x29); // LUT from MPT, no add scan, VCOM default, TS_AUTO, VGN default, VCOM default, VCOM floating after refresh
  904. _writeCommand(0x01); // PWRR
  905. _writeData(0x07); // VMODE default, VSPL internal, VSP/VSN internal, VGP/VGN internal
  906. _writeData(0x00); // VGP/VGN 20V
  907. _writeCommand(0x03); // POFS
  908. _writeData(0x10); // T_VDGP_OFF 40ms, T_VDS_OFF 20ms
  909. _writeData(0x54); // VGP_LEN 2500ms, VGP_EXT 2000ms
  910. _writeData(0x44); // XON_DLY 2000ms, XON_LEN 2000ms
  911. _writeCommand(0x06); // BTST Booster Soft Start
  912. _writeData(0x05); // PHB_SFT, PHA_SFT
  913. _writeData(0x00); // PHA_ON
  914. _writeData(0x3F); // PHA_OFF
  915. _writeData(0x0A); // PHB_ON
  916. _writeData(0x25); // PHB_OFF
  917. _writeData(0x12); // PHC_ON
  918. _writeData(0x1A); // PHC_OFF
  919. _writeCommand(0x50); // CDI
  920. _writeData(0x37); // (VBD 1, DDX 1) Gray1, 10hsync
  921. //_writeData(0x17); // black border
  922. //_writeData(0x37); // white border (default)
  923. //_writeData(0x57); // yellow border
  924. //_writeData(0x77); // red border
  925. //_writeData(0x97); // floating border
  926. _writeCommand(0x60); // TCON
  927. _writeData(0x02); // ?
  928. _writeData(0x02); // ?
  929. _writeCommand(0x61); // TRES
  930. _writeData(WIDTH / 256); // Source_BITS_H
  931. _writeData(WIDTH % 256); // Source_BITS_L
  932. _writeData(HEIGHT / 256); // Gate_BITS_H
  933. _writeData(HEIGHT % 256); // Gate_BITS_L
  934. _writeCommand(0xE7); // ??
  935. _writeData(0x1C);
  936. _writeCommand(0xE3); // PWS
  937. _writeData(0x22);
  938. _writeCommand(0xB4); // LVD
  939. _writeData(0xD0);
  940. _writeCommand(0xB5); // ??
  941. _writeData(0x03);
  942. _writeCommand(0xE9); // ??
  943. _writeData(0x01);
  944. _writeCommand(0x30); // PLL
  945. _writeData(0x08); // Dynamic frame rate enabled
  946. _PowerOn();
  947. _init_display_done = true;
  948. }