GxEPD2_750.cpp 9.8 KB

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