GxEPD2_213c.cpp 7.6 KB

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