GxEPD2_420c.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_420c.h"
  12. #include "WaveTables.h"
  13. GxEPD2_420c::GxEPD2_420c(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. _initial = true;
  17. _power_is_on = false;
  18. }
  19. void GxEPD2_420c::init(uint32_t serial_diag_bitrate)
  20. {
  21. GxEPD2_EPD::init(serial_diag_bitrate);
  22. _initial = true;
  23. _power_is_on = false;
  24. }
  25. void GxEPD2_420c::clearScreen(uint8_t value)
  26. {
  27. clearScreen(value, 0xFF);
  28. }
  29. void GxEPD2_420c::clearScreen(uint8_t black_value, uint8_t color_value)
  30. {
  31. _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) / 8; i++)
  36. {
  37. _writeData(black_value);
  38. }
  39. _writeCommand(0x13);
  40. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  41. {
  42. _writeData(color_value);
  43. }
  44. _Update_Part();
  45. _writeCommand(0x92); // partial out
  46. _initial = false;
  47. }
  48. void GxEPD2_420c::writeScreenBuffer(uint8_t value)
  49. {
  50. writeScreenBuffer(value, 0xFF);
  51. }
  52. void GxEPD2_420c::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  53. {
  54. _Init_Part();
  55. _writeCommand(0x91); // partial in
  56. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  57. _writeCommand(0x10);
  58. for (int16_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  59. {
  60. _writeData(black_value);
  61. }
  62. _writeCommand(0x13);
  63. for (int16_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  64. {
  65. _writeData(color_value);
  66. }
  67. _writeCommand(0x92); // partial out
  68. }
  69. void GxEPD2_420c::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  70. {
  71. writeImage(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
  72. }
  73. void GxEPD2_420c::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)
  74. {
  75. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  76. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  77. x -= x % 8; // byte boundary
  78. w = wb * 8; // byte boundary
  79. int16_t x1 = x < 0 ? 0 : x; // limit
  80. int16_t y1 = y < 0 ? 0 : y; // limit
  81. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  82. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  83. int16_t dx = x1 - x;
  84. int16_t dy = y1 - y;
  85. w1 -= dx;
  86. h1 -= dy;
  87. if ((w1 <= 0) || (h1 <= 0)) return;
  88. _Init_Part();
  89. _writeCommand(0x91); // partial in
  90. _setPartialRamArea(x1, y1, w1, h1);
  91. _writeCommand(0x10);
  92. for (int16_t i = 0; i < h1; i++)
  93. {
  94. for (int16_t j = 0; j < w1 / 8; j++)
  95. {
  96. uint8_t data = 0xFF;
  97. if (black)
  98. {
  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(&black[idx]);
  105. #else
  106. data = black[idx];
  107. #endif
  108. }
  109. else
  110. {
  111. data = black[idx];
  112. }
  113. if (invert) data = ~data;
  114. }
  115. _writeData(data);
  116. }
  117. }
  118. _writeCommand(0x13);
  119. for (int16_t i = 0; i < h1; i++)
  120. {
  121. for (int16_t j = 0; j < w1 / 8; j++)
  122. {
  123. uint8_t data = 0xFF;
  124. if (color)
  125. {
  126. // use wb, h of bitmap for index!
  127. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  128. if (pgm)
  129. {
  130. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  131. data = pgm_read_byte(&color[idx]);
  132. #else
  133. data = color[idx];
  134. #endif
  135. }
  136. else
  137. {
  138. data = color[idx];
  139. }
  140. if (invert) data = ~data;
  141. }
  142. _writeData(data);
  143. }
  144. }
  145. _writeCommand(0x92); // partial out
  146. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  147. }
  148. void GxEPD2_420c::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)
  149. {
  150. if (data1)
  151. {
  152. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  153. }
  154. }
  155. void GxEPD2_420c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  156. {
  157. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  158. refresh(x, y, w, h);
  159. }
  160. void GxEPD2_420c::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)
  161. {
  162. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  163. refresh(x, y, w, h);
  164. }
  165. void GxEPD2_420c::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)
  166. {
  167. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  168. refresh(x, y, w, h);
  169. }
  170. void GxEPD2_420c::refresh(bool partial_update_mode)
  171. {
  172. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  173. else _Update_Full();
  174. }
  175. void GxEPD2_420c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  176. {
  177. x -= x % 8; // byte boundary
  178. w -= x % 8; // byte boundary
  179. int16_t x1 = x < 0 ? 0 : x; // limit
  180. int16_t y1 = y < 0 ? 0 : y; // limit
  181. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  182. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  183. w1 -= x1 - x;
  184. h1 -= y1 - y;
  185. _Init_Part();
  186. _setPartialRamArea(x1, y1, w1, h1);
  187. _Update_Part();
  188. }
  189. void GxEPD2_420c::powerOff()
  190. {
  191. _PowerOff();
  192. }
  193. void GxEPD2_420c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  194. {
  195. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  196. uint16_t ye = y + h - 1;
  197. x &= 0xFFF8; // byte boundary
  198. xe |= 0x0007; // byte boundary
  199. _writeCommand(0x90); // partial window
  200. _writeData(x / 256);
  201. _writeData(x % 256);
  202. _writeData(xe / 256);
  203. _writeData(xe % 256);
  204. _writeData(y / 256);
  205. _writeData(y % 256);
  206. _writeData(ye / 256);
  207. _writeData(ye % 256);
  208. //_writeData(0x01); // distortion on full right half
  209. _writeData(0x00); // distortion on right half
  210. }
  211. void GxEPD2_420c::_PowerOn()
  212. {
  213. if (!_power_is_on)
  214. {
  215. _writeCommand(0x04);
  216. _waitWhileBusy("_PowerOn", power_on_time);
  217. }
  218. _power_is_on = true;
  219. }
  220. void GxEPD2_420c::_PowerOff()
  221. {
  222. _writeCommand(0x02); // power off
  223. _waitWhileBusy("_PowerOff", power_off_time);
  224. _power_is_on = false;
  225. }
  226. void GxEPD2_420c::_InitDisplay()
  227. {
  228. _writeCommand(0x06); //boost
  229. _writeData(0x17);
  230. _writeData(0x17);
  231. _writeData(0x17);
  232. _writeCommand(0x00);
  233. _writeData(0x0f); // LUT from OTP Pixel with B/W/R.
  234. }
  235. void GxEPD2_420c::_Init_Full()
  236. {
  237. _InitDisplay();
  238. _PowerOn();
  239. }
  240. void GxEPD2_420c::_Init_Part()
  241. {
  242. _InitDisplay();
  243. _PowerOn();
  244. }
  245. void GxEPD2_420c::_Update_Full()
  246. {
  247. _writeCommand(0x12); //display refresh
  248. _waitWhileBusy("_Update_Full", full_refresh_time);
  249. }
  250. void GxEPD2_420c::_Update_Part()
  251. {
  252. _writeCommand(0x12); //display refresh
  253. _waitWhileBusy("_Update_Part", partial_refresh_time);
  254. }