GxEPD2_290_C90c.cpp 12 KB

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