GxEPD2_213.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. #include "GxEPD2_213.h"
  12. #include "WaveTables.h"
  13. GxEPD2_213::GxEPD2_213(int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
  14. GxEPD2_EPD(cs, dc, rst, busy, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  15. {
  16. _initial = true;
  17. _power_is_on = false;
  18. _using_partial_mode = false;
  19. }
  20. void GxEPD2_213::init(uint32_t serial_diag_bitrate)
  21. {
  22. GxEPD2_EPD::init(serial_diag_bitrate);
  23. _initial = true;
  24. _power_is_on = false;
  25. _using_partial_mode = false;
  26. }
  27. void GxEPD2_213::clearScreen(uint8_t value)
  28. {
  29. if (_initial)
  30. {
  31. _Init_Full();
  32. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  33. _writeCommand(0x24);
  34. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  35. {
  36. _writeData(value);
  37. }
  38. _Update_Full();
  39. }
  40. else
  41. {
  42. if (!_using_partial_mode) _Init_Part();
  43. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  44. _writeCommand(0x24);
  45. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  46. {
  47. _writeData(value);
  48. }
  49. _Update_Part();
  50. }
  51. if (!_using_partial_mode) _Init_Part();
  52. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  53. _writeCommand(0x24);
  54. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  55. {
  56. _writeData(value);
  57. }
  58. _Update_Part();
  59. _initial = false;
  60. }
  61. void GxEPD2_213::writeScreenBuffer(uint8_t value)
  62. {
  63. if (_initial) clearScreen(value);
  64. else _writeScreenBuffer(value);
  65. }
  66. void GxEPD2_213::_writeScreenBuffer(uint8_t value)
  67. {
  68. if (!_using_partial_mode) _Init_Part();
  69. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  70. _writeCommand(0x24);
  71. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  72. {
  73. _writeData(value);
  74. }
  75. }
  76. void GxEPD2_213::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  77. {
  78. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  79. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  80. x -= x % 8; // byte boundary
  81. w = wb * 8; // byte boundary
  82. int16_t x1 = x < 0 ? 0 : x; // limit
  83. int16_t y1 = y < 0 ? 0 : y; // limit
  84. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  85. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  86. int16_t dx = x1 - x;
  87. int16_t dy = y1 - y;
  88. w1 -= dx;
  89. h1 -= dy;
  90. if ((w1 <= 0) || (h1 <= 0)) return;
  91. if (!_using_partial_mode) _Init_Part();
  92. _setPartialRamArea(x1, y1, w1, h1);
  93. _writeCommand(0x24);
  94. for (int16_t i = 0; i < h1; i++)
  95. {
  96. for (int16_t j = 0; j < w1 / 8; j++)
  97. {
  98. uint8_t data;
  99. // use wb, h of bitmap for index!
  100. int16_t idx = mirror_y ? j + dx / 8 + (h - 1 - i) * wb : j + dx / 8 + i * wb;
  101. if (pgm)
  102. {
  103. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  104. data = pgm_read_byte(&bitmap[idx]);
  105. #else
  106. data = bitmap[idx];
  107. #endif
  108. }
  109. else
  110. {
  111. data = bitmap[idx];
  112. }
  113. if (invert) data = ~data;
  114. else _writeData(data);
  115. }
  116. }
  117. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  118. }
  119. void GxEPD2_213::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)
  120. {
  121. if (black)
  122. {
  123. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  124. }
  125. }
  126. void GxEPD2_213::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)
  127. {
  128. if (data1)
  129. {
  130. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  131. }
  132. }
  133. void GxEPD2_213::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  134. {
  135. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  136. refresh(x, y, w, h);
  137. }
  138. void GxEPD2_213::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)
  139. {
  140. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  141. refresh(x, y, w, h);
  142. }
  143. void GxEPD2_213::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)
  144. {
  145. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  146. refresh(x, y, w, h);
  147. }
  148. void GxEPD2_213::refresh(bool partial_update_mode)
  149. {
  150. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  151. else
  152. {
  153. if (_using_partial_mode) _Init_Full();
  154. _Update_Full();
  155. }
  156. }
  157. void GxEPD2_213::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  158. {
  159. x -= x % 8; // byte boundary
  160. w -= x % 8; // byte boundary
  161. int16_t x1 = x < 0 ? 0 : x; // limit
  162. int16_t y1 = y < 0 ? 0 : y; // limit
  163. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  164. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  165. w1 -= x1 - x;
  166. h1 -= y1 - y;
  167. if (!_using_partial_mode) _Init_Part();
  168. _setPartialRamArea(x1, y1, w1, h1);
  169. _Update_Part();
  170. }
  171. void GxEPD2_213::powerOff(void)
  172. {
  173. _PowerOff();
  174. }
  175. void GxEPD2_213::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  176. {
  177. _writeCommand(0x11); // set ram entry mode
  178. _writeData(0x01); // x increase, y decrease : as in demo code
  179. _writeCommand(0x44);
  180. _writeData(x / 8);
  181. _writeData((x + w - 1) / 8);
  182. _writeCommand(0x45);
  183. _writeData((y + h - 1) % 256);
  184. _writeData((y + h - 1) / 256);
  185. _writeData(y % 256);
  186. _writeData(y / 256);
  187. _writeCommand(0x4e);
  188. _writeData(x / 8);
  189. _writeCommand(0x4f);
  190. _writeData((y + h - 1) % 256);
  191. _writeData((y + h - 1) / 256);
  192. }
  193. void GxEPD2_213::_PowerOn()
  194. {
  195. if (!_power_is_on)
  196. {
  197. _writeCommand(0x22);
  198. _writeData(0xc0);
  199. _writeCommand(0x20);
  200. _waitWhileBusy("_PowerOn", power_on_time);
  201. }
  202. _power_is_on = true;
  203. }
  204. void GxEPD2_213::_PowerOff()
  205. {
  206. _writeCommand(0x22);
  207. _writeData(0xc3);
  208. _writeCommand(0x20);
  209. _waitWhileBusy("_PowerOff", power_off_time);
  210. _power_is_on = false;
  211. _using_partial_mode = false;
  212. }
  213. void GxEPD2_213::_InitDisplay()
  214. {
  215. _writeCommand(0x01); // Panel configuration, Gate selection
  216. _writeData((HEIGHT - 1) % 256);
  217. _writeData((HEIGHT - 1) / 256);
  218. _writeData(0x00);
  219. _writeCommand(0x0c); // softstart
  220. _writeData(0xd7);
  221. _writeData(0xd6);
  222. _writeData(0x9d);
  223. _writeCommand(0x2c); // VCOM setting
  224. _writeData(0xa8); // * different
  225. _writeCommand(0x3a); // DummyLine
  226. _writeData(0x1a); // 4 dummy line per gate
  227. _writeCommand(0x3b); // Gatetime
  228. _writeData(0x08); // 2us per line
  229. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  230. }
  231. void GxEPD2_213::_Init_Full()
  232. {
  233. _InitDisplay();
  234. _writeCommandDataPGM(GxGDE0213B1_LUTDefault_full, sizeof(GxGDE0213B1_LUTDefault_full));
  235. _PowerOn();
  236. _using_partial_mode = false;
  237. }
  238. void GxEPD2_213::_Init_Part()
  239. {
  240. _InitDisplay();
  241. _writeCommandDataPGM(GxGDE0213B1_LUTDefault_part, sizeof(GxGDE0213B1_LUTDefault_part));
  242. _PowerOn();
  243. _using_partial_mode = true;
  244. }
  245. void GxEPD2_213::_Update_Full()
  246. {
  247. _writeCommand(0x22);
  248. _writeData(0xc4);
  249. _writeCommand(0x20);
  250. _waitWhileBusy("_Update_Full", full_refresh_time);
  251. _writeCommand(0xff);
  252. }
  253. void GxEPD2_213::_Update_Part()
  254. {
  255. _writeCommand(0x22);
  256. _writeData(0x04);
  257. _writeCommand(0x20);
  258. _waitWhileBusy("_Update_Part", partial_refresh_time);
  259. _writeCommand(0xff);
  260. }