GxEPD2_290_T94.cpp 12 KB

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