GxEPD2_290_T94.cpp 12 KB

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