GxEPD2_266c.cpp 12 KB

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