GxEPD2_420.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_420.h"
  12. #include "WaveTables.h"
  13. GxEPD2_420::GxEPD2_420(int8_t cs, int8_t dc, int8_t rst, int8_t busy) : GxEPD2_EPD(cs, dc, rst, busy, LOW, 10000000)
  14. {
  15. _initial = true;
  16. _power_is_on = false;
  17. _using_partial_mode = false;
  18. }
  19. void GxEPD2_420::init(uint32_t serial_diag_bitrate)
  20. {
  21. GxEPD2_EPD::init(serial_diag_bitrate);
  22. _initial = true;
  23. _power_is_on = false;
  24. _using_partial_mode = false;
  25. }
  26. void GxEPD2_420::clearScreen(uint8_t value)
  27. {
  28. if (_initial)
  29. {
  30. _Init_Full();
  31. _writeCommand(0x13);
  32. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  33. {
  34. _writeData(value);
  35. }
  36. _Update_Full();
  37. _initial = false;
  38. }
  39. if (!_using_partial_mode) _Init_Part();
  40. _writeCommand(0x91); // partial in
  41. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  42. _writeCommand(0x13);
  43. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  44. {
  45. _writeData(value);
  46. }
  47. _Update_Part();
  48. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  49. _writeCommand(0x13);
  50. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  51. {
  52. _writeData(value);
  53. }
  54. _Update_Part();
  55. _writeCommand(0x92); // partial out
  56. _initial = false;
  57. }
  58. void GxEPD2_420::writeScreenBuffer(uint8_t value)
  59. {
  60. if (_initial) clearScreen(value);
  61. else _writeScreenBuffer(value);
  62. }
  63. void GxEPD2_420::_writeScreenBuffer(uint8_t value)
  64. {
  65. if (!_using_partial_mode) _Init_Part();
  66. _writeCommand(0x91); // partial in
  67. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  68. _writeCommand(0x13);
  69. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  70. {
  71. _writeData(value);
  72. }
  73. _writeCommand(0x92); // partial out
  74. }
  75. void GxEPD2_420::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  76. {
  77. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  78. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  79. x -= x % 8; // byte boundary
  80. w = wb * 8; // byte boundary
  81. int16_t x1 = x < 0 ? 0 : x; // limit
  82. int16_t y1 = y < 0 ? 0 : y; // limit
  83. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  84. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  85. int16_t dx = x1 - x;
  86. int16_t dy = y1 - y;
  87. w1 -= dx;
  88. h1 -= dy;
  89. if ((w1 <= 0) || (h1 <= 0)) return;
  90. if (!_using_partial_mode) _Init_Part();
  91. _writeCommand(0x91); // partial in
  92. _setPartialRamArea(x1, y1, w1, h1);
  93. _writeCommand(0x13);
  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 + dy))) * wb : j + dx / 8 + (i + dy) * 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. _writeData(data);
  115. }
  116. }
  117. _writeCommand(0x92); // partial out
  118. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  119. }
  120. void GxEPD2_420::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)
  121. {
  122. if (black)
  123. {
  124. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  125. }
  126. }
  127. void GxEPD2_420::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)
  128. {
  129. if (data1)
  130. {
  131. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  132. }
  133. }
  134. void GxEPD2_420::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  135. {
  136. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  137. refresh(x, y, w, h);
  138. }
  139. void GxEPD2_420::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)
  140. {
  141. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  142. refresh(x, y, w, h);
  143. }
  144. void GxEPD2_420::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)
  145. {
  146. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  147. refresh(x, y, w, h);
  148. }
  149. void GxEPD2_420::refresh(bool partial_update_mode)
  150. {
  151. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  152. else
  153. {
  154. if (_using_partial_mode) _Init_Full();
  155. _Update_Full();
  156. }
  157. }
  158. void GxEPD2_420::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  159. {
  160. x -= x % 8; // byte boundary
  161. w -= x % 8; // byte boundary
  162. int16_t x1 = x < 0 ? 0 : x; // limit
  163. int16_t y1 = y < 0 ? 0 : y; // limit
  164. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  165. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  166. w1 -= x1 - x;
  167. h1 -= y1 - y;
  168. if (!_using_partial_mode) _Init_Part();
  169. _writeCommand(0x91); // partial in
  170. _setPartialRamArea(x1, y1, w1, h1);
  171. _Update_Part();
  172. _writeCommand(0x92); // partial out
  173. }
  174. void GxEPD2_420::powerOff(void)
  175. {
  176. _PowerOff();
  177. }
  178. void GxEPD2_420::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  179. {
  180. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  181. uint16_t ye = y + h - 1;
  182. x &= 0xFFF8; // byte boundary
  183. _writeCommand(0x90); // partial window
  184. _writeData(x / 256);
  185. _writeData(x % 256);
  186. _writeData(xe / 256);
  187. _writeData(xe % 256);
  188. _writeData(y / 256);
  189. _writeData(y % 256);
  190. _writeData(ye / 256);
  191. _writeData(ye % 256);
  192. _writeData(0x01); // don't see any difference
  193. //_writeData(0x00); // don't see any difference
  194. }
  195. void GxEPD2_420::_PowerOn()
  196. {
  197. if (!_power_is_on)
  198. {
  199. _writeCommand(0x04);
  200. _waitWhileBusy("_PowerOn", power_on_time);
  201. }
  202. _power_is_on = true;
  203. }
  204. void GxEPD2_420::_PowerOff()
  205. {
  206. _writeCommand(0x02); // power off
  207. _waitWhileBusy("_PowerOff", power_off_time);
  208. _power_is_on = false;
  209. _using_partial_mode = false;
  210. }
  211. void GxEPD2_420::_InitDisplay()
  212. {
  213. _writeCommand(0x06); // boost
  214. _writeData(0x17);
  215. _writeData(0x17);
  216. _writeData(0x17);
  217. _writeCommand(0x00);
  218. //_writeData(0x1f); // LUT from OTP Pixel with B/W.
  219. _writeData(0x3F); //300x400 B/W mode, LUT set by register
  220. }
  221. void GxEPD2_420::_Init_Full()
  222. {
  223. _InitDisplay();
  224. _writeCommand(0x20);
  225. _writeDataPGM(GxGDEW042T2_lut_20_vcom0_full, sizeof(GxGDEW042T2_lut_20_vcom0_full));
  226. _writeCommand(0x21);
  227. _writeDataPGM(GxGDEW042T2_lut_21_ww_full, sizeof(GxGDEW042T2_lut_21_ww_full));
  228. _writeCommand(0x22);
  229. _writeDataPGM(GxGDEW042T2_lut_22_bw_full, sizeof(GxGDEW042T2_lut_22_bw_full));
  230. _writeCommand(0x23);
  231. _writeDataPGM(GxGDEW042T2_lut_23_wb_full, sizeof(GxGDEW042T2_lut_23_wb_full));
  232. _writeCommand(0x24);
  233. _writeDataPGM(GxGDEW042T2_lut_24_bb_full, sizeof(GxGDEW042T2_lut_24_bb_full));
  234. _PowerOn();
  235. _using_partial_mode = false;
  236. }
  237. void GxEPD2_420::_Init_Part()
  238. {
  239. _InitDisplay();
  240. _writeCommand(0x20);
  241. _writeDataPGM(GxGDEW042T2_lut_20_vcom0_partial, sizeof(GxGDEW042T2_lut_20_vcom0_partial));
  242. _writeCommand(0x21);
  243. _writeDataPGM(GxGDEW042T2_lut_21_ww_partial, sizeof(GxGDEW042T2_lut_21_ww_partial));
  244. _writeCommand(0x22);
  245. _writeDataPGM(GxGDEW042T2_lut_22_bw_partial, sizeof(GxGDEW042T2_lut_22_bw_partial));
  246. _writeCommand(0x23);
  247. _writeDataPGM(GxGDEW042T2_lut_23_wb_partial, sizeof(GxGDEW042T2_lut_23_wb_partial));
  248. _writeCommand(0x24);
  249. _writeDataPGM(GxGDEW042T2_lut_24_bb_partial, sizeof(GxGDEW042T2_lut_24_bb_partial));
  250. _PowerOn();
  251. _using_partial_mode = true;
  252. }
  253. void GxEPD2_420::_Update_Full()
  254. {
  255. _writeCommand(0x12); //display refresh
  256. _waitWhileBusy("_Update_Full", full_refresh_time);
  257. }
  258. void GxEPD2_420::_Update_Part()
  259. {
  260. _writeCommand(0x12); //display refresh
  261. _waitWhileBusy("_Update_Part", partial_refresh_time);
  262. }