GxEPD2_290_T94.cpp 12 KB

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