GxEPD2_583.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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: IL0371 : http://www.e-paper-display.com/download_detail/downloadsId=536.html
  6. //
  7. // Author: Jean-Marc Zingg
  8. //
  9. // Version: see library.properties
  10. //
  11. // Library: https://github.com/ZinggJM/GxEPD2
  12. #include "GxEPD2_583.h"
  13. GxEPD2_583::GxEPD2_583(int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
  14. GxEPD2_EPD(cs, dc, rst, busy, LOW, 20000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  15. {
  16. }
  17. void GxEPD2_583::clearScreen(uint8_t value)
  18. {
  19. if (value == 0xFF) value = 0x33; // white value for this controller
  20. if (_initial)
  21. {
  22. _Init_Full();
  23. _writeCommand(0x10);
  24. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  25. {
  26. _writeData(value);
  27. }
  28. _Update_Full();
  29. _initial = false;
  30. }
  31. if (!_using_partial_mode) _Init_Part();
  32. _writeCommand(0x91); // partial in
  33. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  34. _writeCommand(0x10);
  35. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  36. {
  37. _writeData(value);
  38. }
  39. _Update_Part();
  40. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  41. _writeCommand(0x10);
  42. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  43. {
  44. _writeData(value);
  45. }
  46. _Update_Part();
  47. _writeCommand(0x92); // partial out
  48. _initial = false;
  49. }
  50. void GxEPD2_583::writeScreenBuffer(uint8_t value)
  51. {
  52. if (value == 0xFF) value = 0x33; // white value for this controller
  53. if (_initial) clearScreen(value);
  54. else _writeScreenBuffer(value);
  55. }
  56. void GxEPD2_583::_writeScreenBuffer(uint8_t value)
  57. {
  58. if (!_using_partial_mode) _Init_Part();
  59. _writeCommand(0x91); // partial in
  60. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  61. _writeCommand(0x10);
  62. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  63. {
  64. _writeData(value);
  65. }
  66. _writeCommand(0x92); // partial out
  67. }
  68. void GxEPD2_583::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  69. {
  70. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  71. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  72. x -= x % 8; // byte boundary
  73. w = wb * 8; // byte boundary
  74. int16_t x1 = x < 0 ? 0 : x; // limit
  75. int16_t y1 = y < 0 ? 0 : y; // limit
  76. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  77. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  78. int16_t dx = x1 - x;
  79. int16_t dy = y1 - y;
  80. w1 -= dx;
  81. h1 -= dy;
  82. if ((w1 <= 0) || (h1 <= 0)) return;
  83. if (!_using_partial_mode) _Init_Part();
  84. _writeCommand(0x91); // partial in
  85. _setPartialRamArea(x1, y1, w1, h1);
  86. _writeCommand(0x10);
  87. for (int16_t i = 0; i < h1; i++)
  88. {
  89. for (int16_t j = 0; j < w1 / 8; j++)
  90. {
  91. uint8_t data;
  92. // use wb, h of bitmap for index!
  93. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  94. if (pgm)
  95. {
  96. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  97. data = pgm_read_byte(&bitmap[idx]);
  98. #else
  99. data = bitmap[idx];
  100. #endif
  101. }
  102. else
  103. {
  104. data = bitmap[idx];
  105. }
  106. if (invert) data = ~data;
  107. _send8pixel(~data);
  108. }
  109. }
  110. _writeCommand(0x92); // partial out
  111. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  112. }
  113. void GxEPD2_583::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)
  114. {
  115. if (black)
  116. {
  117. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  118. }
  119. }
  120. void GxEPD2_583::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)
  121. {
  122. if (data1)
  123. {
  124. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  125. int16_t wb = (w + 1) / 2; // width bytes, bitmaps are padded
  126. x -= x % 2; // byte boundary
  127. w = wb * 2; // byte boundary
  128. int16_t x1 = x < 0 ? 0 : x; // limit
  129. int16_t y1 = y < 0 ? 0 : y; // limit
  130. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  131. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  132. int16_t dx = x1 - x;
  133. int16_t dy = y1 - y;
  134. w1 -= dx;
  135. h1 -= dy;
  136. if ((w1 <= 0) || (h1 <= 0)) return;
  137. if (!_using_partial_mode) _Init_Part();
  138. _writeCommand(0x91); // partial in
  139. _setPartialRamArea(x1, y1, w1, h1);
  140. _writeCommand(0x10);
  141. for (int16_t i = 0; i < h1; i++)
  142. {
  143. for (int16_t j = 0; j < w1 / 2; j++)
  144. {
  145. uint8_t data;
  146. // use wb, h of bitmap for index!
  147. int16_t idx = mirror_y ? j + dx / 2 + ((h - 1 - (i + dy))) * wb : j + dx / 2 + (i + dy) * wb;
  148. if (pgm)
  149. {
  150. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  151. data = pgm_read_byte(&data1[idx]);
  152. #else
  153. data = data1[idx];
  154. #endif
  155. }
  156. else
  157. {
  158. data = data1[idx];
  159. }
  160. if (invert) data = ~data;
  161. _writeData(data);
  162. }
  163. }
  164. _writeCommand(0x92); // partial out
  165. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  166. }
  167. }
  168. void GxEPD2_583::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  169. {
  170. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  171. refresh(x, y, w, h);
  172. }
  173. void GxEPD2_583::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)
  174. {
  175. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  176. refresh(x, y, w, h);
  177. }
  178. void GxEPD2_583::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)
  179. {
  180. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  181. refresh(x, y, w, h);
  182. }
  183. void GxEPD2_583::refresh(bool partial_update_mode)
  184. {
  185. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  186. else
  187. {
  188. if (_using_partial_mode) _Init_Full();
  189. _Update_Full();
  190. }
  191. }
  192. void GxEPD2_583::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  193. {
  194. x -= x % 8; // byte boundary
  195. w -= x % 8; // byte boundary
  196. int16_t x1 = x < 0 ? 0 : x; // limit
  197. int16_t y1 = y < 0 ? 0 : y; // limit
  198. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  199. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  200. w1 -= x1 - x;
  201. h1 -= y1 - y;
  202. if (!_using_partial_mode) _Init_Part();
  203. _writeCommand(0x91); // partial in
  204. _setPartialRamArea(x1, y1, w1, h1);
  205. _Update_Part();
  206. _writeCommand(0x92); // partial out
  207. }
  208. void GxEPD2_583::powerOff(void)
  209. {
  210. _PowerOff();
  211. }
  212. void GxEPD2_583::hibernate()
  213. {
  214. _PowerOff();
  215. if (_rst >= 0)
  216. {
  217. // check if it supports this command!
  218. _writeCommand(0x07); // deep sleep
  219. _writeData(0xA5); // check code
  220. _hibernating = true;
  221. }
  222. }
  223. void GxEPD2_583::_send8pixel(uint8_t data)
  224. {
  225. for (uint8_t j = 0; j < 8; j++)
  226. {
  227. uint8_t t = data & 0x80 ? 0x00 : 0x03;
  228. t <<= 4;
  229. data <<= 1;
  230. j++;
  231. t |= data & 0x80 ? 0x00 : 0x03;
  232. data <<= 1;
  233. _writeData(t);
  234. }
  235. }
  236. void GxEPD2_583::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  237. {
  238. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  239. uint16_t ye = y + h - 1;
  240. x &= 0xFFF8; // byte boundary
  241. _writeCommand(0x90); // partial window
  242. _writeData(x / 256);
  243. _writeData(x % 256);
  244. _writeData(xe / 256);
  245. _writeData(xe % 256);
  246. _writeData(y / 256);
  247. _writeData(y % 256);
  248. _writeData(ye / 256);
  249. _writeData(ye % 256);
  250. _writeData(0x01); // don't see any difference
  251. //_writeData(0x00); // don't see any difference
  252. }
  253. void GxEPD2_583::_PowerOn()
  254. {
  255. if (!_power_is_on)
  256. {
  257. _writeCommand(0x04);
  258. _waitWhileBusy("_PowerOn", power_on_time);
  259. }
  260. _power_is_on = true;
  261. }
  262. void GxEPD2_583::_PowerOff()
  263. {
  264. _writeCommand(0x02); // power off
  265. _waitWhileBusy("_PowerOff", power_off_time);
  266. _power_is_on = false;
  267. _using_partial_mode = false;
  268. }
  269. void GxEPD2_583::_InitDisplay()
  270. {
  271. if (_hibernating) _reset();
  272. _writeCommand(0x01);
  273. _writeData (0x37); // POWER SETTING
  274. _writeData (0x00);
  275. _writeCommand(0X00); // PANNEL SETTING
  276. _writeData(0xCF);
  277. _writeData(0x08);
  278. _writeCommand(0x06); // boost
  279. _writeData (0xc7);
  280. _writeData (0xcc);
  281. _writeData (0x28);
  282. _writeCommand(0x30); // PLL setting
  283. //_writeData (0x39); // PLL: 7s refresh
  284. _writeData (0x3a); // PLL: 15s refresh
  285. //_writeData (0x3c); // PLL: 30s refresh
  286. _writeCommand(0X41); // TEMPERATURE SETTING
  287. _writeData(0x00);
  288. _writeCommand(0X50); // VCOM AND DATA INTERVAL SETTING
  289. _writeData(0x77);
  290. _writeCommand(0X60); // TCON SETTING
  291. _writeData(0x22);
  292. _writeCommand(0x61); // 600*448
  293. _writeData (0x02); // source 600
  294. _writeData (0x58);
  295. _writeData (0x01); // gate 448
  296. _writeData (0xc0);
  297. _writeCommand(0X82); // VCOM VOLTAGE SETTING
  298. _writeData(0x28); // all temperature range
  299. _writeCommand(0xe5); // FLASH MODE
  300. _writeData(0x03);
  301. }
  302. void GxEPD2_583::_Init_Full()
  303. {
  304. _InitDisplay();
  305. _PowerOn();
  306. _using_partial_mode = false;
  307. }
  308. void GxEPD2_583::_Init_Part()
  309. {
  310. _InitDisplay();
  311. _PowerOn();
  312. _using_partial_mode = true;
  313. }
  314. void GxEPD2_583::_Update_Full()
  315. {
  316. _writeCommand(0x12); //display refresh
  317. _waitWhileBusy("_Update_Full", full_refresh_time);
  318. }
  319. void GxEPD2_583::_Update_Part()
  320. {
  321. _writeCommand(0x12); //display refresh
  322. _waitWhileBusy("_Update_Part", partial_refresh_time);
  323. }